rustapi/actions/
new_tile.rs1use rusterix::Texture;
2
3use crate::editor::DOCKMANAGER;
4use crate::prelude::*;
5
6pub struct NewTile {
7 id: TheId,
8 nodeui: TheNodeUI,
9}
10
11impl Action for NewTile {
12 fn new() -> Self
13 where
14 Self: Sized,
15 {
16 let mut nodeui: TheNodeUI = TheNodeUI::default();
17
18 let item = TheNodeUIItem::IntEditSlider(
19 "actionNewTileSize".into(),
20 "".into(),
21 "".into(),
22 16,
23 8..=64,
24 false,
25 );
26 nodeui.add_item(item);
27
28 let item = TheNodeUIItem::IntEditSlider(
29 "actionNewTileFrames".into(),
30 "".into(),
31 "".into(),
32 1,
33 1..=8,
34 false,
35 );
36 nodeui.add_item(item);
37
38 let item = TheNodeUIItem::Markdown("desc".into(), "".into());
39 nodeui.add_item(item);
40
41 Self {
42 id: TheId::named(&fl!("action_new_tile")),
43 nodeui,
44 }
45 }
46
47 fn id(&self) -> TheId {
48 self.id.clone()
49 }
50
51 fn info(&self) -> String {
52 fl!("action_new_tile_desc")
53 }
54
55 fn role(&self) -> ActionRole {
56 ActionRole::Dock
57 }
58
59 fn accel(&self) -> Option<TheAccelerator> {
60 None
61 }
62
63 fn is_applicable(
64 &self,
65 _map: &Map,
66 _ctx: &mut TheContext,
67 _server_ctx: &ServerContext,
68 ) -> bool {
69 DOCKMANAGER.read().unwrap().dock == "Tiles"
70 }
71
72 fn apply_project(
73 &self,
74 project: &mut Project,
75 _ui: &mut TheUI,
76 ctx: &mut TheContext,
77 _server_ctx: &mut ServerContext,
78 ) {
79 let size = self
80 .nodeui
81 .get_i32_value("actionNewTileSize")
82 .unwrap_or(16)
83 .clamp(8, 64) as u32;
84
85 let frames = self
86 .nodeui
87 .get_i32_value("actionNewTileFrames")
88 .unwrap_or(1)
89 .clamp(1, 8) as u32;
90
91 let mut textures = vec![];
92 let mut c = [128, 128, 128, 255];
93 if let Some(col) = project.palette.get_current_color() {
94 c = col.to_u8_array();
95 }
96
97 for _ in 0..frames {
98 let mut texture = Texture::alloc(size as usize, size as usize);
99 texture.fill(c);
100 textures.push(texture);
101 }
102
103 let mut tile = rusterix::Tile::from_textures(textures);
104 tile.set_default_materials();
105 project.tiles.insert(tile.id, tile);
106
107 ctx.ui.send(TheEvent::Custom(
108 TheId::named("Update Tilepicker"),
109 TheValue::Empty,
110 ));
111
112 ctx.ui.send(TheEvent::Custom(
113 TheId::named("Update Tiles"),
114 TheValue::Empty,
115 ));
116 }
117
118 fn params(&self) -> TheNodeUI {
119 self.nodeui.clone()
120 }
121
122 fn handle_event(
123 &mut self,
124 event: &TheEvent,
125 _project: &mut Project,
126 _ui: &mut TheUI,
127 _ctx: &mut TheContext,
128 _server_ctx: &mut ServerContext,
129 ) -> bool {
130 self.nodeui.handle_event(event)
131 }
132}