pierro 0.1.0

An immediate mode UI library for Rust
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602

use std::collections::HashMap;

use crate::{TSTransform, Vec2};

use super::{Id, LayoutMemory, Memory, UIRef, UI};

mod key;
pub use key::*;

/// The state of an input device that can either be down or not.
/// Has utilities for detecting the frame the button was first pressed, released, etc.
#[derive(Clone, Copy)]
pub struct ButtonInput {
    state: bool,
    prev_state: bool,
    clicked: bool,
    time_since_press: f32,
    time_since_release: f32,
    click_count: u32
}

impl ButtonInput {

    pub fn new() -> Self {
        Self {
            state: false,
            prev_state: false,
            clicked: false,
            time_since_press: 0.0,
            time_since_release: 0.0,
            click_count: 0
        }
    }

    fn timer_tick(&mut self, delta_time: f32) {
        self.time_since_press += delta_time;
        self.time_since_release += delta_time;
        if self.pressed() {
            self.time_since_press = 0.0;
        }
        if self.released() {
            self.clicked = self.time_since_press < 1.0;
            if self.time_since_release > 0.3 {
                self.click_count = 0;
            }
            if self.clicked {
                self.click_count += 1;
            }
            self.time_since_release = 0.0;
        } else {
            self.clicked = false;
        }
    }

    /// Update the button with a new state
    pub fn tick(&mut self, state: bool, delta_time: f32) {
        self.prev_state = self.state;
        self.state = state;
        self.timer_tick(delta_time);
    } 

    /// Set the button to be down 
    pub fn press(&mut self) {
        self.state = true;
    }

    /// Set the button to be up
    pub fn release(&mut self) {
        self.state = false;
    }

    /// Update the button without providing a new state
    pub fn tick_with_same_state(&mut self, delta_time: f32) {
        self.prev_state = self.state;
        self.timer_tick(delta_time);
    }

    /// Is the button down?
    pub fn down(&self) -> bool {
        self.state
    }

    /// Has the button just been pressed?
    pub fn pressed(&self) -> bool {
        self.state && !self.prev_state
    } 

    /// Has the button just been released?
    pub fn released(&self) -> bool {
        !self.state && self.prev_state
    }

    pub fn click_count(&self) -> u32 {
        if self.clicked {
            self.click_count
        } else {
            0
        }
    }

    /// Was the button clicked?
    pub fn clicked(&self) -> bool {
        self.clicked
    }

    /// Was the button clicked once?
    pub fn single_clicked(&self) -> bool {
        self.click_count() == 1
    }

    /// Was the button double clicked?
    pub fn double_clicked(&self) -> bool {
        self.click_count() == 2
    }

    /// Was the button triple clicked?
    pub fn triple_clicked(&self) -> bool {
        self.click_count() == 3
    }

}

/// The raw input given to the application by the windowing library
pub(crate) struct RawInput {

    /// The amount of time elapsed since the last redraw
    pub(crate) delta_time: f32,

    /// Mouse position in physical pixels. None if the mouse left the window
    pub(crate) mouse_pos: Option<Vec2>,
    /// Is the left mouse button currently down?
    pub(crate) l_mouse_down: bool,
    /// Is the right mouse button currently down?
    pub(crate) r_mouse_down: bool,
    /// How much has the mouse scrolled
    pub(crate) scroll: Vec2,

    /// What keys were pressed this frame?
    pub(crate) keys_pressed: Vec<Key>,
    /// What keys were released this frame?
    pub(crate) keys_released: Vec<Key>,

    /// What is the current IME preedit?
    pub(crate) ime_preedit: String,
    /// What IME text input was commited this frame?
    pub(crate) ime_commit: Option<String>
}

impl RawInput {

    pub(crate) fn new() -> Self {
        Self {
            delta_time: 0.0,
            mouse_pos: None,
            l_mouse_down: false,
            r_mouse_down: false,
            scroll: Vec2::ZERO,
            keys_pressed: Vec::new(),
            keys_released: Vec::new(),
            ime_preedit: String::new(),
            ime_commit: None
        }
    }

}

/// The state of a mouse button
#[derive(Clone, Copy)]
pub struct MouseButton {
    /// Is the mouse button pressed?
    pub state: ButtonInput,
    /// The position of the mouse when this button was first pressed. `None` if the button is not pressed. 
    pub press_pos: Option<Vec2>,
    /// Is this mouse button being dragged?
    pub dragging: ButtonInput
}

impl MouseButton {

    fn new() -> Self {
        Self {
            state: ButtonInput::new(),
            press_pos: None,
            dragging: ButtonInput::new(),
        }
    }

    fn update(&mut self, down: bool, mouse_pos: Option<Vec2>, delta_time: f32) {
        self.state.tick(down, delta_time);
        self.dragging.tick_with_same_state(delta_time);
        if self.state.pressed() {
            self.press_pos = mouse_pos;
        }
        if self.state.down() {
            if mouse_pos.unwrap_or(Vec2::INFINITY).distance(self.press_pos.unwrap_or(Vec2::INFINITY)) > 2.5 {
                self.dragging.press();
            }
        }
        if self.state.released() {
            self.press_pos = None;
            self.dragging.release();
        }
    }

    pub fn down(&self) -> bool {
        self.state.down()
    }

    pub fn pressed(&self) -> bool {
        self.state.pressed()
    }

    pub fn released(&self) -> bool {
        self.state.released()
    }

    pub fn clicked(&self) -> bool {
        self.state.clicked()
    }

    pub fn double_clicked(&self) -> bool {
        self.state.double_clicked()
    }

    pub fn triple_clicked(&self) -> bool {
        self.state.triple_clicked()
    }

    pub fn dragging(&self) -> bool {
        self.dragging.down()
    }

    pub fn drag_started(&self) -> bool {
        self.dragging.pressed()
    }

    pub fn drag_stopped(&self) -> bool {
        self.dragging.released()
    }

}

pub struct Input {
    pub prev_mouse_pos: Option<Vec2>,
    pub mouse_pos: Option<Vec2>,
    pub l_mouse: MouseButton,
    pub r_mouse: MouseButton,
    pub scroll: Vec2,

    keys: HashMap<Key, ButtonInput>,
    pub keys_pressed: Vec<Key>,
    pub keys_released: Vec<Key>,

    pub ime_preedit: String,
    pub ime_commit: Option<String>
}

/// The memory storing what inputs are provided to a node
pub(crate) struct Interaction {
    pub(crate) hovered: bool,
    pub(crate) through_hovered: bool,
    pub(crate) l_mouse: MouseButton,
    pub(crate) r_mouse: MouseButton,
    pub(crate) scroll: Vec2
}

impl Default for Interaction {

    fn default() -> Self {
        Self {
            hovered: false,
            through_hovered: false,
            l_mouse: MouseButton::new(),
            r_mouse: MouseButton::new(),
            scroll: Vec2::ZERO
        }
    }

}

fn find_interacted_node<F: Fn(&mut LayoutMemory) -> bool>(memory: &mut Memory, node: Id, pos: Vec2, ignore: Option<Id>, criteria: &F) -> Option<Id> {
    // Check priority nodes first
    let mut child = memory.get::<LayoutMemory>(node).first_child;
    while let Some(child_id) = child {
        if memory.get::<LayoutMemory>(child_id).has_interaction_priority {
            if let Some(node) = find_interacted_node(memory, child_id, pos, ignore, criteria) {
                return Some(node);
            }
        }
        child = memory.get::<LayoutMemory>(child_id).next;
    } 

    // Then check non-priority nodes
    let mut child = memory.get::<LayoutMemory>(node).first_child;
    while let Some(child_id) = child {
        if !memory.get::<LayoutMemory>(child_id).has_interaction_priority {
            if let Some(node) = find_interacted_node(memory, child_id, pos, ignore, criteria) {
                return Some(node);
            }
        }
        child = memory.get::<LayoutMemory>(child_id).next;
    }

    let layout_mem = memory.get::<LayoutMemory>(node);
    if layout_mem.interaction_rect.contains(pos) && criteria(layout_mem) && Some(node) != ignore {
        return Some(node);
    }

    None

}

/// Find the node hovered at a given position in screen space
fn find_hover_node(memory: &mut Memory, node: Id, pos: Vec2, ignore: Option<Id>) -> Option<Id> {
    find_interacted_node(memory, node, pos, ignore, &|mem| mem.sense_mouse)
}

/// Find the scrollable at a given position in screen space
fn find_scrollable_node(memory: &mut Memory, node: Id, pos: Vec2, ignore: Option<Id>) -> Option<Id> {
    find_interacted_node(memory, node, pos, ignore, &|mem| mem.sense_scroll)
}

impl Input {

    /// The change in mouse position between the previous and current frame
    pub fn mouse_delta(&self) -> Vec2 {
        let Some(mouse_pos) = self.mouse_pos else { return Vec2::ZERO };
        let Some(prev_mouse_pos) = self.prev_mouse_pos else { return Vec2::ZERO };
        mouse_pos - prev_mouse_pos
    }

