Crate bevy_egui_kbgp

Crate bevy_egui_kbgp 

Source
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_input when triggering a state transition as a response to a click on an egui widget. To control the focus in the new state, use kbgp_focus_label (and kbgp_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_released instead of egui’s clicked to 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§

pub use bevy_egui;
pub use bevy_egui::egui;

Modules§

prelude

Structs§

KbgpInputManualHandle
Handle for kbgp_pending_input_manual.
KbgpNavBindings
Input mapping for navigation.
KbgpPlugin
Adds KBGP input handling system and KbgpSettings.
KbgpPreparePendingInput
An option of KbgpPrepare.
KbgpSettings
General configuration resource for KBGP.

Enums§

KbgpInput
Input from the keyboard or from a gamepad.
KbgpInputSource
Input from the keyboard or from a gamepad.
KbgpNavActivation
KbgpNavCommand
KbgpPrepare
Object used to configure KBGP’s behavior in kbgp_prepare.

Traits§

KbgpEguiResponseExt
Extensions for egui’s Response to activate KBGP’s functionality.
KbgpEguiUiCtxExt
Extensions for egui’s UI and 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.