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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use geng_core as geng;
use geng_core::*;
use prelude::*;

pub use geng_ui_derive::*;

mod config;
mod layout_widgets;
mod theme;
pub mod widget;
mod widgets;

pub use config::*;
pub use layout_widgets::*;
pub use theme::*;
pub use widget::*;
pub use widgets::*;

use geng_core::Event as RawEvent;

pub enum Event {
    MouseMove {
        position: Vec2<f64>,
    },
    MouseDown {
        button: MouseButton,
        position: Vec2<f64>,
    },
    MouseUp {
        button: MouseButton,
        position: Vec2<f64>,
    },
    Click {
        button: MouseButton,
    },
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum IDImpl {
    Regular(usize),
    Void,
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub(crate) struct ID(IDImpl);

impl ID {
    fn new() -> Self {
        static NEXT_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
        ID(IDImpl::Regular(
            NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
        ))
    }
    fn void() -> Self {
        ID(IDImpl::Void)
    }
}

pub trait State {
    fn ui<'a>(&'a mut self) -> Box<dyn Widget + 'a>;
}

pub struct Controller {
    captured_id: Option<ID>,
    last_touch_pos: Option<Vec2<f64>>,
}

impl Controller {
    pub fn new() -> Self {
        Self {
            captured_id: None,
            last_touch_pos: None,
        }
    }
}

fn traverse_mut<F: FnMut(&mut dyn Widget), G: FnMut(&mut dyn Widget)>(
    widget: &mut dyn Widget,
    on_enter: &mut F,
    on_leave: &mut G,
) {
    on_enter(widget);
    widget.walk_children_mut(Box::new(|widget| traverse_mut(widget, on_enter, on_leave)));
    on_leave(widget);
}

impl Controller {
    pub fn update<T: Widget>(&mut self, mut root: T, delta_time: f64) {
        traverse_mut(
            &mut root,
            &mut |widget| widget.update(delta_time),
            &mut |_| {},
        );
    }
    pub fn draw<T: Widget>(&mut self, mut root: T, framebuffer: &mut ugli::Framebuffer) {
        traverse_mut(&mut root, &mut |_| {}, &mut |widget| {
            widget.calc_constraints();
        });
        root.calc_constraints();
        root.core_mut().position =
            AABB::from_corners(vec2(0.0, 0.0), framebuffer.size().map(|x| x as f64));
        traverse_mut(
            &mut root,
            &mut |widget| {
                widget.layout_children();
            },
            &mut |_| {},
        );
        traverse_mut(
            &mut root,
            &mut |widget| {
                widget.draw(framebuffer);
            },
            &mut |_| {},
        );
    }
    pub fn handle_event<T: Widget>(&mut self, mut root: T, event: RawEvent) -> bool {
        traverse_mut(
            &mut root,
            &mut |widget| {
                if let Some(id) = self.captured_id {
                    if id != widget.core().id {
                        return;
                    }
                }
                match event {
                    RawEvent::MouseMove { position } => {
                        widget.core_mut().hovered = widget.core().position.contains(position);
                        widget.handle_event(&Event::MouseMove { position });
                    }
                    RawEvent::MouseDown { button, position } => {
                        if widget.core().position.contains(position) {
                            widget.core_mut().captured = true;
                            self.captured_id = Some(widget.core().id);
                            widget.handle_event(&Event::MouseDown { button, position });
                        } else if widget.core().captured {
                            widget.handle_event(&Event::MouseDown { button, position });
                        }
                    }
                    RawEvent::MouseUp { button, position } => {
                        let was_captured = widget.core().captured;
                        widget.core_mut().captured = false;
                        if was_captured {
                            widget.handle_event(&Event::MouseUp { button, position });
                        }
                        if was_captured && widget.core().position.contains(position) {
                            widget.handle_event(&Event::Click { button });
                        }
                    }
                    RawEvent::TouchStart { ref touches } if touches.len() == 1 => {
                        let position = touches[0].position;
                        self.last_touch_pos = Some(position);
                        if widget.core().position.contains(position) {
                            widget.core_mut().captured = true;
                            self.captured_id = Some(widget.core().id);
                            widget.handle_event(&Event::MouseDown {
                                button: MouseButton::Left,
                                position,
                            });
                        } else if widget.core().captured {
                            widget.handle_event(&Event::MouseDown {
                                button: MouseButton::Left,
                                position,
                            });
                        }
                    }
                    RawEvent::TouchEnd => {
                        if let Some(position) = self.last_touch_pos {
                            let was_captured = widget.core().captured;
                            widget.core_mut().captured = false;
                            if was_captured {
                                widget.handle_event(&Event::MouseUp {
                                    button: MouseButton::Left,
                                    position,
                                });
                            }
                            if was_captured && widget.core().position.contains(position) {
                                widget.handle_event(&Event::Click {
                                    button: MouseButton::Left,
                                });
                            }
                        }
                    }
                    RawEvent::TouchMove { ref touches } if touches.len() == 1 => {
                        let position = touches[0].position;
                        self.last_touch_pos = Some(position);
                        widget.core_mut().hovered = widget.core().position.contains(position);
                        widget.handle_event(&Event::MouseMove { position });
                    }
                    _ => {}
                }
            },
            &mut |_| {},
        );
        match event {
            RawEvent::MouseDown { .. } | RawEvent::TouchStart { .. } => {
                if self.captured_id.is_none() {
                    self.captured_id = Some(ID(IDImpl::Void));
                }
            }
            RawEvent::MouseUp { .. } | RawEvent::TouchEnd => {
                self.captured_id = None;
            }
            _ => {}
        }
        match self.captured_id {
            Some(ID(IDImpl::Void)) | None => false,
            _ => true,
        }
    }
}