multithreading
This commit is contained in:
@@ -6,5 +6,6 @@ use std::sync::Arc;
|
||||
use crate::widget::Widget;
|
||||
|
||||
pub trait Extension {
|
||||
fn init(&self, _widgets: &Vec<Arc<Widget>>) {}
|
||||
fn render(&self, _widget: &Arc<Widget>) {}
|
||||
}
|
||||
|
||||
+14
-5
@@ -2,13 +2,22 @@ use std::sync::Arc;
|
||||
|
||||
use crate::{component, extensions::Extension, widget::Widget};
|
||||
|
||||
pub struct TickExtension;
|
||||
pub struct TickExtension(pub u16);
|
||||
|
||||
impl Extension for TickExtension {
|
||||
fn render(&self, _widget: &Arc<Widget>) {
|
||||
if let Some(on_tick) = _widget.get::<OnTick>() {
|
||||
(on_tick.0)(_widget)
|
||||
}
|
||||
fn init(&self, widgets: &Vec<Arc<Widget>>) {
|
||||
let rate_dur = 1000 / self.0 as u64;
|
||||
std::thread::spawn({
|
||||
let widgets = widgets.clone();
|
||||
move || loop {
|
||||
for w in &widgets {
|
||||
if let Some(on_tick) = w.get::<OnTick>() {
|
||||
(on_tick.0)(&w)
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-7
@@ -16,21 +16,21 @@ pub mod utils;
|
||||
pub mod widget;
|
||||
|
||||
pub struct Screen {
|
||||
pub elements: Vec<Arc<Widget>>,
|
||||
pub widgets: Vec<Arc<Widget>>,
|
||||
extensions: Vec<Arc<Box<dyn Extension>>>,
|
||||
}
|
||||
|
||||
impl Screen {
|
||||
pub fn new() -> Screen {
|
||||
Screen {
|
||||
elements: Vec::new(),
|
||||
widgets: Vec::new(),
|
||||
extensions: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw<E: Element + 'static>(&mut self, element: E) -> &Arc<Widget> {
|
||||
self.elements.push(Arc::new(Widget::new(Box::new(element))));
|
||||
self.elements.last().unwrap()
|
||||
self.widgets.push(Arc::new(Widget::new(Box::new(element))));
|
||||
self.widgets.last().unwrap()
|
||||
}
|
||||
|
||||
pub fn extension<E: Extension + 'static>(&mut self, ext: E) {
|
||||
@@ -38,15 +38,19 @@ impl Screen {
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> std::io::Result<()> {
|
||||
for elem in &mut self.elements {
|
||||
for elem in &mut self.widgets {
|
||||
elem.component(Transform::new());
|
||||
}
|
||||
|
||||
for ext in &self.extensions {
|
||||
ext.init(&self.widgets);
|
||||
}
|
||||
|
||||
utils::hide_cursor()?;
|
||||
|
||||
loop {
|
||||
self.render()?;
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
std::thread::sleep(std::time::Duration::from_millis(28));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +58,7 @@ impl Screen {
|
||||
let mut scope = RenderScope::new();
|
||||
|
||||
utils::clear()?;
|
||||
for elem in &self.elements {
|
||||
for elem in &self.widgets {
|
||||
scope.clear();
|
||||
if let Some(t) = elem.get() {
|
||||
scope.set_transform(&t);
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ use osui::{
|
||||
fn main() {
|
||||
let mut screen = Screen::new();
|
||||
screen.extension(VelocityExtension);
|
||||
screen.extension(TickExtension);
|
||||
screen.extension(TickExtension(60));
|
||||
|
||||
screen
|
||||
.draw(format!("Hello, World!"))
|
||||
@@ -20,7 +20,7 @@ fn main() {
|
||||
if let Some(mut v) = w.get::<Velocity>() {
|
||||
if let Some(t) = w.get::<Transform>() {
|
||||
if let Position::Const(c) = t.x {
|
||||
if c == 30 {
|
||||
if c >= 30 {
|
||||
v.0 = 0;
|
||||
w.set_component(v);
|
||||
}
|
||||
|
||||
+7
-4
@@ -6,21 +6,24 @@ use std::{
|
||||
|
||||
use crate::render_scope::RenderScope;
|
||||
|
||||
pub trait Element {
|
||||
pub type BoxedElement = Box<dyn Element + Send + Sync>;
|
||||
pub type BoxedComponent = Box<dyn Component + Send + Sync>;
|
||||
|
||||
pub trait Element: Send + Sync {
|
||||
#[allow(unused)]
|
||||
fn render(&mut self, scope: &mut RenderScope) {}
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
pub trait Component {
|
||||
pub trait Component: Send + Sync {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
pub struct Widget(
|
||||
pub Mutex<Box<dyn Element>>,
|
||||
Mutex<HashMap<TypeId, Box<dyn Component>>>,
|
||||
pub Mutex<BoxedElement>,
|
||||
Mutex<HashMap<TypeId, BoxedComponent>>,
|
||||
);
|
||||
|
||||
impl Widget {
|
||||
|
||||
Reference in New Issue
Block a user