-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.lua
219 lines (179 loc) · 5.63 KB
/
list.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
----------------------
-- List object
----------------------
local List = {}
function inside(mx, my, x, y, w, h)
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
end
function List:new(x, y, w, h)
o = {}
setmetatable(o, self)
self.__index = self
o.items = {}
o.hoveritem = 0
o.onclick = nil
o.x = x
o.y = y
o.bar = { size = 20, pos = 0, maxpos = 0, width = 15, lock = nil}
o.width = w - o.bar.width
o.height = h
o.item_height = 23
o.sum_item_height = 0
o.colors = {}
o.colors.normal = {bg = {0.19, 0.61, 0.88}, fg = {0.77, 0.91, 1}}
o.colors.hover = {bg = {0.28, 0.51, 0.66}, fg = {1, 1, 1}}
o.windowcolor = {0.19, 0.61, 0.88}
o.bordercolor = {0.28, 0.51, 0.66}
return o
end
function List:add(title, id)
local item = {}
item.title = title
item.id = id
--if type(tooltip) == "string" then
--item.tooltip = tooltip
--else
--item.tooltip = ""
--end
table.insert(self.items, item)
return item
end
function List:done()
self.items.n = #self.items
-- Recalc bar size.
self.bar.pos = 0
local num_items = (self.height / self.item_height)
local ratio = num_items / self.items.n
self.bar.size = self.height * ratio
self.bar.maxpos = self.height - self.bar.size - 3
-- Calculate height of everything.
self.sum_item_height = (self.item_height+1) * self.items.n + 2
end
function List:hasBar()
return self.sum_item_height > self.height
end
function List:getBarRatio()
return self.bar.pos / self.bar.maxpos
end
function List:getOffset()
local ratio = self.bar.pos / self.bar.maxpos
return math.floor((self.sum_item_height - self.height) * ratio + 0.5)
end
function List:update(dt)
if self.bar.lock then
local dy = math.floor(love.mouse.getY()-self.bar.lock.y+0.5)
self.bar.pos = self.bar.pos + dy
if self.bar.pos < 0 then
self.bar.pos = 0
elseif self.bar.pos > self.bar.maxpos then
self.bar.pos = self.bar.maxpos
end
self.bar.lock.y = love.mouse.getY()
end
end
function List:mousepressed(x, y, b, it)
if b == 1 and self:hasBar() then
local rx, ry, rw, rh = self:getBarRect()
if inside(x, y, rx, ry, rw, rh) then
self.bar.lock = { x = x, y = y }
return
end
end
if type(self.onclick) == "function" and
inside(x, y, self.x + 2, self.y + 1, self.width - 3, self.height - 3) then
local tx, ty = x - self.x, y + self:getOffset() - self.y
local index = math.floor((ty / self.sum_item_height) * self.items.n)
local item = self.items[index + 1]
if item then
self.onclick(item, index + 1, b)
end
end
end
function List:mousereleased(x, y, b, it)
if b == 1 and self:hasBar() then
self.bar.lock = nil
end
end
function List:mousemoved(x, y, dx, dy)
self.hoveritem = 0
if self:hasBar() then
local rx, ry, rw, rh = self:getBarRect()
if inside(x, y, rx, ry, rw, rh) then
self.hoveritem = -1
return
end
end
if inside(x, y, self.x + 2, self.y + 1, self.width - 3, self.height - 3) then
local tx, ty = x - self.x, y + self:getOffset() - self.y
local index = math.floor((ty / self.sum_item_height) * self.items.n)
self.hoveritem = index + 1
end
end
function List:wheelmoved(x, y)
if self:hasBar() then
local per_pixel = (self.sum_item_height - self.height) / self.bar.maxpos
local bar_pixel_dt = math.floor((self.item_height * 3) / per_pixel + 0.5)
self.bar.pos = self.bar.pos - y * bar_pixel_dt
if self.bar.pos > self.bar.maxpos then self.bar.pos = self.bar.maxpos end
if self.bar.pos < 0 then self.bar.pos = 0 end
end
end
function List:getBarRect()
return self.x, self.y + self.bar.pos + 1,
self.bar.width - 3, self.bar.size
--return self.x + self.width + 2, self.y + self.bar.pos + 1,
--self.bar.width - 3, self.bar.size
end
function List:getItemRect(i)
return self.x + self.bar.width, self.y + ((self.item_height + 1) * (i - 1) + 1) - self:getOffset(),
self.width - self.bar.width, self.item_height
--return self.x + 2, self.y + ((self.item_height + 1) * (i - 1) + 1) - self:getOffset(),
--self.width - 3, self.item_height
end
function List:draw()
love.graphics.setLineWidth(1)
love.graphics.setLineStyle("rough")
love.graphics.setColor(self.windowcolor)
-- Get interval to display
local start_i = math.floor(self:getOffset() / (self.item_height + 1)) + 1
local end_i = start_i + math.floor(self.height / (self.item_height + 1)) + 1
if end_i > self.items.n then end_i = self.items.n end
love.graphics.setScissor(self.x, self.y, self.width, self.height)
-- Items
local rx, ry, rw, rh
local colorset
for i = start_i, end_i do
if i == self.hoveritem then
colorset = self.colors.hover
else
colorset = self.colors.normal
end
rx, ry, rw, rh = self:getItemRect(i)
local item = self.items[i]
if type(item.ondraw) == "function" then
item.ondraw(item.self, item, rx, ry, rw, rh)
else
love.graphics.setColor(colorset.bg)
love.graphics.rectangle("fill", rx, ry, rw, rh)
love.graphics.setColor(colorset.fg)
love.graphics.print(self.items[i].title, rx + 10, ry + 5)
end
end
love.graphics.setScissor()
-- Bar
if self:hasBar() then
if self.hoveritem == -1 or self.bar.lock then
colorset = self.colors.hover
else
colorset = self.colors.normal
end
rx, ry, rw, rh = self:getBarRect()
love.graphics.setColor(colorset.bg)
love.graphics.rectangle("fill", rx, ry, rw, rh)
end
-- Border
love.graphics.setColor(self.bordercolor)
--love.graphics.rectangle("line", self.x + self.width, self.y, self.bar.width, self.height)
--love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
return List