spring_ai_rs/ai_interface/callback/drawer/debug/
graph.rs1use std::ffi::CString;
2
3use spring_ai_sys::COMMAND_TO_ID_ENGINE;
4
5use crate::ai_interface::callback::{
6 command::{
7 command_data::{
8 debug_drawer::{
9 AddPointLineGraphDrawerDebugCommandData,
10 DeletePointsLineGraphDrawerDebugCommandData,
11 SetColorLineGraphDrawerDebugCommandData, SetLabelLineGraphDrawerDebugCommandData,
12 SetPositionGraphDrawerDebugCommandData, SetSizeGraphDrawerDebugCommandData,
13 },
14 CommandData,
15 },
16 command_topic::CommandTopic,
17 },
18 engine::handle_command,
19};
20
21pub struct GraphLine {
22 pub ai_id: i32,
23 pub line_id: i32,
24}
25
26impl GraphLine {
27 pub fn add_point(&self, x: f32, y: f32) -> Result<(), String> {
28 let mut command_c_data = AddPointLineGraphDrawerDebugCommandData {
29 line_id: self.line_id,
30 x,
31 y,
32 }
33 .c_data();
34
35 handle_command(
36 self.ai_id,
37 COMMAND_TO_ID_ENGINE,
38 -1,
39 CommandTopic::DebugDrawerGraphLineAddPoint.into(),
40 &mut command_c_data,
41 )?;
42
43 Ok(())
44 }
45 pub fn delete_points(&self, num_points: i32) -> Result<(), String> {
46 let mut command_c_data = DeletePointsLineGraphDrawerDebugCommandData {
47 line_id: self.line_id,
48 num_points,
49 }
50 .c_data();
51
52 handle_command(
53 self.ai_id,
54 COMMAND_TO_ID_ENGINE,
55 -1,
56 CommandTopic::DebugDrawerGraphLineDeletePoints.into(),
57 &mut command_c_data,
58 )?;
59
60 Ok(())
61 }
62 pub fn set_color(&self, color: [i16; 3]) -> Result<(), String> {
63 let mut command_c_data = SetColorLineGraphDrawerDebugCommandData {
64 line_id: self.line_id,
65 color,
66 }
67 .c_data();
68
69 handle_command(
70 self.ai_id,
71 COMMAND_TO_ID_ENGINE,
72 -1,
73 CommandTopic::DebugDrawerGraphLineSetColor.into(),
74 &mut command_c_data,
75 )?;
76
77 Ok(())
78 }
79 pub fn set_label(&self, label: &str) -> Result<(), String> {
80 let mut command_c_data = SetLabelLineGraphDrawerDebugCommandData {
81 line_id: self.line_id,
82 label: CString::new(label).map_err(|e| e.to_string())?,
83 }
84 .c_data();
85
86 handle_command(
87 self.ai_id,
88 COMMAND_TO_ID_ENGINE,
89 -1,
90 CommandTopic::DebugDrawerGraphLineSetLabel.into(),
91 &mut command_c_data,
92 )?;
93
94 Ok(())
95 }
96}
97
98pub struct Graph {
99 pub ai_id: i32,
100}
101
102impl Graph {
103 pub fn set_position(&self, x: f32, y: f32) -> Result<(), String> {
104 let mut command_c_data = SetPositionGraphDrawerDebugCommandData { x, y }.c_data();
105
106 handle_command(
107 self.ai_id,
108 COMMAND_TO_ID_ENGINE,
109 -1,
110 CommandTopic::DebugDrawerGraphSetPOS.into(),
111 &mut command_c_data,
112 )?;
113
114 Ok(())
115 }
116 pub fn set_size(&self, w: f32, h: f32) -> Result<(), String> {
117 let mut command_c_data = SetSizeGraphDrawerDebugCommandData { w, h }.c_data();
118
119 handle_command(
120 self.ai_id,
121 COMMAND_TO_ID_ENGINE,
122 -1,
123 CommandTopic::DebugDrawerGraphSetSize.into(),
124 &mut command_c_data,
125 )?;
126
127 Ok(())
128 }
129}