Skip to main content

radiantkit_core/tools/
tool_manager.rs

1use crate::RadiantTool;
2use std::collections::BTreeMap;
3
4pub type ToolId = u32;
5
6pub struct RadiantToolManager<M> {
7    pub tools: BTreeMap<ToolId, Box<dyn RadiantTool<M>>>,
8    pub active_tool_id: ToolId,
9}
10
11impl<M> RadiantToolManager<M> {
12    pub fn new<T: RadiantTool<M> + 'static>(id: ToolId, tool: Box<T>) -> Self {
13        Self {
14            tools: BTreeMap::from([(id, tool as Box<dyn RadiantTool<M>>)]),
15            active_tool_id: id,
16        }
17    }
18
19    pub fn register_tool<T: RadiantTool<M> + 'static>(&mut self, tool_id: ToolId, tool: Box<T>) {
20        self.tools.insert(tool_id, tool);
21    }
22
23    pub fn active_tool(&mut self) -> &mut dyn RadiantTool<M> {
24        self.tools
25            .get_mut(&self.active_tool_id)
26            .expect("Active tool not found")
27            .as_mut()
28    }
29
30    pub fn activate_tool(&mut self, id: u32) {
31        if self.tools.len() > id as usize {
32            self.active_tool_id = id;
33        }
34    }
35}