Skip to content

Commit bf5b250

Browse files
committed
[add] more support for mouse inputs.
1 parent 5fe0304 commit bf5b250

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed

crates/lambda-rs/examples/triangle.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use lambda::{
44
events::{
55
ComponentEvent,
66
Events,
7-
KeyEvent,
7+
Key,
88
WindowEvent,
99
},
1010
render::{
@@ -73,19 +73,19 @@ impl Component for DemoComponent {
7373
}
7474
},
7575
Events::Keyboard { event, issued_at } => match event {
76-
KeyEvent::KeyPressed {
76+
Key::Pressed {
7777
scan_code,
7878
virtual_key,
7979
} => {
8080
println!("Key pressed: {:?}", virtual_key);
8181
}
82-
KeyEvent::KeyReleased {
82+
Key::Released {
8383
scan_code,
8484
virtual_key,
8585
} => {
8686
println!("Key released: {:?}", virtual_key);
8787
}
88-
KeyEvent::ModifierPressed {
88+
Key::ModifierPressed {
8989
modifier,
9090
virtual_key,
9191
} => {

crates/lambda-rs/examples/triangles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use lambda::{
33
core::runtime::start_runtime,
44
events::{
55
Events,
6-
KeyEvent,
6+
Key,
77
VirtualKey,
88
WindowEvent,
99
},
@@ -159,7 +159,7 @@ impl Component for TrianglesComponent {
159159
},
160160
Events::Component { event, issued_at } => todo!(),
161161
Events::Keyboard { event, issued_at } => match event {
162-
KeyEvent::KeyPressed {
162+
Key::Pressed {
163163
scan_code,
164164
virtual_key,
165165
} => match virtual_key {

crates/lambda-rs/src/events.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ pub enum RuntimeEvent {
2626
pub use lambda_platform::winit::winit_exports::VirtualKeyCode as VirtualKey;
2727

2828
#[derive(Debug, Clone)]
29-
pub enum KeyEvent {
30-
KeyPressed {
29+
pub enum Key {
30+
Pressed {
3131
scan_code: u32,
3232
virtual_key: Option<VirtualKey>,
3333
},
34-
KeyReleased {
34+
Released {
3535
scan_code: u32,
3636
virtual_key: Option<VirtualKey>,
3737
},
@@ -51,10 +51,34 @@ pub enum Button {
5151

5252
#[derive(Debug, Clone)]
5353
pub enum Mouse {
54-
Moved { x: f64, y: f64, dx: f64, dy: f64 },
55-
WheelPressed { x: f64, y: f64, button: u32 },
56-
Pressed { x: f64, y: f64, button: Button },
57-
Released { x: f64, y: f64, button: Button },
54+
Moved {
55+
x: f64,
56+
y: f64,
57+
dx: f64,
58+
dy: f64,
59+
device_id: u32,
60+
},
61+
Scrolled {
62+
device_id: u32,
63+
},
64+
Pressed {
65+
x: f64,
66+
y: f64,
67+
button: Button,
68+
device_id: u32,
69+
},
70+
Released {
71+
x: f64,
72+
y: f64,
73+
button: Button,
74+
device_id: u32,
75+
},
76+
LeftWindow {
77+
device_id: u32,
78+
},
79+
EnteredWindow {
80+
device_id: u32,
81+
},
5882
}
5983

6084
/// Generic Event Enum which encapsulates all possible events that will be
@@ -74,7 +98,7 @@ pub enum Events {
7498
issued_at: Instant,
7599
},
76100
Keyboard {
77-
event: KeyEvent,
101+
event: Key,
78102
issued_at: Instant,
79103
},
80104
Mouse {

crates/lambda-rs/src/runtimes/application.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
Button,
2323
ComponentEvent,
2424
Events,
25-
KeyEvent,
25+
Key,
2626
Mouse,
2727
RuntimeEvent,
2828
WindowEvent,
@@ -205,7 +205,7 @@ impl Runtime for ApplicationRuntime {
205205
} => match (input.state, is_synthetic) {
206206
(ElementState::Pressed, false) => {
207207
Some(Events::Keyboard {
208-
event: KeyEvent::KeyPressed {
208+
event: Key::Pressed {
209209
scan_code: input.scancode,
210210
virtual_key: input.virtual_keycode,
211211
},
@@ -214,7 +214,7 @@ impl Runtime for ApplicationRuntime {
214214
}
215215
(ElementState::Released, false) => {
216216
Some(Events::Keyboard {
217-
event: KeyEvent::KeyReleased {
217+
event: Key::Released {
218218
scan_code: input.scancode,
219219
virtual_key: input.virtual_keycode,
220220
},
@@ -240,26 +240,40 @@ impl Runtime for ApplicationRuntime {
240240
x: position.x,
241241
y: position.y,
242242
dx: 0.0,
243-
dy: 0.0
243+
dy: 0.0,
244+
device_id: 0
244245
},
245246
issued_at: Instant::now(),
246247
})
247248
}
248-
WinitWindowEvent::CursorEntered { device_id } => {None}
249-
WinitWindowEvent::CursorLeft { device_id } => {None}
249+
WinitWindowEvent::CursorEntered { device_id } => {
250+
Some(Events::Mouse {
251+
event: Mouse::EnteredWindow { device_id: 0 },
252+
issued_at: Instant::now(),
253+
})
254+
}
255+
WinitWindowEvent::CursorLeft { device_id } => {
256+
Some(Events::Mouse { event: Mouse::LeftWindow { device_id: 0 }, issued_at: Instant::now() })
257+
}
250258
WinitWindowEvent::MouseWheel {
251259
device_id,
252260
delta,
253261
phase,
254262
modifiers,
255-
} => { None }
263+
} => {
264+
Some(Events::Mouse{
265+
event: Mouse::Scrolled { device_id: 0 },
266+
issued_at: Instant::now(),
267+
})
268+
}
256269
WinitWindowEvent::MouseInput {
257270
device_id,
258271
state,
259272
button,
260273
modifiers,
261274
} => {
262275

276+
// Map winit button to our button type
263277
let button = match button {
264278
MouseButton::Left => Button::Left,
265279
MouseButton::Right => Button::Right,
@@ -272,11 +286,13 @@ impl Runtime for ApplicationRuntime {
272286
button,
273287
x: 0.0,
274288
y: 0.0,
289+
device_id: 0,
275290
},
276291
ElementState::Released => Mouse::Released {
277292
button,
278293
x: 0.0,
279-
y: 0.0
294+
y: 0.0,
295+
device_id: 0
280296
},
281297
};
282298

0 commit comments

Comments
 (0)