Compare commits
4 commits
master
...
fix_keypre
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a646c1259d | ||
![]() |
2b2834ff9d | ||
![]() |
ed0d5f871c | ||
![]() |
324104c7c3 |
6 changed files with 97 additions and 18 deletions
|
@ -8,6 +8,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
|
|||
* MSRV (Minimum Supported Rust Version) is now `1.65.0` ([#2314](https://github.com/emilk/egui/pull/2314)).
|
||||
* ⚠️ BREAKING: egui now expects integrations to do all color blending in gamma space ([#2071](https://github.com/emilk/egui/pull/2071)).
|
||||
* ⚠️ BREAKING: if you have overlapping interactive widgets, only the top widget (last added) will be interactive ([#2244](https://github.com/emilk/egui/pull/2244)).
|
||||
* Keyboard press events are only pressent at the frame when the key was pressed, consistent with how key releases work.
|
||||
|
||||
### Added ⭐
|
||||
* Added helper functions for animating panels that collapse/expand ([#2190](https://github.com/emilk/egui/pull/2190)).
|
||||
|
|
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -2070,6 +2070,14 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "keyboard_events"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"eframe",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "khronos-egl"
|
||||
version = "4.1.0"
|
||||
|
|
|
@ -138,7 +138,11 @@ impl Default for InputState {
|
|||
|
||||
impl InputState {
|
||||
#[must_use]
|
||||
pub fn begin_frame(mut self, new: RawInput, requested_repaint_last_frame: bool) -> InputState {
|
||||
pub fn begin_frame(
|
||||
mut self,
|
||||
mut new: RawInput,
|
||||
requested_repaint_last_frame: bool,
|
||||
) -> InputState {
|
||||
let time = new.time.unwrap_or(self.time + new.predicted_dt as f64);
|
||||
let unstable_dt = (time - self.time) as f32;
|
||||
|
||||
|
@ -160,24 +164,26 @@ impl InputState {
|
|||
let mut keys_down = self.keys_down;
|
||||
let mut scroll_delta = Vec2::ZERO;
|
||||
let mut zoom_factor_delta = 1.0;
|
||||
for event in &new.events {
|
||||
match event {
|
||||
Event::Key { key, pressed, .. } => {
|
||||
if *pressed {
|
||||
keys_down.insert(*key);
|
||||
} else {
|
||||
keys_down.remove(key);
|
||||
}
|
||||
new.events.retain(|event| match event {
|
||||
Event::Key { key, pressed, .. } => {
|
||||
if *pressed {
|
||||
//return false (thus filtering out the Event) if the key already existed in the set of pressed keys
|
||||
keys_down.insert(*key)
|
||||
} else {
|
||||
keys_down.remove(key);
|
||||
true
|
||||
}
|
||||
Event::Scroll(delta) => {
|
||||
scroll_delta += *delta;
|
||||
}
|
||||
Event::Zoom(factor) => {
|
||||
zoom_factor_delta *= *factor;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Event::Scroll(delta) => {
|
||||
scroll_delta += *delta;
|
||||
true
|
||||
}
|
||||
Event::Zoom(factor) => {
|
||||
zoom_factor_delta *= *factor;
|
||||
true
|
||||
}
|
||||
_ => true,
|
||||
});
|
||||
InputState {
|
||||
pointer,
|
||||
touch_states: self.touch_states,
|
||||
|
@ -280,7 +286,7 @@ impl InputState {
|
|||
self.num_presses(desired_key) > 0
|
||||
}
|
||||
|
||||
/// How many times were the given key pressed this frame?
|
||||
/// How many times was the given key pressed this frame?
|
||||
pub fn num_presses(&self, desired_key: Key) -> usize {
|
||||
self.events
|
||||
.iter()
|
||||
|
|
13
examples/keyboard_events/Cargo.toml
Normal file
13
examples/keyboard_events/Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "keyboard_events"
|
||||
version = "0.1.0"
|
||||
authors = ["Jose Palazon <jose@palako.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.65"
|
||||
publish = false
|
||||
|
||||
|
||||
[dependencies]
|
||||
eframe = { path = "../../crates/eframe" }
|
||||
tracing-subscriber = "0.3"
|
3
examples/keyboard_events/README.md
Normal file
3
examples/keyboard_events/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
```sh
|
||||
cargo run -p hello_world
|
||||
```
|
48
examples/keyboard_events/src/main.rs
Normal file
48
examples/keyboard_events/src/main.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use eframe::egui;
|
||||
use egui::*;
|
||||
fn main() {
|
||||
// Log to stdout (if you run with `RUST_LOG=debug`).
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = eframe::NativeOptions::default();
|
||||
eframe::run_native(
|
||||
"Keyboard events",
|
||||
options,
|
||||
Box::new(|_cc| Box::new(Content::default())),
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Content {
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl eframe::App for Content {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("Press/Hold/Release example. Press A to test.");
|
||||
if ui.button("Clear").clicked() {
|
||||
self.text.clear();
|
||||
}
|
||||
ScrollArea::vertical()
|
||||
.auto_shrink([false; 2])
|
||||
.stick_to_bottom(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label(&self.text);
|
||||
});
|
||||
|
||||
if ctx.input().key_pressed(Key::A) {
|
||||
self.text.push_str("\nPressed");
|
||||
}
|
||||
if ctx.input().key_down(Key::A) {
|
||||
self.text.push_str("\nHeld");
|
||||
ui.ctx().request_repaint(); // make sure we note the holding.
|
||||
}
|
||||
if ctx.input().key_released(Key::A) {
|
||||
self.text.push_str("\nReleased");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue