Skip to content

Commit

Permalink
Fixes deselection bug
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Nov 5, 2023
1 parent 87594a6 commit 9fe71b8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 42 deletions.
16 changes: 9 additions & 7 deletions crates/core/src/nodes/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl<N: RadiantNode> RadiantDocumentNode<N> {
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<u64>) {
if id == self.selected_node_id {
return;
}
self.artboards.iter_mut().for_each(|artboard| {
Expand All @@ -57,14 +57,16 @@ impl<N: RadiantNode> RadiantDocumentNode<N> {
}
}
}
if let Some(node) = artboard.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<SelectionComponent>() {
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::<SelectionComponent>() {
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> {
Expand Down
22 changes: 16 additions & 6 deletions crates/core/src/render/render_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})],
Expand Down
21 changes: 13 additions & 8 deletions crates/core/src/tools/selection_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum SelectionToolMessage {
SelectNode {
id: u64,
id: Option<u64>,
},
TransformNode {
id: u64,
Expand All @@ -31,13 +31,18 @@ impl SelectionTool {

impl<M: From<SelectionToolMessage>> RadiantTool<M> for SelectionTool {
fn on_mouse_down(&mut self, node_id: u64, _position: [f32; 2]) -> Option<M> {
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<M> {
Expand Down
2 changes: 0 additions & 2 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion crates/runtime/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum RadiantMessage {
id: u64,
},
SelectNode {
id: u64,
id: Option<u64>,
},
AddNode {
node_type: String,
Expand Down
30 changes: 17 additions & 13 deletions crates/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>

impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> RadiantView<M, N> {
pub fn on_mouse_down(&mut self, position: [f32; 2]) -> Option<M> {
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()
Expand Down

0 comments on commit 9fe71b8

Please sign in to comment.