Skip to main content

unbanished_ui/
input.rs

1mod keyboard;
2mod pointer;
3mod tooltip;
4
5use crate::controls::{intersect, scroll_thumb};
6use crate::*;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9/// A key the interface reacts to.
10pub enum Key {
11    /// Tab: the next control.
12    Tab,
13    /// Shift and Tab: the previous control.
14    BackTab,
15    /// Enter.
16    Enter,
17    /// Escape.
18    Escape,
19    /// The space bar.
20    Space,
21    /// The left arrow.
22    Left,
23    /// The right arrow.
24    Right,
25    /// The up arrow.
26    Up,
27    /// The down arrow.
28    Down,
29    /// Home.
30    Home,
31    /// End.
32    End,
33    /// Page Up.
34    PageUp,
35    /// Page Down.
36    PageDown,
37    /// Backspace.
38    Backspace,
39    /// Delete.
40    Delete,
41    /// A character key.
42    Character(char),
43}
44
45impl Ui {
46    /// The element that has the keyboard focus.
47    pub fn focused(&self) -> Option<NodeId> {
48        self.focus
49    }
50    /// The pointer in UI units, as last moved.
51    pub fn pointer(&self) -> (i32, i32) {
52        self.pointer
53    }
54    /// Whether the current pointer target is a control that consumes pointer input.
55    pub fn pointer_over_interactive(&self) -> bool {
56        self.hot.is_some_and(|id| self.interactive(id))
57    }
58    /// `DialogInstance::GetMouseCursorIndex`: the cursor index of `id`, unless one of its
59    /// visible children holds `(x, y)`, then that child's, the last such child winning.
60    pub fn cursor_index(&self, id: NodeId, x: i32, y: i32) -> i32 {
61        let n = &self.nodes[id];
62        let mut cursor = n.cursor;
63        for &c in &n.children {
64            let child = &self.nodes[c];
65            if child.visible() && child.contains(x, y) {
66                cursor = self.cursor_index(c, x, y);
67            }
68        }
69        cursor
70    }
71
72    /// Whether key input should be reserved for an active text edit control.
73    pub fn text_edit_focused(&self) -> bool {
74        self.focus
75            .is_some_and(|id| matches!(&self.nodes[id].kind, Kind::Control(Control::Edit(_))))
76    }
77    /// The events since the last call, in order. (A click is returned by
78    /// [`pointer_button`](Self::pointer_button) instead.)
79    pub fn drain_events(&mut self) -> Vec<Event> {
80        std::mem::take(&mut self.pending)
81    }
82    /// Set the key that clicks an element: an upper-case character code, or 13 (Enter) or 27
83    /// (Escape).
84    pub fn set_shortcut(&mut self, id: NodeId, key: u32) {
85        self.nodes[id].shortcut = Some(key);
86    }
87    /// Forget the pointer, the focus and the pending events, for example when the window loses the
88    /// focus.
89    pub fn clear_input(&mut self) {
90        self.focus = None;
91        self.hot = None;
92        self.pressed = None;
93        self.pointer_down = false;
94        self.pending.clear();
95        self.input_root = None;
96        self.tooltip_instance = None;
97    }
98}
99
100fn contains(r: [i32; 4], x: i32, y: i32) -> bool {
101    x >= r[0] && x < r[2] && y >= r[1] && y < r[3]
102}
103
104pub(crate) fn event_node(e: Event) -> NodeId {
105    match e {
106        Event::Clicked(n)
107        | Event::Selected(n, _)
108        | Event::Changed(n)
109        | Event::Submitted(n)
110        | Event::Link(n, _) => n,
111    }
112}
113
114/// The type ids (CRC-32 of the class name) of the game's tooltip controllers that the interface
115/// handles itself.
116pub mod tooltip_controller {
117    /// `HotKeyToolTipUI`: a tooltip that shows the element's key.
118    pub const HOT_KEY: u32 = 0x5dd8_3c93;
119    /// `TitleDescToolTipUI`: a tooltip with a title and a description.
120    pub const TITLE_DESC: u32 = 0x33e6_1003;
121    /// The tooltip that lists what a building needs.
122    pub const BUILD_REQUIREMENT: u32 = 0xc3fe_f549;
123}