Skip to main content

rustapi/actions/
set_tile_material.rs

1use crate::editor::DOCKMANAGER;
2use crate::prelude::*;
3
4pub struct SetTileMaterial {
5    id: TheId,
6    nodeui: TheNodeUI,
7}
8
9impl Action for SetTileMaterial {
10    fn new() -> Self
11    where
12        Self: Sized,
13    {
14        let mut nodeui: TheNodeUI = TheNodeUI::default();
15
16        let item = TheNodeUIItem::FloatEditSlider(
17            "actionSetTileMaterialRoughness".into(),
18            "".into(),
19            "".into(),
20            0.5,
21            0.0..=1.0,
22            false,
23        );
24        nodeui.add_item(item);
25
26        let item = TheNodeUIItem::FloatEditSlider(
27            "actionSetTileMaterialMetallic".into(),
28            "".into(),
29            "".into(),
30            0.0,
31            0.0..=1.0,
32            false,
33        );
34        nodeui.add_item(item);
35
36        let item = TheNodeUIItem::FloatEditSlider(
37            "actionSetTileMaterialOpacity".into(),
38            "".into(),
39            "".into(),
40            1.0,
41            0.0..=1.0,
42            false,
43        );
44        nodeui.add_item(item);
45
46        let item = TheNodeUIItem::FloatEditSlider(
47            "actionSetTileMaterialEmissive".into(),
48            "".into(),
49            "".into(),
50            0.0,
51            0.0..=1.0,
52            false,
53        );
54        nodeui.add_item(item);
55
56        let item = TheNodeUIItem::Markdown("desc".into(), "".into());
57        nodeui.add_item(item);
58
59        Self {
60            id: TheId::named(&fl!("action_set_tile_material")),
61            nodeui,
62        }
63    }
64
65    fn id(&self) -> TheId {
66        self.id.clone()
67    }
68
69    fn info(&self) -> String {
70        fl!("action_set_tile_material_desc")
71    }
72
73    fn role(&self) -> ActionRole {
74        ActionRole::Dock
75    }
76
77    fn accel(&self) -> Option<TheAccelerator> {
78        Some(TheAccelerator::new(TheAcceleratorKey::ALT, 'a'))
79    }
80
81    fn is_applicable(&self, _map: &Map, _ctx: &mut TheContext, server_ctx: &ServerContext) -> bool {
82        DOCKMANAGER.read().unwrap().dock == "Tiles" && server_ctx.curr_tile_id.is_some()
83    }
84
85    fn apply_project(
86        &self,
87        project: &mut Project,
88        _ui: &mut TheUI,
89        ctx: &mut TheContext,
90        server_ctx: &mut ServerContext,
91    ) {
92        let roughness = self
93            .nodeui
94            .get_f32_value("actionSetTileMaterialRoughness")
95            .unwrap_or(0.5);
96
97        let metallic = self
98            .nodeui
99            .get_f32_value("actionSetTileMaterialMetallic")
100            .unwrap_or(0.0);
101
102        let opacity = self
103            .nodeui
104            .get_f32_value("actionSetTileMaterialOpacity")
105            .unwrap_or(1.0);
106
107        let emissive = self
108            .nodeui
109            .get_f32_value("actionSetTileMaterialEmissive")
110            .unwrap_or(0.0);
111
112        if let Some(tile_id) = server_ctx.curr_tile_id {
113            if let Some(tile) = project.tiles.get_mut(&tile_id) {
114                for texture in &mut tile.textures {
115                    texture.set_materials_all(roughness, metallic, opacity, emissive);
116                }
117            }
118        }
119
120        ctx.ui.send(TheEvent::Custom(
121            TheId::named("Update Tiles"),
122            TheValue::Empty,
123        ));
124
125        ctx.ui.send(TheEvent::Custom(
126            TheId::named("Render SceneManager Map"),
127            TheValue::Empty,
128        ));
129    }
130
131    fn params(&self) -> TheNodeUI {
132        self.nodeui.clone()
133    }
134
135    fn handle_event(
136        &mut self,
137        event: &TheEvent,
138        _project: &mut Project,
139        _ui: &mut TheUI,
140        _ctx: &mut TheContext,
141        _server_ctx: &mut ServerContext,
142    ) -> bool {
143        self.nodeui.handle_event(event)
144    }
145}