-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
622 lines (481 loc) · 20.5 KB
/
init.lua
File metadata and controls
622 lines (481 loc) · 20.5 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
-- stellar
local stellar = {}
print((...):match("^(.*%.?[^.]+)$"):gsub("%.", "/"))
local selfpath = (...):match("^(.*%.?[^.]+)$"):gsub("%.", "/")
love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";" .. love.filesystem.getRequirePath():gsub("(%?%.lua)", selfpath .. "/%1"))
print(love.filesystem.getRequirePath())
local ffi = require("ffi")
local utf = require("utf8")
local paletteClass = require("classes.Palette")
-- documentation
---@meta
---@diagnostic disable
---@alias pixels number Amount of pixels.
---@alias seconds number Amount of seconds
---@alias DimensionsXYWH {[1]: pixels, [2]: pixels, [3]: pixels, [4]: pixels} Structure for some object's X position, Y position, width and height.
---@alias ObjectDescriptor {name: string, aliases: string[]?, new: (fun(prototype: ObjectPrototype):ObjectUI), rules: ObjectParsingRule[], cursors: table<string, love.Cursor>?, construct: fun(ObjectDefinition): ObjectUI}
---@alias ObjectDefinition table Table, which contains UI object definition fields, and is to be processed by UI object definition Parser
---@alias ObjectParser fun(definition: ObjectDefinition, sink: ObjectPrototype):boolean A function, that processes UI object definition and outputs prepared parameters into sink. If it fails to process given definition, it will return false.
---@alias ObjectParserName string
---@alias ObjectParsingRule ObjectParserName|function|{[1]: ObjectParserName, [2]: ObjectDefinition}|{[1]: string[], [2]: string, [3]: ObjectDefinition, [4]: ParsingRule[]?}
---@alias ObjectPrototype table Table, which contains valid object keys and parameters. Usually returned from parseDefinition
---@alias registeredIndex number Index of the registered UI object that it can be referenced by in lib functions
-- config
local externalTypesDir = selfpath .. "/classes/objects"
-- consts
local PATTERN_FILENAME = "[^\\/]+$"
local DEFAULT_CURSOR = "arrow"
local DEFAULT_DOUBLE_CLICK_TIME = 0.5
-- vars
local currentCursor = DEFAULT_CURSOR ---@type love.CursorType
local doubleClickTime = DEFAULT_DOUBLE_CLICK_TIME
local object_descriptors = {} ---@type table<string, ObjectDescriptor>
local definition_parsers = {} ---@type {[string]: ObjectParser} Collection of parsers for UI objects parameters
local cursorStorage = {} ---@type {[love.CursorType|string]: love.Cursor}
local registeredObjects = {} ---@type {[registeredIndex]: ObjectUI} Currently registered objects in the system that are eligible for update and draw.
local registeredAssociative = {} ---@type {[ObjectUI]: registeredIndex} Associative array of registered objects used to get registration index by object reference
local currentHl ---@type ObjectUI UI object mouse cursor currently on
local heldObject = {} ---@type 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(registeredAssociative, {__mode = 'k'})
setmetatable(stellar, {
__index = function (self, key)
if object_descriptors[key] then
return object_descriptors[key].construct
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
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 seconds
local function stellar_update(dt)
local hlObject
local x, y = love.mouse.getX(), love.mouse.getY()
for _, registered in pairs(registeredObjects) do
hlObject = registered:isActive() and registered:checkHover(x, y) or hlObject
end
---@cast hlObject ObjectUI?
if hlObject ~= currentHl then
local newCursor = DEFAULT_CURSOR
if currentHl then
currentHl:hoverOff(x, y)
end
if hlObject then
newCursor = hlObject:hoverOn(x, y) or newCursor
end
if newCursor ~= currentCursor then
currentCursor = newCursor
love.mouse.setCursor(cursorStorage[newCursor])
end
currentHl = hlObject
end
for _, registered in pairs(registeredObjects) do
if registered:isActive() then
registered:tick(dt)
end
end
end
---Standard draw function for the functionality of StellarGUI
local function stellar_draw()
love.graphics.push("transform")
for _, registered in pairs(registeredObjects) do
if registered:isDrawn() then
love.graphics.translate(registered:getTranslation())
registered:paint()
love.graphics.origin()
end
end
love.graphics.pop()
end
--#region Parsers collection
---Parser for object width and height.<br>Will parse size (ex. width = 200, height = 100) correctly if it is defined in the definition table as any of following:<br>{0, 0, 200, 100}<br>{"objectName", 0, 0, 200, 100}<br>{w = 200, h = 100}<br>{width = 200, height = 100}<br>{ size = {200, 100} }
---@type ObjectParser
function definition_parsers.sizeRectangular(def, sink)
local width = sink.w or def.w or def.width or (def.size or {})[1] or (type(def[1]) == "number") and def[3] or (type(def[2]) == "number") and def[4]
local height = sink.h or def.h or def.height or (def.size or {})[2] or (type(def[1]) == "number") and def[4] or (type(def[2]) == "number") and def[5]
sink.w = width
sink.h = height
if not width or not height then
return false
end
return true
end
---Parser for object position on the screen.<br>Will parse position (ex. x = 200, y = 100) correctly if it is defined in the definition table as any of following:<br>{200, 100}<br>{"objectName", 200, 100}<br>{x = 200, y = 100}<br>{horizontal = 200, vertical = 100}<br>{ position = {200, 100} }<br>{ pos = {200, 100} }<br>{ coordinates = {200, 100} }<br>**Also supports setting one of the dimensions to the string<br>"center"/"centered"/"middle"/"mid" (any of the following) to center object's position based on its size.<br>Also supports negative coordinates.<br>Position of the object will be counted from the other edge of screen in this case (respecting object's size)**
---@type ObjectParser
function definition_parsers.position(def, sink)
local x = sink.x or def.x or def.horizontal or (def.position or {})[1] or (def.pos or {})[1] or (def.coordinates or {})[1] or (type(def[1]) == "number") and def[1] or (type(def[2]) == "number") and def[2]
local y = sink.y or def.y or def.vertical or (def.position or {})[2] or (def.pos or {})[2] or (def.coordinates or {})[2] or (type(def[1]) == "number") and def[2] or (type(def[2]) == "number") and def[3]
local sw, sh = love.graphics.getWidth(), love.graphics.getHeight()
if type(x) == "string" then
if x == "center" or x == "centered" or x == "middle" or x == "mid" then
x = math.floor(sw/2-sink.w/2)
else
x = nil
end
elseif x and x < 0 then
x = sw - sink.w + x
end
if type(y) == "string" then
if y == "center" or y == "centered" or y == "middle" or y == "mid" then
y = math.floor(sh/2-sink.h/2)
else
y = nil
end
elseif y and y < 0 then
y = sh - sink.h + y
end
sink.x = x
sink.y = y
if not x or not y then
return false
end
return true
end
function definition_parsers.palette(def, sink)
local palette = def.palette or def.palete or def.colors
if palette then
if type(palette) == "table" and palette.container then
sink.palette = palette
return true
end
sink.palette = paletteClass.new(palette)
return true
end
local color = def.color or def.mainColor or def.colorMain or def.main_color
local text = def.textColor or def.colorText or def.text_color
local alt = def.frameColor or def.colorFrame or def.additionalColor or def.colorAdditional or def.bgColor or def.hlColor
if sink.palette then
if not sink.palette:getColorByIndex(1) then
sink.palette:setColor(1, color)
end
if not sink.palette:getColorByIndex(2) then
sink.palette:setColor(2, text)
end
if not sink.palette:getColorByIndex(3) then
sink.palette:setColor(3, alt)
end
return true
end
sink.palette = paletteClass.new({color, text, alt})
if not color or not text or not alt then
return false
end
return true
end
--#endregion
---Parse provided definition into object prototype
---@param definition ObjectDefinition
---@param rules ObjectParsingRule[]
---@return ObjectPrototype
local function parseDefinition(definition, rules)
local objectPrototype = {}
for _, parser in ipairs(rules) do
if type(parser) == "string" then -- 1. Stupid parsing (not aware of failure)
definition_parsers[parser](definition, objectPrototype)
elseif type(parser) == "function" then -- 2. Custom object type-provided parser
parser(definition, objectPrototype)
elseif type(parser) == "table" then
if type(parser[1]) == "string" then -- 3. Parsing with default value set
if not definition_parsers[parser[1]](definition, objectPrototype) then
definition_parsers[parser[1]](parser[2], objectPrototype)
end
elseif type(parser[1]) == "table" then -- 4. Simple parsing
local possibleInputNames, outputName, defaultValue, appliedRules = parser[1], parser[2], parser[3], parser[4]
local value
for _, name in ipairs(possibleInputNames) do
if type(definition[name]) ~= "nil" then
value = definition[name]
break
end
end
if type(value) == "nil" then
value = defaultValue
end
if not appliedRules then -- 4.1. Generalized passthrough
objectPrototype[outputName] = value
else -- 4.2 Generalized recursive parsing
objectPrototype[outputName] = parseDefinition(value, appliedRules)
end
end
end
end
return objectPrototype
end
---@debug parsing test print(parseDefinition({numb = false, aquarium = {12, 123}}, {{"position", {10, 20}}, {{"isWhale"}, "fish", "false"}, {{"numb"}, "n", 123}, {{"fishtank", "aquarium"}, "aqua", {}, {"position"}}}).aqua.x)
---Register UI object type descriptor
---@param typeDescriptor ObjectDescriptor
local function registerType(typeDescriptor)
function typeDescriptor.construct(def)
local prototype = parseDefinition(def, typeDescriptor.rules)
return typeDescriptor.new(prototype)
end
object_descriptors[typeDescriptor.name] = typeDescriptor
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
-- classes
---UI objects meta-parent, UI state can be manipulated through this object. Meant to be singleton
---@class StateUI : ObjectUI
local StateUI = {}
--Volunteerly revoke focus from self and optionally give it to another object.
---@param origin ObjectUI
---@param successor ObjectUI?
function StateUI:revokeFocus(origin, successor)
if focusedObject == origin then
focusedObject:loseFocus()
if successor and successor:isInteractible() then
focusedObject = successor
successor:gainFocus()
end
end
end
---Change current system cursor type
---@param origin ObjectUI
---@param type love.CursorType?
function StateUI:setCursor(origin, type)
type = type or DEFAULT_CURSOR
if type ~= currentCursor then
currentCursor = type
love.mouse.setCursor(cursorStorage[type])
end
end
---Volunteerly unregister origin object
---@param origin ObjectUI
function StateUI:unregisterMyself(origin)
stellar.unregister(origin)
end
-- stellar fnc
---Register the UI object descriptor in a system for update and draw
---@param uiobj ObjectUI A UI object to register
---@return registeredIndex index Index of a registered UI object that it can be referenced by in other functions
function stellar.register(uiobj)
local newIndex = #registeredObjects+1
uiobj.parent = StateUI
registeredAssociative[uiobj] = newIndex
registeredObjects[newIndex] = uiobj
return newIndex
end
---Unregister the UI object descriptor in a system by object pointer or registeredIndex
---@param uiobj ObjectUI|registeredIndex A UI object/index to unregister
---@param message string Unregister message
---@see ObjectUI.unregister for ui object unregister behavior
function stellar.unregister(uiobj, message)
local registeredIndex
if type(uiobj) == "table" then
registeredIndex = registeredAssociative[uiobj]
if not registeredIndex then
return false
end
elseif type(uiobj) == "number" then
if registeredObjects[uiobj] then
registeredIndex = uiobj
else
return false
end
else
return false
end
if not registeredObjects[registeredIndex] then
return false
end
if not registeredObjects[registeredIndex]:unregister() then
registeredObjects[registeredIndex] = nil
end
return true
end
---Unregisters all objects from the system
function stellar.unregisterAll()
for i, _ in pairs(registeredObjects) do
stellar.unregister(i)
end
end
function stellar.loadExternalObjects(path)
local 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 {}, ", ")))
return true
end
local items = love.filesystem.getDirectoryItems(path)
for _, item in ipairs(items) do
print(string.format("Loading object from %s", path .. "/" .. item))
stellar.loadExternalObjects(path .. "/" .. item)
end
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
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.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.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
currentHl:click(x, y, but)
-- 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(x, y, but)
lastClickTime = 0 -- avoid counting possible third click as double click
return
end
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
if heldObject[but]:isInteractible() then
if not heldObject[but]:clickRelease(x, y, but) then
heldObject[but] = nil
return
end
end
if currentHl and (heldObject[but] ~= currentHl) and currentHl:isInteractible() then
currentHl:clickReleaseExterior(x, y, but, heldObject[but])
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
if 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
) 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
if 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
) 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)
else
love_textinput(text)
end
end
hooked = true
return stellar
end
return stellar