    /// Get the state of a key
    pub fn key_state(&self, key: Key) -> ButtonInput {
        self.keys.get(&key).map(|state| *state).unwrap_or(ButtonInput::new())
    }

    /// Is a key down?
    pub fn key_down(&self, key: Key) -> bool {
        self.key_state(key).down()
    }

    /// Has a key just been pressed?
    pub fn key_pressed(&self, key: Key) -> bool {
        self.key_state(key).pressed()
    }

    /// Has a key just been released?
    pub fn key_released(&self, key: Key) -> bool {
        self.key_state(key).released()
    }

    /// Get a mutable reference to the state of a key
    fn key_state_mut(&mut self, key: &Key) -> &mut ButtonInput {
        if !self.keys.contains_key(&key) {
            self.keys.insert(key.clone(), ButtonInput::new());
        }
        self.keys.get_mut(&key).unwrap()
    }

    pub(crate) fn new() -> Self {
        Self {
            prev_mouse_pos: None,
            mouse_pos: None,
            l_mouse: MouseButton::new(),
            r_mouse: MouseButton::new(),
            scroll: Vec2::ZERO,
            keys: HashMap::new(),
            keys_pressed: Vec::new(),
            keys_released: Vec::new(),
            ime_preedit: String::new(),
            ime_commit: None
        }
    }

    /// Update the input given the raw input from the window.
    /// Resets the raw input in preparation for the next frame.
    pub(crate) fn update(&mut self, raw_input: &mut RawInput, scale_factor: f32) {
        self.prev_mouse_pos = self.mouse_pos;
        self.mouse_pos = raw_input.mouse_pos.map(|pos| pos / scale_factor);

        self.l_mouse.update(raw_input.l_mouse_down, self.mouse_pos, raw_input.delta_time);
        self.r_mouse.update(raw_input.r_mouse_down, self.mouse_pos, raw_input.delta_time);

        // If we start dragging, set the mouse position to the previous mouse position
        // so that the drag starting is registered on the same widget where the mouse began
        if self.l_mouse.drag_started() || self.r_mouse.drag_started() {
            self.mouse_pos = self.prev_mouse_pos;
        }
        
        self.scroll = raw_input.scroll / scale_factor;
        raw_input.scroll = Vec2::ZERO;

        self.keys_pressed = std::mem::replace(&mut raw_input.keys_pressed, Vec::new());
        self.keys_released = std::mem::replace(&mut raw_input.keys_released, Vec::new());
        for key in self.keys_pressed.clone() {
            self.key_state_mut(&key).press();
        }
        for key in self.keys_released.clone() {
            self.key_state_mut(&key).release();
        }
        for (_key, state) in self.keys.iter_mut() {
            state.tick_with_same_state(raw_input.delta_time);
        }

        self.ime_preedit = raw_input.ime_preedit.clone();
        self.ime_commit = std::mem::replace(&mut raw_input.ime_commit, None);

    }

    /// Distribute the input to nodes, taking foucs into account.
    pub(crate) fn distribute(&self, memory: &mut Memory) {
        let layer_ids = memory.layer_ids.clone();
        let hovered_node = memory.get_focus().or_else(|| {
            let mouse_pos = self.mouse_pos?;
            for layer in layer_ids.iter().rev() { 
                if let Some(hovered_node) = find_hover_node(memory, *layer, mouse_pos, None) {
                    return Some(hovered_node)
                }
            }
            None
        });
        let scrollable_node = (|| {
            let mouse_pos = self.mouse_pos?;
            for layer in layer_ids.iter().rev() { 
                if let Some(scrollable_node) = find_scrollable_node(memory, *layer, mouse_pos, None) {
                    return Some(scrollable_node)
                }
            }
            None
        })();
        let through_hovered_node = self.mouse_pos.map(|mouse_pos| {
            for layer in layer_ids.iter().rev() { 
                if let Some(hovered_node) = find_hover_node(memory, *layer, mouse_pos, memory.get_focus()) {
                    return Some(hovered_node)
                }
            }
            None
        }).flatten();

        for (id, interaction) in memory.iter_mut::<Interaction>() {
            let hovered = Some(id) == hovered_node;
            let scrollable = Some(id) == scrollable_node;
            interaction.hovered = hovered;
            interaction.through_hovered = Some(id) == through_hovered_node;
            interaction.l_mouse = if hovered { self.l_mouse } else { MouseButton::new() };
            interaction.r_mouse = if hovered { self.r_mouse } else { MouseButton::new() };
            interaction.scroll = if scrollable { self.scroll } else { Vec2::ZERO };
        }

        // If we're not holding the mouse down, we can't be drag and dropping anything
        if !self.l_mouse.down() && !self.l_mouse.released() {
            memory.clear_dnd_payload();
        }
    }

}

