Skip to content

Commit

Permalink
da wardo
Browse files Browse the repository at this point in the history
  • Loading branch information
noxware committed Jun 11, 2024
1 parent 77a655b commit 4e63e79
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 35 deletions.
56 changes: 28 additions & 28 deletions hframe-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
use crate::{
composed_area::ComposedArea, platform::Platform, test_platform::TestPlatform, tree::Node,
world::World,
};

struct Context<P: Platform> {
tree: Node<ComposedArea>,
world: World,
platform: P,
}

#[cfg(test)]
mod tests {
use crate::{
composed_area::{ComposedAreaKind, ComposedHtml},
geo::{Pos, Size},
geo::{Pos, Rect, Size},
id::Id,
};

use super::*;

#[test]
fn it_works() {
let ctx = Context {
tree: Node::new(ComposedArea {
id: Id::from("root"),
size: Size::new(100.0, 100.0),
abs_pos: Pos::new(0.0, 0.0),
kind: ComposedAreaKind::Canvas,
})
.nest(
Node::new(ComposedArea {
id: Id::from("child"),
size: Size::new(50.0, 50.0),
abs_pos: Pos::new(10.0, 10.0),
kind: ComposedAreaKind::Canvas,
})
.nest(Node::new(ComposedArea {
id: Id::from("grandchild"),
size: Size::new(25.0, 25.0),
abs_pos: Pos::new(5.0, 5.0),
kind: ComposedAreaKind::Html(ComposedHtml {
content: "<div>hello</div>".into(),
}),
})),
),
let mut ctx = Context {
platform: TestPlatform {
mouse_pos: Pos::new(0.0, 0.0),
},
world: World::new(Rect::from((0.0, 0.0, 100.0, 100.0))),
};

ctx.tree
.find(|node| node.read(|data| data.value.id == Id::from("grandchild")))
.unwrap();
ctx.world.add(
Id::root(),
ComposedArea {
id: Id::from("child"),
size: Size::new(50.0, 50.0),
abs_pos: Pos::new(10.0, 10.0),
kind: ComposedAreaKind::Canvas,
},
);

ctx.world.add(
Id::from("child"),
ComposedArea {
id: Id::from("grandchild"),
size: Size::new(25.0, 25.0),
abs_pos: Pos::new(5.0, 5.0),
kind: ComposedAreaKind::Html(ComposedHtml {
content: "<div>hello</div>".into(),
}),
},
);

ctx.world.get(Id::from("grandchild")).unwrap();
}
}
21 changes: 14 additions & 7 deletions hframe-core/src/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ impl Size {
}
}

/*
#[derive(Debug, Clone, Copy, PartialEq, Default)]
struct Rect {
pos: Pos,
size: Size,
pub(crate) struct Rect {
pub(crate) pos: Pos,
pub(crate) size: Size,
}

impl Rect {
fn new(pos: Pos, size: Size) -> Self {
impl From<(Pos, Size)> for Rect {
fn from((pos, size): (Pos, Size)) -> Self {
Rect { pos, size }
}
}
*/

impl From<(f64, f64, f64, f64)> for Rect {
fn from((x, y, width, height): (f64, f64, f64, f64)) -> Self {
Rect {
pos: Pos::new(x, y),
size: Size::new(width, height),
}
}
}
7 changes: 7 additions & 0 deletions hframe-core/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use std::hash::{DefaultHasher, Hash, Hasher};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct Id(u64);

impl Id {
/// A semantic id representing the root of a tree-like resource.
pub(crate) fn root() -> Self {
Id(42)
}
}

impl From<u64> for Id {
fn from(id: u64) -> Self {
Id(id)
Expand Down
1 change: 1 addition & 0 deletions hframe-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod platform;
mod test_platform;
mod tree;
mod web_platform;
mod world;

pub fn add(left: usize, right: usize) -> usize {
left + right
Expand Down
3 changes: 3 additions & 0 deletions hframe-core/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ impl<T: Debug> Node<T> {
}))
}

/// Nests a node inside this node.
/// Returns self allowing you to chain more `nest` calls for this.
// TODO: This allows mutation without `read_mut`. This should not exist here.
pub(crate) fn nest(&self, node: Node<T>) -> Node<T> {
self.read_mut(|data| data.children.push(node));
self.clone()
Expand Down
40 changes: 40 additions & 0 deletions hframe-core/src/world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::{
composed_area::{ComposedArea, ComposedAreaKind},
geo::Rect,
id::Id,
tree::Node,
};

pub(crate) struct World(Node<ComposedArea>);

impl World {
pub(crate) fn new(viewport: Rect) -> Self {
World(Node::new(ComposedArea {
id: Id::root(),
size: viewport.size,
abs_pos: viewport.pos,
kind: ComposedAreaKind::Canvas,
}))
}

/// Getter for the root node of this world.
pub(crate) fn root(&self) -> Node<ComposedArea> {
self.0.clone()
}

/// Fetch a node from the tree by it's Id. Returns None if no node with the given Id is found.
pub(crate) fn get(&self, id: Id) -> Option<Node<ComposedArea>> {
self.0.find(|node| node.read(|data| data.value.id == id))
}

/// Add a new node to the tree under a specific Id.
/// If the parent Id is not found, this function will panic.
pub(crate) fn add(&mut self, parent_id: Id, area: ComposedArea) {
let parent = self
.0
.find(|node| node.read(|data| data.value.id == parent_id))
.expect("parent not found");

parent.nest(Node::new(area));
}
}

0 comments on commit 4e63e79

Please sign in to comment.