-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.tl
363 lines (312 loc) · 10.2 KB
/
init.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
love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";?.lua;?/init.lua;scenes/nback3/?.lua")
require "globals"
require "menu-main"
require "common"
require "help"
require "languageselector"
require "love"
require "nbtypes"
require "pviewer"
require "tiledbackground"
--preventiveFirstRun = false
--onAndroid = true
--require "common"
--local profi = require "ProFi"
--local lg = love.graphics
local inspect = require "inspect"
--local pallete = require "pallete"
--local splash = require "splash"
--local timer = require "libs/Timer"()
local timer = require "Timer".new()
local keyconfig = require "keyconfig"
local i18n = require "i18n"
local linesbuf = require "kons".new()
local cam = require "camera".new()
local profiCam = require "camera".new()
--local fonts = require "fonts"
local screenMode = "win" -- or "fs"
local type Resizable = record
resize: function(any, number, number)
end
--local to_resize: {any} = {}
local to_resize: {Resizable} = {}
global languageSelector: LanguageSelector
function dispatchWindowResize(neww: number, newh: number)
for _, v in ipairs(to_resize) do
if v and v.resize then v:resize(neww, newh) end
end
end
local function quit()
SETTINGS.firstRun = false
writeSettings()
end
function loadLocales()
local files = love.filesystem.getDirectoryItems(SCENEPREFIX .. "locales")
print("locale files", inspect(files))
for _, v in ipairs(files) do
i18n.loadFile("locales/" .. v, function(path: string): any
local chunk, errmsg = love.filesystem.load(SCENEPREFIX .. path)
if not chunk then
error(errmsg)
end
return chunk
end)
end
end
function setupLocale(locale: string)
print("setupLocale", locale)
i18n.setLocale(locale)
end
function bindKeys()
local kc = keyconfig
--local Shortcut = KeyConfig.Shortcut
kc.bind("keypressed", { key = "2" }, function(sc: kc.Shortcut): boolean, kc.Shortcut
linesbuf.show = not linesbuf.show
return false, sc
end, "show or hide debug output", "linesbufftoggle")
-- XXX сломано
--kc.bindKeyPressed("startprofi", {"9"}, function()
--startProfiling()
--linesbuf:push(1, "profiler started.")
--end, "start program profiling.")
--kc.bindKeyPressed("stopprofi", {"0"}, function()
--local time = stopProfiling()
--linesbuf:push(1, "profiler stoped. gathered for %d seconds.", time)
--end, "stop program profiling.")
--kc.bindKeyPressed("dbg", {"d", "lctrl"}, function()
--debug.debug()
--end, "Start debugger.")
--kc.bindKeyPressed("savehistory", {"s", "lctrl"}, function()
--nback:save_to_history()
--end, "save statistic file in history file.")
--kc.bindKeyPressed("changescreenmode", {"return", "lalt"}, function()
---- код дерьмовый, но работает
--if screenMode == "fs" then
--love.window.setMode(1024, 768, {fullscreen = false}) screenMode = "win"
--dispatchWindowResize(love.graphics.getDimensions())
--else
--love.window.setMode(0, 0, {fullscreen = true, fullscreentype = "exclusive"})
--screenMode = "fs"
--dispatchWindowResize(love.graphics.getDimensions())
--end
--end, "Turn to fullscreen or windowed mode.")
--kc.bindKeyPressed("screenshot", {"f12"}, function()
--make_screenshot()
--end, "Save screenshot in data folder.")
end
local subInitCounter = 0
function subInit()
bindKeys()
--initGlobals()
nback:init(SAVE_NAME)
pviewer:init(SAVE_NAME)
help:init()
print('main menu', mainMenu)
mainMenu:init()
mainMenu:clear()
mainMenu:addItem(i18n("mainMenu.play"), nback as MenuObject)
mainMenu:addItem(i18n("mainMenu.viewProgress"), pviewer as MenuObject)
mainMenu:addItem(i18n("mainMenu.help"), help as MenuObject)
if not ON_ANDROID then
mainMenu:addItem(i18n("mainMenu.exit"), function() love.event.quit() end as MenuObject)
end
subInitCounter = subInitCounter + 1
--if subInitCounter > 1 then
--error("Function 'subInit' called more than once.")
--end
print('subInitCounter', subInitCounter)
end
local function init()
SETTINGS = readSettings()
print('settings:', inspect(SETTINGS))
loadLocales()
-- TODO get cmd arg table for language changing
print("arg", inspect(arg))
local locale = "en"
if arg[1] then
locale = string.match(arg[1], "locale=(%S+)") or "en"
end
if SETTINGS.firstRun or PREVENTIVE_FIRST_RUN then
languageSelector = require "languageselector".new()
end
setupLocale(locale)
math.randomseed(os.time())
love.window.setTitle("nback")
--require "splash".init()
subInit()
table.insert(to_resize, nback as Resizable)
table.insert(to_resize, mainMenu as Resizable)
table.insert(to_resize, pviewer as Resizable)
table.insert(to_resize, help as Resizable)
table.insert(to_resize, languageSelector as Resizable)
table.insert(to_resize, tiledback as Resizable)
if ON_ANDROID then
love.window.setMode(0, 0, {fullscreen = true})
--love.window.setMode(0, 0, {fullscreen = true, fullscreentype = "exclusive"})
cam = require "camera".new()
screenMode = "fs"
dispatchWindowResize(love.graphics.getDimensions())
end
--profiReportFont = fonts.profi
linesbuf.show = false
end
function update(dt: number)
if languageSelector then
languageSelector:update(dt)
if languageSelector.locale then
setupLocale(languageSelector.locale)
subInit()
languageSelector = nil
end
else
mainMenu:update(dt)
timer:update(dt)
end
linesbuf:update()
end
--[[
function stopProfiling()
if profiStartTime then
local res = love.timer.getTime() - profiStartTime
profi:stop()
local fname = "profile_report.txt"
profi:writeReport(fname)
profiReportText = nil
profiReportText = lg.newText(profiReportFont)
profiReportHeight = 0
local maxIndex = 0
local x, y = 0, 0 -- FIXME а если захочется отобразить в других координатах?
local i = 5
for line in io.lines(fname) do
if i < 0 then
maxIndex = profiReportText:add(line, x, y)
y = y + profiReportFont:getHeight()
profiReportHeight = profiReportHeight + profiReportFont:getHeight()
else
i = i - 1
end
end
-- выбираю максимальную ширину строки и сохраняю ее в переменной
profiReportWidth = 0
for i = 1, maxIndex do
local w = profiReportText:getWidth(i)
profiReportWidth = w > profiReportWidth and w or profiReportWidth
end
end
return res or 0
end
--]]
--[[
function drawProfilingReport()
if profiReportText then
profiCam:attach()
lg.setColor{0.1, 0.1, 0.1}
lg.rectangle("fill", 0, 0, profiReportWidth, profiReportHeight)
lg.setColor{1, 1, 1}
lg.draw(profiReportText)
profiCam:detach()
end
end
--]]
--[[
function startProfiling()
profi:start("once")
profiStartTime = love.timer.getTime()
end
--]]
local function keypressed(scancode: string)
--if onAndroid then return end
if languageSelector and languageSelector.keypressed then
languageSelector:keypressed(scancode)
else
keyconfig.keypressed(scancode)
mainMenu:keypressed(scancode)
end
end
local function keyreleased(key: string)
mainMenu:keyreleased(key)
end
local function draw()
if languageSelector then
tiledback:draw()
languageSelector:draw()
else
cam:attach()
mainMenu:draw()
cam:detach()
--drawProfilingReport()
--linesbuf:pushi("average FPS for 30 seconds")
end
linesbuf:pushi("FPS %d", love.timer.getFPS())
linesbuf:draw()
love.timer.sleep(0.01)
end
local function wheelmoved(_, y: number)
if y == 1 then
profiCam:zoom(0.98)
elseif y == -1 then
profiCam:zoom(1.02)
end
end
local function mousemoved(x: number, y: number, dx: number, dy: number, istouch: boolean)
if love.keyboard.isDown("lshift") and love.mouse.isDown(1) then
profiCam:move(-dx, -dy)
end
if languageSelector then
languageSelector:mousemoved(x, y, dx, dy, istouch)
else
mainMenu:mousemoved(x, y, dx, dy, istouch)
end
end
local function mousepressed(x: number, y: number, button: number, istouch: boolean)
if languageSelector then
languageSelector:mousepressed(x, y, button, istouch)
else
mainMenu:mousepressed(x, y, button, istouch)
end
end
local function mousereleased(x: number, y: number, button: number, istouch: boolean)
if languageSelector then
-- почему отлючено?
--languageSelector:mousereleased(x, y, button, istouch)
else
mainMenu:mousereleased(x, y, button, istouch)
end
end
local function touchpressed(id: number, x: number, y: number, dx: number, dy: number, pressure: number)
if languageSelector then
languageSelector:touchpressed(id, x, y)
else
mainMenu:touchpressed(id, x, y, dx, dy, pressure)
end
end
local function touchreleased(id: number, x: number, y: number, dx: number, dy: number, pressure: number)
if languageSelector then
languageSelector:touchreleased(id as any, x, y)
else
mainMenu:touchreleased(id, x, y, dx, dy, pressure)
end
end
local function touchmoved(id: number, x: number, y: number, dx: number, dy: number, pressure: number)
if languageSelector then
languageSelector:touchmoved(id as any, x, y)
else
mainMenu:touchmoved(id, x, y, dx, dy, pressure)
end
end
return {
init = init,
quit = quit,
draw = draw,
--drawui = drawui,
update = update,
keypressed = keypressed,
keyreleased = keyreleased,
mousemoved = mousemoved,
wheelmoved = wheelmoved,
mousereleased = mousereleased,
mousepressed = mousepressed,
touchpressed = touchpressed,
touchreleased = touchreleased,
touchmoved = touchmoved,
}