Skip to content

Commit

Permalink
add a way to intuit if yakui has eaten your current frames mouse or k…
Browse files Browse the repository at this point in the history
…eyboard input, and you should ignore it in your game.
  • Loading branch information
profan committed Nov 28, 2023
1 parent 8e0435a commit 0f0a11e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yakui-miniquad"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
readme = "README.md"
repository = "https://github.com/profan/yakui-miniquad"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
yakui-miniquad
-----------------------
[![CI](https://github.com/profan/yakui-miniquad/actions/workflows/rust.yml/badge.svg)](https://github.com/profan/yakui-miniquad/actions/workflows/rust.yml)
[![Docs](https://docs.rs/yakui-miniquad/badge.svg?version=0.2.2)](https://docs.rs/yakui-miniquad/0.2.2/yakui_miniquad/)
[![Docs](https://docs.rs/yakui-miniquad/badge.svg?version=0.2.3)](https://docs.rs/yakui-miniquad/0.2.3/yakui_miniquad/)
[![Crates.io version](https://img.shields.io/crates/v/yakui-miniquad.svg)](https://crates.io/crates/yakui-miniquad)

This is a simple library integrating yakui with miniquad, allowing you to use yakui with your miniquad projects easily.
Expand Down
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,30 @@ struct YakuiVertex {
pub struct YakuiMiniQuad {
ui: Yakui,
state: YakuiMiniquadState,
has_keyboard_focus: bool,
has_mouse_focus: bool,
}

impl YakuiMiniQuad {
pub fn new(ctx: &mut GraphicsContext) -> YakuiMiniQuad {
YakuiMiniQuad {
ui: Yakui::new(),
state: YakuiMiniquadState::new(ctx),
has_keyboard_focus: false,
has_mouse_focus: false,
}
}

/// Returns true if the last keyboard event was sunk by yakui, and should not be handled by your game.
pub fn has_keyboard_focus(&self) -> bool {
self.has_keyboard_focus
}

/// Returns true if the last mouse event was sunk by yakui, and should not be handled by your game.
pub fn has_mouse_focus(&self) -> bool {
self.has_mouse_focus
}

/// Returns a reference to the internal Yakui context.
pub fn ctx(&mut self) -> &mut Yakui {
&mut self.ui
Expand Down Expand Up @@ -232,7 +246,7 @@ impl EventHandler for YakuiMiniQuad {
_y: f32,
) {
if let Some(mouse_button) = miniquad_mouse_button_to_yakui(button) {
self.ui.handle_event(Event::MouseButtonChanged {
self.has_mouse_focus = self.ui.handle_event(Event::MouseButtonChanged {
button: mouse_button,
down: true,
});
Expand All @@ -241,7 +255,7 @@ impl EventHandler for YakuiMiniQuad {

fn mouse_button_up_event(&mut self, _ctx: &mut Context, button: MouseButton, _x: f32, _y: f32) {
if let Some(mouse_button) = miniquad_mouse_button_to_yakui(button) {
self.ui.handle_event(Event::MouseButtonChanged {
self.has_mouse_focus = self.ui.handle_event(Event::MouseButtonChanged {
button: mouse_button,
down: false,
});
Expand All @@ -256,7 +270,7 @@ impl EventHandler for YakuiMiniQuad {
_repeat: bool,
) {
if let Some(key_code) = miniquad_key_to_yakui(keycode) {
self.ui.handle_event(Event::KeyChanged {
self.has_keyboard_focus = self.ui.handle_event(Event::KeyChanged {
key: key_code,
down: true,
});
Expand All @@ -265,15 +279,15 @@ impl EventHandler for YakuiMiniQuad {

fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, _keymods: KeyMods) {
if let Some(key_code) = miniquad_key_to_yakui(keycode) {
self.ui.handle_event(Event::KeyChanged {
self.has_keyboard_focus = self.ui.handle_event(Event::KeyChanged {
key: key_code,
down: false,
});
}
}

fn mouse_wheel_event(&mut self, _ctx: &mut Context, x: f32, y: f32) {
self.ui.handle_event(Event::MouseScroll {
self.has_mouse_focus = self.ui.handle_event(Event::MouseScroll {
delta: yakui_core::geometry::Vec2 { x, y },
});
}
Expand All @@ -285,7 +299,7 @@ impl EventHandler for YakuiMiniQuad {
_keymods: KeyMods,
_repeat: bool,
) {
self.ui.handle_event(Event::TextInput(character));
self.has_keyboard_focus = self.ui.handle_event(Event::TextInput(character));
}

fn resize_event(&mut self, _ctx: &mut Context, width: f32, height: f32) {
Expand Down

0 comments on commit 0f0a11e

Please sign in to comment.