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
//! System for the navigation tree and default input systems to get started.
use crate::events::{Direction, NavRequest, ScopeDirection};
use crate::{resolve::max_by_in_iter, Focusable, Focused};
use bevy::ecs::system::SystemParam;
use bevy::math::Vec3Swizzles;
use bevy::prelude::*;

/// Control default ui navigation input buttons
pub struct InputMapping {
    /// Deadzone on the gamepad left stick for ui navigation
    pub joystick_ui_deadzone: f32,
    /// X axis of gamepad stick
    pub move_x: GamepadAxisType,
    /// Y axis of gamepad stick
    pub move_y: GamepadAxisType,
    /// X axis of gamepad dpad
    pub move_x_dpad: GamepadAxisType,
    /// Y axis of gamepad dpad
    pub move_y_dpad: GamepadAxisType,
    /// Gamepad button for [`NavRequest::Action`]
    pub action_button: GamepadButtonType,
    /// Gamepad button for [`NavRequest::Cancel`]
    pub cancel_button: GamepadButtonType,
    /// Gamepad button for [`ScopeDirection::Previous`] [`NavRequest::ScopeMove`]
    pub previous_button: GamepadButtonType,
    /// Gamepad button for [`ScopeDirection::Next`] [`NavRequest::ScopeMove`]
    pub next_button: GamepadButtonType,
    /// Gamepad button for [`NavRequest::Free`]
    pub free_button: GamepadButtonType,
    /// Keyboard key for [`Direction::West`] [`NavRequest::Move`]
    pub key_left: KeyCode,
    /// Keyboard key for [`Direction::East`] [`NavRequest::Move`]
    pub key_right: KeyCode,
    /// Keyboard key for [`Direction::North`] [`NavRequest::Move`]
    pub key_up: KeyCode,
    /// Keyboard key for [`Direction::South`] [`NavRequest::Move`]
    pub key_down: KeyCode,
    /// Alternative keyboard key for [`Direction::West`] [`NavRequest::Move`]
    pub key_left_alt: KeyCode,
    /// Alternative keyboard key for [`Direction::East`] [`NavRequest::Move`]
    pub key_right_alt: KeyCode,
    /// Alternative keyboard key for [`Direction::North`] [`NavRequest::Move`]
    pub key_up_alt: KeyCode,
    /// Alternative keyboard key for [`Direction::South`] [`NavRequest::Move`]
    pub key_down_alt: KeyCode,
    /// Keyboard key for [`NavRequest::Action`]
    pub key_action: KeyCode,
    /// Keyboard key for [`NavRequest::Cancel`]
    pub key_cancel: KeyCode,
    /// Keyboard key for [`ScopeDirection::Next`] [`NavRequest::ScopeMove`]
    pub key_next: KeyCode,
    /// Alternative keyboard key for [`ScopeDirection::Next`] [`NavRequest::ScopeMove`]
    pub key_next_alt: KeyCode,
    /// Keyboard key for [`ScopeDirection::Previous`] [`NavRequest::ScopeMove`]
    pub key_previous: KeyCode,
    /// Keyboard key for [`NavRequest::Free`]
    pub key_free: KeyCode,
    /// Mouse button for [`NavRequest::Action`]
    pub mouse_action: MouseButton,
}
impl Default for InputMapping {
    fn default() -> Self {
        InputMapping {
            joystick_ui_deadzone: 0.36,
            move_x: GamepadAxisType::LeftStickX,
            move_y: GamepadAxisType::LeftStickY,
            move_x_dpad: GamepadAxisType::DPadX,
            move_y_dpad: GamepadAxisType::DPadY,
            action_button: GamepadButtonType::South,
            cancel_button: GamepadButtonType::East,
            previous_button: GamepadButtonType::LeftTrigger,
            next_button: GamepadButtonType::RightTrigger,
            free_button: GamepadButtonType::Start,
            key_left: KeyCode::A,
            key_right: KeyCode::D,
            key_up: KeyCode::W,
            key_down: KeyCode::S,
            key_left_alt: KeyCode::Left,
            key_right_alt: KeyCode::Right,
            key_up_alt: KeyCode::Up,
            key_down_alt: KeyCode::Down,
            key_action: KeyCode::Space,
            key_cancel: KeyCode::Back,
            key_next: KeyCode::E,
            key_next_alt: KeyCode::Tab,
            key_previous: KeyCode::Q,
            key_free: KeyCode::Escape,
            mouse_action: MouseButton::Left,
        }
    }
}

