diff --git a/Cargo.toml b/Cargo.toml index 813e624..8c22adc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] package.authors = ["Daniel Bulant"] - +resolver = "2" members = [ "ui", "mangades" diff --git a/mangades/src/main.rs b/mangades/src/main.rs index ca444aa..a79f581 100644 --- a/mangades/src/main.rs +++ b/mangades/src/main.rs @@ -1,6 +1,6 @@ use std::sync::{RwLock, Arc}; -use mangui::{self, nodes::{layout::Layout, self, Style, TaffyStyle}, taffy::{self, prelude::Size, style::Dimension}, femtovg::{Paint, Color}, CurrentRenderer, SharedTNode}; +use mangui::{self, nodes::{layout::Layout, self, Style, TaffyStyle}, taffy::{self, prelude::Size, style::Dimension}, femtovg::{Paint, Color}, CurrentRenderer, SharedNode}; fn main() { let mut root = Layout::::new(); @@ -81,7 +81,7 @@ fn main() { fill: Paint::color(Color::rgb(0, 0, 255)), radius: 0. }))); - let groot: SharedTNode = Arc::new(RwLock::new(root)); + let groot: SharedNode = Arc::new(RwLock::new(root)); mangui::run_event_loop(groot); } diff --git a/ui/src/lib.rs b/ui/src/lib.rs index 6e377eb..779ed7f 100644 --- a/ui/src/lib.rs +++ b/ui/src/lib.rs @@ -1,7 +1,6 @@ -use std::collections::HashMap; use std::num::NonZeroU32; use std::ops::Deref; -use std::sync::{Arc, RwLock, Weak, RwLockReadGuard}; +use std::sync::{Arc, RwLock, Weak}; use femtovg::renderer::OpenGl; use femtovg::{Canvas, Color}; @@ -32,13 +31,12 @@ pub use taffy; pub use femtovg; pub type CurrentRenderer = OpenGl; -pub type TNode = dyn Node; -pub type SharedTNode = Arc>>; -type WeakTNode = Weak>>; -type TNodePtr = Option>>; -type NodeLayoutMap = PtrWeakKeyHashMap>>, taffy::node::Node>; +pub type SharedNode = Arc>; +type WeakNode = Weak>; +type NodePtr = Option>; +type NodeLayoutMap = PtrWeakKeyHashMap>, taffy::node::Node>; -pub fn run_event_loop(root_node: SharedTNode) -> ! { +pub fn run_event_loop(root_node: SharedNode) -> ! { let event_loop = EventLoop::new(); let (buffer_context, gl_display, window, surface) = create_window(&event_loop); @@ -119,6 +117,7 @@ pub fn run_event_loop(root_node: SharedTNode) -> ! { // dbg!(&root); render(&buffer_context, &surface, &window, &mut context, &root); }, + // In the future, window should be created after resuming from suspend (for android support) _ => {} }) } @@ -165,8 +164,8 @@ fn render( buffer_context: &PossiblyCurrentContext, surface: &Surface, window: &Window, - context: &mut RenderContext, - root_node: &SharedTNode + context: &mut RenderContext, + root_node: &SharedNode ) { let size = window.inner_size(); context.canvas.reset(); diff --git a/ui/src/nodes/layout.rs b/ui/src/nodes/layout.rs index 4d4f3fb..857ad15 100644 --- a/ui/src/nodes/layout.rs +++ b/ui/src/nodes/layout.rs @@ -1,17 +1,17 @@ use std::fmt::{Debug, Formatter}; use femtovg::Renderer; -use crate::nodes::{Node, NodeChildren, Overflow, RenderContext, Style}; +use crate::nodes::{Node, NodeChildren, Overflow, Style}; use taffy::style::{Style as TaffyStyle, Dimension}; #[derive(Clone, Default)] -pub struct Layout { +pub struct Layout { pub style: Style, - pub children: NodeChildren + pub children: NodeChildren } -impl Layout { - pub fn new() -> Layout { +impl Layout { + pub fn new() -> Layout { Layout { style: Style { layout: TaffyStyle::default(), @@ -22,7 +22,7 @@ impl Layout { } } -impl Debug for Layout { +impl Debug for Layout { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("Layout") .field("style", &self.style) @@ -31,11 +31,11 @@ impl Debug for Layout { } } -impl Node for Layout { +impl Node for Layout { fn style(&self) -> &Style { &self.style } - fn children(&self) -> Option<&NodeChildren> { + fn children(&self) -> Option<&NodeChildren> { Some(&self.children) } fn resize(&mut self, width: f32, height: f32) { diff --git a/ui/src/nodes/mod.rs b/ui/src/nodes/mod.rs index f4053ec..26756d3 100644 --- a/ui/src/nodes/mod.rs +++ b/ui/src/nodes/mod.rs @@ -3,26 +3,26 @@ pub mod primitives; use std::fmt::Debug; use std::sync::{Arc, RwLock}; -use femtovg::{Canvas, Renderer, Color}; +use femtovg::{Canvas, Color}; use taffy::layout::Layout; pub use taffy::style::Style as TaffyStyle; use taffy::Taffy; -use crate::{NodeLayoutMap, TNodePtr}; +use crate::{NodeLayoutMap, NodePtr, CurrentRenderer}; -type SharedTNode = Arc>>; +type SharedTNode = Arc>; -pub struct RenderContext { - pub canvas: Canvas, - pub node_layout: NodeLayoutMap, +pub struct RenderContext { + pub canvas: Canvas, + pub node_layout: NodeLayoutMap, pub taffy: Taffy, - pub mouse: TNodePtr, - pub keyboard_focus: TNodePtr + pub mouse: NodePtr, + pub keyboard_focus: NodePtr } -impl RenderContext { +impl RenderContext { /// Fills a rectangle area with the specified color, using the current transform of the canvas. /// Rotation WILL break this, this is mostly for simple scaling and translation. - fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: Color) { + pub fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: Color) { let transform = self.canvas.transform(); let x = transform[0] * x as f32 + transform[2] * y as f32 + transform[4]; let y = transform[1] * x as f32 + transform[3] * y as f32 + transform[5]; @@ -50,26 +50,26 @@ pub struct Style { pub overflow: Overflow } -type NodeChildren = Vec>; +type NodeChildren = Vec; -pub trait Node: Debug { +pub trait Node: Debug { /// Return style. Usually, you just want self.style. fn style(&self) -> &Style; /// Returns the children of the node. If the node has no children, return None (empty Vec also works, None is mainly for nodes without children support). - fn children(&self) -> Option<&NodeChildren>; + fn children(&self) -> Option<&NodeChildren>; /// Render the node, called before rendering it's children /// Canvas considers 0, 0 to be top left corner (for location after layouting happens) - fn render_pre_children(&self, _context: &mut RenderContext, _layout: Layout) {} + fn render_pre_children(&self, _context: &mut RenderContext, _layout: Layout) {} /// Render the node, called after rendering it's children /// Canvas considers 0, 0 to be top left corner (for location after layouting happens) - fn render_post_children(&self, _context: &mut RenderContext, _layout: Layout) {} + fn render_post_children(&self, _context: &mut RenderContext, _layout: Layout) {} /// Called when the size of window changes on the root node. Layouts do implement this. /// Is an optional function instead of another trait because of missing support for trait upcasting // TODO: When rust supports trait upcasting, make this a trait - fn resize(&mut self, width: f32, height: f32) {} + fn resize(&mut self, _width: f32, _height: f32) {} } -pub fn layout_recursively(node: &SharedTNode, context: &mut RenderContext) -> taffy::node::Node { +pub fn layout_recursively(node: &SharedTNode, context: &mut RenderContext) -> taffy::node::Node { let taffy_node = context.node_layout.get(node); let taffy_node = match taffy_node { Some(taffy_node) => taffy_node, @@ -96,7 +96,7 @@ pub fn layout_recursively(node: &SharedTNode, context: &mut Rend taffy_node } -pub fn render_recursively(node: &SharedTNode, context: &mut RenderContext) { +pub fn render_recursively(node: &SharedTNode, context: &mut RenderContext) { let read_node = node.read().unwrap(); let styles = read_node.style(); let taffy_node = context.node_layout.get(node).unwrap(); diff --git a/ui/src/nodes/primitives.rs b/ui/src/nodes/primitives.rs index f84c925..5cec0a9 100644 --- a/ui/src/nodes/primitives.rs +++ b/ui/src/nodes/primitives.rs @@ -19,14 +19,14 @@ impl Rectangle { } } -impl Node for Rectangle { +impl Node for Rectangle { fn style(&self) -> &Style { &self.style } - fn children(&self) -> Option<&NodeChildren> { + fn children(&self) -> Option<&NodeChildren> { None } - fn render_pre_children(&self, context: &mut RenderContext, layout: Layout) { + fn render_pre_children(&self, context: &mut RenderContext, layout: Layout) { let mut path = Path::new(); path.rounded_rect( 0.,