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 bevy_egui_kbgp::{egui, bevy_egui};
use bevy::prelude::*;
use bevy_egui::{EguiContext, EguiPlugin};
use bevy_egui_kbgp::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(EguiPlugin)
        .add_plugin(KbgpPlugin)
        .add_system(ui_system)
        .run();
}

fn ui_system(
    mut egui_context: ResMut<EguiContext>,
    keys: Res<Input<KeyCode>>,
) {
    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
        }
    });
}

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

Structs

Input mapping for navigation.
Adds KBGP input handling system and KbgpSettings.
General configuration resource for KBGP.

Enums

Input from the keyboard or from a gamepad.
Input from the keyboard or from a gamepad.
Object used to configure KBGP’s behavior in kbgp_prepare.

Traits

Extensions for egui’s Response to activate KBGP’s functionality.
Extensions for egui’s UI and Context to activate KBGP’s functionality.

Functions

Transfer focus when the mouse moves into a widget.
Hide from egui Space and Enter key events.
Cancel’s any tab-based navigation egui did in its BeginFrame.
Must be called every frame, either manually or by using KbgpPlugin.
Make sure there is always an egui widget that has the focus.