Skip to content

Commit

Permalink
Refactors message out from core to scene
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Oct 26, 2023
1 parent 2717612 commit fdc2a00
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 140 deletions.
22 changes: 11 additions & 11 deletions crates/core/src/interactions/bounding_box.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
RadiantMessage, RadiantInteraction, RadiantLineNode, RadiantNode,
RadiantInteraction, RadiantLineNode, RadiantNode,
RadiantRectangleNode, RadiantTessellatable, RadiantTransformable,
ScreenDescriptor, TransformComponent,
ScreenDescriptor, TransformComponent, InteractionMessage
};
use epaint::ClippedPrimitive;

Expand Down Expand Up @@ -188,45 +188,45 @@ impl RadiantInteraction for BoundingBoxInteraction {
}

impl BoundingBoxInteraction {
pub fn handle(&mut self, id: u64, transform: [f32; 2]) -> Option<RadiantMessage> {
pub fn handle(&mut self, id: u64, transform: [f32; 2]) -> Option<InteractionMessage> {
let Some(node_id) = self.active_node_id else { return None; };
match id {
BOUNDING_BOX_TOP_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_TOP_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [0.0, transform[1]],
scale: [0.0, -transform[1]],
}),
BOUNDING_BOX_RIGHT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_RIGHT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [0.0, 0.0],
scale: [transform[0], 0.0],
}),
BOUNDING_BOX_BOTTOM_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_BOTTOM_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [0.0, 0.0],
scale: [0.0, transform[1]],
}),
BOUNDING_BOX_LEFT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_LEFT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [transform[0], 0.0],
scale: [-transform[0], 0.0],
}),
BOUNDING_BOX_TOP_RIGHT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_TOP_RIGHT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [0.0, transform[1]],
scale: [transform[0], -transform[1]],
}),
BOUNDING_BOX_BOTTOM_RIGHT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_BOTTOM_RIGHT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [0.0, 0.0],
scale: [transform[0], transform[1]],
}),
BOUNDING_BOX_BOTTOM_LEFT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_BOTTOM_LEFT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [transform[0], 0.0],
scale: [-transform[0], transform[1]],
}),
BOUNDING_BOX_TOP_LEFT_ID => Some(RadiantMessage::TransformNode {
BOUNDING_BOX_TOP_LEFT_ID => Some(InteractionMessage::TransformNode {
id: node_id,
position: [transform[0], transform[1]],
scale: [-transform[0], -transform[1]],
Expand Down
29 changes: 22 additions & 7 deletions crates/core/src/interactions/interaction_manager.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use epaint::ClippedPrimitive;
use serde::{Deserialize, Serialize};
use crate::{BoundingBoxInteraction, RadiantNode, ScreenDescriptor};

use crate::{BoundingBoxInteraction, RadiantMessage, RadiantNode, ScreenDescriptor};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum InteractionMessage {
TransformNode {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
}

pub struct RadiantInteractionManager {
pub struct RadiantInteractionManager<M> {
pub bounding_box_interaction: BoundingBoxInteraction,
_phantom: std::marker::PhantomData<M>,
}

impl RadiantInteractionManager {
impl<M: From<InteractionMessage> + TryInto<InteractionMessage>> RadiantInteractionManager<M> {
pub fn new() -> Self {
Self {
bounding_box_interaction: BoundingBoxInteraction::new(),
_phantom: std::marker::PhantomData,
}
}

Expand Down Expand Up @@ -39,10 +50,14 @@ impl RadiantInteractionManager {
.update(node, screen_descriptor);
}

pub fn handle_interaction(&mut self, message: RadiantMessage) -> Option<RadiantMessage> {
match message {
RadiantMessage::TransformNode { id, position, .. } if self.is_interaction(id) => {
self.bounding_box_interaction.handle(id, position)
pub fn handle_interaction(&mut self, message: M) -> Option<M> {
match message.try_into() {
Ok(InteractionMessage::TransformNode { id, position, .. }) if self.is_interaction(id) => {
if let Some(m) = self.bounding_box_interaction.handle(id, position) {
Some(m.into())
} else {
None
}
}
_ => None,
}
Expand Down
2 changes: 0 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ pub mod nodes;
pub mod render;
pub mod texture;
pub mod tools;
pub mod message;

pub use components::*;
pub use interactions::*;
pub use nodes::*;
pub use render::*;
pub use texture::*;
pub use tools::*;
pub use message::*;

/// Information about the screen used for rendering.
pub struct ScreenDescriptor {
Expand Down
49 changes: 0 additions & 49 deletions crates/core/src/message.rs

This file was deleted.

11 changes: 4 additions & 7 deletions crates/core/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ pub use rectangle_tool::*;
pub use selection_tool::*;
pub use tool_manager::*;

use crate::RadiantMessage;

pub trait RadiantTool {
fn tool_id(&self) -> u32;
pub trait RadiantTool<M> {
fn on_mouse_down(
&mut self,
node_id: u64,
position: [f32; 2],
) -> Option<RadiantMessage>;
) -> Option<M>;
fn on_mouse_move(
&mut self,
position: [f32; 2],
) -> Option<RadiantMessage>;
) -> Option<M>;
fn on_mouse_up(
&mut self,
position: [f32; 2],
) -> Option<RadiantMessage>;
) -> Option<M>;
}
38 changes: 25 additions & 13 deletions crates/core/src/tools/rectangle_tool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
use crate::{RadiantMessage, RadiantTool};
use serde::{Serialize, Deserialize};

use crate::RadiantTool;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RectangleToolMessage {
AddNode {
node_type: String,
position: [f32; 2],
scale: [f32; 2],
},
TransformNode {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
}

pub struct RectangleTool {
active_node_id: Option<u64>,
Expand All @@ -16,41 +32,37 @@ impl RectangleTool {
}
}

impl RadiantTool for RectangleTool {
fn tool_id(&self) -> u32 {
1
}

impl<M: From<RectangleToolMessage>> RadiantTool<M> for RectangleTool {
fn on_mouse_down(
&mut self,
node_id: u64,
position: [f32; 2],
) -> Option<RadiantMessage> {
let message = RadiantMessage::AddNode {
) -> Option<M> {
let message = RectangleToolMessage::AddNode {
node_type: String::from("Rectangle"),
position,
scale: [10.0, 10.0],
};
self.active_node_id = Some(node_id);
self.start_position = position;
self.prev_position = position;
Some(message)
Some(message.into())
}

fn on_mouse_move(
&mut self,
position: [f32; 2],
) -> Option<RadiantMessage> {
) -> Option<M> {
let result = if let Some(id) = self.active_node_id {
let message = RadiantMessage::TransformNode {
let message = RectangleToolMessage::TransformNode {
id: id,
position: [0.0, 0.0],
scale: [
position[0] - self.prev_position[0],
position[1] - self.prev_position[1],
],
};
Some(message)
Some(message.into())
} else {
None
};
Expand All @@ -61,7 +73,7 @@ impl RadiantTool for RectangleTool {
fn on_mouse_up(
&mut self,
_position: [f32; 2],
) -> Option<RadiantMessage> {
) -> Option<M> {
self.active_node_id = None;
self.start_position = [0.0, 0.0];
self.prev_position = [0.0, 0.0];
Expand Down
33 changes: 20 additions & 13 deletions crates/core/src/tools/selection_tool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
use crate::{RadiantMessage, RadiantTool};
use serde::{Serialize, Deserialize};
use crate::RadiantTool;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum SelectionToolMessage {
SelectNode(u64),
TransformNode {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
}

pub struct SelectionTool {
active_node_id: Option<u64>,
Expand All @@ -14,20 +25,16 @@ impl SelectionTool {
}
}

impl RadiantTool for SelectionTool {
fn tool_id(&self) -> u32 {
0
}

impl<M: From<SelectionToolMessage>> RadiantTool<M> for SelectionTool {
fn on_mouse_down(
&mut self,
node_id: u64,
_position: [f32; 2],
) -> Option<RadiantMessage> {
) -> Option<M> {
if node_id > 0 {
self.active_node_id = Some(node_id - 1);
let message = RadiantMessage::SelectNode(node_id - 1);
Some(message)
let message = SelectionToolMessage::SelectNode(node_id - 1);
Some(message.into())
} else {
None
}
Expand All @@ -36,17 +43,17 @@ impl RadiantTool for SelectionTool {
fn on_mouse_move(
&mut self,
position: [f32; 2],
) -> Option<RadiantMessage> {
) -> Option<M> {
let result = if let Some(id) = self.active_node_id {
let message = RadiantMessage::TransformNode {
let message = SelectionToolMessage::TransformNode {
id: id,
position: [
position[0] - self.prev_position[0],
position[1] - self.prev_position[1],
],
scale: [0.0, 0.0],
};
Some(message)
Some(message.into())
} else {
None
};
Expand All @@ -57,7 +64,7 @@ impl RadiantTool for SelectionTool {
fn on_mouse_up(
&mut self,
_position: [f32; 2],
) -> Option<RadiantMessage> {
) -> Option<M> {
self.active_node_id = None;
self.prev_position = [0.0, 0.0];
None
Expand Down
Loading

0 comments on commit fdc2a00

Please sign in to comment.