#[derive(Clone, Copy)]
pub struct Response {
    pub id: Id,
    pub node_ref: UIRef,

    pub hovered: bool,
    /// Is this node being hovered through the currently focused node?
    pub through_hovered: bool,
    pub l_mouse: MouseButton,
    pub r_mouse: MouseButton,
    pub scroll: Vec2
}

impl Response {

    pub fn contains_mouse(&self, ui: &mut UI) -> bool {
        let Some(pos) = ui.input().mouse_pos else { return false; };
        ui.memory().get::<LayoutMemory>(self.id).screen_rect.contains(pos)
    }

    /// Returns the position of the mouse relative to the node
    pub fn mouse_pos(&self, ui: &mut UI) -> Option<Vec2> {
        let screen_pos = ui.input().mouse_pos?;
        let layout_memory = ui.memory().get::<LayoutMemory>(self.id);
        let rect = layout_memory.screen_rect;
        let scale = layout_memory.transform.scale;
        Some((screen_pos - rect.tl()) / scale) 
    }

    pub fn mouse_down(&self) -> bool {
        self.l_mouse.down()
    }

    pub fn mouse_pressed(&self) -> bool {
        self.l_mouse.pressed()
    }

    pub fn mouse_released(&self) -> bool {
        self.l_mouse.released()
    }

    pub fn mouse_clicked(&self) -> bool {
        self.l_mouse.clicked()
    }

    pub fn mouse_double_clicked(&self) -> bool {
        self.l_mouse.double_clicked()
    }

    pub fn mouse_triple_clicked(&self) -> bool {
        self.l_mouse.triple_clicked()
    }

    pub fn dragging(&self) -> bool {
        self.l_mouse.dragging()
    }

    pub fn drag_started(&self) -> bool {
        self.l_mouse.drag_started()
    }

    pub fn drag_stopped(&self) -> bool {
        self.l_mouse.drag_stopped()
    }

    pub fn drag_delta(&self, ui: &mut UI) -> Vec2 {
        if !self.dragging() {
            return Vec2::ZERO;
        }
        let scale = self.scale(ui);
        ui.input().mouse_delta() / scale
    }

    pub fn right_mouse_down(&self) -> bool {
        self.r_mouse.down()
    }

    pub fn right_mouse_pressed(&self) -> bool {
        self.r_mouse.pressed()
    }

    pub fn right_mouse_released(&self) -> bool {
        self.r_mouse.released()
    }

    pub fn right_mouse_clicked(&self) -> bool {
        self.r_mouse.clicked()
    }

    pub fn right_mouse_double_clicked(&self) -> bool {
        self.r_mouse.double_clicked()
    }

    pub fn right_mouse_triple_clicked(&self) -> bool {
        self.r_mouse.triple_clicked()
    }

    pub fn right_dragging(&self) -> bool {
        self.r_mouse.dragging()
    }

    pub fn right_drag_started(&self) -> bool {
        self.r_mouse.drag_started()
    }

    pub fn right_drag_stopped(&self) -> bool {
        self.r_mouse.drag_stopped()
    }

    pub fn right_drag_delta(&self, ui: &mut UI) -> Vec2 {
        if !self.right_dragging() {
            return Vec2::ZERO;
        }
        let scale = self.scale(ui);
        ui.input().mouse_delta() / scale
    }

    pub fn mouse_pressed_outside(&self, ui: &mut UI) -> bool {
        (ui.input().l_mouse.pressed() || ui.input().r_mouse.pressed()) && !self.contains_mouse(ui)
    }

    pub fn is_focused(&self, ui: &mut UI) -> bool {
        ui.memory().is_focused(self.id)
    }

    pub fn request_focus(&self, ui: &mut UI) {
        ui.memory().request_focus(self.id);
    }

    pub fn release_focus(&self, ui: &mut UI) {
        if self.is_focused(ui) {
            ui.memory().release_focus();
        }
    }

    pub fn transform(&self, ui: &mut UI) -> TSTransform {
        ui.memory().get::<LayoutMemory>(self.id).transform
    }

    pub fn scale(&self, ui: &mut UI) -> f32 {
        self.transform(ui).scale
    }

}