working extension system
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
pub mod tick;
|
||||
pub mod velocity;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::widget::Widget;
|
||||
|
||||
pub trait Extension {
|
||||
fn render(&self, _widget: &Arc<Widget>) {}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{component, extensions::Extension, widget::Widget};
|
||||
|
||||
pub struct TickExtension;
|
||||
|
||||
impl Extension for TickExtension {
|
||||
fn render(&self, _widget: &Arc<Widget>) {
|
||||
if let Some(on_tick) = _widget.get::<OnTick>() {
|
||||
(on_tick.0)(_widget)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
component!(OnTick(pub fn(&Arc<Widget>)));
|
||||
@@ -0,0 +1,44 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
component,
|
||||
extensions::Extension,
|
||||
style::{Position, Transform},
|
||||
widget::Widget,
|
||||
};
|
||||
|
||||
pub struct VelocityExtension;
|
||||
|
||||
impl Extension for VelocityExtension {
|
||||
fn render(&self, _widget: &Arc<Widget>) {
|
||||
if let Some(velocity) = _widget.get::<Velocity>() {
|
||||
if let Some(mut t) = _widget.get::<Transform>() {
|
||||
match &mut t.x {
|
||||
Position::Const(x) => {
|
||||
if velocity.0 > 0 {
|
||||
*x += 1;
|
||||
} else if velocity.0 < 0 {
|
||||
*x -= 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match &mut t.y {
|
||||
Position::Const(y) => {
|
||||
if velocity.1 > 0 {
|
||||
*y += 1;
|
||||
} else if velocity.1 < 0 {
|
||||
*y -= 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
_widget.set_component(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
component!(Velocity(pub i32, pub i32));
|
||||
Reference in New Issue
Block a user