Skip to content

Commit

Permalink
Moves app to view
Browse files Browse the repository at this point in the history
  • Loading branch information
codeneix committed Nov 5, 2023
1 parent 2c18565 commit 61981ec
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 149 deletions.
2 changes: 0 additions & 2 deletions crates/path/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod path_node;
pub mod path_tool;

pub use path_node::*;
pub use path_tool::*;
67 changes: 0 additions & 67 deletions crates/path/src/path_tool.rs

This file was deleted.

1 change: 1 addition & 0 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use radiant_path_node::RadiantPathNode;
pub use radiant_text_node::RadiantTextNode;

pub use radiant_winit::run_native;
pub use radiant_winit::Runtime;

#[cfg(target_arch = "wasm32")]
pub mod wasm;
Expand Down
1 change: 0 additions & 1 deletion crates/runtime/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize};
#[combine_enum(radiant_core::InteractionMessage)]
#[combine_enum(radiant_image_node::RadiantImageMessage)]
#[combine_enum(radiant_text_node::RadiantTextMessage)]
#[combine_enum(radiant_path_node::PathToolMessage)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RadiantMessage {
AddArtboard {},
Expand Down
62 changes: 31 additions & 31 deletions crates/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ use radiant_core::{
};
use radiant_image_node::RadiantImageNode;
use radiant_text_node::RadiantTextNode;
use radiant_winit::{RadiantApp, Runtime};
use radiant_winit::{RadiantView, Runtime};

use crate::{RadiantMessage, RadiantNodeType, RadiantResponse};

pub struct RadiantRuntime {
pub app: RadiantApp<RadiantMessage, RadiantNodeType>,
pub view: RadiantView<RadiantMessage, RadiantNodeType>,
}

impl RadiantRuntime {
pub async fn new() -> Self {
Self {
app: RadiantApp::new(SelectionTool::new()).await,
view: RadiantView::new(SelectionTool::new()).await,
}
}
}

impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime {
fn app(&mut self) -> &mut RadiantApp<RadiantMessage, RadiantNodeType> {
&mut self.app
fn view(&mut self) -> &mut RadiantView<RadiantMessage, RadiantNodeType> {
&mut self.view
}

fn handle_message(&mut self, message: RadiantMessage) -> Option<RadiantResponse> {
match message {
RadiantMessage::AddArtboard {} => {
self.app.scene.document.add_artboard();
self.view.scene.document.add_artboard();
}
RadiantMessage::SelectArtboard { id } => {
self.app.scene.document.set_active_artboard(id);
self.view.scene.document.set_active_artboard(id);
}
RadiantMessage::SelectNode { id } => {
if !self.app.scene.interaction_manager.is_interaction(id) {
self.app.scene.document.select(id);
if let Some(node) = self.app.scene.document.get_node(id) {
self.app
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.app.scene.screen_descriptor);
.enable_interactions(node, &self.view.scene.screen_descriptor);
return Some(RadiantResponse::NodeSelected(node.clone()));
} else {
self.app.scene.interaction_manager.disable_interactions();
self.view.scene.interaction_manager.disable_interactions();
}
}
}
Expand All @@ -52,15 +52,15 @@ impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntim
position,
scale,
} => {
let id = self.app.scene.document.counter;
let id = self.view.scene.document.counter;
let node = match node_type.as_str() {
"Rectangle" => Some(RadiantNodeType::Rectangle(RadiantRectangleNode::new(
id, position, scale,
))),
_ => None,
};
if let Some(node) = node {
self.app.scene.add(node);
self.view.scene.add(node);
return self.handle_message(RadiantMessage::SelectNode { id });
}
}
Expand All @@ -69,16 +69,16 @@ impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntim
position,
scale,
} => {
if self.app.scene.interaction_manager.is_interaction(id) {
if self.view.scene.interaction_manager.is_interaction(id) {
if let Some(message) = self
.app
.view
.scene
.interaction_manager
.handle_interaction(message)
{
return self.handle_message(message);
}
} else if let Some(node) = self.app.scene.document.get_node_mut(id) {
} else if let Some(node) = self.view.scene.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<TransformComponent>() {
component.transform_xy(&position);
component.transform_scale(&scale);
Expand All @@ -90,10 +90,10 @@ impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntim
};

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

return Some(response);
}
Expand All @@ -104,61 +104,61 @@ impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntim
position,
scale,
} => {
if let Some(node) = self.app.scene.document.get_node_mut(id) {
if let Some(node) = self.view.scene.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.app
self.view
.scene
.interaction_manager
.update_interactions(node, &self.app.scene.screen_descriptor);
.update_interactions(node, &self.view.scene.screen_descriptor);
}
}
}
RadiantMessage::SetFillColor { id, fill_color } => {
if let Some(node) = self.app.scene.document.get_node_mut(id) {
if let Some(node) = self.view.scene.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<ColorComponent>() {
component.set_fill_color(fill_color);
node.set_needs_tessellation();
}
}
}
RadiantMessage::SetStrokeColor { id, stroke_color } => {
if let Some(node) = self.app.scene.document.get_node_mut(id) {
if let Some(node) = self.view.scene.document.get_node_mut(id) {
if let Some(component) = node.get_component_mut::<ColorComponent>() {
component.set_stroke_color(stroke_color);
node.set_needs_tessellation();
}
}
}
RadiantMessage::SelectTool { id } => {
self.app.scene.tool_manager.activate_tool(id);
self.view.scene.tool_manager.activate_tool(id);
}
RadiantMessage::AddImage { .. } => {
let image = epaint::ColorImage::new([400, 100], epaint::Color32::RED);
let texture_handle =
self.app
self.view
.scene
.texture_manager
.load_texture("test", image, Default::default());

let id = self.app.scene.document.counter;
let id = self.view.scene.document.counter;
let node = RadiantNodeType::Image(RadiantImageNode::new(
id,
[400.0, 100.0],
[100.0, 100.0],
texture_handle,
));
self.app.scene.add(node);
self.view.scene.add(node);
return self.handle_message(RadiantMessage::SelectNode { id });
}
RadiantMessage::AddText { position, .. } => {
let id = self.app.scene.document.counter;
let id = self.view.scene.document.counter;
let node =
RadiantNodeType::Text(RadiantTextNode::new(id, position, [100.0, 100.0]));
self.app.scene.add(node);
self.view.scene.add(node);
return self.handle_message(RadiantMessage::SelectNode { id });
}
}
Expand Down
14 changes: 4 additions & 10 deletions crates/runtime/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::{Arc, RwLock};

