Skip to main content

rustapi/actions/
apply_tile.rs

1use crate::editor::DOCKMANAGER;
2use crate::prelude::*;
3use rusterix::PixelSource;
4
5pub struct ApplyTile {
6    id: TheId,
7    nodeui: TheNodeUI,
8}
9
10impl Action for ApplyTile {
11    fn new() -> Self
12    where
13        Self: Sized,
14    {
15        let mut nodeui: TheNodeUI = TheNodeUI::default();
16
17        let item = TheNodeUIItem::Selector(
18            "actionApplyTileMode".into(),
19            "".into(),
20            "".into(),
21            vec!["repeat".into(), "scale".into()],
22            0,
23        );
24        nodeui.add_item(item);
25
26        let item = TheNodeUIItem::Markdown("desc".into(), "".into());
27        nodeui.add_item(item);
28
29        Self {
30            id: TheId::named(&fl!("action_apply_tile")),
31            nodeui,
32        }
33    }
34
35    fn id(&self) -> TheId {
36        self.id.clone()
37    }
38
39    fn info(&self) -> String {
40        fl!("action_apply_tile_desc")
41    }
42
43    fn role(&self) -> ActionRole {
44        ActionRole::Dock
45    }
46
47    fn accel(&self) -> Option<TheAccelerator> {
48        Some(TheAccelerator::new(TheAcceleratorKey::ALT, 'a'))
49    }
50
51    fn is_applicable(&self, map: &Map, _ctx: &mut TheContext, server_ctx: &ServerContext) -> bool {
52        !map.selected_sectors.is_empty()
53            && DOCKMANAGER.read().unwrap().dock == "Tiles"
54            && server_ctx.curr_tile_id.is_some()
55    }
56
57    fn apply(
58        &self,
59        map: &mut Map,
60        _ui: &mut TheUI,
61        _ctx: &mut TheContext,
62        server_ctx: &mut ServerContext,
63    ) -> Option<ProjectUndoAtom> {
64        let mut changed = false;
65        let prev = map.clone();
66
67        let mut mode = self
68            .nodeui
69            .get_i32_value("actionApplyTileMode")
70            .unwrap_or(0);
71
72        mode = match mode {
73            1 => 0,
74            _ => 1,
75        };
76
77        if let Some(tile_id) = server_ctx.curr_tile_id {
78            for sector_id in &map.selected_sectors.clone() {
79                if let Some(sector) = map.find_sector_mut(*sector_id) {
80                    let mut source = "source";
81
82                    if server_ctx.pc.is_screen() {
83                        if server_ctx.selected_hud_icon_index == 1 {
84                            source = "ceiling_source";
85                        }
86                    }
87
88                    sector
89                        .properties
90                        .set(source, Value::Source(PixelSource::TileId(tile_id)));
91                    sector.properties.set("tile_mode", Value::Int(mode));
92                    changed = true;
93                }
94            }
95        }
96
97        if changed {
98            Some(ProjectUndoAtom::MapEdit(
99                server_ctx.pc,
100                Box::new(prev),
101                Box::new(map.clone()),
102            ))
103        } else {
104            None
105        }
106    }
107
108    fn params(&self) -> TheNodeUI {
109        self.nodeui.clone()
110    }
111
112    fn handle_event(
113        &mut self,
114        event: &TheEvent,
115        _project: &mut Project,
116        _ui: &mut TheUI,
117        _ctx: &mut TheContext,
118        _server_ctx: &mut ServerContext,
119    ) -> bool {
120        self.nodeui.handle_event(event)
121    }
122}