-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.lua
131 lines (114 loc) · 3.4 KB
/
gui.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
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
local gui = {}
function gui.isInBounds(mouseX, mouseY, x, y, width, height)
return ((mouseX > x and mouseX < x + width) and (mouseY > y and mouseY < y + height))
end
local function tween(start, goal, delta)
return start + ((goal-start) * delta)
end
function gui.newButton(text, x, y, width, height, _color, alpha, textColor, textAlpha, _font, fontSize, clicked, mouseover, tweens, tweentime, data)
local self = {
text = text,
x = x, y = y, width = width, height = height,
startx = x, starty = y,
color = _color,
alpha = alpha,
delta = 0,
textColor = textColor,
textAlpha = textAlpha,
clicked = clicked,
mouseIsOver = false}
if data then
for i, v in pairs(data) do
self[i] = v
end
end
local tweenstarts = {}
if tweens then
for val, goal in pairs(tweens) do
tweenstarts[val] = self[val]
end
end
local font = love.graphics.newFont(_font, fontSize)
function self.draw()
love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.alpha)
love.graphics.rectangle("fill", self.x, self.y, width, height) -- make text look better
love.graphics.setColor(self.textColor.r, self.textColor.g, self.textColor.b, self.textAlpha)
love.graphics.setFont(font)
love.graphics.print(text, self.x, self.y)
end
function self.step(dt, mx, my)
if self.mouseIsOver then
if self.delta < 1 and tweens then
self.delta = math.min(self.delta + (dt/tweentime), 1)
end
else
if self.delta > 0 and tweens then
self.delta = math.max(self.delta - (dt/tweentime), 0)
end
end
if tweens then
for val, goal in pairs(tweens) do
self[val] = tween(tweenstarts[val], goal, self.delta)
end
end
end
function self.reset()
self.delta = 0
for val, goal in pairs(tweens) do
self[val] = tweenstarts[val]
end
end
return self
end
function gui.constrain(n, min, max)
if n < min then
return min
elseif n > max then
return max
end
return n
end
local function snap(n)
local c, f = math.ceil(n), math.floor(n)
if math.abs(n-c) > math.abs(n-f) then
return f
else
return c
end
end
function gui.newSlider(x, y, barWidth, barHeight, sliderWidth, sliderHeight, barColor, sliderColor, defaultValue, minValue, maxValue, padding, step)
local self = {
x = x, y = y - (barHeight/2), width = barWidth, height = barHeight,
sliderWidth = sliderWidth, sliderHeight = sliderHeight, sliderColor = sliderColor,
value = defaultValue,
maxValue = maxValue,
minValue = minValue,
range = maxValue - minValue,
clicked = false,
update = step
}
local minPos = x + (padding)
local maxPos = x + barWidth - padding
local posRange = maxPos - minPos
self.pos = padding + (self.range*x + posRange * (defaultValue - self.minValue))/self.range
function self.draw()
love.graphics.setColor(barColor.args())
love.graphics.rectangle("fill", self.x, self.y, barWidth, barHeight)
love.graphics.setColor(self.sliderColor.args())
love.graphics.rectangle("fill",
self.pos - (sliderWidth/2),
y - (sliderHeight/2), sliderWidth, sliderHeight)
end
function self.step(dt, mx, my)
self:update(dt, mx, my)
self.value = gui.constrain(self.value, self.minValue, self.maxValue)
if self.clicked then
self.pos = gui.constrain(mx, minPos, maxPos)
self.value = snap(((self.pos - x - padding)/(posRange) * self.range) + self.minValue)
else
self.pos = padding + (self.range*x + posRange * (self.value - self.minValue))/self.range
end
end
return self
end
return gui