1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Event handling functionality.
use crate::camera::Camera2d;
use crate::camera::Camera3d;
use crate::event::{Action, EventManager, Key, MouseButton, WindowEvent};
use super::Window;
impl Window {
/// Returns an event manager for accessing window events.
///
/// The event manager provides an iterator over events that occurred since the last frame,
/// such as keyboard input, mouse movement, and window resizing.
///
/// # Returns
/// An `EventManager` that can be iterated to process events
///
/// # Example
/// ```no_run
/// # use kiss3d::prelude::*;
/// # #[kiss3d::main]
/// # async fn main() {
/// # let mut window = Window::new("Example").await;
/// # let mut camera = OrbitCamera3d::default();
/// # let mut scene = SceneNode3d::empty();
/// # while window.render_3d(&mut scene, &mut camera).await {
/// for event in window.events().iter() {
/// match event.value {
/// WindowEvent::Key(Key::Escape, Action::Release, _) => {
/// println!("Escape pressed!");
/// }
/// _ => {}
/// }
/// }
/// # }
/// # }
/// ```
pub fn events(&self) -> EventManager {
EventManager::new(self.events.clone(), self.unhandled_events.clone())
}
/// Gets the current state of a keyboard key.
///
/// # Arguments
/// * `key` - The key to check
///
/// # Returns
/// The current `Action` state (e.g., `Action::Press`, `Action::Release`)
pub fn get_key(&self, key: Key) -> Action {
self.canvas.get_key(key)
}
/// Gets the current state of a mouse button.
///
/// # Arguments
/// * `button` - The mouse button to check
///
/// # Returns
/// The current `Action` state (e.g., `Action::Press`, `Action::Release`)
pub fn get_mouse_button(&self, button: MouseButton) -> Action {
self.canvas.get_mouse_button(button)
}
/// Gets the last known position of the mouse cursor.
///
/// The position is automatically updated when the mouse moves over the window.
/// Coordinates are in pixels, with (0, 0) at the top-left corner.
///
/// # Returns
/// `Some((x, y))` with the cursor position, or `None` if the cursor position is unknown
pub fn cursor_pos(&self) -> Option<(f64, f64)> {
self.canvas.cursor_pos()
}
#[inline]
pub(crate) fn handle_events(
&mut self,
camera: &mut dyn Camera3d,
camera_2d: &mut dyn Camera2d,
) {
let unhandled_events = self.unhandled_events.clone(); // TODO: could we avoid the clone?
let events = self.events.clone(); // TODO: could we avoid the clone?
for event in unhandled_events.borrow().iter() {
self.handle_event(camera, camera_2d, event)
}
for event in events.try_iter() {
self.handle_event(camera, camera_2d, &event)
}
unhandled_events.borrow_mut().clear();
self.canvas.poll_events();
}
pub(crate) fn handle_event(
&mut self,
camera: &mut dyn Camera3d,
camera_2d: &mut dyn Camera2d,
event: &WindowEvent,
) {
if let Some(binding_key) = self.close_key {
if let WindowEvent::Key(key, Action::Release, modifiers) = event {
if binding_key == *key
&& (Some(*modifiers) == self.close_modifiers || self.close_modifiers.is_none())
{
self.close()
}
}
}
if *event == WindowEvent::Close {
self.close();
}
// Feed events to egui and check if it wants to capture input
#[cfg(feature = "egui")]
{
self.feed_egui_event(event);
if event.is_keyboard_event() && self.is_egui_capturing_keyboard() {
return;
}
if event.is_mouse_event() && self.is_egui_capturing_mouse() {
return;
}
}
camera.handle_event(&self.canvas, event);
camera_2d.handle_event(&self.canvas, event);
}
}