Skip to content

Commit

Permalink
Moves common messages to RadiantSceneMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
codeneix committed Nov 5, 2023
1 parent 9fe71b8 commit 61ddaa9
Show file tree
Hide file tree
Showing 27 changed files with 360 additions and 301 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions crates/core/src/interactions/bounding_box.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
InteractionMessage, RadiantInteraction, RadiantLineNode, RadiantNode, RadiantRectangleNode,
RadiantInteraction, RadiantLineNode, RadiantNode, RadiantRectangleNode, RadiantSceneMessage,
RadiantTessellatable, RadiantTransformable, ScreenDescriptor, TransformComponent,
};
use epaint::ClippedPrimitive;
Expand Down Expand Up @@ -187,45 +187,45 @@ impl RadiantInteraction for BoundingBoxInteraction {
}

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

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

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

impl<M: From<InteractionMessage> + TryInto<InteractionMessage>> RadiantInteractionManager<M> {
impl<M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage>> RadiantInteractionManager<M> {
pub fn new() -> Self {
Self {
bounding_box_interaction: BoundingBoxInteraction::new(),
Expand Down Expand Up @@ -54,7 +42,7 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>> RadiantInteracti

pub fn handle_interaction(&mut self, message: M) -> Option<M> {
match message.try_into() {
Ok(InteractionMessage::TransformNode { id, position, .. })
Ok(RadiantSceneMessage::TransformNode { id, position, .. })
if self.is_interaction(id) =>
{
if let Some(m) = self.bounding_box_interaction.handle(id, position) {
Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod components;
pub mod interactions;
pub mod message;
pub mod nodes;
pub mod render;
pub mod scene;
Expand All @@ -8,6 +9,7 @@ pub mod tools;

pub use components::*;
pub use interactions::*;
pub use message::*;
pub use nodes::*;
pub use render::*;
pub use scene::*;
Expand All @@ -33,14 +35,14 @@ impl ScreenDescriptor {
}
}

pub trait View<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> {
pub trait View<M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage>, N: RadiantNode> {
fn scene(&self) -> &RadiantScene<M, N>;
fn scene_mut(&mut self) -> &mut RadiantScene<M, N>;
}

pub trait Runtime<
'a,
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'a,
M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage> + 'a,
N: RadiantNode + 'a,
R: 'a,
>
Expand Down
46 changes: 46 additions & 0 deletions crates/core/src/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use serde::{Deserialize, Serialize};

use crate::RadiantNode;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RadiantSceneMessage {
AddArtboard {},
SelectArtboard {
id: u64,
},
SelectNode {
id: Option<u64>,
},
TransformNode {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
SetTransform {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
SetFillColor {
id: u64,
fill_color: epaint::Color32,
},
SetStrokeColor {
id: u64,
stroke_color: epaint::Color32,
},
SelectTool {
id: u32,
},
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RadiantSceneResponse<M, N: RadiantNode> {
Message(M),
NodeSelected(N),
TransformUpdated {
id: u64,
position: [f32; 2],
scale: [f32; 2],
},
}
2 changes: 1 addition & 1 deletion crates/core/src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait RadiantTessellatable {
) -> Vec<ClippedPrimitive>;
}

pub trait RadiantNode: RadiantTessellatable + RadiantComponentProvider {
pub trait RadiantNode: Clone + RadiantTessellatable + RadiantComponentProvider {
fn get_id(&self) -> u64;
fn set_id(&mut self, id: u64);
fn get_bounding_rect(&self) -> [f32; 4];
Expand Down
113 changes: 108 additions & 5 deletions crates/core/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
InteractionMessage, RadiantDocumentNode, RadiantInteractionManager, RadiantNode,
RadiantRenderManager, RadiantTessellatable, RadiantTextureManager, RadiantTool,
RadiantToolManager, ScreenDescriptor,
ColorComponent, RadiantDocumentNode, RadiantInteractionManager, RadiantNode,
RadiantRenderManager, RadiantSceneMessage, RadiantSceneResponse, RadiantTessellatable,
RadiantTextureManager, RadiantTool, RadiantToolManager, RadiantTransformable, ScreenDescriptor,
TransformComponent,
};
use epaint::{text::FontDefinitions, ClippedPrimitive, Fonts, TextureId};

Expand All @@ -17,7 +18,9 @@ pub struct RadiantScene<M, N: RadiantNode> {
pub texture_manager: RadiantTextureManager,
}

impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> RadiantScene<M, N> {
impl<M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage>, N: RadiantNode>
RadiantScene<M, N>
{
pub fn new(
config: wgpu::SurfaceConfiguration,
surface: wgpu::Surface,
Expand All @@ -41,7 +44,7 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>

fonts_manager,
render_manager,
tool_manager: RadiantToolManager::new(Box::new(default_tool)),
tool_manager: RadiantToolManager::new(0u32, Box::new(default_tool)),
interaction_manager: RadiantInteractionManager::new(),
texture_manager,
}
Expand Down Expand Up @@ -100,3 +103,103 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>
primitives
}
}

impl<M: From<RadiantSceneMessage> + TryInto<RadiantSceneMessage>, N: RadiantNode>
RadiantScene<M, N>
{
pub fn handle_message(
&mut self,
message: RadiantSceneMessage,
) -> Option<RadiantSceneResponse<M, N>> {
match message {
RadiantSceneMessage::AddArtboard {} => {
self.document.add_artboard();
}
RadiantSceneMessage::SelectArtboard { id } => {
self.document.set_active_artboard(id);
}
RadiantSceneMessage::SelectNode { id } => {
self.document.select(id);
if let Some(id) = id {
if !self.interaction_manager.is_interaction(id) {
if let Some(node) = self.document.get_node(id) {
self.interaction_manager
.enable_interactions(node, &self.screen_descriptor);
return Some(RadiantSceneResponse::NodeSelected(node.clone()));
} else {
self.interaction_manager.disable_interactions();
}
}
} else {
self.interaction_manager.disable_interactions();
}
}
RadiantSceneMessage::TransformNode {
id,
position,
scale,
} => {
if self.interaction_manager.is_interaction(id) {
if let Some(message) =
self.interaction_manager.handle_interaction(message.into())
{
return Some(RadiantSceneResponse::Message(message));
}
} else if let Some(node) = self.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<TransformComponent>() {
component.transform_xy(&position);
component.transform_scale(&scale);

let response = RadiantSceneResponse::TransformUpdated {
id,
position: component.get_xy(),
scale: component.get_scale(),
};

node.set_needs_tessellation();
self.interaction_manager
.update_interactions(node, &self.screen_descriptor);

return Some(response);
}
}
}
RadiantSceneMessage::SetTransform {
id,
position,
scale,
} => {
if let Some(node) = self.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<TransformComponent>() {
component.set_xy(&position);
component.set_scale(&scale);
node.set_needs_tessellation();

self.interaction_manager
.update_interactions(node, &self.screen_descriptor);
}
}
}
RadiantSceneMessage::SetFillColor { id, fill_color } => {
if let Some(node) = self.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<ColorComponent>() {
component.set_fill_color(fill_color);
node.set_needs_tessellation();
}
}
}
RadiantSceneMessage::SetStrokeColor { id, stroke_color } => {
if let Some(node) = self.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<ColorComponent>() {
component.set_stroke_color(stroke_color);
node.set_needs_tessellation();
}
}
}
RadiantSceneMessage::SelectTool { id } => {
self.tool_manager.activate_tool(id);
}
}
None
}
}
2 changes: 1 addition & 1 deletion crates/core/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use selection_tool::*;
pub use tool_manager::*;

pub trait RadiantTool<M> {
fn on_mouse_down(&mut self, node_id: u64, position: [f32; 2]) -> Option<M>;
fn on_mouse_down(&mut self, node_id: u64, node_count: u64, position: [f32; 2]) -> Option<M>;
fn on_mouse_move(&mut self, position: [f32; 2]) -> Option<M>;
fn on_mouse_up(&mut self, position: [f32; 2]) -> Option<M>;
}
Loading

0 comments on commit 61ddaa9

Please sign in to comment.