-
Notifications
You must be signed in to change notification settings - Fork 4
/
ToggleParameter.lua
38 lines (32 loc) · 1.15 KB
/
ToggleParameter.lua
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
---
--- A parameter that the user can adjust by pressing keys in LOVE
---
---@class ToggleParameter
ToggleParameter = CpObject()
---@param noCallback boolean|nil if true, just change the value, do not call the callback, for instance, a parameter
--- which does not require the course to be regenerated because only affects the display
function ToggleParameter:init(name, value, toggle, noCallback)
self.name = name
self.value = value
self.toggle = toggle
self.noCallback = noCallback
end
function ToggleParameter:onKey(key, callback)
if key == self.toggle then
self.value = not self.value
if not self.noCallback then
callback()
end
end
end
function ToggleParameter:get()
return self.value
end
---@return table to use with love.graphics.print()
function ToggleParameter:toColoredText(nameColor, keyColor, valueColor)
return {nameColor, self.name, keyColor, string.format(' (%s): ', self.toggle),
valueColor, tostring(self.value)}
end
function ToggleParameter:__tostring()
return string.format('%s (%s): %s', self.name, self.toggle, self.value)
end