sao-ui-rs/src/widgets.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2022-07-08 19:48:32 +00:00
use crate::anim::Animation;
2022-07-08 18:48:31 +00:00
use crate::draw::DrawContext;
2022-07-08 19:48:32 +00:00
use crate::{Color, CursorEventKind, Vec2};
2022-07-08 18:48:31 +00:00
pub trait Widget {
fn update(&mut self, dt: f32);
fn draw(&mut self, ctx: &DrawContext);
2022-07-08 19:48:32 +00:00
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2);
2022-07-08 18:48:31 +00:00
}
pub struct RoundButton {
pub pos: Vec2,
2022-07-08 19:48:32 +00:00
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),
}
}
2022-07-08 18:48:31 +00:00
}
impl Widget for RoundButton {
2022-07-08 19:48:32 +00:00
fn update(&mut self, dt: f32) {
self.shrink_anim.update(dt);
}
2022-07-08 18:48:31 +00:00
fn draw(&mut self, ctx: &DrawContext) {
2022-07-08 19:48:32 +00:00
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);
},
_ => {}
}
2022-07-08 18:48:31 +00:00
}
}