Skip to main content

rustapi/actions/
create_center_vertex.rs

1use crate::prelude::*;
2
3pub struct CreateCenterVertex {
4    id: TheId,
5    nodeui: TheNodeUI,
6}
7
8impl Action for CreateCenterVertex {
9    fn new() -> Self
10    where
11        Self: Sized,
12    {
13        let mut nodeui: TheNodeUI = TheNodeUI::default();
14        let item = TheNodeUIItem::Markdown("desc".into(), fl!("action_create_center_vertex_desc"));
15        nodeui.add_item(item);
16
17        Self {
18            id: TheId::named(&fl!("action_create_center_vertex")),
19            nodeui,
20        }
21    }
22
23    fn id(&self) -> TheId {
24        self.id.clone()
25    }
26
27    fn info(&self) -> String {
28        fl!("action_create_center_vertex_desc")
29    }
30
31    fn role(&self) -> ActionRole {
32        ActionRole::Editor
33    }
34
35    fn accel(&self) -> Option<TheAccelerator> {
36        None
37    }
38
39    fn is_applicable(&self, map: &Map, _ctx: &mut TheContext, _server_ctx: &ServerContext) -> bool {
40        !map.selected_sectors.is_empty()
41    }
42
43    fn apply(
44        &self,
45        map: &mut Map,
46        _ui: &mut TheUI,
47        _ctx: &mut TheContext,
48        server_ctx: &mut ServerContext,
49    ) -> Option<ProjectUndoAtom> {
50        let mut changed = false;
51        let prev = map.clone();
52
53        for sector_id in &map.selected_sectors.clone() {
54            if let Some(sector) = map.find_sector(*sector_id) {
55                if let Some(pos) = sector.center_3d(map) {
56                    _ = map.add_vertex_at_3d(pos.x, pos.y, pos.z, false);
57                    changed = true;
58                }
59            }
60        }
61
62        if changed {
63            Some(ProjectUndoAtom::MapEdit(
64                server_ctx.pc,
65                Box::new(prev),
66                Box::new(map.clone()),
67            ))
68        } else {
69            None
70        }
71    }
72
73    fn params(&self) -> TheNodeUI {
74        self.nodeui.clone()
75    }
76
77    fn handle_event(
78        &mut self,
79        event: &TheEvent,
80        _project: &mut Project,
81        _ui: &mut TheUI,
82        _ctx: &mut TheContext,
83        _server_ctx: &mut ServerContext,
84    ) -> bool {
85        self.nodeui.handle_event(event)
86    }
87}