Skip to content

Commit

Permalink
feat: setup gamepad to control egui focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Oct 2, 2023
1 parent 7feece2 commit 50ff46b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn main() {
.install_plugin(DefaultGamePlugin)
.install_plugin(settings::game_plugin)
.install_plugin(input::game_plugin)
.install_plugin(ui::game_plugin)
.install_plugin(core::game_plugin)
// We initialize the asset server and register asset types
.init_shared_resource::<AssetServer>()
Expand Down
62 changes: 62 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,68 @@ use crate::prelude::*;
pub mod main_menu;
pub mod pause_menu;

pub fn game_plugin(game: &mut Game) {
game.insert_shared_resource(EguiInputHook::new(update_gamepad_ui_inputs));
}

/// Takes the player controls from gamepads and converts them to arrow key inputs for egui so that
/// you can navigate the menu with the gamepad.
fn update_gamepad_ui_inputs(game: &mut Game, input: &mut egui::RawInput) {
let Some(player_controls) = game.shared_resource::<GlobalPlayerControls>() else { return };

for player_control in player_controls.iter() {
if player_control.just_moved {
if player_control.move_direction.y > 0.1 {
input.events.push(egui::Event::Key {
key: egui::Key::ArrowUp,
pressed: true,
repeat: false,
modifiers: default(),
});
} else if player_control.move_direction.y < -0.1 {
input.events.push(egui::Event::Key {
key: egui::Key::ArrowDown,
pressed: true,
repeat: false,
modifiers: default(),
});
} else if player_control.move_direction.x < -0.1 {
input.events.push(egui::Event::Key {
key: egui::Key::ArrowLeft,
pressed: true,
repeat: false,
modifiers: default(),
});
} else if player_control.move_direction.x > 0.1 {
input.events.push(egui::Event::Key {
key: egui::Key::ArrowRight,
pressed: true,
repeat: false,
modifiers: default(),
});
}
}

if player_control.menu_confirm_just_pressed {
input.events.push(egui::Event::Key {
key: egui::Key::Enter,
pressed: true,
repeat: false,
modifiers: default(),
});
}

if player_control.menu_back_just_pressed {
input.events.push(egui::Event::Key {
key: egui::Key::Escape,
pressed: true,
repeat: false,
modifiers: default(),
});
}
}
}

#[derive(HasSchema, Clone, Debug)]
#[repr(C)]
pub struct UiTheme {
Expand Down

0 comments on commit 50ff46b

Please sign in to comment.