From 04c5e35f52480621f6af4226253b30a35e93d60b Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Fri, 2 Nov 2018 21:17:05 +0900 Subject: [PATCH] WIP keyboard Event classes. --- src/main.cr | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main.cr b/src/main.cr index 119a5b3..ca0e4a8 100644 --- a/src/main.cr +++ b/src/main.cr @@ -80,6 +80,23 @@ class Naka::Event end end + # FIXME: Split in KeyUp and KeyDown. + class KeyUp + getter key : LibSDL::Keycode + getter scan_code : LibSDL::Scancode + + def initialize(@key, @scan_code) + end + end + class KeyDown + getter key : LibSDL::Keycode + getter scan_code : LibSDL::Scancode + getter repeat : Bool + + def initialize(@key, @scan_code, @repeat) + end + end + # FIXME: THIS MAIN LOOP IS **NOT** READY FOR PRODUCTION # Update events are still missing, and both Draw and Update should # be time-based. @@ -101,6 +118,13 @@ class Naka::Event r_value = yield case event when SDL::Event::Quit Naka::Event::Quit.new + when SDL::Event::Keyboard + keysym = event.keysym + if event.type == LibSDL::EventType::KEYUP + Naka::Event::KeyUp.new keysym.sym, keysym.scancode + elsif event.type == LibSDL::EventType::KEYDOWN + Naka::Event::KeyDown.new keysym.sym, keysym.scancode, event.repeat != 0 + end else # FIXME: Most event types may need proper Naka:: classes. event end @@ -144,10 +168,10 @@ Naka::Event.loop window do |event| when Naka::Event::Quit next Naka::Event::Quit when Naka::Event::Update - p "Update(#{event.dt})" when Naka::Event::Draw - p "Draw" event.window.draw naka, 20, 20 + when Naka::Event::KeyUp, Naka::Event::KeyDown + pp! event end end