diff --git a/crates/core/src/nodes/document.rs b/crates/core/src/nodes/document.rs index 67b2805..b97eb8e 100644 --- a/crates/core/src/nodes/document.rs +++ b/crates/core/src/nodes/document.rs @@ -44,8 +44,8 @@ impl RadiantDocumentNode { self.artboards.get(self.active_artboard_id as usize) } - pub fn select(&mut self, id: u64) { - if Some(id) == self.selected_node_id { + pub fn select(&mut self, id: Option) { + if id == self.selected_node_id { return; } self.artboards.iter_mut().for_each(|artboard| { @@ -57,14 +57,16 @@ impl RadiantDocumentNode { } } } - if let Some(node) = artboard.get_node_mut(id) { - if let Some(component) = node.get_component_mut::() { - component.set_selected(true); - node.set_needs_tessellation(); + if let Some(id) = id { + if let Some(node) = artboard.get_node_mut(id) { + if let Some(component) = node.get_component_mut::() { + component.set_selected(true); + node.set_needs_tessellation(); + } } } }); - self.selected_node_id = Some(id); + self.selected_node_id = id } pub fn get_node(&self, id: u64) -> Option<&N> { diff --git a/crates/core/src/render/render_manager.rs b/crates/core/src/render/render_manager.rs index 3ab836c..6462fae 100644 --- a/crates/core/src/render/render_manager.rs +++ b/crates/core/src/render/render_manager.rs @@ -153,18 +153,28 @@ impl RadiantRenderManager { }); { + let background_color = if selection { + wgpu::Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + } + } else { + wgpu::Color { + r: 0.1, + g: 0.2, + b: 0.3, + a: 1.0, + } + }; let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Render Pass"), color_attachments: &[Some(wgpu::RenderPassColorAttachment { view: view, resolve_target: None, ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, - a: 1.0, - }), + load: wgpu::LoadOp::Clear(background_color), store: true, }, })], diff --git a/crates/core/src/tools/selection_tool.rs b/crates/core/src/tools/selection_tool.rs index e308662..d78d3d7 100644 --- a/crates/core/src/tools/selection_tool.rs +++ b/crates/core/src/tools/selection_tool.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub enum SelectionToolMessage { SelectNode { - id: u64, + id: Option, }, TransformNode { id: u64, @@ -31,13 +31,18 @@ impl SelectionTool { impl> RadiantTool for SelectionTool { fn on_mouse_down(&mut self, node_id: u64, _position: [f32; 2]) -> Option { - if node_id > 0 { - self.active_node_id = Some(node_id - 1); - let message = SelectionToolMessage::SelectNode { id: node_id - 1 }; - Some(message.into()) - } else { - None - } + Some( + if node_id > 0 { + self.active_node_id = Some(node_id - 1); + SelectionToolMessage::SelectNode { + id: Some(node_id - 1), + } + } else { + self.active_node_id = None; + SelectionToolMessage::SelectNode { id: None } + } + .into(), + ) } fn on_mouse_move(&mut self, position: [f32; 2]) -> Option { diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 3888e4f..a8f54fa 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -15,5 +15,3 @@ pub use radiant_winit::run_native; #[cfg(target_arch = "wasm32")] pub mod wasm; -#[cfg(target_arch = "wasm32")] -pub use radiant_winit::run_wasm; diff --git a/crates/runtime/src/message.rs b/crates/runtime/src/message.rs index 4d75b19..e97cd9b 100644 --- a/crates/runtime/src/message.rs +++ b/crates/runtime/src/message.rs @@ -16,7 +16,7 @@ pub enum RadiantMessage { id: u64, }, SelectNode { - id: u64, + id: Option, }, AddNode { node_type: String, diff --git a/crates/runtime/src/runtime.rs b/crates/runtime/src/runtime.rs index 2138a42..6543d95 100644 --- a/crates/runtime/src/runtime.rs +++ b/crates/runtime/src/runtime.rs @@ -40,17 +40,21 @@ impl Runtime<'_, RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRu self.view.scene.document.set_active_artboard(id); } RadiantMessage::SelectNode { id } => { - if !self.view.scene.interaction_manager.is_interaction(id) { - self.view.scene.document.select(id); - if let Some(node) = self.view.scene.document.get_node(id) { - self.view - .scene - .interaction_manager - .enable_interactions(node, &self.view.scene.screen_descriptor); - return Some(RadiantResponse::NodeSelected(node.clone())); - } else { - self.view.scene.interaction_manager.disable_interactions(); + self.view.scene.document.select(id); + if let Some(id) = id { + if !self.view.scene.interaction_manager.is_interaction(id) { + if let Some(node) = self.view.scene.document.get_node(id) { + self.view + .scene + .interaction_manager + .enable_interactions(node, &self.view.scene.screen_descriptor); + return Some(RadiantResponse::NodeSelected(node.clone())); + } else { + self.view.scene.interaction_manager.disable_interactions(); + } } + } else { + self.view.scene.interaction_manager.disable_interactions(); } } RadiantMessage::AddNode { @@ -67,7 +71,7 @@ impl Runtime<'_, RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRu }; if let Some(node) = node { self.view.scene.add(node); - return self.handle_message(RadiantMessage::SelectNode { id }); + return self.handle_message(RadiantMessage::SelectNode { id: Some(id) }); } } RadiantMessage::TransformNode { @@ -158,14 +162,14 @@ impl Runtime<'_, RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRu texture_handle, )); self.view.scene.add(node); - return self.handle_message(RadiantMessage::SelectNode { id }); + return self.handle_message(RadiantMessage::SelectNode { id: Some(id) }); } RadiantMessage::AddText { position, .. } => { let id = self.view.scene.document.counter; let node = RadiantNodeType::Text(RadiantTextNode::new(id, position, [100.0, 100.0])); self.view.scene.add(node); - return self.handle_message(RadiantMessage::SelectNode { id }); + return self.handle_message(RadiantMessage::SelectNode { id: Some(id) }); } } None diff --git a/crates/winit/src/lib.rs b/crates/winit/src/lib.rs index 31fc51f..8ab658f 100644 --- a/crates/winit/src/lib.rs +++ b/crates/winit/src/lib.rs @@ -241,11 +241,7 @@ impl + TryInto, N: RadiantNode> impl + TryInto, N: RadiantNode> RadiantView { pub fn on_mouse_down(&mut self, position: [f32; 2]) -> Option { - let mut id = pollster::block_on(self.scene.select(position)); - // Todo: Hack - To be removed - if id > 1000 { - id = self.scene.document.counter; - } + let id = pollster::block_on(self.scene.select(position)); self.scene .tool_manager .active_tool()