spring_ai_rs/ai_interface/callback/drawer/debug/
overlay_texture.rs

1use std::ffi::CString;
2
3use spring_ai_sys::COMMAND_TO_ID_ENGINE;
4
5use crate::ai_interface::{
6    callback::{
7        command::{
8            command_data::{
9                debug_drawer::{
10                    AddOverlayTextureDrawerDebugCommandData,
11                    DeleteOverlayTextureDrawerDebugCommandData,
12                    SetLabelOverlayTextureDrawerDebugCommandData,
13                    SetPositionOverlayTextureDrawerDebugCommandData,
14                    SetSizeOverlayTextureDrawerDebugCommandData,
15                    UpdateOverlayTextureDrawerDebugCommandData,
16                },
17                CommandData,
18            },
19            command_topic::CommandTopic,
20        },
21        engine::handle_command,
22    },
23    AIInterface,
24};
25
26impl AIInterface {
27    pub fn add_overlay_texture(
28        &self,
29        texture_data: Vec<f32>,
30        w: i32,
31        h: i32,
32    ) -> Result<OverlayTexture, String> {
33        OverlayTexture::add(self.ai_id, texture_data, w, h)
34    }
35}
36
37pub struct OverlayTexture {
38    pub ai_id: i32,
39    pub texture_id: i32,
40}
41
42impl OverlayTexture {
43    fn add(ai_id: i32, texture_data: Vec<f32>, w: i32, h: i32) -> Result<Self, String> {
44        let mut command_c_data =
45            AddOverlayTextureDrawerDebugCommandData { texture_data, w, h }.c_data();
46
47        handle_command(
48            ai_id,
49            COMMAND_TO_ID_ENGINE,
50            -1,
51            CommandTopic::DebugDrawerOverlaytextureAdd.into(),
52            &mut command_c_data,
53        )?;
54
55        Ok(Self {
56            ai_id,
57            texture_id: command_c_data.ret_overlayTextureId,
58        })
59    }
60    pub fn delete(&self) -> Result<(), String> {
61        let mut command_c_data = DeleteOverlayTextureDrawerDebugCommandData {
62            overlay_texture_id: self.texture_id,
63        }
64        .c_data();
65
66        handle_command(
67            self.ai_id,
68            COMMAND_TO_ID_ENGINE,
69            -1,
70            CommandTopic::DebugDrawerOverlaytextureDelete.into(),
71            &mut command_c_data,
72        )?;
73
74        Ok(())
75    }
76    pub fn set_label(&self, label: &str) -> Result<(), String> {
77        let mut command_c_data = SetLabelOverlayTextureDrawerDebugCommandData {
78            overlay_texture_id: self.texture_id,
79            label: CString::new(label).map_err(|e| e.to_string())?,
80        }
81        .c_data();
82
83        handle_command(
84            self.ai_id,
85            COMMAND_TO_ID_ENGINE,
86            -1,
87            CommandTopic::DebugDrawerOverlaytextureSetLabel.into(),
88            &mut command_c_data,
89        )?;
90
91        Ok(())
92    }
93    pub fn set_position(&self, x: f32, y: f32) -> Result<(), String> {
94        let mut command_c_data = SetPositionOverlayTextureDrawerDebugCommandData {
95            overlay_texture_id: self.texture_id,
96            x,
97            y,
98        }
99        .c_data();
100
101        handle_command(
102            self.ai_id,
103            COMMAND_TO_ID_ENGINE,
104            -1,
105            CommandTopic::DebugDrawerOverlaytextureSetPOS.into(),
106            &mut command_c_data,
107        )?;
108
109        Ok(())
110    }
111    pub fn set_size(&self, w: f32, h: f32) -> Result<(), String> {
112        let mut command_c_data = SetSizeOverlayTextureDrawerDebugCommandData {
113            overlay_texture_id: self.texture_id,
114            w,
115            h,
116        }
117        .c_data();
118
119        handle_command(
120            self.ai_id,
121            COMMAND_TO_ID_ENGINE,
122            -1,
123            CommandTopic::DebugDrawerOverlaytextureSetSize.into(),
124            &mut command_c_data,
125        )?;
126
127        Ok(())
128    }
129    pub fn update(
130        &self,
131        texture_data: Vec<f32>,
132        x: i32,
133        y: i32,
134        w: i32,
135        h: i32,
136    ) -> Result<(), String> {
137        let mut command_c_data = UpdateOverlayTextureDrawerDebugCommandData {
138            overlay_texture_id: self.texture_id,
139            tex_data: texture_data,
140            x,
141            y,
142            w,
143            h,
144        }
145        .c_data();
146
147        handle_command(
148            self.ai_id,
149            COMMAND_TO_ID_ENGINE,
150            -1,
151            CommandTopic::DebugDrawerOverlaytextureUpdate.into(),
152            &mut command_c_data,
153        )?;
154
155        Ok(())
156    }
157}