#13 cleanup extensions
This commit is contained in:
@@ -4,6 +4,8 @@ use crate::{component, extensions::Extension, widget::Widget, Screen};
|
|||||||
|
|
||||||
pub struct IdExtension(pub Arc<Screen>);
|
pub struct IdExtension(pub Arc<Screen>);
|
||||||
|
|
||||||
|
component!(Id(pub usize));
|
||||||
|
|
||||||
impl Extension for Arc<IdExtension> {}
|
impl Extension for Arc<IdExtension> {}
|
||||||
|
|
||||||
impl IdExtension {
|
impl IdExtension {
|
||||||
@@ -23,5 +25,3 @@ impl IdExtension {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
component!(Id(pub usize));
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
pub mod id;
|
pub mod id;
|
||||||
pub mod resources;
|
|
||||||
pub mod tick;
|
pub mod tick;
|
||||||
pub mod velocity;
|
pub mod velocity;
|
||||||
|
|
||||||
pub use id::*;
|
pub use id::*;
|
||||||
pub use resources::*;
|
|
||||||
pub use tick::*;
|
pub use tick::*;
|
||||||
pub use velocity::*;
|
pub use velocity::*;
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct VecResource<T>(Arc<Mutex<Box<dyn FnMut(&T) + Send + Sync>>>);
|
|
||||||
|
|
||||||
impl<T: Send + Sync + 'static> VecResource<T> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self(Arc::new(Mutex::new(Box::new(|_| {}))))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iterate<F: FnMut(&T) + Send + Sync + 'static>(&self, f: F) {
|
|
||||||
*self.0.lock().unwrap() = Box::new(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push(&self, e: T) {
|
|
||||||
let i = self.0.clone();
|
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
(i.lock().unwrap())(&e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,8 @@ use crate::{event, Screen};
|
|||||||
|
|
||||||
pub struct TickExtension(pub u16);
|
pub struct TickExtension(pub u16);
|
||||||
|
|
||||||
|
event!(TickEvent(pub u32));
|
||||||
|
|
||||||
impl Extension for TickExtension {
|
impl Extension for TickExtension {
|
||||||
fn init(&mut self, screen: Arc<Screen>) {
|
fn init(&mut self, screen: Arc<Screen>) {
|
||||||
let rate_dur = 1000 / self.0 as u64;
|
let rate_dur = 1000 / self.0 as u64;
|
||||||
@@ -25,5 +27,3 @@ impl Extension for TickExtension {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
event!(TickEvent(pub u32));
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ use crate::{
|
|||||||
|
|
||||||
pub struct VelocityExtension;
|
pub struct VelocityExtension;
|
||||||
|
|
||||||
|
component!(Velocity(pub i32, pub i32));
|
||||||
|
|
||||||
impl VelocityExtension {
|
impl VelocityExtension {
|
||||||
fn apply_velocity(ticks: u16, velocity: i32, x: &mut u16) {
|
fn apply_velocity(ticks: u16, velocity: i32, x: &mut u16) {
|
||||||
if velocity.abs() != 0 && ticks as i32 % (1000 / velocity.abs()) == 0 {
|
if velocity.abs() != 0 && ticks as i32 % (1000 / velocity.abs()) == 0 {
|
||||||
@@ -20,9 +22,7 @@ impl VelocityExtension {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl VelocityExtension {
|
|
||||||
fn apply_velocity_xy(ticks: u16, widget: &Arc<Widget>) {
|
fn apply_velocity_xy(ticks: u16, widget: &Arc<Widget>) {
|
||||||
if let Some(velocity) = widget.get::<Velocity>() {
|
if let Some(velocity) = widget.get::<Velocity>() {
|
||||||
if let Some(mut t) = widget.get::<Transform>() {
|
if let Some(mut t) = widget.get::<Transform>() {
|
||||||
@@ -62,5 +62,3 @@ impl Extension for VelocityExtension {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
component!(Velocity(pub i32, pub i32));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user