sao-ui-rs/src/widgets.rs

58 lines
1.4 KiB
Rust

use crate::anim::Animation;
use crate::draw::DrawContext;
use crate::{Color, CursorEventKind, Vec2};
pub trait Widget {
fn update(&mut self, dt: f32);
fn draw(&mut self, ctx: &DrawContext);
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2);
}
pub struct RoundButton {
pub pos: Vec2,
pub radius: f32,
pub spacing: f32,
pub thickness: f32,
pub shrink_anim: Animation,
}
impl Default for RoundButton {
fn default() -> Self {
Self {
pos: Default::default(),
radius: 0.3,
spacing: 0.01,
thickness: 0.01,
shrink_anim: Animation::new(1.0),
}
}
}
impl Widget for RoundButton {
fn update(&mut self, dt: f32) {
self.shrink_anim.update(dt);
}
fn draw(&mut self, ctx: &DrawContext) {
let color = Color {
r: 1.0,
g: 0.0,
b: 1.0,
a: 1.0,
};
let spacing = self.shrink_anim.get() * self.spacing;
ctx.draw_circle(&self.pos, self.radius, &color);
ctx.draw_ring(&self.pos, self.radius + spacing, self.thickness, &color);
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
match kind {
CursorEventKind::Select => if at.distance(self.pos) < self.radius {
self.shrink_anim.ease_to(Box::new(keyframe::functions::EaseIn), 0.2, 0.0);
},
_ => {}
}
}
}