testing for event handling
This commit is contained in:
@@ -25,7 +25,6 @@ cc = "1.0"
|
||||
|
||||
[dependencies]
|
||||
crossterm = "0.28.1"
|
||||
downcast-rs = "2.0.1"
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
|
||||
+15
-11
@@ -1,17 +1,21 @@
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
collections::HashMap,
|
||||
};
|
||||
|
||||
pub mod text;
|
||||
pub mod macros;
|
||||
|
||||
use std::fmt::Debug;
|
||||
pub trait Event: Any {}
|
||||
|
||||
use downcast_rs::{impl_downcast, DowncastSync};
|
||||
pub trait Component: DowncastSync + Debug {}
|
||||
impl_downcast!(sync Component);
|
||||
pub struct Screen {
|
||||
element: Box<dyn Element>,
|
||||
event_handlers: HashMap<TypeId, Box<dyn FnMut(Box<dyn Event>)>>,
|
||||
}
|
||||
|
||||
impl Screen {
|
||||
|
||||
}
|
||||
|
||||
pub trait Element {
|
||||
fn get_component<T: Component + 'static>(&mut self) -> Option<&mut T>;
|
||||
fn add_component<T: Component + 'static>(&mut self, component: T);
|
||||
}
|
||||
|
||||
pub trait Extension {
|
||||
fn tick(&mut self);
|
||||
fn init(&mut self, screen: &mut Screen);
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#[macro_export]
|
||||
macro_rules! components {
|
||||
($($a:ty: $e:expr),*) => {
|
||||
{
|
||||
let mut hm: HashMap<TypeId, Box<dyn Component>> = HashMap::new();
|
||||
$(
|
||||
hm.insert(TypeId::of::<$a>(), Box::new($e));
|
||||
)*
|
||||
hm
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,12 +1,3 @@
|
||||
use osui::{
|
||||
text::{text, DisplayComponent},
|
||||
Element,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
let mut e = text("Hello, World!");
|
||||
|
||||
if let Some(comp) = e.get_component::<DisplayComponent>() {
|
||||
println!("Ok: {:?}", comp);
|
||||
}
|
||||
}
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
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<TypeId, Box<dyn Component>>,
|
||||
}
|
||||
|
||||
impl Element for Text {
|
||||
fn get_component<T: Component + 'static>(&mut self) -> Option<&mut T> {
|
||||
self.components
|
||||
.get_mut(&TypeId::of::<T>())?
|
||||
.downcast_mut::<T>()
|
||||
}
|
||||
|
||||
fn add_component<T: Component + 'static>(&mut self, component: T) {
|
||||
if !self.components.contains_key(&TypeId::of::<T>()) {
|
||||
self.components
|
||||
.insert(TypeId::of::<T>(), Box::new(component));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text(text: &str) -> Text {
|
||||
Text {
|
||||
text: text.to_string(),
|
||||
components: components!(DisplayComponent: DisplayComponent()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user