Device events in lua

This commit is contained in:
Dane Johnson 2022-04-15 20:15:48 -05:00
parent 9623552213
commit 267188a571
5 changed files with 33 additions and 42 deletions

View File

@ -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

View File

@ -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<Key, ElementState>
}
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);
}
}

View File

@ -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();

View File

@ -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);
}

View File

@ -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();
}
}