mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -04:00
Add support for text input in emilib and glium.
Add input inspectors
This commit is contained in:
@@ -9,6 +9,8 @@ pub struct Context {
|
||||
/// The default style for new regions
|
||||
pub(crate) style: Mutex<Style>,
|
||||
pub(crate) fonts: Arc<Fonts>,
|
||||
/// Raw input from last frame. Use `input()` instead.
|
||||
pub(crate) last_raw_input: RawInput,
|
||||
pub(crate) input: GuiInput,
|
||||
pub(crate) memory: Mutex<Memory>,
|
||||
pub(crate) graphics: Mutex<GraphicLayers>,
|
||||
@@ -25,7 +27,8 @@ impl Clone for Context {
|
||||
Context {
|
||||
style: Mutex::new(self.style()),
|
||||
fonts: self.fonts.clone(),
|
||||
input: self.input,
|
||||
last_raw_input: self.last_raw_input.clone(),
|
||||
input: self.input.clone(),
|
||||
memory: Mutex::new(self.memory.lock().clone()),
|
||||
graphics: Mutex::new(self.graphics.lock().clone()),
|
||||
output: Mutex::new(self.output.lock().clone()),
|
||||
@@ -39,6 +42,7 @@ impl Context {
|
||||
Context {
|
||||
style: Default::default(),
|
||||
fonts: Arc::new(Fonts::new(pixels_per_point)),
|
||||
last_raw_input: Default::default(),
|
||||
input: Default::default(),
|
||||
memory: Default::default(),
|
||||
graphics: Default::default(),
|
||||
@@ -52,6 +56,11 @@ impl Context {
|
||||
(point * self.input.pixels_per_point).round() / self.input.pixels_per_point
|
||||
}
|
||||
|
||||
/// Raw input from last frame. Use `input()` instead.
|
||||
pub fn last_raw_input(&self) -> &RawInput {
|
||||
&self.last_raw_input
|
||||
}
|
||||
|
||||
pub fn input(&self) -> &GuiInput {
|
||||
&self.input
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{mesher::*, widgets::*, *};
|
||||
use crate::{containers::*, mesher::*, widgets::*, *};
|
||||
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct Stats {
|
||||
@@ -38,10 +38,12 @@ impl Emigui {
|
||||
}
|
||||
|
||||
let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input);
|
||||
self.last_input = new_input;
|
||||
self.last_input = new_input.clone(); // TODO: also stored in Context. Remove this one
|
||||
|
||||
// TODO: avoid this clone
|
||||
let mut new_ctx = (*self.ctx).clone();
|
||||
|
||||
new_ctx.last_raw_input = new_input;
|
||||
new_ctx.begin_frame(gui_input);
|
||||
self.ctx = Arc::new(new_ctx);
|
||||
}
|
||||
@@ -100,6 +102,17 @@ impl Emigui {
|
||||
}
|
||||
});
|
||||
|
||||
region.collapsing("Input", |region| {
|
||||
CollapsingHeader::new("Raw Input")
|
||||
.default_open()
|
||||
.show(region, |region| {
|
||||
region.ctx().last_raw_input().clone().ui(region)
|
||||
});
|
||||
CollapsingHeader::new("Input")
|
||||
.default_open()
|
||||
.show(region, |region| region.input().clone().ui(region));
|
||||
});
|
||||
|
||||
region.collapsing("Stats", |region| {
|
||||
region.add(label!(
|
||||
"Screen size: {} x {} points, pixels_per_point: {}",
|
||||
@@ -132,3 +145,35 @@ fn font_definitions_ui(font_definitions: &mut FontDefinitions, region: &mut Regi
|
||||
*font_definitions = crate::fonts::default_font_definitions();
|
||||
}
|
||||
}
|
||||
|
||||
impl RawInput {
|
||||
pub fn ui(&self, region: &mut Region) {
|
||||
// TODO: simpler way to show values, e.g. `region.value("Mouse Pos:", self.mouse_pos);
|
||||
region.add(label!("mouse_down: {}", self.mouse_down));
|
||||
region.add(label!("mouse_pos: {:.1?}", self.mouse_pos));
|
||||
region.add(label!("scroll_delta: {:?}", self.scroll_delta));
|
||||
region.add(label!("screen_size: {:?}", self.screen_size));
|
||||
region.add(label!("pixels_per_point: {}", self.pixels_per_point));
|
||||
region.add(label!("time: {:.3} s", self.time));
|
||||
region.add(label!("text: {:?}", self.text));
|
||||
// region.add(label!("dropped_files: {}", self.dropped_files));
|
||||
// region.add(label!("hovered_files: {}", self.hovered_files));
|
||||
}
|
||||
}
|
||||
|
||||
impl GuiInput {
|
||||
pub fn ui(&self, region: &mut Region) {
|
||||
region.add(label!("mouse_down: {}", self.mouse_down));
|
||||
region.add(label!("mouse_pressed: {}", self.mouse_pressed));
|
||||
region.add(label!("mouse_released: {}", self.mouse_released));
|
||||
region.add(label!("mouse_pos: {:?}", self.mouse_pos));
|
||||
region.add(label!("mouse_move: {:?}", self.mouse_move));
|
||||
region.add(label!("scroll_delta: {:?}", self.scroll_delta));
|
||||
region.add(label!("screen_size: {:?}", self.screen_size));
|
||||
region.add(label!("pixels_per_point: {}", self.pixels_per_point));
|
||||
region.add(label!("time: {}", self.time));
|
||||
region.add(label!("text: {:?}", self.text));
|
||||
// region.add(label!("dropped_files: {}", self.dropped_files));
|
||||
// region.add(label!("hovered_files: {}", self.hovered_files));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
|
||||
/// What the integration gives to the gui.
|
||||
/// All coordinates in emigui is in point/logical coordinates.
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize)]
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct RawInput {
|
||||
/// Is the button currently down?
|
||||
@@ -29,10 +29,19 @@ pub struct RawInput {
|
||||
|
||||
/// Time in seconds. Relative to whatever. Used for animation.
|
||||
pub time: f64,
|
||||
|
||||
/// Text input, e.g. via keyboard or paste action
|
||||
pub text: String,
|
||||
|
||||
/// Files has been dropped into the window.
|
||||
pub dropped_files: Vec<std::path::PathBuf>,
|
||||
|
||||
/// Someone is threatening to drop these on us.
|
||||
pub hovered_files: Vec<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
/// What the gui maintains
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct GuiInput {
|
||||
/// Is the button currently down?
|
||||
/// true the frame when it is pressed,
|
||||
@@ -63,6 +72,15 @@ pub struct GuiInput {
|
||||
|
||||
/// Time in seconds. Relative to whatever. Used for animation.
|
||||
pub time: f64,
|
||||
|
||||
/// Text input, e.g. via keyboard or paste action
|
||||
pub text: String,
|
||||
|
||||
/// Files has been dropped into the window.
|
||||
pub dropped_files: Vec<std::path::PathBuf>,
|
||||
|
||||
/// Someone is threatening to drop these on us.
|
||||
pub hovered_files: Vec<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
impl GuiInput {
|
||||
@@ -81,6 +99,9 @@ impl GuiInput {
|
||||
screen_size: new.screen_size,
|
||||
pixels_per_point: new.pixels_per_point,
|
||||
time: new.time,
|
||||
text: new.text.clone(),
|
||||
dropped_files: new.dropped_files.clone(),
|
||||
hovered_files: new.hovered_files.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user