Skip to main content

rustapi/actions/
set_editing_surface.rs

1use crate::prelude::*;
2
3pub struct SetEditingSurface {
4    id: TheId,
5    nodeui: TheNodeUI,
6}
7
8impl Action for SetEditingSurface {
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_set_edit_surface_desc"));
15        nodeui.add_item(item);
16
17        Self {
18            id: TheId::named(&fl!("action_set_edit_surface")),
19            nodeui,
20        }
21    }
22
23    fn id(&self) -> TheId {
24        self.id.clone()
25    }
26
27    fn info(&self) -> String {
28        fl!("action_set_edit_surface_desc")
29    }
30
31    fn role(&self) -> ActionRole {
32        ActionRole::Editor
33    }
34
35    fn accel(&self) -> Option<TheAccelerator> {
36        Some(TheAccelerator::new(TheAcceleratorKey::ALT, 'u'))
37    }
38
39    fn is_applicable(&self, map: &Map, _ctx: &mut TheContext, server_ctx: &ServerContext) -> bool {
40        map.selected_sectors.len() == 1 && server_ctx.editor_view_mode != EditorViewMode::D2
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        if let Some(sector_id) = map.selected_sectors.first().cloned() {
51            let mut profile_to_add = None;
52
53            if let Some(surface) = map.get_surface_for_sector_id_mut(sector_id) {
54                // If there is no profile yet for the surface we add one
55                if surface.profile.is_none() {
56                    let profile = Map::default();
57                    surface.profile = Some(profile.id);
58                    profile_to_add = Some(profile);
59                }
60
61                let mut surface = surface.clone();
62                if let Some(sector) = map.find_sector(sector_id) {
63                    if let Some(vertices) = sector.vertices_world(map) {
64                        surface.world_vertices = vertices;
65                    }
66                }
67
68                server_ctx.editing_surface = Some(surface.clone());
69                server_ctx.editor_view_mode = EditorViewMode::D2;
70            }
71
72            if let Some(profile_to_add) = profile_to_add {
73                map.profiles.insert(profile_to_add.id, profile_to_add);
74            }
75
76            ctx.ui.send(TheEvent::Custom(
77                TheId::named("Render SceneManager Map"),
78                TheValue::Empty,
79            ));
80
81            ctx.ui.send(TheEvent::Custom(
82                TheId::named("Backup Editing Position"),
83                TheValue::Empty,
84            ));
85        }
86
87        None
88    }
89
90    fn params(&self) -> TheNodeUI {
91        self.nodeui.clone()
92    }
93
94    fn handle_event(
95        &mut self,
96        event: &TheEvent,
97        _project: &mut Project,
98        _ui: &mut TheUI,
99        _ctx: &mut TheContext,
100        _server_ctx: &mut ServerContext,
101    ) -> bool {
102        self.nodeui.handle_event(event)
103    }
104}