working extension system

This commit is contained in:
2025-06-29 11:49:11 -05:00
parent af719d83db
commit 85bfa0f45f
5 changed files with 115 additions and 7 deletions
+10
View File
@@ -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>) {}
}
+15
View File
@@ -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>)));
+44
View File
@@ -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));