From 097730461d5e25189b4fde8198452da29f6d9be6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 11 Jun 2020 18:07:21 +0200 Subject: [PATCH] [input] add queries for if egui is interested in mouse/kb --- egui/src/context.rs | 29 +++++++++++++++++++++++++++++ egui/src/memory.rs | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/egui/src/context.rs b/egui/src/context.rs index 0896d41d..3bed6f80 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -260,6 +260,35 @@ impl Context { // --------------------------------------------------------------------- + /// Is the mouse over any Egui area? + pub fn is_mouse_over_area(&self) -> bool { + if let Some(mouse_pos) = self.input.mouse.pos { + if let Some(layer) = self.layer_at(mouse_pos) { + layer.order != Order::Background + } else { + false + } + } else { + false + } + } + + /// True if Egui is currently interested in the mouse. + /// Could be the mouse is hovering over a Egui window, + /// or the user is dragging an Egui widget. + /// If false, the mouse is outside of any Egui area and so + /// you may be interested in what it is doing (e.g. controlling your game). + pub fn wants_mouse_input(&self) -> bool { + self.is_mouse_over_area() || self.memory().interaction.is_using_mouse() + } + + /// If true, Egui is currently listening on text input (e.g. typing text in a `TextEdit`). + pub fn wants_keyboard_input(&self) -> bool { + self.memory().kb_focus_id.is_some() + } + + // --------------------------------------------------------------------- + pub fn layer_at(&self, pos: Pos2) -> Option { let resize_interact_radius_side = self.style().resize_interact_radius_side; self.memory().layer_at(pos, resize_interact_radius_side) diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 34b8244f..512a267f 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -61,6 +61,12 @@ pub struct Interaction { pub drag_interest: bool, } +impl Interaction { + pub fn is_using_mouse(&self) -> bool { + self.click_id.is_some() || self.drag_id.is_some() + } +} + #[derive(Clone, Debug, Default)] #[cfg_attr(feature = "with_serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "with_serde", serde(default))]