/// `mapping { XYZ::X => ABC::A, XYZ::Y => ABC::B, XYZ::Z => ABC::C }: [(XYZ, ABC)]`
macro_rules! mapping {
    ($($from:expr => $to:expr),* ) => ([$( ( $from, $to ) ),*])
}

/// A system to send gamepad control events to the focus system
///
/// Dpad and left stick for movement, `LT` and `RT` for scopped menus, `A` `B`
/// for selection and cancel.
///
/// The button mapping may be controlled through the [`InputMapping`] resource.
/// You may however need to customize the behavior of this system (typically
/// when integrating in the game) in this case, you should write your own
/// system that sends [`NavRequest`](crate::NavRequest) events
pub fn default_gamepad_input(
    mut nav_cmds: EventWriter<NavRequest>,
    has_focused: Query<(), With<Focused>>,
    input_mapping: Res<InputMapping>,
    buttons: Res<Input<GamepadButton>>,
    axis: Res<Axis<GamepadAxis>>,
    mut ui_input_status: Local<bool>,
) {
    use Direction::*;
    use NavRequest::{Action, Cancel, Free, Move, ScopeMove};

    if has_focused.is_empty() {
        // Do not compute navigation if there is no focus to change
        return;
    }

    let pad = Gamepad(0);
    macro_rules! axis_delta {
        ($dir:ident, $axis:ident) => {
            axis.get(GamepadAxis(pad, input_mapping.$axis))
                .map_or(Vec2::ZERO, |v| Vec2::$dir * v)
        };
    }

    let stick_move = axis_delta!(Y, move_y) + axis_delta!(X, move_x);
    let dpad_move = axis_delta!(Y, move_y_dpad) + axis_delta!(X, move_x_dpad);
    let dpad_greater = dpad_move.length_squared() > stick_move.length_squared();
    let delta = if dpad_greater { dpad_move } else { stick_move };
    if delta.length_squared() > input_mapping.joystick_ui_deadzone && !*ui_input_status {
        let direction = match () {
            () if delta.y < delta.x && delta.y < -delta.x => South,
            () if delta.y < delta.x => East,
            () if delta.y >= delta.x && delta.y > -delta.x => North,
            () => West,
        };
        nav_cmds.send(Move(direction));
        *ui_input_status = true;
    } else if delta.length_squared() <= input_mapping.joystick_ui_deadzone {
        *ui_input_status = false;
    }

    let command_mapping = mapping! {
        input_mapping.action_button => Action,
        input_mapping.cancel_button => Cancel,
        input_mapping.next_button => ScopeMove(ScopeDirection::Next),
        input_mapping.free_button => Free,
        input_mapping.previous_button => ScopeMove(ScopeDirection::Previous)
    };
    for (key, request) in command_mapping {
        if buttons.just_pressed(GamepadButton(pad, key)) {
            nav_cmds.send(request)
        }
    }
}

