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