Commit 620380d0 authored by William's avatar William

Add KeyboardState to Global and the Event System

parent cb0a6041
Pipeline #3878 passed with stages
in 6 minutes and 3 seconds
use crate::properties::KeyboardState;
use std::collections::HashMap; use std::collections::HashMap;
use dces::prelude::Entity; use dces::prelude::Entity;
...@@ -10,4 +11,7 @@ pub struct Global { ...@@ -10,4 +11,7 @@ pub struct Global {
/// Used to reference widgets by its css id. /// Used to reference widgets by its css id.
pub id_map: HashMap<String, Entity>, pub id_map: HashMap<String, Entity>,
/// Stores the state of the keyboard
pub keyboard_state: KeyboardState,
} }
use crate::event::{KeyDownEvent, KeyUpEvent};
use std::{ use std::{
any::TypeId, any::TypeId,
cell::{Cell, RefCell}, cell::{Cell, RefCell},
...@@ -14,7 +15,7 @@ use crate::{ ...@@ -14,7 +15,7 @@ use crate::{
check_mouse_condition, ClickEvent, EventBox, EventHandler, EventStrategy, MouseDownEvent, check_mouse_condition, ClickEvent, EventBox, EventHandler, EventStrategy, MouseDownEvent,
MouseUpEvent, SystemEvent, WindowEvent, MouseUpEvent, SystemEvent, WindowEvent,
}, },
properties::{Bounds, Constraint, Enabled, Focused, Pressed, Selected, ConstraintExtension}, properties::{Bounds, Constraint, ConstraintExtension, Enabled, Focused, Pressed, Selected},
structs::Size, structs::Size,
widgets::WidgetContainer, widgets::WidgetContainer,
Global, Global,
...@@ -35,7 +36,8 @@ impl EventSystem { ...@@ -35,7 +36,8 @@ impl EventSystem {
_tree: &Tree, _tree: &Tree,
_ecm: &mut EntityComponentManager, _ecm: &mut EntityComponentManager,
_new_events: &mut Vec<EventBox>, _new_events: &mut Vec<EventBox>,
) {} ) {
}
fn process_bottom_up_event( fn process_bottom_up_event(
&self, &self,
...@@ -170,6 +172,22 @@ impl EventSystem { ...@@ -170,6 +172,22 @@ impl EventSystem {
} }
} }
// KeyDown handling
if let Ok(event) = event.downcast_ref::<KeyDownEvent>() {
if let Ok(global) = ecm.borrow_mut_component::<Global>(tree.root) {
// Set this value on the keyboard state
global.keyboard_state.set_key_state(event.key, true);
}
}
// KeyUp handling
if let Ok(event) = event.downcast_ref::<KeyUpEvent>() {
if let Ok(global) = ecm.borrow_mut_component::<Global>(tree.root) {
// Set this value on the keyboard state
global.keyboard_state.set_key_state(event.key, false);
}
}
// remove focus from previous focused entity // remove focus from previous focused entity
let mut old_focused_widget = None; let mut old_focused_widget = None;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment