Skip to main content

siglus_scene_vm/runtime/commands/
layer.rs

1use anyhow::Result;
2
3use crate::layer::LayerManager;
4use crate::runtime::commands::util::strip_vm_meta;
5use crate::runtime::{Command, CommandContext, Value};
6
7/// Layer commands.
8///
9/// These exist to help validate the renderer and command plumbing.
10pub fn handle(ctx: &mut CommandContext, cmd: &Command) -> Result<bool> {
11    let name = cmd.name.to_ascii_uppercase();
12    let args = strip_vm_meta(&cmd.args);
13
14    match name.as_str() {
15        // Reset both the layer manager and the gfx runtime.
16        "LAYER_RESET" | "LAYER_CLEAR" | "CLS" => {
17            ctx.layers = LayerManager::new();
18            ctx.gfx = crate::runtime::graphics::GfxRuntime::new();
19            return Ok(true);
20        }
21        // Select current layer for subsequent CHR/object operations.
22        "LAYER" | "LAYER_SET" | "LAYER_SEL" | "LAYER_SELECT" => {
23            if let Some(layer) = args.iter().rev().find_map(|v| match v {
24                Value::Int(i) => Some(*i),
25                _ => None,
26            }) {
27                ctx.gfx.current_layer = layer.clamp(i32::MIN as i64, i32::MAX as i64) as i32;
28                return Ok(true);
29            }
30            return Ok(false);
31        }
32        // Clear a specific layer.
33        "LAYER_CLR" => {
34            if let Some(layer) = args.iter().rev().find_map(|v| match v {
35                Value::Int(i) => Some(*i),
36                _ => None,
37            }) {
38                if layer >= 0 {
39                    ctx.layers.clear_layer(layer as usize);
40                }
41                return Ok(true);
42            }
43            return Ok(false);
44        }
45        _ => {}
46    }
47
48    if name.starts_with("LAYER") {
49        return Ok(true);
50    }
51
52    Ok(false)
53}