aboutsummaryrefslogtreecommitdiffstats
path: root/core.lua
blob: d97e83a88b1b1afc9e05f2eeabb1add2593f0aa9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-- state
local context = {maxid = 0}
local draw_items = {n = 0}
local NO_WIDGET = function()end

local function generateID()
	context.maxid = context.maxid + 1
	return context.maxid
end

local function setHot(id) context.hot = id end
local function isHot(id)  return context.hot == id end

local function setActive(id) context.active = id end
local function isActive(id)  return context.active == id end

local function setKeyFocus(id) context.keyfocus = id end
local function hasKeyFocus(id) return context.keyfocus == id end

local function disableKeyFocus() return setKeyFocus{} end
local function clearKeyFocus() return setKeyFocus(nil) end

-- input
local mouse = {x = 0, y = 0, down = false}
local keyboard = {key = nil, code = -1}
keyboard.cycle = {
	-- binding = {key = key, modifier1, modifier2, ...} XXX: modifiers are OR-ed!
	prev = {key = 'tab', 'lshift', 'rshift'},
	next = {key = 'tab'},
}

function mouse.updateState(id, widgetHit, ...)
	if widgetHit(mouse.x, mouse.y, ...) then
		setHot(id)
		if not context.active and mouse.down then
			setActive(id)
		end
	end
end

function mouse.releasedOn(id)
	return not mouse.down and isHot(id) and isActive(id)
end

function keyboard.pressed(key, code)
	keyboard.key = key
	keyboard.code = code
end

function keyboard.tryGrab(id)
	if not context.keyfocus then
		setKeyFocus(id)
	end
end

function keyboard.isBindingDown(bind)
	local modifiersDown = #bind == 0 or love.keyboard.isDown(unpack(bind))
	return keyboard.key == bind.key and modifiersDown
end

local function makeCyclable(id)
	keyboard.tryGrab(id)
	if hasKeyFocus(id) then
		if keyboard.isBindingDown(keyboard.cycle.prev) then
			setKeyFocus(context.lastwidget)
			keyboard.key = nil
		elseif keyboard.isBindingDown(keyboard.cycle.next) then
			setKeyFocus(nil)
			keyboard.key = nil
		end
	end
	context.lastwidget = id
end

-- helper functions
local function strictAnd(...)
	local n = select("#", ...)
	local ret = true
	for i = 1,n do ret = select(i, ...) and ret end
	return ret
end

local function strictOr(...)
	local n = select("#", ...)
	local ret = false
	for i = 1,n do ret = select(i, ...) or ret end
	return ret
end

-- allow packed nil
local function save_pack(...)
	return {n = select('#', ...), ...}
end

local function save_unpack(t, i)
	i = i or 1
	if i >= t.n then return t[i] end
	return t[i], save_unpack(t, i+1)
end

local function registerDraw(id, f, ...)
	assert(type(f) == 'function' or (getmetatable(f) or {}).__call,
	       'Drawing function is not a callable type!')

	local state = 'normal'
	if isHot(id) or hasKeyFocus(id) then
		state = isActive(id) and 'active' or 'hot'
	end
	local rest = save_pack(...)
	draw_items.n = draw_items.n + 1
	draw_items[draw_items.n] = function() f(state, save_unpack(rest)) end
end

-- actually update-and-draw
local function draw()
	-- close frame state
	if not mouse.down then -- released
		setActive(nil)
	elseif not context.active then -- clicked outside
		setActive(NO_WIDGET)
	end

	for i = 1,draw_items.n do draw_items[i]() end

	-- prepare for next frame
	draw_items.n = 0
	context.maxid = 0

	-- update mouse status
	setHot(nil)
	mouse.x, mouse.y = love.mouse.getPosition()
	mouse.down = love.mouse.isDown('l')

	keyboard.key, keyboard.code = nil, -1
end

return {
	mouse           = mouse,
	keyboard        = keyboard,

	generateID      = generateID,
	setHot          = setHot,
	setActive       = setActive,
	setKeyFocus     = setKeyFocus,
	isHot           = isHot,
	isActive        = isActive,
	hasKeyFocus     = hasKeyFocus,

	disableKeyFocus = disableKeyFocus,
	enableKeyFocus  = clearKeyFocus,
	clearKeyFocus   = clearKeyFocus,
	makeCyclable    = makeCyclable,

	style           = require((...):match("(.-)[^%.]+$") .. '.style-default'),
	color           = color,
	registerDraw    = registerDraw,
	draw            = draw,

	strictAnd       = strictAnd,
	strictOr        = strictOr,
	save_pack       = save_pack,
	save_unpack     = save_unpack,
}