/// A system to send keyboard control events to the focus system
///
/// supports `WASD` and arrow keys for the directions, `E`, `Q` and `Tab` for
/// scopped menus, `Backspace` and `Enter` for cancel and selection
///
/// The button mapping may be controlled through the [`InputMapping`] resource.
/// You may however need to customize the behavior of this system (typically
/// when integrating in the game) in this case, you should write your own
/// system that sends [`NavRequest`](crate::NavRequest) events
pub fn default_keyboard_input(
    has_focused: Query<(), With<Focused>>,
    keyboard: Res<Input<KeyCode>>,
    input_mapping: Res<InputMapping>,
    mut nav_cmds: EventWriter<NavRequest>,
) {
    use Direction::*;
    use NavRequest::*;

    if has_focused.is_empty() {
        // Do not compute navigation if there is no focus to change
        return;
    }

    let command_mapping = mapping! {
        input_mapping.key_action => Action,
        input_mapping.key_cancel => Cancel,
        input_mapping.key_up => Move(North),
        input_mapping.key_down => Move(South),
        input_mapping.key_left => Move(West),
        input_mapping.key_right => Move(East),
        input_mapping.key_up_alt => Move(North),
        input_mapping.key_down_alt => Move(South),
        input_mapping.key_left_alt => Move(West),
        input_mapping.key_right_alt => Move(East),
        input_mapping.key_next => ScopeMove(ScopeDirection::Next),
        input_mapping.key_next_alt => ScopeMove(ScopeDirection::Next),
        input_mapping.key_free => Free,
        input_mapping.key_previous => ScopeMove(ScopeDirection::Previous)
    };
    for (key, request) in command_mapping {
        if keyboard.just_pressed(key) {
            nav_cmds.send(request)
        }
    }
}

/// [`SystemParam`](https://docs.rs/bevy/0.6.0/bevy/ecs/system/trait.SystemParam.html)
/// used to compute UI focusable physical positions in mouse input systems.
#[derive(SystemParam)]
pub struct NodePosQuery<'w, 's, T: Component, U: Component> {
    entities: Query<'w, 's, (Entity, &'static T, &'static GlobalTransform), With<Focusable>>,
    cam: Query<'w, 's, &'static GlobalTransform, With<U>>,
}
impl<'w, 's, T: Component, U: Component> NodePosQuery<'w, 's, T, U> {
    fn camera_offset(&self) -> Vec2 {
        self.cam
            .get_single()
            .ok()
            .map_or(Vec2::ZERO, |trans| trans.translation.xy())
    }
}

fn is_in_node<T: ScreenSize>(at: Vec2, (_, node, trans): &(Entity, &T, &GlobalTransform)) -> bool {
    let ui_pos = trans.translation.truncate();
    let node_half_size = node.size() / 2.0;
    let min = ui_pos - node_half_size;
    let max = ui_pos + node_half_size;
    (min.x..max.x).contains(&at.x) && (min.y..max.y).contains(&at.y)
}

/// Check which [`Focusable`] displays below `at` if any
pub fn ui_focusable_at<T, U>(at: Vec2, query: &NodePosQuery<T, U>) -> Option<Entity>
where
    T: ScreenSize + Component,
    U: Component,
{
    let ui_cam_position = query.camera_offset();
    let under_mouse = query
        .entities
        .iter()
        .filter(|query_elem| is_in_node(at + ui_cam_position, query_elem));
    max_by_in_iter(under_mouse, |elem| elem.2.translation.z).map(|elem| elem.0)
}

fn cursor_pos(windows: &Windows) -> Option<Vec2> {
    windows.get_primary().and_then(|w| w.cursor_position())
}

pub trait ScreenSize {
    fn size(&self) -> Vec2;
}

#[cfg(feature = "bevy-ui")]
impl ScreenSize for Node {
    fn size(&self) -> Vec2 {
        self.size
    }
}

/// Marker component for "camera" entity.
///
/// We will use the `GlobalTransform` of this entity to compute the offset
/// needed to correct mouse positions in real-world coordinate.
#[cfg(feature = "bevy-ui")]
#[doc(hidden)]
#[derive(Component)]
pub struct UiCamera;

