bevy_egui_kbgp

Trait KbgpEguiResponseExt

Source
pub trait KbgpEguiResponseExt: Sized {
Show 16 methods // Required methods fn kbgp_focus_label<T: 'static + PartialEq<T>>(self, label: T) -> Self; fn kbgp_navigation(self) -> Self; fn kbgp_user_action<T: 'static + Clone>(&self) -> Option<T>; fn kbgp_activated<T: 'static + Clone>(&self) -> KbgpNavActivation<T>; fn kbgp_activate_released<T: 'static + Clone>(&self) -> KbgpNavActivation<T>; fn kbgp_click_released(&self) -> bool; fn kbgp_user_action_released<T: 'static + Clone>(&self) -> Option<T>; fn kbgp_pending_input(&self) -> Option<KbgpInput>; fn kbgp_pending_input_of_source( &self, source: KbgpInputSource, ) -> Option<KbgpInput>; fn kbgp_pending_input_vetted( &self, pred: impl FnMut(KbgpInput) -> bool, ) -> Option<KbgpInput>; fn kbgp_pending_chord(&self) -> Option<HashSet<KbgpInput>>; fn kbgp_pending_chord_of_source( &self, source: KbgpInputSource, ) -> Option<HashSet<KbgpInput>>; fn kbgp_pending_chord_same_source(&self) -> Option<HashSet<KbgpInput>>; fn kbgp_pending_chord_vetted( &self, pred: impl FnMut(&HashSet<KbgpInput>, KbgpInput) -> bool, ) -> Option<HashSet<KbgpInput>>; fn kbgp_pending_input_manual<T>( &self, dlg: impl FnOnce(&Self, KbgpInputManualHandle<'_>) -> Option<T>, ) -> Option<T>; // Provided method fn kbgp_initial_focus(self) -> Self { ... }
}
Expand description

Extensions for egui’s Response to activate KBGP’s functionality.

if ui
    .button("My Button")
    .kbgp_initial_focus() // focus on this button when starting the UI
    .kbgp_navigation() // navigate to and from this button with keyboard/gamepad
    .clicked()
{
    // ...
}

Required Methods§

Source

fn kbgp_focus_label<T: 'static + PartialEq<T>>(self, label: T) -> Self

Focus on this widget when kbgp_set_focus_label is called with the same label.

This will only happen if kbgp_set_focus_label was called in the previous frame. A single widget can be marked with multiple labels by calling kbgp_focus_label multiple times.

#[derive(PartialEq)]
enum FocusLabel {
    Left,
    Right,
}
if ui
    .button("Focus >")
    .kbgp_navigation()
    .kbgp_focus_label(FocusLabel::Left)
    .clicked()
{
    ui.kbgp_set_focus_label(FocusLabel::Right);
}
if ui
    .button("< Focus")
    .kbgp_navigation()
    .kbgp_focus_label(FocusLabel::Right)
    .clicked()
{
    ui.kbgp_set_focus_label(FocusLabel::Left);
}
Source

fn kbgp_navigation(self) -> Self

Navigate to and from this widget.

Source

fn kbgp_user_action<T: 'static + Clone>(&self) -> Option<T>

Check if the player pressed a user action button while focused on this widget.

match ui.button("Button").kbgp_user_action() {
    None => {}
    Some(MyUserAction::Action1) => println!("User action 1"),
    Some(MyUserAction::Action2) => println!("User action 2"),
}
Source

fn kbgp_activated<T: 'static + Clone>(&self) -> KbgpNavActivation<T>

Check if the player activated this widget or pressed a user action button while focused on it.

match ui.button("Button").kbgp_activated() {
    KbgpNavActivation::Clicked => println!("Regular activateion"),
    KbgpNavActivation::ClickedSecondary | KbgpNavActivation::User(SpecialAction::Special1) => println!("Special activateion 1"),
    KbgpNavActivation::ClickedMiddle | KbgpNavActivation::User(SpecialAction::Special2) => println!("Special activateion 2"),
    _ => {}
}
Source

fn kbgp_activate_released<T: 'static + Clone>(&self) -> KbgpNavActivation<T>

Similar to kbgp_activated, but only returns a non-KbgpNavActivation::None value when the key/button is released.

Source

fn kbgp_click_released(&self) -> bool

Similar to egui’s clicked, but only returns true when the key/button is released.

Source

fn kbgp_user_action_released<T: 'static + Clone>(&self) -> Option<T>

Similar to kbgp_user_action, but only returns Some when the key/button is released.

Source

fn kbgp_pending_input(&self) -> Option<KbgpInput>

