From 267188a571015b52443cfb1e899c54474e8305fc Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 15 Apr 2022 20:15:48 -0500 Subject: [PATCH] Device events in lua --- main.lua | 8 ++++++-- src/device.rs | 32 -------------------------------- src/main.rs | 13 ++++++++----- src/scripting.rs | 2 ++ src/scripting/lua.rs | 20 +++++++++++++++++--- 5 files changed, 33 insertions(+), 42 deletions(-) delete mode 100644 src/device.rs diff --git a/main.lua b/main.lua index c6a3ff3..b6b5ddf 100644 --- a/main.lua +++ b/main.lua @@ -1,12 +1,16 @@ print("Hello from a lua script!") -function init() +function couch.init() print("Hello from \"init\"") end -function update(delta) +function couch.update(delta) local transform = couch.root:find_node("cube"):get_transform() transform.rotation[2] = transform.rotation[2] + delta transform.rotation[1] = transform.rotation[1] - delta * 0.3 couch.root:find_node("cube"):set_transform(transform) end + +function couch.keyreleased(scancode) + print("Key was released") +end diff --git a/src/device.rs b/src/device.rs deleted file mode 100644 index 1909889..0000000 --- a/src/device.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::collections::HashMap; - -use glium::glutin; -use glutin::event::*; - -pub use glutin::event::VirtualKeyCode as Key; - -pub struct DeviceManager { - key_map: HashMap -} - -impl DeviceManager { - pub fn handle(&mut self, event: DeviceEvent) { - match event { - DeviceEvent::Key( - KeyboardInput{ virtual_keycode: Some(keycode), state, .. } - ) => self.update(keycode, state), - _ => (), - }; - } - pub fn new() -> Self { - DeviceManager { - key_map: HashMap::new(), - } - } - pub fn is_pressed(&self, key: &Key) -> bool { - matches!(self.key_map.get(key), Some(ElementState::Pressed)) - } - fn update(&mut self, key: Key, state: ElementState) { - self.key_map.insert(key, state); - } -} diff --git a/src/main.rs b/src/main.rs index a79567c..5298b59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -mod device; - mod model; use model::Model; @@ -31,8 +29,6 @@ fn main() { let fragment_shader_src = include_str!("flat.frag"); let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); - let mut device_manager = crate::device::DeviceManager::new(); - let root = Rc::new(RefCell::new(Node::new("_root_".to_string(), GameObject::Null))); { // Initial scene setup @@ -64,7 +60,14 @@ fn main() { } }, glutin::event::Event::DeviceEvent { event, .. } => { - device_manager.handle(event); + if let glutin::event::DeviceEvent::Key( + glutin::event::KeyboardInput { scancode, state, .. } + ) = event { + match state { + glutin::event::ElementState::Pressed => script_lang.key_pressed(scancode), + glutin::event::ElementState::Released => script_lang.key_released(scancode), + } + }; }, glutin::event::Event::MainEventsCleared => { let now = std::time::Instant::now(); diff --git a/src/scripting.rs b/src/scripting.rs index 1eda5a5..fa130c4 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -4,4 +4,6 @@ pub use lua::Lua; pub trait ScriptLang { fn init(&mut self); fn update(&mut self, delta: f32); + fn key_pressed(&mut self, key: u32); + fn key_released(&mut self, key: u32); } diff --git a/src/scripting/lua.rs b/src/scripting/lua.rs index 726fa84..2b21528 100644 --- a/src/scripting/lua.rs +++ b/src/scripting/lua.rs @@ -87,12 +87,26 @@ impl Lua { impl ScriptLang for Lua { fn init(&mut self) { - self.ctx.load(&"init()").exec().ok(); + self.ctx.load(chunk! { + if couch.init then couch.init() end + }).exec().unwrap(); } fn update(&mut self, delta: f32) { self.ctx.load(chunk! { - update($delta) - }).exec().ok(); + if couch.update then couch.update($delta) end + }).exec().unwrap(); + } + + fn key_pressed(&mut self, key: u32) { + self.ctx.load(chunk! { + if couch.keypressed then couch.keypressed($key) end + }).exec().unwrap(); + } + + fn key_released(&mut self, key: u32) { + self.ctx.load(chunk! { + if couch.keyreleased then couch.keyreleased($key) end + }).exec().unwrap(); } }