-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
scrollbar.lua
230 lines (185 loc) · 5.27 KB
/
scrollbar.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
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
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Scrollbar class
--
--==========================================================================--
class "gui.scrollbar" ( "gui.box" )
local scrollbar = gui.scrollbar
function scrollbar:scrollbar( parent, name )
gui.box.box( self, parent, name )
self:setDisplay( "block" )
self:setPosition( "absolute" )
self.width = 4
self.height = parent:getHeight()
self.disabled = false
self.min = 0
self.max = 0
self.rangeWindow = 0
self.value = 0
self:setUseFullscreenCanvas( true )
end
function scrollbar:draw()
local length = self:getThumbLength()
if ( length == self:getHeight() ) then
return
end
local color = self:getScheme( "scrollbar.backgroundColor" )
local width = self:getWidth()
local x = 0
if ( self:isDisabled() ) then
color = self:getScheme( "scrollbar.disabled.backgroundColor" )
end
love.graphics.setColor( color )
love.graphics.rectangle( "fill", x, self:getThumbPos(), width, length )
gui.box.draw( self )
end
accessor( scrollbar, "min" )
accessor( scrollbar, "max" )
function scrollbar:getRange()
return self.min, self.max
end
accessor( scrollbar, "rangeWindow" )
function scrollbar:getThumbLength()
local range = self:getMax() - self:getMin()
local size = self:getRangeWindow() / range
return size * self:getHeight()
end
function scrollbar:getThumbPos()
local min = self:getMin()
local range = self:getMax() - min
local percent = ( self:getValue() + min ) / range
return percent * self:getHeight()
end
accessor( scrollbar, "value" )
function scrollbar:invalidateLayout()
local parent = self:getParent()
self:setPos( parent:getWidth() - self:getWidth(), 0 )
self:setHeight( parent:getHeight() )
end
gui.accessor( scrollbar, "disabled", "is" )
local localX, localY = 0, 0
function scrollbar:mousepressed( x, y, button, istouch )
if ( self.mouseover and button == 1 ) then
self.mousedown = true
end
if ( self.mousedown and button == 1 ) then
localX, localY = self:screenToLocal( x, y )
local thumbPos = self:getThumbPos()
if ( localY < thumbPos ) then
self:pageUp()
return
elseif ( localY > thumbPos + self:getThumbLength() ) then
self:pageDown()
return
end
self.grabbedX = localX
self.grabbedY = localY
end
end
function scrollbar:mousereleased( x, y, button, istouch )
self.mousedown = false
self.grabbedX = nil
self.grabbedY = nil
end
function scrollbar:onValueChanged( oldValue, newValue )
end
function scrollbar:pageDown()
self:scrollDown( self:getRangeWindow() )
end
function scrollbar:pageUp()
self:scrollUp( self:getRangeWindow() )
end
function scrollbar:scrollDown( value )
self:setValue( self:getValue() + value )
end
function scrollbar:scrollUp( value )
self:setValue( self:getValue() - value )
end
function scrollbar:scrollToBottom()
self:setValue( self:getMax() )
end
function scrollbar:scrollToTop()
self:setValue( self:getMin() )
end
local min, max = 0, 0
local range = 0
local newRange = 0
local percent = 0
local function updateRange( self, min, max )
min, max = self:getRange()
range = max - min
if ( range ~= 0 ) then
newRange = max - min
percent = ( self:getValue() + self:getMin() ) / range
self.value = percent * newRange + self:getMin()
if ( self.value > self:getMax() - self:getRangeWindow() ) then
self.value = self:getMax() - self:getRangeWindow()
end
end
end
function scrollbar:setMin( min )
updateRange( self, min, self:getMax() )
self.min = min
end
function scrollbar:setMax( max )
updateRange( self, self:getMin(), max )
self.max = max
end
function scrollbar:setRange( min, max )
self:setMin( min )
self:setMax( max )
end
function scrollbar:setRangeWindow( rangeWindow )
self.rangeWindow = rangeWindow
local rangeSize = self:getMax() - self:getMin()
if ( rangeWindow > rangeSize ) then
self.rangeWindow = rangeSize
end
end
local oldValue = 0
local deltaValue = 0
function scrollbar:setValue( value )
oldValue = self.value
self.value = value
if ( self.value < self.min ) then
deltaValue = self.min - self.value
self.value = self.min
self:onValueChanged( oldValue, self.value )
self:invalidate()
return deltaValue
elseif ( self.value > self.max - self.rangeWindow ) then
deltaValue = self.max - self.rangeWindow - self.value
self.value = self.max - self.rangeWindow
self:onValueChanged( oldValue, self.value )
self:invalidate()
return deltaValue
end
self:onValueChanged( oldValue, self.value )
self:invalidate()
return 0
end
local mouseX, mouseY = 0, 0
local getPosition = love.mouse.getPosition
local deltaX, deltaY = 0, 0
function scrollbar:update( dt )
gui.panel.update( self, dt )
if ( not self:isVisible() or self:isDisabled() ) then
return
end
if ( not ( self.grabbedX or self.grabbedY ) ) then
return
end
mouseX, mouseY = getPosition()
localX, localY = self:screenToLocal( mouseX, mouseY )
deltaX, deltaY = localX - self.grabbedX,
localY - self.grabbedY
if ( deltaX == 0 and deltaY == 0 ) then
return
end
min, max = self:getRange()
range = max - min
deltaValue = ( deltaY / self:getHeight() ) * range
deltaValue = self:setValue( self:getValue() + deltaValue )
self.grabbedX = localX
self.grabbedY = localY + ( deltaValue / range ) * self:getHeight()
end