-
Notifications
You must be signed in to change notification settings - Fork 0
/
pviewer_list.old.tl_
328 lines (273 loc) · 9.11 KB
/
pviewer_list.old.tl_
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
----------------------
-- List object
----------------------
local pallete = require "pallete"
--local inspect = require "inspect"
require "love"
local gr = love.graphics
local type Item = record
end
local type List = record
items: {Item}
onclick: function()
end
local List_mt: metatable<List> = {
__index = List,
}
function inside(mx: number, my: number, x: number, y: number, w: number, h: number): boolean
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
end
function List.new(x: number, y, w, h, font)
local o = setmetatable({} as List, List_mt)
o.items = {}
o.onclick = nil
o.font = font
o.x, o.y = x, y
o.width, o.height = w, h
o.item_height = 23 -- где учет высоты шрифта?
o.sum_item_height = 0
o.colors = {}
o.colors.normal = {bg = {0, 0, 0, 1}, fg = {0.77, 0.91, 1}}
o.colors.hover = {bg = {0.28, 0.51, 0.66, 1}, fg = {1, 1, 1, 1}}
o.windowcolor = {0.19, 0.61, 0.88, 1}
o.bordercolor = {0.28, 0.51, 0.66, 1}
o.touches = {}
o.drawList = {}
o.activeIndex = 0 -- никакой пунки не активный
o.lastOnclickIndex = 0
return o
end
function List:add(title)
local item = {}
item.title = title
table.insert(self.items, item)
return item
end
function List:done()
self.items.n = #self.items
self.visibleNum = math.floor(self.height / self.item_height)
if onAndroid then
self.visibleNum = self.visibleNum - 1
end
self.maxVisibleNum = self.visibleNum
print("self.visibleNum", self.visibleNum)
self.start_i = 1
if #self.items > 0 then
self.activeIndex = 1
end
if #self.items > self.visibleNum then
self.canDown = true
end
self:prepareDrawing()
end
function List:update(dt)
local touchesSeq = {}
for k, v in pairs(self.touches) do
table.insert(touchesSeq, v)
end
local first = touchesSeq[1]
if #touchesSeq == 1 then
table.insert(self.drawList, function()
love.graphics.setColor{0.5, 0.5, 0.5, 0.5}
love.graphics.circle("fill", first.x, first.y, 15)
end)
end
end
function List:mousepressed(x, y, b, it)
if self.upRect and inside(x, y, self.upRect.x, self.upRect.y, self.upRect.w, self.upRect.h) then
self:scrollUp()
elseif self.downRect and inside(x, y, self.downRect.x, self.downRect.y, self.downRect.w, self.downRect.h) then
self:scrollDown()
elseif type(self.onclick) == "function" then
for i = self.start_i, self.end_i do
local item = self.items[i]
local r = item.rect
if inside(x, y, r.x, r.y, r.w, r.h) then
self.onclick(item, i, b)
self.activeIndex = i
break
end
end
end
print("mousepressed, self.activeIndex", self.activeIndex)
end
function List:mousereleased(x, y, b, it)
end
function List:mousemoved(x, y, dx, dy)
end
function List:touchpressed(id, x, y)
--print(":touchpressed(id, x, y)")
self.touches[id] = {x = x, y = y}
end
function List:touchreleased(id, x, y)
--print(":touchreleased(id, x, y)")
self.touches[id] = nil
end
function List:touchmoved(id, x, y, dx, dy)
self.touches[id] = {x = x, y = y, dx = dx, dy = dy}
--print("touchmoved", id, x, y, dx, dy)
end
function List:wheelmoved(x, y)
end
function List:getItemRect(i)
return self.x, self.y + self.item_height * (i - 1), self.width, self.item_height
end
function List:draw()
love.graphics.setLineWidth(1)
--love.graphics.setLineStyle("rough")
love.graphics.setColor(self.windowcolor)
--love.graphics.setScissor(self.x, self.y, self.width, self.height)
local rx, ry, rw, rh
local colorset
local relativeI = 0
if self.canUp then
rx, ry, rw, rh = self.x, self.y + self.item_height * relativeI, self.width, self.item_height
self.upRect = {x = rx, y = ry, w = rw, h = rh}
love.graphics.setColor{1, 1, 1, 1}
love.graphics.draw(self.upCanvas, rx, ry)
relativeI = relativeI + 1
end
if self.canDown and self.canUp then
self.end_i = self.start_i + self.visibleNum - 2
elseif self.canDown or self.canUp then
self.end_i = self.start_i + self.visibleNum - 1
else
self.end_i = self.start_i + #self.items - 1
end
for i = self.start_i, self.end_i do
rx, ry, rw, rh = self.x, self.y + self.item_height * relativeI, self.width, self.item_height
local item = self.items[i]
item.rect = {x = rx, y = ry, w = rw, h = rh}
love.graphics.setColor{1, 1, 1, 1}
love.graphics.draw(item.canvas, rx, ry)
if self.activeIndex == i then
love.graphics.setColor{1, 1, 1}
love.graphics.rectangle("line", rx + 1, ry + 1, rw - 1, rh - 1)
end
relativeI = relativeI + 1
end
if self.canDown then
rx, ry, rw, rh = self.x, self.y + self.item_height * self.maxVisibleNum, self.width, self.item_height
self.downRect = {x = rx, y = ry, w = rw, h = rh}
love.graphics.setColor{1, 1, 1, 1}
love.graphics.draw(self.downCanvas, rx, ry)
end
for k, v in pairs(self.drawList) do
v()
end
love.graphics.setColor(pallete.pviewer.scrollLine)
love.graphics.setLineWidth(2)
love.graphics.setLineStyle("smooth")
love.graphics.line(self.x + 2, self.y, self.x + 2, self.y + self.height)
love.graphics.setColor(pallete.pviewer.circle)
end
function List:putActiveInVisiblePlace()
if self.activeIndex < self.start_i then
self.activeIndex = self.start_i
elseif self.activeIndex > self.end_i then
self.activeIndex = self.end_i - 1
end
print("self.activeIndex", self.activeIndex)
--if self.activeIndex ~= self.lastOnclickIndex then
--[[
[do
[ if type(self.onclick) == "function" then
[ local item = self.items[self.activeIndex]
[ --print("item", inspect(item))
[ --self.onclick(item, self.activeIndex, nil)
[ --self.onclick(item, 3, nil)
[ self.lastOnclickIndex = self.activeIndex
[ end
[end
]]
end
function List:scrollUp()
print("List:scrollUp()")
if self.canUp and self.start_i - 2 == 0 then
self.canUp = false
self.start_i = self.start_i - 1
else
if self.start_i - 1 > 0 then
self.canDown = true
self.start_i = self.start_i - 1
else
self.canUp = false
self.upRect = nil
end
end
self:putActiveInVisiblePlace()
if self.activeIndex - 1 >= 1 then
self.activeIndex = self.activeIndex - 1
local item = self.items[self.activeIndex]
self.onclick(item, self.activeIndex)
end
--linesbuf:push(2, "scrollUp %d", self.activeIndex)
end
function List:scrollDown()
print("List:scrollDown()")
if self.canDown then
if self.start_i + self.visibleNum <= #self.items then
if not self.canUp then
self.start_i = self.start_i + 2
else
self.start_i = self.start_i + 1
end
self.canUp = true
else
self.canDown = false
self.downRect = nil
end
end
self:putActiveInVisiblePlace()
if self.activeIndex + 1 <= self.end_i then
self.activeIndex = self.activeIndex + 1
local item = self.items[self.activeIndex]
self.onclick(item, self.activeIndex)
end
end
function List:prepareDrawing()
love.graphics.setLineWidth(1)
love.graphics.setLineStyle("rough")
love.graphics.setColor(self.windowcolor)
love.graphics.setFont(self.font)
local rx, ry, rw, rh
local colorset
for i = 1, self.items.n do
colorset = self.colors.normal
rx, ry, rw, rh = self:getItemRect(i)
local item = self.items[i]
item.canvas = love.graphics.newCanvas(rw, rh)
love.graphics.setCanvas(item.canvas)
love.graphics.clear(0, 0, 0, 0)
love.graphics.setColor(colorset.bg)
love.graphics.rectangle("fill", 0, 0, rw, rh)
love.graphics.setColor(colorset.fg)
love.graphics.print(item.title, 10, 5)
love.graphics.setCanvas()
end
self:prepareArrows()
self:saveCanvases()
end
function List:prepareArrows()
local _, _, rw, rh = self:getItemRect(1)
local x, y, w, h = 1, 1, rw - 1, rh - 1
self.downCanvas = love.graphics.newCanvas(rw, rh)
love.graphics.setCanvas(self.downCanvas)
love.graphics.setColor(pallete.pviewer.scrollTriangle)
love.graphics.polygon("fill", w / 2 - w / 3, y, w / 2, h, w / 2 + w / 3, y)
self.upCanvas = love.graphics.newCanvas(rw, rh)
love.graphics.setCanvas(self.upCanvas)
love.graphics.setColor(pallete.pviewer.scrollTriangle)
love.graphics.polygon("fill", w / 2 - w / 3, h, w / 2, y, w / 2 + w / 3, h)
love.graphics.setCanvas()
end
function List:saveCanvases()
self.upCanvas:newImageData():encode("png", "up.png")
self.downCanvas:newImageData():encode("png", "down.png")
for i in ipairs(self.items) do
local imageData = self.items[i].canvas:newImageData()
local file = imageData:encode("png", string.format("item_%d.png", i))
print(file)
end
end
return List