spitfire_gui/
interactions.rs

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
use raui_core::prelude::*;
use spitfire_input::{ArrayInputCombinator, InputActionRef, InputCharactersRef};

const ZERO_THRESHOLD: f32 = 1.0e-6;

#[derive(Default)]
pub struct GuiInteractionsInputs {
    pub trigger: InputActionRef,
    pub context: InputActionRef,
    pub cancel: InputActionRef,
    pub left: InputActionRef,
    pub right: InputActionRef,
    pub up: InputActionRef,
    pub down: InputActionRef,
    pub prev: InputActionRef,
    pub next: InputActionRef,
    pub text: InputCharactersRef,
    pub text_start: InputActionRef,
    pub text_end: InputActionRef,
    pub text_delete_left: InputActionRef,
    pub text_delete_right: InputActionRef,
    pub pointer_position: ArrayInputCombinator<2>,
    pub pointer_trigger: InputActionRef,
    pub pointer_context: InputActionRef,
    pub scroll: ArrayInputCombinator<2>,
}

#[derive(Default)]
pub struct GuiInteractionsEngine {
    pub inputs: GuiInteractionsInputs,
    pub engine: DefaultInteractionsEngine,
    cached_pointer_position: Vec2,
}

impl GuiInteractionsEngine {
    pub fn maintain(&mut self, mapping: &CoordsMapping) {
        if self.engine.focused_text_input().is_some() {
            if let Some(mut text) = self.inputs.text.write() {
                for character in text.take().chars() {
                    self.engine
                        .interact(Interaction::Navigate(NavSignal::TextChange(
                            NavTextChange::InsertCharacter(character),
                        )));
                }
            }
            if self.inputs.left.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::MoveCursorLeft,
                    )));
            }
            if self.inputs.right.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::MoveCursorRight,
                    )));
            }
            if self.inputs.text_start.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::MoveCursorStart,
                    )));
            }
            if self.inputs.text_end.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::MoveCursorEnd,
                    )));
            }
            if self.inputs.text_delete_left.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::DeleteLeft,
                    )));
            }
            if self.inputs.text_delete_right.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::DeleteRight,
                    )));
            }
            if self.inputs.trigger.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::TextChange(
                        NavTextChange::NewLine,
                    )));
            }
        } else {
            if self.inputs.up.get().is_pressed() {
                self.engine.interact(Interaction::Navigate(NavSignal::Up));
            }
            if self.inputs.down.get().is_pressed() {
                self.engine.interact(Interaction::Navigate(NavSignal::Down));
            }
            if self.inputs.left.get().is_pressed() {
                self.engine.interact(Interaction::Navigate(NavSignal::Left));
            }
            if self.inputs.right.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::Right));
            }
            if self.inputs.prev.get().is_pressed() {
                self.engine.interact(Interaction::Navigate(NavSignal::Prev));
            }
            if self.inputs.next.get().is_pressed() {
                self.engine.interact(Interaction::Navigate(NavSignal::Next));
            }
            if self.inputs.trigger.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::Accept(true)));
            }
            if self.inputs.context.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::Context(true)));
            }
            if self.inputs.cancel.get().is_pressed() {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::Cancel(true)));
            }
        }
        let pointer_position = {
            let [x, y] = self.inputs.pointer_position.get();
            let position = mapping.real_to_virtual_vec2(Vec2 { x, y }, false);
            if (position.x - self.cached_pointer_position.x).abs() > ZERO_THRESHOLD
                || (position.y - self.cached_pointer_position.y).abs() > ZERO_THRESHOLD
            {
                self.cached_pointer_position = position;
                self.engine.interact(Interaction::PointerMove(position));
            }
            position
        };
        let pointer_trigger = self.inputs.pointer_trigger.get();
        let pointer_context = self.inputs.pointer_context.get();
        if pointer_trigger.is_pressed() {
            self.engine.interact(Interaction::PointerDown(
                PointerButton::Trigger,
                pointer_position,
            ));
        }
        if pointer_trigger.is_released() {
            self.engine.interact(Interaction::PointerUp(
                PointerButton::Trigger,
                pointer_position,
            ));
        }
        if pointer_context.is_pressed() {
            self.engine.interact(Interaction::PointerDown(
                PointerButton::Context,
                pointer_position,
            ));
        }
        if pointer_context.is_released() {
            self.engine.interact(Interaction::PointerUp(
                PointerButton::Context,
                pointer_position,
            ));
        }
        {
            let [x, y] = self.inputs.scroll.get();
            if x.abs() > ZERO_THRESHOLD || y.abs() > ZERO_THRESHOLD {
                self.engine
                    .interact(Interaction::Navigate(NavSignal::Jump(NavJump::Scroll(
                        NavScroll::Units(Vec2 { x: -x, y: -y }, true),
                    ))));
            }
        }
    }
}

impl InteractionsEngine<DefaultInteractionsEngineResult, ()> for GuiInteractionsEngine {
    fn perform_interactions(
        &mut self,
        app: &mut Application,
    ) -> Result<DefaultInteractionsEngineResult, ()> {
        self.engine.perform_interactions(app)
    }
}