Skip to content

Commit

Permalink
Adds generics for View and Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
codeneix committed Nov 5, 2023
1 parent 5800549 commit 2fe4c28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
18 changes: 18 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ impl ScreenDescriptor {
]
}
}

pub trait View<M: From<InteractionMessage> + TryInto<InteractionMessage>, 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>, N: RadiantNode, R: 'a> {
type View: View<M, N>;

fn view(&self) -> &Self::View;
fn view_mut(&mut self) -> &mut Self::View;

fn handle_message(&mut self, message: M) -> Option<R>;

fn scene(&'a self) -> &RadiantScene<M, N> { self.view().scene() }
fn scene_mut(&'a mut self) -> &mut RadiantScene<M, N> { self.view_mut().scene_mut() }
// fn add(&'a mut self, node: N) { self.scene_mut().add(node); }
}
1 change: 0 additions & 1 deletion crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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
14 changes: 10 additions & 4 deletions crates/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use radiant_core::{
ColorComponent, RadiantComponentProvider, RadiantRectangleNode, RadiantTessellatable,
RadiantTransformable, SelectionTool, TransformComponent,
RadiantTransformable, SelectionTool, TransformComponent, Runtime,
};
use radiant_image_node::RadiantImageNode;
use radiant_text_node::RadiantTextNode;
use radiant_winit::{RadiantView, Runtime};
use radiant_winit::RadiantView;

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

Expand All @@ -20,8 +20,14 @@ impl RadiantRuntime {
}
}

impl Runtime<RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime {
fn view(&mut self) -> &mut RadiantView<RadiantMessage, RadiantNodeType> {
impl Runtime<'_, RadiantMessage, RadiantNodeType, RadiantResponse> for RadiantRuntime {
type View = RadiantView<RadiantMessage, RadiantNodeType>;

fn view(&self) -> &RadiantView<RadiantMessage, RadiantNodeType> {
&self.view
}

fn view_mut(&mut self) -> &mut RadiantView<RadiantMessage, RadiantNodeType> {
&mut self.view
}

Expand Down
32 changes: 18 additions & 14 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use radiant_core::{InteractionMessage, RadiantNode, RadiantScene, RadiantTool, ScreenDescriptor};
use radiant_core::{InteractionMessage, RadiantNode, RadiantScene, RadiantTool, ScreenDescriptor, Runtime, View};
use winit::event_loop::EventLoop;
use winit::window::{Window, WindowBuilder};
use winit::{event::*, event_loop::ControlFlow};
Expand Down Expand Up @@ -262,25 +262,29 @@ impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode>
}
}

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>;
impl<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode> View<M, N>
for RadiantView<M, N>
{
fn scene(&self) -> &RadiantScene<M, N> {
&self.scene
}

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

pub fn run_native<
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'static,
N: RadiantNode + 'static,
R: 'static,
>(
mut runtime: impl Runtime<M, N, R> + 'static,
mut runtime: impl Runtime<'static, M, N, R, View = RadiantView<M, N>> + 'static,
handler: Box<dyn Fn(R)>,
) {
if let Some(event_loop) = std::mem::replace(&mut runtime.view().event_loop, None) {
if let Some(event_loop) = std::mem::replace(&mut runtime.view_mut().event_loop, None) {
event_loop.run(move |event, _, control_flow| {
if let Some(message) = runtime.view().handle_event(&event, control_flow) {
if let Some(message) = runtime.view_mut().handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_message(message) {
handler(response);
}
Expand All @@ -289,7 +293,7 @@ pub fn run_native<
match event {
RedrawRequested(..) => {
let output_frame = std::mem::replace(
&mut runtime.view().scene.render_manager.current_texture,
&mut runtime.view_mut().scene_mut().render_manager.current_texture,
None,
);
output_frame.unwrap().present();
Expand All @@ -304,15 +308,15 @@ pub fn run_native<
pub fn run_wasm<
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'static,
N: RadiantNode + 'static,
R: serde::ser::Serialize,
R: serde::ser::Serialize + 'static,
>(
runtime: Arc<RwLock<impl Runtime<M, N, R> + 'static>>,
runtime: Arc<RwLock<impl Runtime<'static, M, N, R, View = RadiantView<M, N>> + 'static>>,
f: js_sys::Function,
) {
let event_loop;
{
let Ok(mut runtime_) = runtime.write() else { return; };
event_loop = std::mem::replace(&mut runtime_.view().event_loop, None);
event_loop = std::mem::replace(&mut runtime_.view_mut().event_loop, None);
}

let weak_runtime = Arc::downgrade(&runtime);
Expand All @@ -321,7 +325,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.view().handle_event(&event, control_flow) {
if let Some(message) = runtime.view_mut().handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_message(message) {
let this = JsValue::null();
let _ =
Expand Down
8 changes: 5 additions & 3 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use radiant_runtime::{
run_native, RadiantPathNode, RadiantRectangleNode, RadiantResponse,
RadiantRuntime, Runtime,
RadiantRuntime,
};

async fn run() {
Expand All @@ -16,14 +16,16 @@ async fn run() {

let mut runtime = RadiantRuntime::new().await;
runtime
.scene()
.view
.scene
.add(RadiantRectangleNode::new(
1,
[100.0, 100.0],
[200.0, 200.0],
).into());
runtime
.scene()
.view
.scene
.add(RadiantPathNode::new(
2,
[400.0, 400.0],
Expand Down

0 comments on commit 2fe4c28

Please sign in to comment.