1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
use super::*; #[derive(Debug, Copy, Clone)] pub struct Constraints { pub min_size: Vec2<f64>, pub flex: Vec2<f64>, } pub struct WidgetCore { pub(crate) id: ID, pub(crate) hovered: bool, pub(crate) captured: bool, pub(crate) position: AABB<f64>, pub(crate) constraints: Constraints, } impl WidgetCore { pub fn new() -> Self { Self { id: ID::new(), hovered: false, captured: false, position: AABB::from_corners(vec2(0.0, 0.0), vec2(1.0, 1.0)), constraints: Constraints { min_size: vec2(0.0, 0.0), flex: vec2(1.0, 1.0), }, } } pub(crate) fn void() -> Self { Self { id: ID::void(), hovered: false, captured: false, position: AABB::from_corners(vec2(0.0, 0.0), vec2(1.0, 1.0)), constraints: Constraints { min_size: vec2(0.0, 0.0), flex: vec2(1.0, 1.0), }, } } pub fn hovered(&self) -> bool { self.hovered } pub fn captured(&self) -> bool { self.captured } pub fn position(&self) -> AABB<f64> { self.position } } pub trait Widget { fn core(&self) -> &WidgetCore; fn core_mut(&mut self) -> &mut WidgetCore; fn calc_constraints(&mut self) {} fn layout_children(&mut self) {} fn update(&mut self, delta_time: f64) { #![allow(unused_variables)] } fn draw(&mut self, framebuffer: &mut ugli::Framebuffer) { #![allow(unused_variables)] } fn handle_event(&mut self, event: &Event) { #![allow(unused_variables)] } fn walk_children_mut<'a>(&mut self, f: Box<dyn FnMut(&mut dyn Widget) + 'a>) { #![allow(unused_variables)] } } impl Widget for Box<dyn Widget + '_> { fn core(&self) -> &WidgetCore { (**self).core() } fn core_mut(&mut self) -> &mut WidgetCore { (**self).core_mut() } fn calc_constraints(&mut self) { (**self).calc_constraints(); } fn layout_children(&mut self) { (**self).layout_children(); } fn update(&mut self, delta_time: f64) { (**self).update(delta_time); } fn draw(&mut self, framebuffer: &mut ugli::Framebuffer) { (**self).draw(framebuffer); } fn handle_event(&mut self, event: &Event) { (**self).handle_event(event); } fn walk_children_mut<'a>(&mut self, f: Box<dyn FnMut(&mut dyn Widget) + 'a>) { (**self).walk_children_mut(f); } } pub struct ControlPanel<'a> { core: &'a mut WidgetCore, } pub fn control_panel(core: &mut WidgetCore) -> ControlPanel<'_> { ControlPanel { core } } impl Deref for ControlPanel<'_> { type Target = WidgetCore; fn deref(&self) -> &WidgetCore { &self.core } } impl DerefMut for ControlPanel<'_> { fn deref_mut(&mut self) -> &mut WidgetCore { &mut self.core } } impl Widget for ControlPanel<'_> { fn core(&self) -> &WidgetCore { &self.core } fn core_mut(&mut self) -> &mut WidgetCore { &mut self.core } }