summaryrefslogtreecommitdiff
path: root/examples/events/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/events/main.lua')
-rw-r--r--examples/events/main.lua80
1 files changed, 80 insertions, 0 deletions
diff --git a/examples/events/main.lua b/examples/events/main.lua
new file mode 100644
index 0000000..65f575c
--- /dev/null
+++ b/examples/events/main.lua
@@ -0,0 +1,80 @@
+local text = ""
+local textPos = { 100, 100 }
+local cursorIn = 0
+
+function RL.init()
+ RL.SetWindowTitle( "Events" )
+ RL.SetWindowState( RL.FLAG_VSYNC_HINT )
+end
+
+local function getEventType( event )
+ if event.type == RL.EVENT_KEY then
+ return "Key"
+ elseif event.type == RL.EVENT_CHAR then
+ return "Char"
+ elseif event.type == RL.EVENT_MOUSE_BUTTON then
+ return "Mouse Button"
+ elseif event.type == RL.EVENT_MOUSE_CURSOR_POS then
+ return "Mouse Cursor Position"
+ elseif event.type == RL.EVENT_MOUSE_SCROLL then
+ return "Mouse Scroll"
+ elseif event.type == RL.EVENT_CURSOR_ENTER then
+ return "Cursor Enter"
+ end
+
+ return "Unknown"
+end
+
+local function getAction( action )
+ if action == RL.GLFW_RELEASE then
+ return "Release"
+ elseif action == RL.GLFW_PRESS then
+ return "Press"
+ elseif action == RL.GLFW_REPEAT then
+ return "Repeat"
+ end
+
+ return "Unknown"
+end
+
+local function keyName( key )
+ for item, value in pairs( RL ) do
+ if value == key and string.match( item, "KEY_", 0 ) ~= nil and item ~= "KEY_MENU" then
+ return string.sub( item, 5 )
+ end
+ end
+
+ return "Unknown"
+end
+
+function RL.event( event )
+ text = "Event: "..getEventType( event ).."\n"
+
+ if event.type == RL.EVENT_KEY then
+ text = text.."key: "..event.key.." scancode: "..event.scancode.." action: "..getAction( event.action ).." mods: "..event.mods
+ text = text .."\nkeyName: "..keyName( event.key )
+ elseif event.type == RL.EVENT_CHAR then
+ text = text.."key: "..event.key
+ -- text = text .."\nchar: "..string.char( event.key )
+ text = text .."\nchar: "..utf8.char( event.key )
+ elseif event.type == RL.EVENT_MOUSE_BUTTON then
+ text = text.."button: "..event.button.." action: "..getAction( event.action ).." mods: "..event.mods
+ elseif event.type == RL.EVENT_MOUSE_CURSOR_POS then
+ text = text.."x: "..event.x.." y: "..event.y
+ elseif event.type == RL.EVENT_MOUSE_SCROLL then
+ text = text.."yoffset: "..event.yoffset.." yoffset: "..event.yoffset
+ elseif event.type == RL.EVENT_CURSOR_ENTER then
+ text = text.."enter: "..event.enter
+ cursorIn = event.enter
+ end
+end
+
+function RL.draw()
+ if 0 < cursorIn then
+ RL.ClearBackground( RL.RAYWHITE )
+ else
+ RL.ClearBackground( RL.RED )
+ end
+
+ RL.DrawText( 0, text, textPos, 20, 2, RL.BLACK )
+end