Skip to main content

rustapi/actions/
remap_tile.rs

1use crate::editor::{DOCKMANAGER, UNDOMANAGER};
2use crate::prelude::*;
3
4pub struct RemapTile {
5    id: TheId,
6    nodeui: TheNodeUI,
7}
8
9impl Action for RemapTile {
10    fn new() -> Self
11    where
12        Self: Sized,
13    {
14        let nodeui: TheNodeUI = TheNodeUI::default();
15
16        Self {
17            id: TheId::named(&fl!("action_remap_tile")),
18            nodeui,
19        }
20    }
21
22    fn id(&self) -> TheId {
23        self.id.clone()
24    }
25
26    fn info(&self) -> String {
27        fl!("action_remap_tile_desc")
28    }
29
30    fn role(&self) -> ActionRole {
31        ActionRole::Dock
32    }
33
34    fn accel(&self) -> Option<TheAccelerator> {
35        None
36    }
37
38    fn is_applicable(&self, _map: &Map, _ctx: &mut TheContext, server_ctx: &ServerContext) -> bool {
39        DOCKMANAGER.read().unwrap().dock == "Tiles" && server_ctx.curr_tile_id.is_some()
40    }
41
42    fn apply_project(
43        &self,
44        project: &mut Project,
45        _ui: &mut TheUI,
46        ctx: &mut TheContext,
47        server_ctx: &mut ServerContext,
48    ) {
49        if let Some(tile_id) = server_ctx.curr_tile_id {
50            if let Some(tile) = project.tiles.get_mut(&tile_id) {
51                let prev = tile.clone();
52
53                for tex in &mut tile.textures {
54                    for y in 0..tex.height {
55                        for x in 0..tex.width {
56                            let mut col = tex.get_pixel(x as u32, y as u32);
57                            let t = col[3];
58                            let color = TheColor::from(col);
59
60                            if let Some(index) = project.palette.find_closest_color_index(&color) {
61                                if let Some(c) = project.palette.colors.get(index) {
62                                    if let Some(c) = c {
63                                        col = c.to_u8_array();
64                                        col[3] = t;
65                                    }
66                                }
67                            }
68
69                            tex.set_pixel(x as u32, y as u32, col);
70                        }
71                    }
72                }
73
74                for tex in &mut tile.textures {
75                    tex.generate_normals(true);
76                }
77
78                let undo_atom = ProjectUndoAtom::TileEdit(prev, tile.clone());
79                UNDOMANAGER.write().unwrap().add_undo(undo_atom, ctx);
80
81                ctx.ui.send(TheEvent::Custom(
82                    TheId::named("Update Tiles"),
83                    TheValue::Empty,
84                ));
85            }
86        }
87    }
88
89    fn params(&self) -> TheNodeUI {
90        self.nodeui.clone()
91    }
92
93    fn handle_event(
94        &mut self,
95        event: &TheEvent,
96        _project: &mut Project,
97        _ui: &mut TheUI,
98        _ctx: &mut TheContext,
99        _server_ctx: &mut ServerContext,
100    ) -> bool {
101        self.nodeui.handle_event(event)
102    }
103}