/// A system to send mouse control events to the focus system
///
/// Unlike [`generic_default_mouse_input`], this system is gated by the
/// `bevy-ui` feature. It relies on bevy/render specific types:
/// `bevy::render::Camera` and `bevy::ui::Node`.
///
/// Which button to press to cause an action event is specified in the
/// [`InputMapping`] resource.
///
/// You may however need to customize the behavior of this system (typically
/// when integrating in the game) in this case, you should write your own
/// system that sends [`NavRequest`](crate::NavRequest) events. You may use
/// [`ui_focusable_at`] to tell which focusable is currently being hovered.
#[cfg(feature = "bevy-ui")]
#[allow(clippy::too_many_arguments)]
pub fn default_mouse_input(
    input_mapping: Res<InputMapping>,
    windows: Res<Windows>,
    mouse: Res<Input<MouseButton>>,
    touch: Res<Touches>,
    focusables: NodePosQuery<Node, UiCamera>,
    names: Query<(Entity, &Camera)>,
    focused: Query<Entity, With<Focused>>,
    mut cmds: Commands,
    nav_cmds: EventWriter<NavRequest>,
    last_pos: Local<Vec2>,
) {
    use bevy::ui::CAMERA_UI;
    if focusables.cam.get_single().is_ok() {
        generic_default_mouse_input(
            input_mapping,
            windows,
            mouse,
            touch,
            focusables,
            focused,
            nav_cmds,
            last_pos,
        );
    } else {
        let ui_cam = names
            .iter()
            .find(|cam| cam.1.name.as_deref() == Some(CAMERA_UI));
        if let Some((ui_cam_entity, _)) = ui_cam {
            cmds.entity(ui_cam_entity).insert(UiCamera);
        }
    }
}

/// A generic system to send mouse control events to the focus system
///
/// `T` must be a component assigned to `Focusable` elements that implements
/// the [`ScreenSize`] trait. `M` is simply a marker component for your camera
/// entity, if no entities has the `M` component, we assume the screen space
/// is not offset from the `GlobalTransform` of focusable elements.
///
/// Which button to press to cause an action event is specified in the
/// [`InputMapping`] resource.
///
/// You may however need to customize the behavior of this system (typically
/// when integrating in the game) in this case, you should write your own
/// system that sends [`NavRequest`](crate::NavRequest) events. You may use
/// [`ui_focusable_at`] to tell which focusable is currently being hovered.
#[allow(clippy::too_many_arguments)]
pub fn generic_default_mouse_input<T: ScreenSize + Component, M: Component>(
    input_mapping: Res<InputMapping>,
    windows: Res<Windows>,
    mouse: Res<Input<MouseButton>>,
    touch: Res<Touches>,
    focusables: NodePosQuery<T, M>,
    focused: Query<Entity, With<Focused>>,
    mut nav_cmds: EventWriter<NavRequest>,
    mut last_pos: Local<Vec2>,
) {
    let cursor_pos = match cursor_pos(&windows) {
        Some(c) => c,
        None => return,
    };
    let ui_camera_position = focusables.camera_offset();
    let released = mouse.just_released(input_mapping.mouse_action) || touch.just_released(0);
    let focused = focused.get_single();
    // Return early if cursor didn't move since last call
    if !released && *last_pos == cursor_pos {
        return;
    } else {
        *last_pos = cursor_pos;
    }
    let not_hovering_focused = |focused: &Entity| {
        let focused = focusables
            .entities
            .get(*focused)
            .expect("Entity with `Focused` component must also have a `Focusable` component");
        !is_in_node(cursor_pos + ui_camera_position, &focused)
    };
    // If the currently hovered node is the focused one, there is no need to
    // find which node we are hovering and to switch focus to it (since we are
    // already focused on it)
    if focused.iter().all(not_hovering_focused) {
        // `ui_focusable_at` is computationally heavy
        let to_target = match ui_focusable_at(cursor_pos, &focusables) {
            Some(c) => c,
            None => return,
        };
        nav_cmds.send(NavRequest::FocusOn(to_target));
    } else if released {
        nav_cmds.send(NavRequest::Action);
    }
}