use crate::RadiantRuntime;
use crate::{RadiantNodeType, RadiantRectangleNode, RadiantResponse, RectangleTool};
use crate::{RadiantNodeType, RadiantRectangleNode, RectangleTool, Runtime};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = RadiantAppController)]
Expand All @@ -17,20 +17,14 @@ impl RadiantAppController {
console_log::init_with_level(log::Level::Info).expect("Couldn't initialize logger");
log::info!("Hello from rust!");

let f2 = f.clone();
let handler: Box<dyn Fn(RadiantResponse)> = Box::new(move |response: RadiantResponse| {
let this = JsValue::null();
let _ = f2.call1(&this, &serde_wasm_bindgen::to_value(&response).unwrap());
});

let mut runtime = RadiantRuntime::new(handler).await;
let mut runtime = RadiantRuntime::new().await;
runtime
.app
.view
.scene
.tool_manager
.register_tool(Box::new(RectangleTool::new()));
runtime
.app
.view
.scene
.add(RadiantNodeType::Rectangle(RadiantRectangleNode::new(
1,
Expand Down
23 changes: 13 additions & 10 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;

pub struct RadiantApp<M, N: RadiantNode> {
pub struct RadiantView<M, N: RadiantNode> {
pub window: Window,
pub event_loop: Option<EventLoop<()>>,

Expand All @@ -24,7 +24,7 @@ pub struct RadiantApp<M, N: RadiantNode> {
mouse_dragging: bool,
}

impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> RadiantApp<M, N> {
impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> RadiantView<M, N> {
pub async fn new(default_tool: impl RadiantTool<M> + 'static) -> Self {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>
}
}

impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> RadiantApp<M, N> {
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
Expand All @@ -262,9 +262,12 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>
}
}

pub trait Runtime<M, N: RadiantNode, R> {
fn app(&mut self) -> &mut RadiantApp<M, N>;
pub trait Runtime<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode, R> {
fn view(&mut self) -> &mut RadiantView<M, N>;
fn handle_message(&mut self, message: M) -> Option<R>;

fn scene(&mut self) -> &mut RadiantScene<M, N> { &mut self.view().scene }
fn add(&mut self, node: N) { self.scene().add(node); }
}

pub fn run_native<
Expand All @@ -275,9 +278,9 @@ pub fn run_native<
mut runtime: impl Runtime<M, N, R> + 'static,
handler: Box<dyn Fn(R)>,
) {
if let Some(event_loop) = std::mem::replace(&mut runtime.app().event_loop, None) {
if let Some(event_loop) = std::mem::replace(&mut runtime.view().event_loop, None) {
event_loop.run(move |event, _, control_flow| {
if let Some(message) = runtime.app().handle_event(&event, control_flow) {
if let Some(message) = runtime.view().handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_message(message) {
handler(response);
}
Expand All @@ -286,7 +289,7 @@ pub fn run_native<
match event {
RedrawRequested(..) => {
let output_frame = std::mem::replace(
&mut runtime.app().scene.render_manager.current_texture,
&mut runtime.view().scene.render_manager.current_texture,
None,
);
output_frame.unwrap().present();
Expand All @@ -309,7 +312,7 @@ pub fn run_wasm<
let event_loop;
{
let Ok(mut runtime_) = runtime.write() else { return; };
event_loop = std::mem::replace(&mut runtime_.app().event_loop, None);
event_loop = std::mem::replace(&mut runtime_.view().event_loop, None);
}

let weak_runtime = Arc::downgrade(&runtime);
Expand All @@ -318,7 +321,7 @@ pub fn run_wasm<
event_loop.spawn(move |event, _, control_flow| {
if let Some(runtime) = weak_runtime.upgrade() {
if let Ok(mut runtime) = runtime.write() {
if let Some(message) = runtime.app().handle_event(&event, control_flow) {
if let Some(message) = runtime.view().handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_message(message) {
let this = JsValue::null();
let _ =
Expand Down
Loading

0 comments on commit 61981ec

Please sign in to comment.