use std::any::Any;
use super::Shape;
use crate::math::{Color, Vector2};
use crate::overlay::Overlay;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Anchor {
At(Vector2),
Fraction(Vector2),
}
impl Anchor {
pub const CENTER: Anchor = Anchor::Fraction(Vector2::new(0.5, 0.5));
pub fn resolve(&self, viewport: Vector2) -> Vector2 {
match self {
Anchor::At(p) => *p,
Anchor::Fraction(f) => Vector2::new(viewport.x * f.x, viewport.y * f.y),
}
}
}
pub struct Layout<'a> {
pub viewport: Vector2,
pub overlay: &'a mut Overlay,
}
#[derive(Debug, Clone, Copy)]
pub struct ComponentInput {
pub dt: f32,
pub hovered: bool,
pub pressed: bool,
}
pub trait Component: Any {
fn layout(&mut self, _layout: &mut Layout) {}
fn shape(&self) -> Shape;
fn update(&mut self, _input: &ComponentInput) {}
fn paint(&self, _overlay: &mut Overlay, _dpr: f32) {}
fn interactive(&self) -> bool {
true
}
}
#[derive(Debug, Clone)]
pub struct Button {
pub label: String,
pub anchor: Anchor,
pub height: f32,
pub padding: f32,
pub font_size: f32,
pub font_weight: u16,
pub text: Color,
pub press_scale: f32,
width: Option<f32>,
center: Vector2,
scale: f32,
velocity: f32,
}
impl Button {
pub fn new(label: impl Into<String>) -> Self {
Self {
label: label.into(),
anchor: Anchor::CENTER,
height: 64.0,
padding: 36.0,
font_size: 20.0,
font_weight: 700,
text: Color::from_hex(0xf4f4f4),
press_scale: 0.96,
width: None,
center: Vector2::ZERO,
scale: 1.0,
velocity: 0.0,
}
}
pub fn at(mut self, anchor: Anchor) -> Self {
self.anchor = anchor;
self
}
pub fn with_text_color(mut self, color: Color) -> Self {
self.text = color;
self
}
pub fn scale(&self) -> f32 {
self.scale
}
fn rest(&self) -> Shape {
Shape::pill(
self.center,
self.width.unwrap_or(self.height * 3.0),
self.height,
)
}
}
impl Component for Button {
fn layout(&mut self, layout: &mut Layout) {
if self.width.is_none() {
#[cfg(feature = "captions")]
{
layout.overlay.text_weight(self.font_weight);
layout.overlay.text_size(self.font_size);
self.width = Some(layout.overlay.measure_text(&self.label).0 + self.padding * 2.0);
}
#[cfg(not(feature = "captions"))]
{
self.width = Some(
self.label.chars().count() as f32 * self.font_size * 0.5 + self.padding * 2.0,
);
}
}
self.center = self.anchor.resolve(layout.viewport);
}
fn shape(&self) -> Shape {
self.rest().scaled(self.scale)
}
fn update(&mut self, input: &ComponentInput) {
let target = if input.pressed { self.press_scale } else { 1.0 };
let dt = input.dt.min(1.0 / 30.0);
self.velocity += ((target - self.scale) * 420.0 - self.velocity * 18.0) * dt;
self.scale += self.velocity * dt;
}
fn paint(&self, o: &mut Overlay, dpr: f32) {
let c = self.center;
o.no_stroke();
#[cfg(not(feature = "captions"))]
let _ = (c, dpr);
#[cfg(feature = "captions")]
{
o.text_weight(self.font_weight);
o.text_size(self.font_size * dpr * self.scale);
let (_, th) = o.measure_text(&self.label);
let (x, y) = (c.x * dpr, c.y * dpr - th / 2.0);
o.fill_alpha(Color::from_hex(0x000000), 0.75);
o.text_centered(x, y + dpr, &self.label);
o.fill(self.text);
o.text_centered(x, y, &self.label);
}
}
}
#[derive(Debug, Clone)]
pub struct Figure {
pub shape: Shape,
pub anchor: Option<Anchor>,
pub interactive: bool,
placed: Shape,
}
impl Figure {
pub fn new(shape: Shape) -> Self {
Self {
placed: shape.clone(),
shape,
anchor: None,
interactive: true,
}
}
pub fn at(mut self, anchor: Anchor) -> Self {
self.anchor = Some(anchor);
self
}
pub fn decorative(mut self) -> Self {
self.interactive = false;
self
}
}
impl Component for Figure {
fn layout(&mut self, layout: &mut Layout) {
self.placed = match self.anchor {
Some(a) => self.shape.centered_at(a.resolve(layout.viewport)),
None => self.shape.clone(),
};
}
fn shape(&self) -> Shape {
self.placed.clone()
}
fn interactive(&self) -> bool {
self.interactive
}
}