transform and components

This commit is contained in:
2025-06-26 09:24:35 +02:00
parent f363bbdae1
commit 82b847314e
5 changed files with 237 additions and 11 deletions
+52 -7
View File
@@ -1,15 +1,20 @@
use std::any::Any;
use std::{
any::{Any, TypeId},
collections::HashMap,
};
use crate::{
events::EventManager,
extensions::{Extension, ExtensionManager},
render_scope::RenderScope,
style::Transform,
};
pub mod events;
pub mod extensions;
pub mod macros;
pub mod render_scope;
pub mod style;
pub mod text;
pub mod utils;
@@ -21,8 +26,39 @@ pub trait Element {
fn as_any(self: Box<Self>) -> Box<dyn Any>;
}
pub trait Component {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub struct Widget(pub Box<dyn Element>, HashMap<TypeId, Box<dyn Component>>);
impl Widget {
pub fn component<C: Component + 'static>(&mut self, c: C) -> &mut Self {
self.1.entry(c.type_id()).or_insert_with(|| Box::new(c));
self
}
pub fn set_component<C: Component + 'static>(&mut self, c: C) -> &mut Self {
self.1.insert(c.type_id(), Box::new(c));
self
}
pub fn get<C: Component + 'static>(&self) -> Option<&C> {
self.1
.get(&TypeId::of::<C>())
.and_then(|c| c.as_any().downcast_ref::<C>())
}
pub fn get_mut<C: Component + 'static>(&mut self) -> Option<&mut C> {
self.1
.get_mut(&TypeId::of::<C>())
.and_then(|c| c.as_any_mut().downcast_mut::<C>())
}
}
pub struct Screen {
pub elements: Vec<Box<dyn Element>>,
pub elements: Vec<Widget>,
}
impl Screen {
@@ -32,8 +68,10 @@ impl Screen {
}
}
pub fn draw<E: Element + 'static>(&mut self, element: E) {
self.elements.push(Box::new(element));
pub fn draw<E: Element + 'static>(&mut self, element: E) -> &mut Widget {
self.elements
.push(Widget(Box::new(element), HashMap::new()));
self.elements.last_mut().unwrap()
}
pub fn run(
@@ -41,10 +79,11 @@ impl Screen {
events: &mut EventManager,
extensions: &mut ExtensionManager,
) -> std::io::Result<()> {
let mut scope = RenderScope {};
let mut scope = RenderScope::new();
for elem in &mut self.elements {
elem.init(events);
elem.component(Transform::new());
elem.0.init(events);
}
extensions.init(self, events);
@@ -52,8 +91,14 @@ impl Screen {
loop {
extensions.tick_start(self, events);
utils::clear().unwrap();
for elem in &mut self.elements {
elem.render(&mut scope);
scope.clear();
elem.0.render(&mut scope);
if let Some(t) = elem.get() {
scope.set_transform(t);
}
scope.draw();
}
extensions.tick_end(self, events);