canary-rs/scripts/sao-ui/src/widgets/mod.rs

83 lines
2.0 KiB
Rust

// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{CursorEventKind, Vec2};
use canary_script::{api::DrawContext, Rect};
pub mod button;
pub mod dialog;
pub mod flex;
pub mod menu;
pub mod palette;
pub mod scroll;
pub mod shell;
pub mod text;
#[allow(unused)]
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) {}
}
impl<T: Widget> Widget for Option<T> {
fn update(&mut self, dt: f32) {
if let Some(inner) = self.as_mut() {
inner.update(dt);
}
}
fn draw(&mut self, ctx: &DrawContext) {
if let Some(inner) = self.as_mut() {
inner.draw(ctx);
}
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
if let Some(inner) = self.as_mut() {
inner.on_cursor_event(kind, at);
}
}
}
pub trait RectBounds {
fn get_bounds(&self) -> Rect;
}
pub trait Button {
fn was_clicked(&self) -> bool;
}
#[allow(unused)]
pub trait Container {
fn with_children(&mut self, f: impl FnMut(&mut dyn Widget));
fn update(&mut self, dt: f32) {}
fn draw(&mut self, ctx: &DrawContext) {}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
}
impl<T: Container> Widget for T {
fn update(&mut self, dt: f32) {
<Self as Container>::update(self, dt);
self.with_children(|w| w.update(dt));
}
fn draw(&mut self, ctx: &DrawContext) {
<Self as Container>::draw(self, ctx);
self.with_children(|w| w.draw(ctx));
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
<Self as Container>::on_cursor_event(self, kind, at);
self.with_children(|w| w.on_cursor_event(kind, at));
}
}
pub mod prelude {
pub use super::*;
pub use crate::anim::Animation;
pub use crate::style::{self, THEME};
pub use canary_script::{*, api::*};
pub use keyframe::functions::*;
}