Skip to content

Commit

Permalink
Adds convenience functions on Runtime Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
codeneix committed Nov 5, 2023
1 parent 2fe4c28 commit 87594a6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 52 deletions.
21 changes: 17 additions & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,28 @@ pub trait View<M: From<InteractionMessage> + TryInto<InteractionMessage>, N: Rad
fn scene_mut(&mut self) -> &mut RadiantScene<M, N>;
}

pub trait Runtime<'a, M: From<InteractionMessage> + TryInto<InteractionMessage>, N: RadiantNode, R: 'a> {
pub trait Runtime<
'a,
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'a,
N: RadiantNode + 'a,
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); }
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);
}
}
4 changes: 1 addition & 3 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ fn derive_node_internal(item: TokenStream2) -> syn::Result<TokenStream2> {
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();
let nodes = item.variants.iter().map(|variant| {
let fields = variant
.fields
.iter();
let fields = variant.fields.iter();
quote! {
#(#fields)*
}
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use radiant_core::{
ColorComponent, RadiantComponentProvider, RadiantRectangleNode, RadiantTessellatable,
RadiantTransformable, SelectionTool, TransformComponent, Runtime,
RadiantTransformable, Runtime, SelectionTool, TransformComponent,
};
use radiant_image_node::RadiantImageNode;
use radiant_text_node::RadiantTextNode;
Expand Down
10 changes: 8 additions & 2 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use radiant_core::{InteractionMessage, RadiantNode, RadiantScene, RadiantTool, ScreenDescriptor, Runtime, View};
use radiant_core::{
InteractionMessage, RadiantNode, RadiantScene, RadiantTool, Runtime, ScreenDescriptor, View,
};
use winit::event_loop::EventLoop;
use winit::window::{Window, WindowBuilder};
use winit::{event::*, event_loop::ControlFlow};
Expand Down Expand Up @@ -293,7 +295,11 @@ pub fn run_native<
match event {
RedrawRequested(..) => {
let output_frame = std::mem::replace(
&mut runtime.view_mut().scene_mut().render_manager.current_texture,
&mut runtime
.view_mut()
.scene_mut()
.render_manager
.current_texture,
None,
);
output_frame.unwrap().present();
Expand Down
20 changes: 3 additions & 17 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use radiant_runtime::{
run_native, RadiantPathNode, RadiantRectangleNode, RadiantResponse,
RadiantRuntime,
run_native, RadiantPathNode, RadiantRectangleNode, RadiantResponse, RadiantRuntime, Runtime,
};

async fn run() {
Expand All @@ -15,21 +14,8 @@ async fn run() {
});

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

run_native(runtime, handler);
}
Expand Down
30 changes: 5 additions & 25 deletions examples/egui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use radiant_runtime::{
RadiantMessage, RadiantNodeType, RadiantPathNode, RadiantRectangleNode, RadiantResponse,
RadiantRuntime, RadiantTextNode, RectangleTool, Runtime,
RadiantMessage, RadiantPathNode, RadiantRectangleNode, RadiantResponse, RadiantRuntime,
RadiantTextNode, RectangleTool, Runtime,
};
use std::iter;
use winit::event::Event::RedrawRequested;
Expand Down Expand Up @@ -58,29 +58,9 @@ async fn run() {
.scene
.tool_manager
.register_tool(Box::new(RectangleTool::new()));
runtime
.view
.scene
.add(RadiantNodeType::Rectangle(RadiantRectangleNode::new(
1,
[200.0, 200.0],
[200.0, 200.0],
)));
runtime
.view
.scene
.add(RadiantNodeType::Path(RadiantPathNode::new(
2,
[400.0, 400.0],
)));
runtime
.view
.scene
.add(RadiantNodeType::Text(RadiantTextNode::new(
3,
[400.0, 600.0],
[200.0, 200.0],
)));
runtime.add(RadiantRectangleNode::new(1, [200.0, 200.0], [200.0, 200.0]).into());
runtime.add(RadiantPathNode::new(2, [400.0, 400.0]).into());
runtime.add(RadiantTextNode::new(3, [400.0, 600.0], [200.0, 200.0]).into());
// view.scene.handle_message(RadiantMessage::AddText {
// text: String::from("Hello World!"),
// position: [400.0, 600.0],
Expand Down

0 comments on commit 87594a6

Please sign in to comment.