-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
601 lines (452 loc) · 16 KB
/
Copy pathinit.lua
File metadata and controls
601 lines (452 loc) · 16 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
-- stellar
local stellar = {}
local selfpath = (...):match("^(.*%.?[^.]+)$"):gsub("%.", "/")
love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";" .. love.filesystem.getRequirePath():gsub("(%?%.lua)", selfpath .. "/%1"))
package.loaded["stellargui"] = stellar
local utf = require("utf8")
local FontStorage = require("classes.FontStorage")
local LocaleStorage = require("classes.LocaleStorage")
local parse = require("scripts.parse")
-- documentation
---@meta
---@alias pixels number Amount of pixels.
---@alias ResolutiionValue "hug"|"fill"|number
---@alias GrowthVariants "vertical"|"horizontal"
---@alias HorizontalAlignmentVariants "left"|"center"|"right"
---@alias VerticalAlignmentVariants "top"|"center"|"bottom"
---@alias FourSides {[1]: number, [2]: number, [3]: number, [4]: number}
---@alias LayoutProperties {w: ResolutiionValue, h: ResolutiionValue, padding: FourSides, growth: GrowthVariants, horizontal: HorizontalAlignmentVariants, vertical: VerticalAlignmentVariants, ignore: boolean?, gap: number}
-- config
local externalTypesDir = selfpath .. "/classes/objects"
-- consts
local DEFAULT_CURSOR = "arrow"
local DEFAULT_DOUBLE_CLICK_TIME = 0.5
local LOCALE_DIR_DEFAULT = "locale"
-- vars
local currentCursor = DEFAULT_CURSOR ---@type love.CursorType
local doubleClickTime = DEFAULT_DOUBLE_CLICK_TIME
local object_descriptors = {} ---@type table<string, ObjectUI>
local cursorStorage = {} ---@type {[love.CursorType|string]: love.Cursor}
local fontStorage ---@type FontStorage
local localeStorage ---@type LocaleStorage
local current_canvas ---@type CanvasObject
local canvases = {} ---@type CanvasObject[]
local currentHl ---@type ObjectUI? UI object mouse cursor currently on
local heldObject = {} ---@type table<integer, ObjectUI> UI object last mousepressed event was on (table element for every mouse button)
local focusedObject ---@type ObjectUI? UI object that currently has keyboard focus
local hooked
-- Double click detection
local lastClickTime, lastClickButton, lastClickPosition, lastClickedObject = 0, 0, {-1, -1}, nil
-- init
local love = love -- НЕ УДАЛЯТЬ. Предотвращает варнинги в других местах, где объявляются коллбеки love
local nopFunc = function() end
setmetatable(stellar, {
__index = function (_, key)
if object_descriptors[key] then
return object_descriptors[key]
end
end
})
setmetatable(cursorStorage, {
__index = function (self, key)
self[key] = love.mouse.getSystemCursor(key)
return self[key]
end
})
--- Figuring out the doubleclick time
if love.system.getOS() == "Windows" then
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetDoubleClickTime();
]]
doubleClickTime = ffi.C.GetDoubleClickTime() / 1000
end
-- fnc
--- utf-aware string.sub()
local function utf_sub(str, b, e)
return string.sub(str, utf.offset(str, b), e ~= -1 and (utf.offset(str, e + 1) - 1) or -1)
end
---Standard update function for the functionality of StellarGUI
---@param dt number
local function stellar_update(dt)
local x, y = love.mouse.getX(), love.mouse.getY()
local hlObject = current_canvas:isActive() and current_canvas:checkHover(x, y) or nil
if hlObject ~= currentHl then
local newCursor = DEFAULT_CURSOR
if currentHl then
currentHl:hoverOff(currentHl:convertGlobalCoords(x, y))
currentHl:redraw()
end
if hlObject then
newCursor = hlObject:hoverOn(hlObject:convertGlobalCoords(x, y)) or newCursor
hlObject:redraw()
end
if newCursor ~= currentCursor then
currentCursor = newCursor
love.mouse.setCursor(cursorStorage[newCursor])
end
currentHl = hlObject
end
current_canvas:tick(dt)
end
---Standard draw function for the functionality of StellarGUI
local function stellar_draw()
current_canvas:paint()
end
---Register UI object type descriptor
---@param typeDescriptor ObjectUI
local function registerType(typeDescriptor)
if object_descriptors[typeDescriptor.name] then
print("Object " .. typeDescriptor.name .. " is already registered within system, skipping.")
return
end
object_descriptors[typeDescriptor.name] = typeDescriptor
typeDescriptor.aliases = typeDescriptor.aliases or typeDescriptor.alias or {}
if type(typeDescriptor.aliases) == "string" then
typeDescriptor.aliases = {typeDescriptor.aliases}
end
if typeDescriptor.aliases then
for _, alias in ipairs(typeDescriptor.aliases) do
object_descriptors[alias] = typeDescriptor
end
end
if typeDescriptor.cursors then
for cursorName, cursor in pairs(typeDescriptor.cursors) do
cursorStorage[cursorName] = cursor
end
end
end
local function stellar_construct(self, definition)
definition = definition or {}
definition.defaults = definition.defaults or {[0] = definition}
if self.default then
definition.defaults[#definition.defaults+1] = self.default
end
local new_object
if self.extends then
new_object = object_descriptors[self.extends](definition)
else
new_object = {}
end
definition.rules = self.rules
setmetatable(new_object, nil)
parse.resolve(definition, new_object)
setmetatable(new_object, self)
self.new(new_object)
return new_object
end
-- stellar fnc
--#region Canvas manipulation
function stellar.createCanvas(prototype)
return stellar.Canvas(prototype)
end
function stellar.getCanvas()
return current_canvas
end
function stellar.setCanvas(canvas)
if type(canvas) == "table" then
current_canvas = canvas
else
current_canvas = canvases[canvas]
end
end
function stellar.storeCanvas(name, canvas)
canvases[name] = canvas
end
--#endregion
--#region Current canvas operations
function stellar.add(object)
current_canvas:add(object)
end
function stellar.remove(object)
current_canvas:remove(object)
end
--#endregion
function stellar.activate_object(obj)
if obj.__index then
return
end
assert(obj.name, "Name is required for an unknown UI object that inherits " .. (obj.extends or "no one"))
obj.__index = obj
obj.new = obj.new or function()end
if obj.extends == nil then
obj.extends = object_descriptors.ObjectUI.name
end
-- Generailze rules
obj.rules = obj.rules or {}
for _, rule in ipairs(obj.rules) do
if type(rule) == "table" then
if type(rule[1]) == "string" then
rule[1] = {rule[1]}
end
if not rule[2] then
rule[2] = rule[1]
end
end
end
if obj.extends then
local parent = object_descriptors[obj.extends]
assert(parent, "There is no such object class as \"" .. tostring(obj.extends) .. "\" to register as a parent of " .. obj.name)
parent.__call = stellar_construct
setmetatable(obj, parent)
obj[obj.extends] = parent
else
setmetatable(obj, {__call = stellar_construct})
end
end
---Loads object(s) from a specified path
---@param path string?
---@param skip_init boolean? Skip ObjectUI:stellar_activate() call. This flag is used internally.
---@return ObjectUI[]
function stellar.loadExternalObjects(path, skip_init)
path = path or externalTypesDir
local path_info = love.filesystem.getInfo(path)
if not path_info then
print(string.format("Failed loading object from %s. Path does not exist", path))
return {}
end
if path_info.type == "file" then
local objectFileChunk = love.filesystem.load(path)
if not objectFileChunk then
print(string.format("Failed loading object from %s. Compilation failed", path))
return {}
end
local descriptor = objectFileChunk()
if type(descriptor) ~= "table" or not descriptor.name then
print(string.format("Failed loading object from %s. Bad returning or table is not an object descriptor", path))
return {}
end
registerType(descriptor)
print(string.format("Loaded object %s from %s, aliases: %s", descriptor.name, path, table.concat(descriptor.aliases or {}, ", ")))
if not skip_init then
stellar.activate_object(descriptor)
end
return {descriptor}
end
local items = love.filesystem.getDirectoryItems(path)
local toactivate = {} ---@type ObjectUI[]
for _, item in ipairs(items) do
print(string.format("Loading object from %s", path .. "/" .. item))
local loaded_objects = stellar.loadExternalObjects(path .. "/" .. item, true)
for _, object in ipairs(loaded_objects) do
toactivate[#toactivate+1] = object
end
end
if not skip_init then
for _, object in ipairs(toactivate) do
stellar.activate_object(object)
end
end
return toactivate
end
function stellar.getObjectDescriptor(descriptor_name)
return object_descriptors[descriptor_name]
end
function stellar.getLocaleStorage()
return localeStorage
end
function stellar.setLocale(new_locale)
localeStorage:setLocale(new_locale)
end
function stellar.getFontStorage()
return fontStorage
end
---@param path love.FileData|string
function stellar.setDefaultFont(path)
fontStorage:registerFont("default", path)
end
--#region Debug functions
function stellar.drawMousePosition()
love.graphics.setColor(
love.mouse.isDown(1) and {0.3, 0.3, 1, 1} or
love.mouse.isDown(2) and {1, 0.3, 0.3, 1} or
love.mouse.isDown(3) and {0.3, 1, 0.3, 1} or
love.mouse.isDown(4, 5, 6) and {1, 1, 0, 1} or
{1, 1, 1, 1}
)
local text = love.mouse.getX() .. "; " .. love.mouse.getY()
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
text = text .. " <SHIFT>"
end
if currentHl then
text = currentHl.name .. "\n" .. text
end
love.graphics.print(text, love.mouse.getX(), love.mouse.getY() - love.graphics.getFont():getHeight())
love.graphics.print(text, 0, 0)
end
--#endregion
--- Stellar hook
function stellar.hook(force)
if hooked and not force then
return stellar
end
--- Set up utf.sub if such is not provided
if not utf.sub then
utf.sub = utf_sub
end
--- Set up callbacks
local love_update, love_draw, love_mousepressed, love_mousereleased, love_keypressed, love_keyreleased, love_textinput, love_resize, love_wheelmoved = love.update or nopFunc, love.draw or nopFunc, love.mousepressed or nopFunc, love.mousereleased or nopFunc, love.keypressed or nopFunc, love.keyreleased or nopFunc, love.textinput or nopFunc, love.resize or nopFunc, love.wheelmoved or nopFunc
love.update = function(dt)
love_update(dt)
stellar_update(dt)
end
love.draw = function()
love_draw()
stellar_draw()
end
love.mousepressed = function (x, y, but)
if currentHl then
if currentHl:isInteractible() then
heldObject[but] = currentHl
if focusedObject ~= currentHl then
if focusedObject then
focusedObject:loseFocus()
end
focusedObject = currentHl
focusedObject:gainFocus()
end
local cx, cy = currentHl:convertGlobalCoords(x, y)
-- Double click shenanigans
if
love.timer.getTime() - lastClickTime <= doubleClickTime and
but == lastClickButton and
lastClickedObject == currentHl and
x == lastClickPosition[1] and y == lastClickPosition[2]
then
currentHl:doubleClick(cx, cy, but)
currentHl:redraw()
lastClickTime = 0 -- avoid counting possible third click as double click
return
end
currentHl:click(cx, cy, but)
currentHl:redraw()
lastClickButton, lastClickTime, lastClickPosition[1], lastClickPosition[2], lastClickedObject = but, love.timer.getTime(), x, y, currentHl
end
else
if focusedObject then
focusedObject:loseFocus()
focusedObject = nil
end
love_mousepressed(x, y, but)
end
end
love.mousereleased = function (x, y, but)
if heldObject[but] then
local cx, cy = heldObject[but]:convertGlobalCoords(x, y)
if heldObject[but]:isInteractible() then
local pass = heldObject[but]:clickRelease(cx, cy, but)
heldObject[but]:redraw()
if not pass then
heldObject[but] = nil
return
end
end
if currentHl and (heldObject[but] ~= currentHl) and currentHl:isInteractible() then
local cx, cy = currentHl:convertGlobalCoords(x, y)
currentHl:clickReleaseExterior(cx, cy, but, heldObject[but])
currentHl:redraw()
heldObject[but] = nil
return
end
else
love_mousereleased(x, y, but)
end
end
love.keypressed = function (key, scancode, isrepeat)
if focusedObject and focusedObject:isInteractible() then
local revoke_focus = focusedObject:keyPress(
key,
love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl"),
love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift"),
love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt"),
scancode,
isrepeat
)
focusedObject:redraw()
if revoke_focus then
focusedObject:loseFocus()
focusedObject = nil
end
else
love_keypressed(key, scancode, isrepeat)
end
end
love.keyreleased = function (key, scancode, isrepeat)
if focusedObject and focusedObject:isInteractible() then
local revoke_focus = focusedObject:keyRelease(
key,
love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl"),
love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift"),
love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt"),
scancode,
isrepeat
)
focusedObject:redraw()
if revoke_focus then
focusedObject:loseFocus()
focusedObject = nil
end
else
love_keyreleased(key, scancode, isrepeat)
end
end
love.textinput = function (text)
if focusedObject and focusedObject:isInteractible() then
focusedObject:textinput(text)
focusedObject:redraw()
else
love_textinput(text)
end
end
love.resize = function (w, h)
for _, canvas in pairs(canvases) do
canvas.layout.w, canvas.layout.h = w, h
canvas:autolayout()
end
love_resize(w, h)
end
love.wheelmoved = function (x, y)
if currentHl then
if currentHl:isInteractible() then
currentHl:wheel(x, y)
end
end
end
stellar.loadExternalObjects(selfpath .. "/classes/primitives")
fontStorage = FontStorage()
parse.setFontStorage(fontStorage)
localeStorage = LocaleStorage()
localeStorage:setLocalePath(LOCALE_DIR_DEFAULT)
localeStorage:setLocale()
local new_canvas = stellar.createCanvas()
stellar.storeCanvas(1, new_canvas)
stellar.setCanvas(1)
hooked = true
return stellar
end
---@diagnostic disable-next-line: inject-field
function love.run()
stellar.hook()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
return function()
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
if love.timer then dt = love.timer.step() end
if love.update then love.update(dt) end
current_canvas:performRepaint()
if love.timer then love.timer.sleep(0.01) end
end
end
return stellar