1use crate::prelude::*;
8
9pub const BACKGROUND: Color = Color::from_rgba(14, 22, 35, 255);
10pub const PANEL: Color = Color::from_rgba(20, 31, 47, 242);
11pub const LINE: Color = Color::from_rgba(43, 59, 77, 255);
12pub const INK: Color = Color::from_rgba(228, 238, 242, 255);
13pub const MUTED: Color = Color::from_rgba(139, 160, 177, 255);
14pub const AMBER: Color = Color::from_rgba(247, 181, 83, 255);
15pub const CYAN: Color = Color::from_rgba(91, 213, 216, 255);
16
17pub fn warm_up_font() {}
19pub fn warm_up_font_sizes(_sizes: &[u16]) {}
20
21pub fn header(_section: &str, title: &str, subtitle: &str) {
23 draw_rectangle(0.0, 0.0, screen_width(), 104.0, PANEL);
24 fitted_text(title, 24.0, 45.0, screen_width() - 48.0, 28.0, INK);
25 fitted_text(subtitle, 25.0, 78.0, screen_width() - 49.0, 16.0, MUTED);
26 draw_line(24.0, 103.0, screen_width() - 24.0, 103.0, 1.0, LINE);
27}
28
29pub fn panel(rect: Rect) {
30 draw_rectangle(rect.x, rect.y, rect.w, rect.h, PANEL);
31 draw_rectangle_lines(rect.x, rect.y, rect.w, rect.h, 1.0, LINE);
32}
33
34pub fn button(rect: Rect, text: &str, selected: bool) -> bool {
36 button_with_options(
37 rect,
38 text,
39 ButtonOptions {
40 selected,
41 ..Default::default()
42 },
43 )
44}
45
46pub fn button_with_options(rect: Rect, text: &str, options: ButtonOptions) -> bool {
57 let hovered = options.enabled && rect.contains(Vec2::from(mouse_position()));
58 let selected = options.enabled && options.selected;
59 draw_rectangle(
60 rect.x,
61 rect.y,
62 rect.w,
63 rect.h,
64 if !options.enabled {
65 LINE
66 } else if selected {
67 AMBER
68 } else if hovered {
69 LINE
70 } else {
71 PANEL
72 },
73 );
74 draw_rectangle_lines(
75 rect.x,
76 rect.y,
77 rect.w,
78 rect.h,
79 1.0,
80 if selected { AMBER } else { LINE },
81 );
82 let font_scale =
85 ((rect.w - 12.0) / measure_text(text, None, 17, 1.0).width.max(1.0)).clamp(0.1, 1.0);
86 let dimensions = measure_text(text, None, 17, font_scale);
87 draw_text_ex(
88 text,
89 rect.x + (rect.w - dimensions.width) * 0.5,
90 rect.y + rect.h * 0.5 + 6.0,
91 TextParams {
92 font_size: 17,
93 font_scale,
94 color: if !options.enabled {
95 MUTED
96 } else if selected {
97 BACKGROUND
98 } else {
99 INK
100 },
101 },
102 );
103 hovered && is_mouse_button_pressed(MouseButton::Left)
104}
105
106fn fitted_text(text: &str, x: f32, y: f32, width: f32, size: f32, color: Color) {
107 let measured = measure_text(text, None, size as u16, 1.0).width;
108 draw_text_ex(
109 text,
110 x,
111 y,
112 TextParams {
113 font_size: size as u16,
114 font_scale: (width.max(1.0) / measured.max(1.0)).min(1.0),
115 color,
116 },
117 );
118}
119
120pub fn stat(x: f32, y: f32, label: &str, value: &str) {
121 draw_text(label, x, y, 14.0, MUTED);
122 draw_text(value, x, y + 24.0, 25.0, INK);
123}
124
125mod shared;
126pub use shared::*;
127pub mod cards;
128pub mod inspector;
129pub mod kit;
130pub mod meters;
131pub mod scroll;
132pub mod stats;
133pub mod text_layout;
134
135#[derive(Clone, Copy, Debug, PartialEq, Eq)]
145pub struct ButtonOptions {
146 pub enabled: bool,
147 pub selected: bool,
148}
149
150impl Default for ButtonOptions {
151 fn default() -> Self {
152 Self {
153 enabled: true,
154 selected: false,
155 }
156 }
157}
158
159impl Ui {
160 pub fn button_with_options(
187 &mut self,
188 id: WidgetId,
189 rect: UiRect,
190 text: &str,
191 options: ButtonOptions,
192 style: ButtonStyle,
193 ) -> Result<(), UiError> {
194 let style = option_style(style, options)?;
195 self.button(id, rect, text, options.enabled, style)
196 }
197
198 pub fn hud_button_with_options(
215 &mut self,
216 id: WidgetId,
217 rect: UiRect,
218 text: &str,
219 options: ButtonOptions,
220 style: ButtonStyle,
221 ) -> Result<(), UiError> {
222 let style = option_style(style, options)?;
223 self.hud_button(id, rect, text, options.enabled, style)
224 }
225}
226
227fn option_style(style: ButtonStyle, options: ButtonOptions) -> Result<ButtonStyle, UiError> {
228 let finite = |c: Color| [c.r, c.g, c.b, c.a].into_iter().all(f32::is_finite);
231 if ![style.fill, style.ink, style.focus, style.disabled]
232 .into_iter()
233 .all(finite)
234 || !style.size.is_finite()
235 || !(1. ..=256.).contains(&style.size)
236 {
237 return Err(UiError::InvalidLayout);
238 }
239 if !options.enabled {
240 let mix = |ink: f32, fill: f32| ((ink as f64 + fill as f64) * 0.5) as f32;
243 Ok(ButtonStyle {
244 ink: Color::new(
245 mix(style.ink.r, style.disabled.r),
246 mix(style.ink.g, style.disabled.g),
247 mix(style.ink.b, style.disabled.b),
248 style.ink.a,
249 ),
250 ..style
251 })
252 } else if options.selected {
253 Ok(ButtonStyle {
254 fill: style.focus,
255 ink: style.fill,
256 focus: style.fill,
257 ..style
258 })
259 } else {
260 Ok(style)
261 }
262}
263
264#[cfg(test)]
265mod widget_tests {
266 use super::*;
267
268 const RECT: UiRect = UiRect {
269 min: [20.; 2],
270 size: [100., 40.],
271 };
272
273 fn reset() {
274 crate::edit(|s| {
275 *s = crate::State::default();
276 s.w = 200;
277 s.h = 200;
278 });
279 crate::set_default_camera();
280 }
281
282 fn next() {
283 crate::edit(|s| {
284 s.frame += 1;
285 s.input.finish_frame();
286 s.pressed.clear();
287 s.clicks.clear();
288 s.vertices.clear();
289 s.batches.clear();
290 s.retained.commands.clear();
291 });
292 }
293
294 fn begin(ui: &mut Ui) {
295 ui.begin(Viewport::responsive([200.; 2], [0.; 4]).unwrap());
296 }
297
298 fn queue(ui: &mut Ui, id: WidgetId, options: ButtonOptions) {
299 ui.button_with_options(id, RECT, "PLAY", options, ButtonStyle::default())
300 .unwrap();
301 }
302
303 fn tap(key: u32) {
304 crate::key(key, true);
305 crate::key(key, false);
306 }
307
308 fn color(vertex: &[f32; 8]) -> Color {
309 Color::new(vertex[4], vertex[5], vertex[6], vertex[7])
310 }
311
312 #[test]
313 fn legacy_enabled_options_preserve_geometry_colors_and_inclusive_hit_edges() {
314 let rect = Rect::new(20., 20., 100., 40.);
315 for selected in [false, true] {
316 for point in [[19., 20.], [20., 20.], [120., 60.], [121., 60.]] {
317 reset();
318 crate::pointer(point[0], point[1], 0., 0.);
319 crate::button(1, true);
320 let old = button(rect, "PLAY", selected);
321 let vertices = crate::read(|s| s.vertices.clone());
322 let fill = if selected {
323 AMBER
324 } else if rect.contains(Vec2::from(point)) {
325 LINE
326 } else {
327 PANEL
328 };
329 assert!(vertices[..6].iter().all(|v| color(v) == fill));
330 assert!(
331 vertices[6..30]
332 .iter()
333 .all(|v| color(v) == if selected { AMBER } else { LINE })
334 );
335 assert!(
336 vertices[30..]
337 .iter()
338 .all(|v| color(v) == if selected { BACKGROUND } else { INK })
339 );
340 crate::edit(|s| {
341 s.vertices.clear();
342 s.batches.clear();
343 });
344 let new = button_with_options(
345 rect,
346 "PLAY",
347 ButtonOptions {
348 selected,
349 ..Default::default()
350 },
351 );
352 assert_eq!(old, new);
353 assert_eq!(new, rect.contains(Vec2::from(point)));
354 crate::read(|s| assert_eq!(s.vertices, vertices));
355 }
356 }
357 }
358
359 #[test]
360 fn disabled_legacy_button_ignores_press_hover_and_selection() {
361 reset();
362 let rect = Rect::new(20., 20., 100., 40.);
363 let options = ButtonOptions {
364 enabled: false,
365 selected: false,
366 };
367 assert!(!button_with_options(rect, "PLAY", options));
368 let vertices = crate::read(|s| s.vertices.clone());
369 assert!(vertices.iter().any(|v| color(v) == MUTED));
370 next();
371 crate::pointer(40., 40., 0., 0.);
372 crate::button(1, true);
373 assert!(!button_with_options(
374 rect,
375 "PLAY",
376 ButtonOptions {
377 selected: true,
378 ..options
379 }
380 ));
381 crate::read(|s| assert_eq!(s.vertices, vertices));
382 assert!(is_mouse_button_pressed(MouseButton::Left));
383 }
384
385 #[test]
386 fn selection_and_focus_have_distinct_paint_without_changing_tab_order() {
387 reset();
388 let mut ui = Ui::new();
389 let selected = ButtonOptions {
390 selected: true,
391 ..Default::default()
392 };
393 begin(&mut ui);
394 queue(&mut ui, 7, selected);
395 ui.finish(true);
396 assert_eq!(ui.focused(), None);
397 ui.paint();
398 let unfocused = crate::read(|s| {
399 assert!(s.vertices[..6].iter().all(|v| color(v) == AMBER));
400 s.vertices.len()
401 });
402
403 next();
404 begin(&mut ui);
405 queue(&mut ui, 7, selected);
406 tap(0xff09);
407 ui.finish(true);
408 assert_eq!(ui.focused(), Some(7));
409 assert!(!ui.activated(7));
410 ui.paint();
411 crate::read(|s| {
412 assert_eq!(s.vertices.len(), unfocused + 24);
413 assert!(s.vertices[..6].iter().all(|v| color(v) == AMBER));
414 assert!(s.vertices[6..30].iter().all(|v| color(v) == PANEL));
415 assert!(s.batches.iter().all(|batch| !batch.depth));
416 });
417
418 next();
421 begin(&mut ui);
422 queue(&mut ui, 7, ButtonOptions::default());
423 queue(&mut ui, 2, selected);
424 ui.finish(true);
425 assert_eq!(ui.focused(), Some(7));
426 next();
427 begin(&mut ui);
428 queue(&mut ui, 7, ButtonOptions::default());
429 queue(&mut ui, 2, selected);
430 tap(0xff09);
431 tap(0xff0d);
432 ui.finish(true);
433 assert_eq!(ui.focused(), Some(2));
434 assert!(ui.activated(2));
435 assert!(!ui.activated(7));
436 }
437
438 #[test]
439 fn disabling_a_captured_selected_button_clears_focus_and_prevents_late_activation() {
440 reset();
441 let mut ui = Ui::new();
442 let selected = ButtonOptions {
443 selected: true,
444 ..Default::default()
445 };
446 begin(&mut ui);
447 queue(&mut ui, 1, selected);
448 crate::pointer(40., 40., 0., 0.);
449 crate::button(1, true);
450 ui.finish(true);
451 assert_eq!(ui.focused(), Some(1));
452 assert!(!ui.activated(1));
453
454 next();
455 begin(&mut ui);
456 queue(
457 &mut ui,
458 1,
459 ButtonOptions {
460 enabled: false,
461 ..selected
462 },
463 );
464 crate::button(1, false);
465 tap(0xff0d);
466 ui.finish(true);
467 assert_eq!(ui.focused(), None);
468 assert!(!ui.activated(1));
469 ui.paint();
470 crate::read(|s| {
471 assert!(s.vertices[..6].iter().all(|v| color(v) == LINE));
472 assert!(s.vertices[6..].iter().all(|v| color(v) != INK));
473 assert!(s.vertices.iter().all(|v| color(v) != AMBER));
474 });
475
476 next();
477 begin(&mut ui);
478 queue(&mut ui, 1, selected);
479 crate::button(1, false);
480 ui.finish(true);
481 assert!(!ui.activated(1));
482 next();
483 begin(&mut ui);
484 queue(&mut ui, 1, selected);
485 crate::button(1, true);
486 crate::button(1, false);
487 ui.finish(true);
488 assert!(ui.activated(1));
489 }
490
491 #[test]
492 fn disabled_overlay_blocks_pointer_without_taking_keyboard_focus() {
493 reset();
494 let mut ui = Ui::new();
495 begin(&mut ui);
496 queue(&mut ui, 1, ButtonOptions::default());
497 queue(
498 &mut ui,
499 2,
500 ButtonOptions {
501 enabled: false,
502 selected: true,
503 },
504 );
505 crate::pointer(40., 40., 0., 0.);
506 crate::button(1, true);
507 crate::button(1, false);
508 ui.finish(true);
509 assert!(!ui.activated(1));
510 assert!(!ui.activated(2));
511 assert_eq!(ui.focused(), None);
512 next();
513 begin(&mut ui);
514 queue(&mut ui, 1, ButtonOptions::default());
515 queue(
516 &mut ui,
517 2,
518 ButtonOptions {
519 enabled: false,
520 selected: true,
521 },
522 );
523 tap(0xff09);
524 tap(0xff0d);
525 ui.finish(true);
526 assert_eq!(ui.focused(), Some(1));
527 assert!(ui.activated(1));
528 assert!(!ui.activated(2));
529 }
530
531 #[test]
532 fn options_preserve_scaled_clips_half_open_hits_and_tiny_widget_paint() {
533 reset();
534 let mut ui = Ui::new();
535 let viewport = Viewport::fit([200.; 2], [100.; 2], [0.; 4]).unwrap();
536 let clip = UiRect {
537 min: [30.; 2],
538 size: [40., 20.],
539 };
540 for selected in [false, true] {
541 for enabled in [false, true] {
542 for (point, inside) in
543 [([30., 30.], true), ([70., 30.], false), ([30., 50.], false)]
544 {
545 next();
546 ui.begin(viewport);
547 ui.push_clip(clip).unwrap();
548 ui.hud_button_with_options(
549 1,
550 RECT,
551 "PLAY",
552 ButtonOptions { enabled, selected },
553 ButtonStyle::default(),
554 )
555 .unwrap();
556 crate::pointer(point[0] * 2., point[1] * 2., 0., 0.);
557 crate::button(1, true);
558 crate::button(1, false);
559 ui.finish(true);
560 assert_eq!(ui.activated(1), enabled && inside);
561 ui.paint();
562 crate::read(|s| {
563 assert!(!s.vertices.is_empty());
564 for vertex in &s.vertices {
565 let logical = [(vertex[0] + 1.) * 50., (1. - vertex[1]) * 50.];
566 for (axis, coordinate) in logical.into_iter().enumerate() {
567 assert!(coordinate >= clip.min[axis] - 0.001);
568 assert!(coordinate <= clip.min[axis] + clip.size[axis] + 0.001);
569 }
570 }
571 });
572 }
573 }
574 }
575 for size in [[1., 1.], [40., 4.], [0., 0.]] {
576 next();
577 begin(&mut ui);
578 ui.hud_button_with_options(
579 1,
580 UiRect { size, ..RECT },
581 "PLAY",
582 ButtonOptions {
583 selected: true,
584 ..Default::default()
585 },
586 ButtonStyle::default(),
587 )
588 .unwrap();
589 tap(0xff09);
590 ui.finish(true);
591 assert_eq!(ui.focused(), (size[0] > 0.).then_some(1));
592 ui.paint();
593 crate::read(|s| {
594 assert_eq!(s.vertices.is_empty(), size[0] == 0.);
595 for vertex in &s.vertices {
596 let pixel = [(vertex[0] + 1.) * 100., (1. - vertex[1]) * 100.];
597 for (axis, coordinate) in pixel.into_iter().enumerate() {
598 assert!(coordinate >= RECT.min[axis] - 0.001);
599 assert!(coordinate <= RECT.min[axis] + size[axis] + 0.001);
600 }
601 }
602 });
603 }
604 }
605
606 #[test]
607 fn invalid_inactive_styles_reject_atomically_and_finite_extremes_stay_finite() {
608 reset();
609 let mut ui = Ui::new();
610 let bad_color = Color::new(f32::NAN, 0., 0., 1.);
611 for selected in [false, true] {
612 for enabled in [false, true] {
613 let options = ButtonOptions { enabled, selected };
614 for style in [
615 ButtonStyle {
616 fill: bad_color,
617 ..Default::default()
618 },
619 ButtonStyle {
620 ink: bad_color,
621 ..Default::default()
622 },
623 ButtonStyle {
624 focus: bad_color,
625 ..Default::default()
626 },
627 ButtonStyle {
628 disabled: bad_color,
629 ..Default::default()
630 },
631 ButtonStyle {
632 size: f32::INFINITY,
633 ..Default::default()
634 },
635 ButtonStyle {
636 size: 0.,
637 ..Default::default()
638 },
639 ButtonStyle {
640 size: 257.,
641 ..Default::default()
642 },
643 ] {
644 next();
645 begin(&mut ui);
646 assert_eq!(
647 ui.button_with_options(1, RECT, "BAD", options, style),
648 Err(UiError::InvalidLayout)
649 );
650 assert_eq!(
651 ui.hud_button_with_options(1, RECT, "BAD", options, style),
652 Err(UiError::InvalidLayout)
653 );
654 ui.paint();
655 crate::read(|s| assert!(s.vertices.is_empty()));
656 queue(&mut ui, 1, ButtonOptions::default());
657 }
658 }
659 }
660 next();
661 begin(&mut ui);
662 ui.button_with_options(
663 1,
664 RECT,
665 "HDR",
666 ButtonOptions {
667 enabled: false,
668 selected: true,
669 },
670 ButtonStyle {
671 ink: Color::new(f32::MAX, -f32::MAX, f32::MAX, 0.75),
672 disabled: Color::new(f32::MAX, f32::MAX, -f32::MAX, 1.),
673 ..Default::default()
674 },
675 )
676 .unwrap();
677 ui.paint();
678 crate::read(|s| {
679 assert!(s.vertices.iter().flatten().all(|v| v.is_finite()));
680 assert!(
681 s.vertices[6..]
682 .iter()
683 .all(|v| color(v) == Color::new(f32::MAX, 0., 0., 0.75))
684 );
685 });
686 }
687
688 #[test]
689 fn options_reuse_budgets_reject_duplicates_and_keep_hud_labels_bounded() {
690 reset();
691 let mut ui = Ui::new();
692 let reserved = ui.reserved_bytes();
693 begin(&mut ui);
694 let options = ButtonOptions {
695 selected: true,
696 ..Default::default()
697 };
698 assert_eq!(
699 ui.button_with_options(
700 1,
701 UiRect {
702 min: [f32::NAN, 0.],
703 ..RECT
704 },
705 "BAD",
706 options,
707 ButtonStyle::default()
708 ),
709 Err(UiError::InvalidLayout)
710 );
711 let huge = "é".repeat(100_000);
712 assert_eq!(
713 ui.button_with_options(1, RECT, &huge, options, ButtonStyle::default()),
714 Err(UiError::Capacity)
715 );
716 ui.hud_button_with_options(1, RECT, &huge, options, ButtonStyle::default())
717 .unwrap();
718 assert_eq!(
719 ui.button_with_options(1, RECT, "DUPLICATE", options, ButtonStyle::default()),
720 Err(UiError::DuplicateId)
721 );
722 for id in 2..=1024 {
725 ui.button_with_options(id, RECT, "", options, ButtonStyle::default())
726 .unwrap();
727 }
728 assert_eq!(
729 ui.button_with_options(1025, RECT, "", options, ButtonStyle::default()),
730 Err(UiError::Capacity)
731 );
732 for frame in 0..12 {
733 next();
734 begin(&mut ui);
735 ui.hud_button_with_options(
736 1,
737 RECT,
738 &huge,
739 ButtonOptions {
740 enabled: frame % 2 == 0,
741 selected: frame % 3 == 0,
742 },
743 ButtonStyle::default(),
744 )
745 .unwrap();
746 ui.finish(true);
747 ui.paint();
748 assert_eq!(ui.reserved_bytes(), reserved);
749 crate::read(|s| assert!(s.vertices.len() < 1024));
750 }
751 }
752}