Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard and mouse blocking functionality #82

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ pub struct ConsoleConfiguration {
pub foreground_color: Color32,
/// Number of suggested commands to show
pub num_suggestions: usize,
/// Blocks mouse from clicking through console
pub block_mouse: bool,
/// Blocks keyboard from interacting outside console when active
pub block_keyboard: bool,
}

impl Default for ConsoleConfiguration {
Expand All @@ -261,6 +265,8 @@ impl Default for ConsoleConfiguration {
background_color: Color32::from_black_alpha(102),
foreground_color: Color32::LIGHT_GRAY,
num_suggestions: 4,
block_mouse: false,
block_keyboard: false,
}
}
}
Expand Down Expand Up @@ -616,6 +622,42 @@ fn set_cursor_pos(ctx: &Context, id: Id, pos: usize) {
}
}

pub fn block_mouse_input(
mut mouse: ResMut<ButtonInput<MouseButton>>,
config: Res<ConsoleConfiguration>,
mut contexts: EguiContexts,
) {
if !config.block_mouse {
return;
}

let Some(context) = contexts.try_ctx_mut() else {
return;
};

if context.is_pointer_over_area() || context.wants_pointer_input() {
mouse.reset_all();
}
}

pub fn block_keyboard_input(
mut keyboard_keycode: ResMut<ButtonInput<KeyCode>>,
config: Res<ConsoleConfiguration>,
mut contexts: EguiContexts,
) {
if !config.block_keyboard {
return;
}

let Some(context) = contexts.try_ctx_mut() else {
return;
};

if context.wants_keyboard_input() {
keyboard_keycode.reset_all();
}
}

#[cfg(test)]
mod tests {
use bevy::input::keyboard::{Key, NativeKey, NativeKeyCode};
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use bevy::prelude::*;
pub use bevy_console_derive::ConsoleCommand;
use bevy_egui::EguiPlugin;
use bevy_egui::{EguiPlugin, EguiSet};
use console::{block_keyboard_input, block_mouse_input};

use crate::commands::clear::{clear_command, ClearCommand};
use crate::commands::exit::{exit_command, ExitCommand};
Expand Down Expand Up @@ -57,6 +58,9 @@ impl Plugin for ConsolePlugin {
.add_console_command::<ClearCommand, _>(clear_command)
.add_console_command::<ExitCommand, _>(exit_command)
.add_console_command::<HelpCommand, _>(help_command)
.add_systems(PreUpdate,
(block_mouse_input, block_keyboard_input)
.after(EguiSet::ProcessInput).before(EguiSet::BeginPass))
.add_systems(
Update,
(
Expand Down