use crate::draw_list::DrawList;
use crate::image::NineSliceImage;
use crate::text::{FontAtlas, GLYPH_HEIGHT, LINE_HEIGHT};
use glam::Vec2;
pub const NATURAL_FONT_SIZE: f32 = GLYPH_HEIGHT as f32;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
pub text: [f32; 4],
pub bg: [f32; 4],
pub bg_hover: [f32; 4],
pub bg_active: [f32; 4],
pub border: [f32; 4],
}
impl Default for Style {
fn default() -> Self {
Self {
text: [1.0, 1.0, 1.0, 1.0], bg: [0.15, 0.15, 0.15, 0.9], bg_hover: [0.25, 0.25, 0.3, 0.9], bg_active: [0.35, 0.35, 0.4, 0.9], border: [0.4, 0.4, 0.4, 1.0], }
}
}
#[derive(Debug, Clone)]
pub struct Label {
pub text: String,
pub font_size: f32,
pub show_bg: bool,
}
impl Label {
#[must_use]
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
font_size: 14.0,
show_bg: false,
}
}
#[must_use]
pub fn with_size(mut self, size: f32) -> Self {
self.font_size = size;
self
}
#[must_use]
pub fn with_bg(mut self, show: bool) -> Self {
self.show_bg = show;
self
}
fn scale(&self) -> f32 {
self.font_size / NATURAL_FONT_SIZE
}
pub fn render(&self, dl: &mut DrawList, pos: Vec2, atlas: &FontAtlas, style: &Style) {
if self.show_bg {
let size = self.size(atlas);
dl.rect(pos, pos + size, style.bg);
}
dl.text(pos, &self.text, style.text, self.font_size);
}
#[must_use]
pub fn width(&self, atlas: &FontAtlas) -> f32 {
atlas.text_width(&self.text, self.scale()) + 4.0
}
#[must_use]
pub fn height_for(&self, atlas: &FontAtlas) -> f32 {
atlas.line_count(&self.text) as f32 * LINE_HEIGHT * self.scale()
}
#[must_use]
pub fn height(&self) -> f32 {
LINE_HEIGHT * self.scale()
}
#[must_use]
pub fn size(&self, atlas: &FontAtlas) -> Vec2 {
Vec2::new(self.width(atlas), self.height_for(atlas))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonState {
Idle,
Hovered,
Pressed,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct SkinInsets {
pub left: f32,
pub right: f32,
pub top: f32,
pub bottom: f32,
}
impl SkinInsets {
pub const NONE: Self = Self {
left: 0.0,
right: 0.0,
top: 0.0,
bottom: 0.0,
};
#[must_use]
pub const fn new(left: f32, right: f32, top: f32, bottom: f32) -> Self {
Self {
left,
right,
top,
bottom,
}
}
#[must_use]
pub const fn uniform(inset: f32) -> Self {
Self::new(inset, inset, inset, inset)
}
#[must_use]
pub fn minimum_size(&self) -> Vec2 {
Vec2::new(self.left + self.right, self.top + self.bottom)
}
#[must_use]
pub fn min_corner(&self) -> Vec2 {
Vec2::new(self.left, self.top)
}
#[must_use]
pub fn max_corner(&self) -> Vec2 {
Vec2::new(self.right, self.bottom)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ButtonSkin {
pub idle: NineSliceImage,
pub hovered: NineSliceImage,
pub pressed: NineSliceImage,
}
impl ButtonSkin {
#[must_use]
pub const fn uniform(frame: NineSliceImage) -> Self {
Self {
idle: frame,
hovered: frame,
pressed: frame,
}
}
#[must_use]
pub const fn frame(&self, state: ButtonState) -> &NineSliceImage {
match state {
ButtonState::Idle => &self.idle,
ButtonState::Hovered => &self.hovered,
ButtonState::Pressed => &self.pressed,
}
}
#[must_use]
pub fn insets(&self) -> SkinInsets {
self.idle.clamped_insets()
}
#[must_use]
pub fn insets_agree(&self) -> bool {
let idle = self.idle.clamped_insets();
self.hovered.clamped_insets() == idle && self.pressed.clamped_insets() == idle
}
pub fn draw(&self, dl: &mut DrawList, state: ButtonState, min: Vec2, max: Vec2, scale: f32) {
dl.nine_slice(min, max, self.frame(state), scale, [1.0; 4]);
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct PointerInput {
pub pos: Vec2,
pub down: bool,
pub released: bool,
pub secondary_pressed: bool,
}
impl PointerInput {
#[must_use]
pub fn hovering(pos: Vec2) -> Self {
Self {
pos,
down: false,
released: false,
secondary_pressed: false,
}
}
}
pub type WidgetId = u64;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct UiState {
active: Option<WidgetId>,
}
impl UiState {
#[must_use]
pub fn new() -> Self {
Self { active: None }
}
#[must_use]
pub fn active(&self) -> Option<WidgetId> {
self.active
}
pub fn clear(&mut self) {
self.active = None;
}
pub fn interact(
&mut self,
id: WidgetId,
hovered: bool,
pointer_down: bool,
pointer_released: bool,
) -> (ButtonState, bool) {
if pointer_down && hovered && self.active.is_none() {
self.active = Some(id);
}
let captured = self.active == Some(id);
let clicked = pointer_released && captured && hovered;
if pointer_released && captured {
self.active = None;
}
let state = if captured {
ButtonState::Pressed
} else if hovered && self.active.is_none() {
ButtonState::Hovered
} else {
ButtonState::Idle
};
(state, clicked)
}
}
#[derive(Debug, Clone)]
pub struct Button {
pub text: String,
pub font_size: f32,
pub padding: Vec2,
pub skin: Option<SkinInsets>,
pub fixed_size: Option<Vec2>,
}
impl Button {
#[must_use]
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
font_size: 14.0,
padding: Vec2::new(8.0, 4.0),
skin: None,
fixed_size: None,
}
}
#[must_use]
pub fn with_size(mut self, size: f32) -> Self {
self.font_size = size;
self
}
#[must_use]
pub fn with_skin(mut self, insets: SkinInsets) -> Self {
self.skin = Some(insets);
self
}
#[must_use]
pub fn with_fixed_size(mut self, size: Vec2) -> Self {
self.fixed_size = Some(size);
self
}
fn scale(&self) -> f32 {
self.font_size / NATURAL_FONT_SIZE
}
fn insets(&self) -> SkinInsets {
self.skin.unwrap_or(SkinInsets::NONE)
}
#[must_use]
pub fn label_size(&self, atlas: &FontAtlas) -> Vec2 {
let scale = self.scale();
Vec2::new(
atlas.text_width(&self.text, scale),
atlas.line_count(&self.text) as f32 * LINE_HEIGHT * scale,
)
}
#[must_use]
pub fn minimum_size(&self, atlas: &FontAtlas) -> Vec2 {
self.insets().minimum_size() + self.label_size(atlas) + self.padding * 2.0
}
#[must_use]
pub fn size(&self, atlas: &FontAtlas) -> Vec2 {
let minimum = self.minimum_size(atlas);
self.fixed_size.map_or(minimum, |asked| asked.max(minimum))
}
#[must_use]
pub fn rect(&self, pos: Vec2, atlas: &FontAtlas) -> (Vec2, Vec2) {
(pos, pos + self.size(atlas))
}
#[must_use]
pub fn content_rect(&self, pos: Vec2, atlas: &FontAtlas) -> (Vec2, Vec2) {
let insets = self.insets();
let (min, max) = self.rect(pos, atlas);
(
min + insets.min_corner() + self.padding,
max - insets.max_corner() - self.padding,
)
}
#[must_use]
pub fn label_pos(&self, pos: Vec2, atlas: &FontAtlas) -> Vec2 {
let (min, max) = self.content_rect(pos, atlas);
min + (max - min - self.label_size(atlas)) * 0.5
}
#[must_use]
pub fn hit_test(&self, pos: Vec2, mouse: Vec2, atlas: &FontAtlas) -> bool {
let (min, max) = self.rect(pos, atlas);
mouse.x >= min.x && mouse.x <= max.x && mouse.y >= min.y && mouse.y <= max.y
}
pub fn interact(
&self,
pos: Vec2,
atlas: &FontAtlas,
ui: &mut UiState,
id: WidgetId,
pointer: PointerInput,
) -> (ButtonState, bool) {
let hovered = self.hit_test(pos, pointer.pos, atlas);
ui.interact(id, hovered, pointer.down, pointer.released)
}
pub fn render(
&self,
dl: &mut DrawList,
pos: Vec2,
atlas: &FontAtlas,
style: &Style,
state: ButtonState,
) {
if self.skin.is_none() {
let (min, max) = self.rect(pos, atlas);
let bg = match state {
ButtonState::Pressed => style.bg_active,
ButtonState::Hovered => style.bg_hover,
ButtonState::Idle => style.bg,
};
dl.rect(min, max, bg);
dl.rect_outline(min, max, 1.0, style.border);
}
dl.text(
self.label_pos(pos, atlas),
&self.text,
style.text,
self.font_size,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::draw_list::DrawCommand;
fn atlas() -> FontAtlas {
FontAtlas::built_in()
}
fn style() -> Style {
Style::default()
}
#[test]
fn label_renders_text_command() {
let label = Label::new("Hello");
let mut dl = DrawList::new();
label.render(&mut dl, Vec2::ZERO, &atlas(), &style());
assert!(!dl.is_empty());
assert!(matches!(dl.commands()[0], DrawCommand::Text { .. }));
}
#[test]
fn label_with_bg_adds_rect() {
let label = Label::new("Hi").with_bg(true);
let mut dl = DrawList::new();
label.render(&mut dl, Vec2::ZERO, &atlas(), &style());
assert_eq!(dl.len(), 2);
assert!(matches!(dl.commands()[0], DrawCommand::Rect { .. }));
assert!(matches!(dl.commands()[1], DrawCommand::Text { .. }));
}
#[test]
fn label_width_and_height_are_positive() {
let label = Label::new("Hello");
assert!(label.width(&atlas()) > 0.0);
assert!(label.height() > 0.0);
}
#[test]
fn declared_bounds_contain_the_glyphs_they_describe() {
let atlas = atlas();
for size in [10.0, 14.0, 28.0] {
let btn = Button::new("LongButtonLabel").with_size(size);
let pos = Vec2::new(50.0, 50.0);
let (min, max) = btn.rect(pos, &atlas);
let mut dl = DrawList::new();
btn.render(&mut dl, pos, &atlas, &style(), ButtonState::Idle);
let (verts, _) = dl.to_triangles(Some(&atlas), None, 1.0);
for v in &verts {
assert!(
v.pos.x >= min.x - 0.001
&& v.pos.x <= max.x + 0.001
&& v.pos.y >= min.y - 0.001
&& v.pos.y <= max.y + 0.001,
"size {size}: vertex {:?} escapes the declared rect {min:?}..{max:?}",
v.pos
);
}
}
}
#[test]
fn label_background_contains_its_glyphs() {
let atlas = atlas();
let label = Label::new("Hi").with_bg(true);
let pos = Vec2::new(10.0, 100.0);
let mut dl = DrawList::new();
label.render(&mut dl, pos, &atlas, &style());
let DrawCommand::Rect { min, max, .. } = dl.commands()[0] else {
panic!("expected a background Rect");
};
let (verts, _) = dl.to_triangles(Some(&atlas), None, 1.0);
assert!(!verts.is_empty());
for v in &verts {
assert!(
v.pos.y >= min.y - 0.001 && v.pos.y <= max.y + 0.001,
"glyph vertex {:?} falls outside the background {min:?}..{max:?}",
v.pos
);
}
}
#[test]
fn button_in_idle_state_renders_rect_and_text() {
let btn = Button::new("Click");
let mut dl = DrawList::new();
btn.render(&mut dl, Vec2::ZERO, &atlas(), &style(), ButtonState::Idle);
assert_eq!(dl.len(), 3);
}
#[test]
fn button_hit_test_inside() {
let btn = Button::new("Go");
let pos = Vec2::new(10.0, 10.0);
let (min, max) = btn.rect(pos, &atlas());
let centre = (min + max) * 0.5;
assert!(btn.hit_test(pos, centre, &atlas()));
}
#[test]
fn button_hit_test_outside() {
let btn = Button::new("Go");
let pos = Vec2::new(10.0, 10.0);
assert!(!btn.hit_test(pos, Vec2::new(0.0, 0.0), &atlas()));
}
#[test]
fn button_hit_test_agrees_with_its_rect_over_a_grid() {
const MARGIN: f32 = 6.0;
const STEP: f32 = 0.5;
let btn = Button::new("Go");
let atlas = atlas();
let pos = Vec2::new(10.0, 10.0);
let (min, max) = btn.rect(pos, &atlas);
let size = max - min;
assert!(
(size.x - size.y).abs() > STEP,
"the premise: a square button hides a transposed axis, and the sweep \
below would pass on one. This button is {size:?}"
);
let mut samples = 0_u32;
let mut inside = 0_u32;
let mut x = min.x - MARGIN;
while x <= max.x + MARGIN {
let mut y = min.y - MARGIN;
while y <= max.y + MARGIN {
let mouse = Vec2::new(x, y);
let want = x >= min.x && x <= max.x && y >= min.y && y <= max.y;
assert_eq!(
btn.hit_test(pos, mouse, &atlas),
want,
"at {mouse:?}, against a button spanning {min:?} to {max:?}"
);
samples += 1;
inside += u32::from(want);
y += STEP;
}
x += STEP;
}
assert!(
inside > 0 && inside < samples,
"a sweep entirely inside or entirely outside asserts nothing about an \
edge: {inside} of {samples} samples hit"
);
for corner in [min, Vec2::new(max.x, min.y), Vec2::new(min.x, max.y), max] {
assert!(
btn.hit_test(pos, corner, &atlas),
"the rectangle is inclusive, so its own corner {corner:?} is a hit"
);
}
}
#[test]
fn a_press_and_release_over_one_button_is_a_click_and_frees_the_capture() {
let btn = Button::new("Fire");
let atlas = atlas();
let mut ui = UiState::new();
let centre = {
let (min, max) = btn.rect(Vec2::ZERO, &atlas);
(min + max) * 0.5
};
let down = PointerInput {
pos: centre,
down: true,
released: false,
secondary_pressed: false,
};
let up = PointerInput {
pos: centre,
down: false,
released: true,
secondary_pressed: false,
};
let (state, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, down);
assert_eq!(state, ButtonState::Pressed);
assert!(!clicked);
let (_, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, up);
assert!(clicked);
assert_eq!(ui.active(), None, "the capture must be released");
}
#[test]
fn button_no_click_when_not_hovered() {
let btn = Button::new("Fire");
let atlas = atlas();
let mut ui = UiState::new();
let outside = PointerInput {
pos: Vec2::new(-100.0, -100.0),
down: false,
released: true,
secondary_pressed: false,
};
let (state, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, outside);
assert_eq!(state, ButtonState::Idle);
assert!(!clicked);
}
#[test]
fn press_on_a_and_release_on_b_clicks_neither() {
let atlas = atlas();
let a = Button::new("A");
let b = Button::new("B");
let a_pos = Vec2::new(0.0, 0.0);
let b_pos = Vec2::new(200.0, 0.0);
let inside = |btn: &Button, pos: Vec2| {
let (min, max) = btn.rect(pos, &atlas);
(min + max) * 0.5
};
let a_centre = inside(&a, a_pos);
let b_centre = inside(&b, b_pos);
let mut ui = UiState::new();
let held = |pos| PointerInput {
pos,
down: true,
released: false,
secondary_pressed: false,
};
let up = |pos| PointerInput {
pos,
down: false,
released: true,
secondary_pressed: false,
};
let (a_state, a_clicked) = a.interact(a_pos, &atlas, &mut ui, 1, held(a_centre));
let (b_state, b_clicked) = b.interact(b_pos, &atlas, &mut ui, 2, held(a_centre));
assert_eq!(a_state, ButtonState::Pressed);
assert_eq!(b_state, ButtonState::Idle);
assert!(!a_clicked && !b_clicked);
let (a_state, _) = a.interact(a_pos, &atlas, &mut ui, 1, held(b_centre));
let (b_state, _) = b.interact(b_pos, &atlas, &mut ui, 2, held(b_centre));
assert_eq!(a_state, ButtonState::Pressed, "A still owns the press");
assert_eq!(b_state, ButtonState::Idle, "B must not steal the capture");
let (_, a_clicked) = a.interact(a_pos, &atlas, &mut ui, 1, up(b_centre));
let (_, b_clicked) = b.interact(b_pos, &atlas, &mut ui, 2, up(b_centre));
assert!(!a_clicked, "A lost the cursor before release");
assert!(!b_clicked, "B never captured the press");
assert_eq!(ui.active(), None);
}
#[test]
fn hover_needs_no_capture_but_a_captured_press_suppresses_it() {
let mut ui = UiState::new();
assert_eq!(ui.interact(1, true, false, false).0, ButtonState::Hovered);
assert_eq!(ui.interact(1, true, true, false).0, ButtonState::Pressed);
assert_eq!(ui.interact(2, true, true, false).0, ButtonState::Idle);
ui.clear();
assert_eq!(ui.active(), None);
}
#[test]
fn button_rect_grows_with_text() {
let short = Button::new("A");
let long = Button::new("LongButton");
let (_, short_max) = short.rect(Vec2::ZERO, &atlas());
let (_, long_max) = long.rect(Vec2::ZERO, &atlas());
assert!(long_max.x > short_max.x);
}
const INSETS: SkinInsets = SkinInsets::new(4.0, 20.0, 3.0, 9.0);
#[test]
fn a_skinned_buttons_minimum_size_is_its_corners_plus_its_label() {
let atlas = atlas();
let plain = Button::new("Play");
let skinned = plain.clone().with_skin(INSETS);
let label = plain.label_size(&atlas);
assert!(label.x > 0.0 && label.y > 0.0);
assert_eq!(
plain.minimum_size(&atlas),
label + plain.padding * 2.0,
"an unskinned button's minimum must still be its label plus padding"
);
assert_eq!(plain.rect(Vec2::ZERO, &atlas).1, plain.minimum_size(&atlas));
assert_eq!(
skinned.minimum_size(&atlas) - plain.minimum_size(&atlas),
Vec2::new(INSETS.left + INSETS.right, INSETS.top + INSETS.bottom),
"the skin's corners must be added to the label's own minimum"
);
let empty = Button::new("").with_skin(INSETS);
let corners = INSETS.minimum_size();
assert!(
empty.minimum_size(&atlas).x >= corners.x && empty.minimum_size(&atlas).y >= corners.y,
"an empty label must still leave room for the four corners"
);
}
#[test]
fn a_button_asked_for_less_than_its_minimum_grows_to_it() {
let atlas = atlas();
let button = Button::new("Play").with_skin(INSETS);
let minimum = button.minimum_size(&atlas);
let squashed = button.clone().with_fixed_size(Vec2::new(1.0, 1.0));
assert_eq!(squashed.size(&atlas), minimum);
assert_eq!(squashed.rect(Vec2::ZERO, &atlas).1, minimum);
let roomy = minimum + Vec2::new(200.0, 40.0);
assert_eq!(button.clone().with_fixed_size(roomy).size(&atlas), roomy);
let mixed = button.with_fixed_size(Vec2::new(minimum.x + 200.0, 1.0));
assert_eq!(
mixed.size(&atlas),
Vec2::new(minimum.x + 200.0, minimum.y),
"the width was fine and must survive the height being clamped"
);
let (content_min, content_max) = mixed.content_rect(Vec2::ZERO, &atlas);
assert!(
content_max.x >= content_min.x && content_max.y >= content_min.y,
"the content box inverted: {content_min:?}..{content_max:?}"
);
}
#[test]
fn the_label_stays_centred_when_the_button_is_resized() {
let atlas = atlas();
let button = Button::new("Play").with_skin(INSETS);
let pos = Vec2::new(37.0, 11.0);
let label = button.label_size(&atlas);
for width in [button.minimum_size(&atlas).x, 200.0, 640.0] {
for height in [button.minimum_size(&atlas).y, 60.0, 180.0] {
let sized = button.clone().with_fixed_size(Vec2::new(width, height));
let (content_min, content_max) = sized.content_rect(pos, &atlas);
let anchor = sized.label_pos(pos, &atlas);
let before = anchor - content_min;
let after = content_max - (anchor + label);
assert!(
(before.x - after.x).abs() < 1e-3 && (before.y - after.y).abs() < 1e-3,
"at {width}x{height} the label is off-centre: {before:?} before, \
{after:?} after"
);
assert!(
before.x >= -1e-3 && before.y >= -1e-3,
"at {width}x{height} the label escapes its content box"
);
}
}
}
#[test]
fn an_unskinned_buttons_label_is_still_anchored_at_pos_plus_padding() {
let atlas = atlas();
for text in ["A", "LongButtonLabel", ""] {
for size in [10.0, 14.0, 28.0] {
let button = Button::new(text).with_size(size);
let pos = Vec2::new(50.0, 50.0);
let moved = (button.label_pos(pos, &atlas) - (pos + button.padding)).abs();
assert!(
moved.x < 1e-4 && moved.y < 1e-4,
"{text:?} at {size}: the label moved by {moved:?} px"
);
}
}
}
#[test]
fn a_skinned_button_draws_its_label_and_no_background() {
let atlas = atlas();
let button = Button::new("Play").with_skin(INSETS);
for state in [
ButtonState::Idle,
ButtonState::Hovered,
ButtonState::Pressed,
] {
let mut dl = DrawList::new();
button.render(&mut dl, Vec2::ZERO, &atlas, &style(), state);
assert_eq!(dl.len(), 1, "{state:?} emitted more than the label");
assert!(
matches!(dl.commands()[0], DrawCommand::Text { .. }),
"{state:?} emitted something that is not the label"
);
}
let mut dl = DrawList::new();
Button::new("Play").render(&mut dl, Vec2::ZERO, &atlas, &style(), ButtonState::Idle);
assert_eq!(dl.len(), 3);
}
#[test]
fn a_resized_skinned_button_is_clickable_across_its_whole_rect() {
let atlas = atlas();
let button = Button::new("Play")
.with_skin(INSETS)
.with_fixed_size(Vec2::new(400.0, 90.0));
let pos = Vec2::new(10.0, 20.0);
let (min, max) = button.rect(pos, &atlas);
assert_eq!(max - min, Vec2::new(400.0, 90.0));
assert!(button.hit_test(pos, (min + max) * 0.5, &atlas));
assert!(
button.hit_test(pos, min + Vec2::new(1.0, 1.0), &atlas),
"the top-left corner of the skin is part of the button"
);
assert!(
button.hit_test(pos, max - Vec2::new(1.0, 1.0), &atlas),
"the bottom-right corner of the skin is part of the button"
);
assert!(
!button.hit_test(pos, max + Vec2::new(2.0, 2.0), &atlas),
"past the skin is not the button"
);
}
#[test]
fn button_different_styles_affect_bg_color() {
let btn = Button::new("Test");
let mut dl_hover = DrawList::new();
btn.render(
&mut dl_hover,
Vec2::ZERO,
&atlas(),
&style(),
ButtonState::Hovered,
);
let mut dl_idle = DrawList::new();
btn.render(
&mut dl_idle,
Vec2::ZERO,
&atlas(),
&style(),
ButtonState::Idle,
);
if let DrawCommand::Rect { color: c1, .. } = &dl_hover.commands()[0] {
if let DrawCommand::Rect { color: c2, .. } = &dl_idle.commands()[0] {
assert_ne!(
c1, c2,
"hover and idle should have different background colors"
);
} else {
panic!("second command not Rect");
}
} else {
panic!("first command not Rect");
}
}
fn skin_with(insets: [SkinInsets; 3]) -> ButtonSkin {
let mut images = crate::image::ImageAtlas::new();
let mut frame = |shade: u8, insets| NineSliceImage {
image: images
.register(16, 16, &[shade; 16 * 16 * 4])
.expect("fits an empty page"),
insets,
};
ButtonSkin {
idle: frame(10, insets[0]),
hovered: frame(20, insets[1]),
pressed: frame(30, insets[2]),
}
}
#[test]
fn a_button_skin_swaps_the_picture_and_leaves_the_rectangles_alone() {
let skin = skin_with([SkinInsets::new(3.0, 5.0, 2.0, 4.0); 3]);
let quads = |state| {
let mut dl = DrawList::new();
skin.draw(
&mut dl,
state,
Vec2::new(10.0, 10.0),
Vec2::new(90.0, 40.0),
2.0,
);
dl.commands()
.iter()
.map(|command| match command {
DrawCommand::Image {
min, max, uv_min, ..
} => (*min, *max, *uv_min),
other => panic!("a skin drew {other:?}"),
})
.collect::<Vec<_>>()
};
let [idle, hovered, pressed] = [
ButtonState::Idle,
ButtonState::Hovered,
ButtonState::Pressed,
]
.map(quads);
assert_eq!(idle.len(), 9);
for index in 0..9 {
assert_eq!(
(idle[index].0, idle[index].1),
(pressed[index].0, pressed[index].1),
"quad {index} moved with the state"
);
assert_eq!(
(hovered[index].0, hovered[index].1),
(idle[index].0, idle[index].1)
);
assert_ne!(idle[index].2, hovered[index].2, "quad {index}");
assert_ne!(hovered[index].2, pressed[index].2, "quad {index}");
assert_ne!(idle[index].2, pressed[index].2, "quad {index}");
}
}
#[test]
fn a_skins_insets_are_its_idle_frames_and_disagreement_is_reported() {
let agreed = skin_with([SkinInsets::new(3.0, 5.0, 2.0, 4.0); 3]);
assert_eq!(agreed.insets(), SkinInsets::new(3.0, 5.0, 2.0, 4.0));
assert!(agreed.insets_agree());
let split = skin_with([
SkinInsets::uniform(4.0),
SkinInsets::uniform(4.0),
SkinInsets::uniform(6.0),
]);
assert!(!split.insets_agree());
let over = skin_with([SkinInsets::new(40.0, 40.0, 0.0, 0.0); 3]);
assert_eq!(over.insets(), SkinInsets::new(16.0, 0.0, 0.0, 0.0));
}
}