-
Notifications
You must be signed in to change notification settings - Fork 0
/
pviewer.tl
378 lines (332 loc) · 10.5 KB
/
pviewer.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
require "button"
require "common"
require "drawstat"
require "layout"
require "love"
require "menu-main"
require "nback"
require "tiledbackground"
local inspect = require "inspect"
--local timer = require "Timer"
local List = require "pviewer_list"
local cam = require "camera".new()
local fonts = require "fonts"
local g = love.graphics
local i18n = require "i18n"
local pallete = require "pallete"
local serpent = require "serpent"
global __MORE_DATA__ = false
global type Pviewer = record
font: love.graphics.Font
w: number
h: number
activeIndex: integer
save_name: string
timer: Timer
data: {History}
statisticRender: StatisticRender
list: List
layout: Layout
layout_nodata: Layout
backButton: Button
rt: g.Canvas
init:function(Pviewer, string)
leave: function(Pviewer)
resize: function(Pviewer, number, number)
buildLayout: function(Pviewer)
enter: function(Pviewer)
sortByDate: function(Pviewer)
updateRender: function(Pviewer, integer)
end
local pviewer_mt: metatable<Pviewer> = {
__index = Pviewer
}
function Pviewer.new(): Pviewer
local self: Pviewer = {
font = require "fonts".pviewer,
activeIndex = 0, -- обработай случаи если таблица истории пустая
}
self.w, self.h = g.getDimensions()
return setmetatable(self, pviewer_mt)
end
function Pviewer:init(save_name: string)
print("save_name", save_name)
self.save_name = save_name
self:resize(g.getDimensions())
--local Timer = require 'Timer'.new
--print('Timer', inspect(Timer))
self.timer = require 'Timer'.new()
end
-- создает новый экземпляр просмотрщика статистики для текущего положения
-- индекса pviewer.activeIndex
-- что за index?
function Pviewer:updateRender(index: integer)
print("updateRender()", index)
if self.data and index >= 1 and index <= #self.data then
local data = self.data[index]
self.statisticRender = require "drawstat".new({
signals = data.signals,
pressed = data.pressed,
level = data.level,
pause_time = data.pause_time,
x0 = 0,
y0 = 0,
--font = nback.font,
font = self.font,
border = nback.border,
durationMin = 0,
durationSec = 0,
})
else
colprint("no self.data or 'index' out of range")
self.statisticRender = nil
end
end
local function removeDataWithoutDateField(data: {History}): {History}
local cleanedData = {}
for _, v in ipairs(data) do
if v.date then
cleanedData[#cleanedData + 1] = v
end
end
return cleanedData
end
function Pviewer:makeList()
if #self.data ~= 0 then
self.list = List.new(self.layout.left.x, self.layout.left.y,
self.layout.left.w, self.layout.left.h, fonts.pviewer)
self.list.onclick = function(_: List, idx: number, _: number)
self:updateRender(math.floor(idx))
end
local str: string
for _, v in ipairs(self.data) do
str = compareDates(os.date("*t"), v.date)
--if k == 1 then
--str = "-- " .. tostring(k) .. string.format(" (%d) ..", #self.data)
--else
--str = "-- " .. tostring(k) .. " .."
--end
local item = self.list:add(str)
item.data = v
--item.color = pallete.levelColors[v.level]
item.colors = pallete.levelColors[v.level]
end
self.list:done()
self.list.onclick(nil, 1)
end
end
function Pviewer:sortByDate()
table.sort(self.data, function(h1: History, h2: History): boolean
local a, b = h1.date, h2.date
return a.year * 365 * 24 + a.yday * 24 + a.hour > b.year * 365 * 24 + b.yday * 24 + b.hour
end)
end
function Pviewer:enter()
print("pviewer:enter()")
local tmp, _ = love.filesystem.read(self.save_name)
if tmp ~= nil then
--local ok, self.data = serpent.load(tmp) as (boolean, {History})
local ok, t = serpent.load(tmp) as (boolean, {History})
self.data = t
if not ok then
-- эту строчку с падением при ошибке заменить на показ пустой
-- статистики.
error("Something wrong in restoring data " .. self.save_name)
end
else
self.data = {}
end
-------------------------------------------
--[[
__MORE_DATA__ = false
--__MORE_DATA__ = true
if __MORE_DATA__ then
local tmp2 = {}
for _ = 1, 6 do
for _, v in ipairs(self.data) do
table.insert(tmp2, v)
end
end
end
--]]
-------------------------------------------
--[[
gooi.components = {}
gooi.setStyle({ font = self.font,
showBorder = true,
bgColor = {0.208, 0.220, 0.222},
})
if #self.data == 0 then
self.backButton = gooi.newButton({ text = i18n("backToMainMenu"),
x = self.layout.nodata.top.x,
y = self.layout.nodata.top.y,
w = self.layout.nodata.top.w,
h = self.layout.nodata.top.h
}):onRelease(function()
menu:goBack()
end)
else
self.backButton = gooi.newButton({ text = i18n("backToMainMenu"),
x = self.layout.top.x,
y = self.layout.top.y,
w = self.layout.top.w,
h = self.layout.top.h
}):onRelease(function()
menu:goBack()
end)
end
--]]
if #self.data == 0 then
print("self.layout", inspect(self.layout))
self.backButton = Button.new(
i18n("backToMainMenu"),
-- что здесь происходит?
((self.layout as {any:any}).nodata as Layout).top.x,
((self.layout as {any:any}).nodata as Layout).top.y,
((self.layout as {any:any}).nodata as Layout).top.w,
((self.layout as {any:any}).nodata as Layout).top.h)
else
self.backButton = Button.new(
i18n("backToMainMenu"),
self.layout.top.x,
self.layout.top.x,
self.layout.top.x,
self.layout.top.x)
end
self.backButton.onMouseReleased = function()
mainMenu:goBack()
end
self.backButton.bgColor = {0.208, 0.220, 0.222}
self.backButton.font = self.font
self:sortByDate()
self:makeList()
self.data = removeDataWithoutDateField(self.data)
self.activeIndex = #self.data >= 1 and 1 or 0
self:updateRender(1)
end
function Pviewer:leave()
print("pviewer:leave()")
self.data = nil
end
function Pviewer:resize(neww: number, newh: number)
--print(string.format("pviewer.resize(%d, %d)", neww, newh))
local w, h = neww, newh
self:buildLayout()
-- обрати внимание на размер создаваемого полотна. Взят от балды.
self.rt = g.newCanvas(w, h, {format = "normal", msaa = 4})
if not self.rt then
error("Sorry, canvases are not supported!")
end
end
function Pviewer:buildLayout()
local screen: Layout = {}
screen.left, screen.right = splitv(makeScreenTable(), 0.2, 0.8)
screen.right.x = screen.right.x + 3
screen.right.w = screen.right.w - 3
screen.top, screen.bottom = splith(screen.right, 0.1, 0.9)
self.layout_nodata = {}
self.layout_nodata.top, self.layout_nodata.bottom = splith(makeScreenTable(), 0.2, 0.8)
self.layout_nodata.top = shrink(self.layout_nodata.top, 3)
self.layout = screen
end
function Pviewer:drawNodata()
local str = i18n("nodata")
g.setFont(self.font)
g.setColor{0, 0, 0, 1}
g.printf(str, 0, self.layout_nodata.bottom.y, self.layout_nodata.bottom.w, "center")
end
function Pviewer:draw()
g.push("all")
g.clear(pallete.background)
tiledback:draw(0.3)
--g.setFont(self.font)
if #self.data == 0 then
self:drawNodata()
self.backButton:draw()
else
self.list:draw()
g.setColor{1, 1, 1}
g.setCanvas(self.rt)
g.clear(pallete.background)
cam:attach()
if self.statisticRender then
self.statisticRender:beforeDraw()
local y = self.layout.bottom.y + (self.layout.bottom.h - self.statisticRender:getHitsRectHeight()) / 2
self.statisticRender:drawHits(self.layout.bottom.x, y)
end
cam:detach()
g.setCanvas()
g.setColor{1, 1, 1}
g.draw(self.rt as g.Drawable)
end
g.pop()
end
-- добавить клавиши управления для постраничной прокрутки списка результатов.
function Pviewer:keypressed(key: string)
if key == "escape" or key == "acback" then
mainMenu:goBack()
elseif key == "return" or key == "space" then
-- TODO по нажатию клавиши показать конечную таблицу игры
elseif key == "home" or key == "kp7" then
elseif key == "end" or key == "kp1" then
elseif key == "up" or key == "k" then
-- TODO
--self.list:scrollUp()
elseif key == "down" or key == "j" then
-- TODO
--self.list:scrollDown()
end
end
function Pviewer:wheelmoved(x: number, y: number)
if self.list then
self.list:wheelmoved(x, y)
end
end
function Pviewer:mousepressed(x: number, y: number, btn: number, _: boolean)
if self.list then
self.list:mousepressed(x, y, btn)
else
self.backButton:mousePressed()
end
end
function Pviewer:mousereleased(x: number, y: number, btn: number, _: boolean)
if self.list then
self.list:mousereleased(x, y, btn)
else
self.backButton:mouseReleased()
end
end
function Pviewer:mousemoved(x: number, y: number, dx: number, dy: number)
local mouse = love.mouse
if mouse.isDown(2) then
cam:move(-dx, -dy)
end
if self.list then
self.list:mousemoved(x, y, dx, dy)
end
end
--[[
function Pviewer:touchpressed(id, x, y)
if self.list then
self.list:touchpressed(id, x, y)
end
end
function Pviewer:touchreleased(id, x, y)
if self.list then
self.list:touchreleased(id, x, y)
end
end
function Pviewer:touchmoved(id, x, y, dx, dy)
if self.list then
self.list:touchmoved(id, x, y, dx, dy)
end
end
--]]
function Pviewer:update(dt: number)
self.backButton:update(dt)
self.timer:update(dt)
end
--return {
--new = Pviewer.new
--}
global pviewer: Pviewer = Pviewer.new()