Expand description
Improve the keyboard and gamepads usage for egui in Bevy.
Usage:
- Add
KbgpPlugin. - Use the extension methods on the egui widgets to add KBGP’s functionality.
- Call
ui.kbgp_clear_inputwhen triggering a state transition as a response to a click on an egui widget. To control the focus in the new state, usekbgp_focus_label(andkbgp_set_focus_label) - otherwise egui will pick the widget to focus on (or elect to drop the focus entirely) - To set special actions, see the example here. To avoid having to deal with both Bevy’s input methods and KBGP’s input, it’s better to use these actions for entering the pause menu from within the game.
- Use
kbgp_click_releasedinstead of egui’sclickedto register the button presss only when the user releases the key/button. This is useful for exiting menus, to avoid having the same key/button that was used to exit the menu registered as actual game input.
use bevy_egui_kbgp::{egui, bevy_egui};
use bevy::prelude::*;
use bevy_egui::{EguiPrimaryContextPass, EguiContexts, EguiPlugin};
use bevy_egui_kbgp::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin::default())
.add_plugins(KbgpPlugin)
.add_systems(EguiPrimaryContextPass, ui_system)
.run();
}
fn ui_system(
mut egui_context: EguiContexts,
keys: Res<ButtonInput<KeyCode>>,
) -> Result {
egui::CentralPanel::default().show(egui_context.ctx_mut()?, |ui| {
if ui
.button("Button")
.kbgp_initial_focus()
.kbgp_navigation()
.clicked()
{
// Button action
}
if let Some(input_selected_by_player) = ui
.button("Set Input")
.kbgp_navigation()
.kbgp_pending_input()
{
// Do something with the input
}
});
Ok(())
}§Creating Key-Setting UI
Use functions like kbgp_pending_input to
convert a regular button to a key-setting button. When the players presses that button, they’ll
be prompted to enter input from the keyboard, the mouse, or a gamepad. That input will be
returned as a KbgpInput.
kbgp_pending_chord is similar, but prompts
the player to enter multiple keys instead of just one.
Both functions have several variants that allow limiting the chords/keys accepted by that button.
By default, mouse wheel input is disabled. The reason is that mouse wheel events are a pain to deal with, and most third party crates that ease input handling don’t support them - so it’s better not to let the player select input that the game is unable to deal with.
Re-exports§
Modules§
Structs§
- Kbgp
Input Manual Handle - Handle for
kbgp_pending_input_manual. - Kbgp
NavBindings - Input mapping for navigation.
- Kbgp
Plugin - Adds KBGP input handling system and
KbgpSettings. - Kbgp
Prepare Pending Input - An option of
KbgpPrepare. - Kbgp
Settings - General configuration resource for KBGP.
Enums§
- Kbgp
Input - Input from the keyboard or from a gamepad.
- Kbgp
Input Source - Input from the keyboard or from a gamepad.
- Kbgp
NavActivation - Kbgp
NavCommand - Kbgp
Prepare - Object used to configure KBGP’s behavior in
kbgp_prepare.
Traits§
- Kbgp
Egui Response Ext - Extensions for egui’s
Responseto activate KBGP’s functionality. - Kbgp
Egui UiCtx Ext - Extensions for egui’s
UIand Context to activate KBGP’s functionality.
Functions§
- kbgp_
focus_ on_ mouse_ movement - Transfer focus when the mouse moves into a widget.
- kbgp_
intercept_ default_ navigation - Cancel’s any tab-based navigation egui did in its
BeginFrame. - kbgp_
prepare - Must be called every frame, either manually or by using
KbgpPlugin. - kbgp_
prevent_ loss_ of_ focus - Make sure there is always an egui widget that has the focus.