Accept a single key/button input from this widget.

Must be called on widgets that had kbgp_navigation called on them.

use bevy::prelude::*;
use bevy_egui::{EguiContexts, EguiPlugin};
use bevy_egui_kbgp::{egui, bevy_egui};
use bevy_egui_kbgp::prelude::*;
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(EguiPlugin)
        .add_plugins(KbgpPlugin)
        .add_systems(Update, ui_system)
        .insert_resource(JumpInput(KbgpInput::Keyboard(KeyCode::Space)))
        .run();
}

#[derive(Resource)]
struct JumpInput(KbgpInput);

fn ui_system(
    mut egui_context: EguiContexts,
    mut jump_input: ResMut<JumpInput>,
) {
    egui::CentralPanel::default().show(egui_context.ctx_mut(), |ui| {
        ui.horizontal(|ui| {
            ui.label("Set button for jumping");
            if let Some(new_jump_input) = ui.button(format!("{}", jump_input.0))
                .kbgp_navigation()
                .kbgp_pending_input()
            {
                jump_input.0 = new_jump_input;
            }
        });
    });
}
Source

fn kbgp_pending_input_of_source( &self, source: KbgpInputSource, ) -> Option<KbgpInput>

Accept a single key/button input from this widget, limited to a specific input source.

Source

fn kbgp_pending_input_vetted( &self, pred: impl FnMut(KbgpInput) -> bool, ) -> Option<KbgpInput>

Accept a single key/button input from this widget, with the ability to filter which inputs to accept.

Source

fn kbgp_pending_chord(&self) -> Option<HashSet<KbgpInput>>

Accept a chord of key/button inputs from this widget.

Must be called on widgets that had kbgp_navigation called on them.

use bevy::prelude::*;
use bevy_egui::{EguiContexts, EguiPlugin};
use bevy_egui_kbgp::{egui, bevy_egui};
use bevy_egui_kbgp::prelude::*;
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(EguiPlugin)
        .add_plugins(KbgpPlugin)
        .add_systems(Update, ui_system)
        .insert_resource(JumpChord(vec![KbgpInput::Keyboard(KeyCode::Space)]))
        .run();
}

#[derive(Resource)]
struct JumpChord(Vec<KbgpInput>);

fn ui_system(
    mut egui_context: EguiContexts,
    mut jump_chord: ResMut<JumpChord>,
) {
    egui::CentralPanel::default().show(egui_context.ctx_mut(), |ui| {
        ui.horizontal(|ui| {
            ui.label("Set chord of buttons for jumping");
            if let Some(new_jump_chord) = ui
                .button(KbgpInput::format_chord(jump_chord.0.iter().cloned()))
                .kbgp_navigation()
                .kbgp_pending_chord()
            {
                jump_chord.0 = new_jump_chord.into_iter().collect();
            }
        });
    });
}
Source

fn kbgp_pending_chord_of_source( &self, source: KbgpInputSource, ) -> Option<HashSet<KbgpInput>>

Accept a chord of key/button inputs from this widget, limited to a specific input source.

Source

fn kbgp_pending_chord_same_source(&self) -> Option<HashSet<KbgpInput>>

Accept a chord of key/button inputs from this widget, where all inputs are from the same source.

“Same source” means either all the inputs are from the same gamepad, or all the inputs are from the keyboard and the mouse.

Source

fn kbgp_pending_chord_vetted( &self, pred: impl FnMut(&HashSet<KbgpInput>, KbgpInput) -> bool, ) -> Option<HashSet<KbgpInput>>

Accept a chord of key/button inputs from this widget, with the ability to filter which inputs to accept.

The predicate accepts as a first argument the inputs that already participate in the chord, to allow vetting the new input based on them.

Source

fn kbgp_pending_input_manual<T>( &self, dlg: impl FnOnce(&Self, KbgpInputManualHandle<'_>) -> Option<T>, ) -> Option<T>

Helper for manually implementing custom methods for input-setting

Inside the delegate, one would usually:

  • Call process_new_input to decide which new input to register.
  • Call show_current_chord to show the tooltip, or generate some other visual cue.
  • Return None if the player did not finish entering the input.

Provided Methods§

Source

fn kbgp_initial_focus(self) -> Self

When the UI is first created, focus on this widget.

Note that if kbgp_set_focus_label was called in the previous frame the widget marked with kbgp_focus_label will receive focus instead. A single widget can be marked with both kbgp_focus_label and kbgp_initial_focus.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl KbgpEguiResponseExt for Response

Implementors§