-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrollbar.lua
More file actions
335 lines (284 loc) · 13.5 KB
/
scrollbar.lua
File metadata and controls
335 lines (284 loc) · 13.5 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
Scrollbar = {}
local private = {}
setmetatable(private, { __mode = 'k' })
local screenWidth, screenHeight = guiGetScreenSize()
local function rgba(r, g, b, a)
return tocolor(r, g, b, a * 255)
end
local function isMouseInPosition(x, y, width, height)
if (not isCursorShowing()) then return false end
local cursorX, cursorY = getCursorPosition()
cursorX, cursorY = cursorX * screenWidth, cursorY * screenHeight
return ((cursorX >= x and cursorX <= x + width) and (cursorY >= y and cursorY <= y + height))
end
local function warn(message)
return error(message)
end
local function clamp(number, min, max)
if (number < min) then
return min
elseif (number > max) then
return max
end
return number
end
function Scrollbar:new(data, postGUI)
if (not (data)) then
warn('Define a table with the data to create a scrollbar')
end
assert(type(data) == 'table', 'Data must be a table, got ' .. type(data))
if (not (data.x and data.y and data.width and data.height and data.minValue and data.maxValue and data.orientation)) then
local outputErr = (not data.x and 'Define a X position to scrollbar') or (not data.y and 'Define a Y position to scrollbar') or (not data.width and 'Define a width to scrollbar') or (not data.height and 'Define a height to scrollbar') or (not data.minValue and 'Define a min value to scrollbar') or (not data.maxValue and 'Define a max value to scrollbar') or (not data.orientation and 'Define a orientation to scrollbar')
warn(outputErr)
end
assert(type(data.x) == 'number', 'X position must be a number, got ' .. type(data.x))
assert(type(data.y) == 'number', 'Y position must be a number, got ' .. type(data.y))
assert(type(data.width) == 'number', 'Width must be a number, got ' .. type(data.width))
assert(type(data.height) == 'number', 'Height must be a number, got ' .. type(data.height))
assert(type(data.minValue) == 'number', 'Min value must be a number, got ' .. type(data.minValue))
assert(type(data.maxValue) == 'number', 'Max value must be a number, got ' .. type(data.maxValue))
assert(type(data.bgRadius) == 'number', 'Background radius must be a number, got ' .. type(data.bgRadius))
assert(type(data.barRadius) == 'number', 'Bar radius must be a number, got ' .. type(data.barRadius))
assert(type(postGUI) == 'boolean', 'Post GUI must be a boolean, got ' .. type(postGUI))
local instance = {}
setmetatable(instance, { __index = self })
instance.x = data.x
instance.y = data.y
instance.width = data.width
instance.height = data.height
instance.minValue = data.minValue
instance.maxValue = data.maxValue
instance.orientation = data.orientation or 'vertical'
instance.bgRadius = data.bgRadius or 0
instance.barRadius = data.barRadius or 0
instance.postGUI = postGUI or false
private[instance] = {}
local sizeBar = (instance.orientation == 'vertical') and instance.height or instance.width
local bar = (sizeBar / instance.maxValue) * instance.minValue
private[instance].widthBar = (bar > 0) and bar or (instance.width / 2) -- width bar
private[instance].heightBar = (bar > 0) and bar or (instance.height / 2) -- height bar
private[instance].bgBarElement = svgCreate(instance.width, instance.height, string.format([[
<svg width='%s' height='%s' xmlns="http://www.w3.org/2000/svg">
<rect rx='%s' width='%s' height='%s' fill='#FFFFFF' />
</svg>
]], instance.width, instance.height, instance.bgRadius, instance.width, instance.height))
local width = (instance.orientation == 'horizontal') and private[instance].widthBar or instance.width
local height = (instance.orientation == 'vertical') and private[instance].heightBar or instance.height
private[instance].barElement = svgCreate(width, height, string.format([[
<svg width='%s' height='%s' xmlns="http://www.w3.org/2000/svg">
<rect rx='%s' width='%s' height='%s' fill='#FFFFFF' />
</svg>
]], width, height, instance.barRadius, width, height))
dxSetTextureEdge(private[instance].bgBarElement, 'wrap')
dxSetTextureEdge(private[instance].barElement, 'wrap')
private[instance].positionClick = {}
private[instance].posBarX = instance.x
private[instance].posBarY = instance.y
private[instance].tickClick = false
private[instance].progress = 'stabilized'
private[instance].duration = 250
private[instance].positionI = nil
private[instance].positionF = nil
private[instance].mouseData = {
clickBar = { x = 0, y = 0 },
clickMoveBar = { x = 0, y = 0 }
}
private[instance].alpha = 1
private[instance].barColor = {255, 255, 255} -- color bar
private[instance].barColorHover = {0, 0, 0} -- color bar when hover
private[instance].bgBarColor = {0, 0, 0} -- color background bar
private[instance].smooth = false
private[instance].scrolling = false
private[instance].scrollOffset = 0
-- Events methods
private[instance].scrolling_event = false -- this event happens when the user is scrolling
private[instance].scrollEnd_event = false -- this event happens when the user stop scrolling
return instance
end
function Scrollbar:render()
if (private[self].smooth and private[self].progress == 'in_progress') then
local progress = (getTickCount() - private[self].tickClick) / private[self].duration
local position = interpolateBetween(
private[self].positionI, 0, 0,
private[self].positionF, 0, 0,
progress, 'InOutQuad'
)
self:setBarPosition(self.orientation, position)
if (progress >= 1) then
private[self].progress = 'stabilized'
private[self].tickClick = nil
private[self].positionI = nil
private[self].positionF = nil
end
end
dxDrawImage(
self.x,
self.y,
self.width,
self.height,
private[self].bgBarElement,
0, 0, 0,
rgba(private[self].bgBarColor[1], private[self].bgBarColor[2], private[self].bgBarColor[3], private[self].alpha),
self.postGUI
)
local barWidth = (self.orientation == 'horizontal') and private[self].widthBar or self.width
local barHeight = (self.orientation == 'vertical') and private[self].heightBar or self.height
dxDrawImage(
private[self].posBarX,
private[self].posBarY,
barWidth,
barHeight,
private[self].barElement,
0, 0, 0,
(
(isMouseInPosition(private[self].posBarX, private[self].posBarY, barWidth, barHeight) or private[self].scrolling)
and rgba(private[self].barColorHover[1], private[self].barColorHover[2], private[self].barColorHover[3], private[self].alpha)
or rgba(private[self].barColor[1], private[self].barColor[2], private[self].barColor[3], private[self].alpha)
),
self.postGUI
)
if (private[self].scrolling) then
local cursorX, cursorY = getCursorPosition()
cursorX, cursorY = cursorX * screenWidth, cursorY * screenHeight
local valueOffset = 0
if (self.orientation == 'vertical') then
local cy = (cursorY - private[self].mouseData.clickMoveBar.y)
self:setBarPosition('vertical', clamp(cy, self.y, self.y + (self.height - private[self].heightBar)))
valueOffset = math.ceil(clamp(((private[self].posBarY - self.y) / (self.height - private[self].heightBar)) * 100, 0, 100))
else
local cx = (cursorX - private[self].mouseData.clickMoveBar.x)
self:setBarPosition('horizontal', clamp(cx, self.x, self.x + (self.width - private[self].widthBar)))
valueOffset = math.ceil(clamp(((private[self].posBarX - self.x) / (self.width - private[self].widthBar)) * 100, 0, 100))
end
private[self].scrollOffset = math.ceil((valueOffset / 100) * (self.maxValue - self.minValue))
if (private[self].scrolling_event) then
private[self].scrolling_event(self:getScrollOffset())
end
end
end
function Scrollbar:click(button, state, abx, aby)
if (button == 'left' and state == 'down') then
if (abx >= self.x and abx <= self.x + self.width and aby >= self.y and aby <= self.y + self.height) then
if (abx >= private[self].posBarX and abx <= (private[self].posBarX + private[self].widthBar) and aby >= private[self].posBarY and aby <= (private[self].posBarY + private[self].heightBar)) then
private[self].scrolling = true
private[self].mouseData.clickMoveBar.x = abx - private[self].posBarX
private[self].mouseData.clickMoveBar.y = aby - private[self].posBarY
return true
end
local valueOffset = 0
private[self].mouseData.clickBar.x = abx - self.x
private[self].mouseData.clickBar.y = aby - self.y
if (private[self].smooth) then
private[self].tickClick = getTickCount()
private[self].progress = 'in_progress'
if (self.orientation == 'vertical') then
local newPosBar = clamp(aby - (private[self].heightBar / 2), self.y, self.y + (self.height - private[self].heightBar))
valueOffset = math.ceil(clamp(((newPosBar - self.y) / (self.height - private[self].heightBar)) * 100, 0, 100))
private[self].positionI, private[self].positionF = private[self].posBarY, newPosBar
elseif (self.orientation == 'horizontal') then
local newPosBar = clamp(abx - (private[self].widthBar / 2), self.x, self.x + (self.width - private[self].widthBar))
valueOffset = math.ceil(clamp(((newPosBar - self.x) / (self.width - private[self].widthBar)) * 100, 0, 100))
private[self].positionI, private[self].positionF = private[self].posBarX, newPosBar
end
else
if (self.orientation == 'vertical') then
self:setBarPosition('vertical', clamp(aby - (private[self].heightBar / 2), self.y, self.y + (self.height - private[self].heightBar)))
valueOffset = math.ceil(clamp(((private[self].posBarY - self.y) / (self.height - private[self].heightBar)) * 100, 0, 100))
elseif (self.orientation == 'horizontal') then
self:setBarPosition('horizontal', clamp(abx - (private[self].widthBar / 2), self.x, self.x + (self.width - private[self].widthBar)))
valueOffset = math.ceil(clamp(((private[self].posBarX - self.x) / (self.width - private[self].widthBar)) * 100, 0, 100))
end
end
private[self].scrollOffset = math.ceil((valueOffset / 100) * (self.maxValue - self.minValue))
if (private[self].scrolling_event) then
private[self].scrolling_event(self:getScrollOffset())
end
return true
end
elseif (button == 'left' and state == 'up') then
private[self].scrolling = false
if (private[self].scrollEnd_event) then
private[self].scrollEnd_event(self:getScrollOffset())
end
return true
end
return false
end
function Scrollbar:setScrollOffset(value)
if (not value) then
warn('Define a value to set scroll offset')
return false
end
local newValue = math.ceil((value * 100) / (self.maxValue - self.minValue))
private[self].scrollOffset = newValue
if (self.orientation == 'vertical') then
self:setBarPosition('vertical', self.y + ((self.height - private[self].heightBar) / 100) * newValue)
elseif (self.orientation == 'horizontal') then
self:setBarPosition('horizontal', self.x + ((self.width - private[self].widthBar) / 100) * newValue)
end
return true
end
function Scrollbar:getScrollOffset()
return private[self].scrollOffset
end
function Scrollbar:onScroll(callback)
if (not callback) then
warn('Define a callback to onScroll event')
return false
end
private[self].scrolling_event = callback
return true
end
function Scrollbar:onScrollEnd(callback)
if (not callback) then
warn('Define a callback to onScrollEnd event')
return false
end
private[self].scrollEnd_event = callback
return true
end
function Scrollbar:setBarPosition(orientation, position)
if (not orientation) then
warn('Define a orientation to set bar position')
return false
end
if (not position) then
warn('Define a position to set bar position')
return false
end
local orientations = {
['vertical'] = function(value)
private[self].posBarY = value
end,
['horizontal'] = function(value)
private[self].posBarX = value
end
}
if (not orientations[orientation]) then
warn('Invalid orientation')
return false
end
orientations[orientation](position)
return true
end
function Scrollbar:setAlpha(alpha)
if (not alpha) then
warn('Define a alpha to set alpha')
return false
end
private[self].alpha = alpha
return true
end
function Scrollbar:setProperty(property, value)
if (not property or not (value ~= nil)) then
local output = (not property and 'Define a property to set property') or (not value and 'Define a value to set property')
warn(output)
return false
end
if (private[self][property] == nil) then
warn('Invalid property')
return false
end
private[self][property] = value
return true
end