resources (not fully working yet)
This commit is contained in:
@@ -54,9 +54,8 @@ impl VGrid {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: &Arc<Self>, element: &Arc<Widget>) -> Arc<Self> {
|
pub fn draw(self: &Arc<Self>, element: &Arc<Widget>) -> Arc<Widget> {
|
||||||
let r2 = self.clone();
|
let r2 = self.clone();
|
||||||
let r = self.clone();
|
|
||||||
element.component(Handler::new(move |elem, e: &RenderWrapperEvent| {
|
element.component(Handler::new(move |elem, e: &RenderWrapperEvent| {
|
||||||
let scope = e.get_scope();
|
let scope = e.get_scope();
|
||||||
scope.clear();
|
scope.clear();
|
||||||
@@ -94,7 +93,7 @@ impl VGrid {
|
|||||||
|
|
||||||
scope.set_parent_size(w, h);
|
scope.set_parent_size(w, h);
|
||||||
}));
|
}));
|
||||||
r
|
element.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
pub mod id;
|
pub mod id;
|
||||||
pub mod tick;
|
pub mod tick;
|
||||||
pub mod velocity;
|
pub mod velocity;
|
||||||
|
pub mod resources;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::{type_name, Any},
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
@@ -45,8 +46,8 @@ impl<E: Event + 'static> Handler<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Event> Debug for Handler<T> {
|
impl<E: Event> Debug for Handler<E> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "Handler")
|
write!(f, "Handler({})", type_name::<E>())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use crate::{event, widget::Component};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Resource<T>(pub T);
|
||||||
|
|
||||||
|
impl<T: Send + Sync + 'static> Component for Resource<T> {
|
||||||
|
fn as_any(&self) -> &dyn std::any::Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
event!(ResourceUpdate);
|
||||||
+20
-3
@@ -3,12 +3,18 @@ use osui::{
|
|||||||
div::Div,
|
div::Div,
|
||||||
grid::{HGrid, VGrid},
|
grid::{HGrid, VGrid},
|
||||||
},
|
},
|
||||||
|
extensions::{
|
||||||
|
resources::Resource,
|
||||||
|
tick::{TickEvent, TickExtension},
|
||||||
|
Handler,
|
||||||
|
},
|
||||||
style::Transform,
|
style::Transform,
|
||||||
Screen,
|
Screen,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut screen = Screen::new();
|
let mut screen = Screen::new();
|
||||||
|
screen.extension(TickExtension(10));
|
||||||
|
|
||||||
let container = Div::new(0xffff00);
|
let container = Div::new(0xffff00);
|
||||||
|
|
||||||
@@ -23,12 +29,23 @@ fn main() {
|
|||||||
for _ in 1..6 {
|
for _ in 1..6 {
|
||||||
let row = HGrid::new(0xff00ff, 1);
|
let row = HGrid::new(0xff00ff, 1);
|
||||||
|
|
||||||
vgrid.draw(screen.draw(row.clone()));
|
let row_widget = vgrid.draw(
|
||||||
|
screen
|
||||||
|
.draw(row.clone())
|
||||||
|
.component(Resource(Vec::<&str>::from(["Test"])))
|
||||||
|
.component(Handler::new(|w, t: &TickEvent| {
|
||||||
|
if t.0 >= 10 {
|
||||||
|
let mut res = w.get::<Resource<Vec<&str>>>().unwrap();
|
||||||
|
res.0.push("Hello World");
|
||||||
|
w.set_component(res);
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
for i in 1..10 {
|
for i in row_widget.get::<Resource<Vec<&str>>>().unwrap().0 {
|
||||||
row.draw(
|
row.draw(
|
||||||
screen
|
screen
|
||||||
.draw((format!("Item {i}"), 0xff0000))
|
.draw((i.to_string(), 0xff0000))
|
||||||
.component(Transform::new()),
|
.component(Transform::new()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user