From 6c0dde8e85ca9728cfb0f030780dd6b906550276 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 24 Jun 2025 17:21:12 +0200 Subject: [PATCH] test --- src/lib.rs | 44 +++++++++++++------------------------------- src/macros.rs | 12 ++++++++++++ src/main.rs | 33 +++++++-------------------------- src/output.rs | 31 ------------------------------- src/text.rs | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 88 deletions(-) create mode 100644 src/macros.rs delete mode 100644 src/output.rs create mode 100644 src/text.rs diff --git a/src/lib.rs b/src/lib.rs index 2fa7fb2..f5ee407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,35 +1,17 @@ -use std::{any::TypeId, thread, time::Duration}; +pub mod text; +pub mod macros; -pub mod output; +use std::fmt::Debug; + +use downcast_rs::{impl_downcast, DowncastSync}; +pub trait Component: DowncastSync + Debug {} +impl_downcast!(sync Component); + +pub trait Element { + fn get_component(&mut self) -> Option<&mut T>; + fn add_component(&mut self, component: T); +} pub trait Extension { - fn tick(&mut self, args: &mut Args); -} - -pub trait BaseElement { - fn init(&mut self); - fn get_components<'a>(&'a mut self) -> &'a mut Vec>; -} - -pub trait ElementComponent { - fn get_type_id(&self) -> TypeId; -} - -pub struct Args { - pub tick_rate: u16, - pub base_elements: Vec>, -} - -pub fn run(mut args: Args, mut extensions: Vec>) { - for elem in &mut args.base_elements { - elem.init(); - } - - loop { - for extension in &mut extensions { - extension.tick(&mut args); - } - - thread::sleep(Duration::from_millis(1000 / args.tick_rate as u64)); - } + fn tick(&mut self); } diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..1583097 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,12 @@ +#[macro_export] +macro_rules! components { + ($($a:ty: $e:expr),*) => { + { + let mut hm: HashMap> = HashMap::new(); + $( + hm.insert(TypeId::of::<$a>(), Box::new($e)); + )* + hm + } + }; +} diff --git a/src/main.rs b/src/main.rs index 06a0713..9f00e6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,12 @@ use osui::{ - output::{OutputExtension, RenderAble}, - run, Args, BaseElement, + text::{text, DisplayComponent}, + Element, }; -pub struct MyElem(*mut Vec>); -impl BaseElement for MyElem { - fn init(&mut self) { - unsafe { - (*self.0).push(RenderAble::new(|| String::from("value"))); - } - } - - fn get_components<'a>(&'a mut self) -> &'a mut Vec> { - unsafe { &mut *self.0 } - } -} - -impl MyElem { - pub fn new(mut v: Vec>) -> Box { - Box::new(MyElem(&mut v)) - } -} - fn main() { - let args = Args { - tick_rate: 40, - base_elements: vec![], - }; - run(args, vec![Box::new(OutputExtension)]); + let mut e = text("Hello, World!"); + + if let Some(comp) = e.get_component::() { + println!("Ok: {:?}", comp); + } } diff --git a/src/output.rs b/src/output.rs deleted file mode 100644 index 13a37c4..0000000 --- a/src/output.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::{any::TypeId, fmt::Debug}; - -use crate::{ElementComponent, Extension}; - -pub struct OutputExtension; - -impl Extension for OutputExtension { - fn tick(&mut self, args: &mut crate::Args) { - for elem in &mut args.base_elements { - for comp in elem.get_components() { - if comp.get_type_id() == TypeId::of::() { - comp - } - } - } - } -} - -pub struct RenderAble(pub Box String>); - -impl ElementComponent for RenderAble { - fn get_type_id(&self) -> std::any::TypeId { - TypeId::of::() - } -} - -impl RenderAble { - pub fn new String + 'static>(f: F) -> Box { - Box::new(RenderAble(Box::new(f))) - } -} diff --git a/src/text.rs b/src/text.rs new file mode 100644 index 0000000..197b2cc --- /dev/null +++ b/src/text.rs @@ -0,0 +1,33 @@ +use crate::{components, Component, Element}; +use std::{any::TypeId, collections::HashMap}; + +#[derive(Debug)] +pub struct DisplayComponent(); +impl Component for DisplayComponent {} + +pub struct Text { + text: String, + components: HashMap>, +} + +impl Element for Text { + fn get_component(&mut self) -> Option<&mut T> { + self.components + .get_mut(&TypeId::of::())? + .downcast_mut::() + } + + fn add_component(&mut self, component: T) { + if !self.components.contains_key(&TypeId::of::()) { + self.components + .insert(TypeId::of::(), Box::new(component)); + } + } +} + +pub fn text(text: &str) -> Text { + Text { + text: text.to_string(), + components: components!(DisplayComponent: DisplayComponent()), + } +}