-
Notifications
You must be signed in to change notification settings - Fork 4
/
AdjustableParameter.lua
41 lines (34 loc) · 1.08 KB
/
AdjustableParameter.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
39
40
41
---
--- A parameter that the user can adjust by pressing keys in LOVE
---
---@class AdjustableParameter
AdjustableParameter = CpObject()
function AdjustableParameter:init(value, name, up, down, step, lowerLimit, upperLimit)
self.value = value
self.name = name
self.up = up
self.down = down
self.step = step
self.lowerLimit = lowerLimit
self.upperLimit = upperLimit
end
function AdjustableParameter:onKey(key, callback)
if key == self.up then
self.value = math.min(self.upperLimit, self.value + self.step)
callback()
elseif key == self.down then
self.value = math.max(self.lowerLimit, self.value - self.step)
callback()
end
end
function AdjustableParameter:get()
return self.value
end
---@return table to use with love.graphics.print()
function AdjustableParameter:toColoredText(nameColor, keyColor, valueColor)
return {nameColor, self.name, keyColor, string.format(' (%s/%s): ', self.down, self.up),
valueColor, string.format('%.2f', self.value)}
end
function AdjustableParameter:__tostring()
return string.format('%s (%s/%s): %.2f', self.name, self.down, self.up, self.value)
end