summaryrefslogtreecommitdiff
path: root/examples/events/main.lua
blob: 65f575ccf1b4a796ee0c1de4b96b5c69f2e71ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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