arcane_core/scripting/
target_ops.rs1use std::cell::RefCell;
24use std::collections::HashMap;
25use std::rc::Rc;
26
27use deno_core::OpState;
28
29use crate::renderer::SpriteCommand;
30use crate::scripting::render_ops::RenderBridgeState;
31
32pub struct TargetState {
34 pub active_target: Option<u32>,
36 pub create_queue: Vec<(u32, u32, u32)>, pub destroy_queue: Vec<u32>,
40 pub target_sprite_queues: HashMap<u32, Vec<SpriteCommand>>,
42}
43
44impl TargetState {
45 pub fn new() -> Self {
46 Self {
47 active_target: None,
48 create_queue: Vec::new(),
49 destroy_queue: Vec::new(),
50 target_sprite_queues: HashMap::new(),
51 }
52 }
53}
54
55#[deno_core::op2(fast)]
61fn op_create_render_target(state: &mut OpState, w: f64, h: f64) -> u32 {
62 let id = {
64 let bridge = state.borrow_mut::<Rc<RefCell<RenderBridgeState>>>();
65 let mut b = bridge.borrow_mut();
66 let id = b.next_texture_id;
67 b.next_texture_id += 1;
68 id
69 };
70 let ts = state.borrow_mut::<Rc<RefCell<TargetState>>>();
71 ts.borrow_mut()
72 .create_queue
73 .push((id, w as u32, h as u32));
74 id
75}
76
77#[deno_core::op2(fast)]
80fn op_begin_render_target(state: &mut OpState, id: u32) {
81 let ts = state.borrow_mut::<Rc<RefCell<TargetState>>>();
82 ts.borrow_mut().active_target = Some(id);
83}
84
85#[deno_core::op2(fast)]
87fn op_end_render_target(state: &mut OpState) {
88 let ts = state.borrow_mut::<Rc<RefCell<TargetState>>>();
89 ts.borrow_mut().active_target = None;
90}
91
92#[deno_core::op2(fast)]
95fn op_destroy_render_target(state: &mut OpState, id: u32) {
96 let ts = state.borrow_mut::<Rc<RefCell<TargetState>>>();
97 let mut ts = ts.borrow_mut();
98 ts.destroy_queue.push(id);
99 ts.target_sprite_queues.remove(&id);
100 if ts.active_target == Some(id) {
102 ts.active_target = None;
103 }
104}
105
106deno_core::extension!(
107 target_ext,
108 ops = [
109 op_create_render_target,
110 op_begin_render_target,
111 op_end_render_target,
112 op_destroy_render_target,
113 ],
114);
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_target_state_new() {
122 let state = TargetState::new();
123 assert!(state.active_target.is_none());
124 assert!(state.create_queue.is_empty());
125 assert!(state.destroy_queue.is_empty());
126 assert!(state.target_sprite_queues.is_empty());
127 }
128
129 #[test]
130 fn test_set_active_target() {
131 let mut state = TargetState::new();
132 assert!(state.active_target.is_none());
133
134 state.active_target = Some(42);
135 assert_eq!(state.active_target, Some(42));
136
137 state.active_target = None;
138 assert!(state.active_target.is_none());
139 }
140
141 #[test]
142 fn test_create_queue() {
143 let mut state = TargetState::new();
144
145 state.create_queue.push((1, 256, 256));
146 state.create_queue.push((2, 128, 128));
147
148 assert_eq!(state.create_queue.len(), 2);
149 assert_eq!(state.create_queue[0], (1, 256, 256));
150 assert_eq!(state.create_queue[1], (2, 128, 128));
151 }
152
153 #[test]
154 fn test_destroy_queue() {
155 let mut state = TargetState::new();
156
157 state.destroy_queue.push(5);
158 state.destroy_queue.push(10);
159
160 assert_eq!(state.destroy_queue.len(), 2);
161 assert!(state.destroy_queue.contains(&5));
162 assert!(state.destroy_queue.contains(&10));
163 }
164
165 #[test]
166 fn test_target_sprite_queues() {
167 let mut state = TargetState::new();
168
169 state.target_sprite_queues.insert(1, Vec::new());
170 state.target_sprite_queues.insert(2, Vec::new());
171
172 assert!(state.target_sprite_queues.contains_key(&1));
173 assert!(state.target_sprite_queues.contains_key(&2));
174 assert!(!state.target_sprite_queues.contains_key(&3));
175
176 state.target_sprite_queues.remove(&1);
177 assert!(!state.target_sprite_queues.contains_key(&1));
178 }
179
180 #[test]
181 fn test_destroy_clears_active() {
182 let mut state = TargetState::new();
183 state.active_target = Some(5);
184
185 state.destroy_queue.push(5);
187 state.target_sprite_queues.remove(&5);
188 if state.active_target == Some(5) {
189 state.active_target = None;
190 }
191
192 assert!(state.active_target.is_none());
193 }
194}