-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
435 lines (407 loc) · 13.5 KB
/
Copy pathinit.lua
File metadata and controls
435 lines (407 loc) · 13.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
local Dina = {
_TITLE = 'Dina - Game Engine (c)',
_VERSION = 'v3.4.0',
_URL = 'https://dina.lacombedominique.com/',
_LICENSE = [[
Copyright (c) 2019-2022 LACOMBE Dominique
ZLIB Licence
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
]]
}
local CurrentFile = (...):gsub("^(.*/+)", "")
local CurrentFolder = (...):gsub('%/'..CurrentFile..'$', '')
local EngineFolders = love.filesystem.getDirectoryItems(CurrentFolder)
local UtilsFolder = CurrentFolder.."/Utils"
-- Local functions
local function InitializeGlobalFunctions()
local files = love.filesystem.getDirectoryItems(UtilsFolder)
for k, filename in ipairs(files) do
local file = filename:gsub('%.lua$', '')
require(UtilsFolder.."/"..file)
end
end
local function CreateComponent(Dina, ComponentType, ...)
if not ComponentType then
return nil
end
local RComponent = Dina:require(ComponentType)
if RComponent then
local Component = RComponent.new(...)
if Dina.loadingstate then
if not Dina.statecomponents[Dina.loadingstate] then
Dina.statecomponents[Dina.loadingstate] = {}
end
table.insert(Dina.statecomponents[Dina.loadingstate], Component)
end
table.insert(Dina.components, Component)
-- On se crée une table contenant l'ID du composant et l'ID de la traduction
if Component.__name then
if Component.__name == "Text" then
Dina.translations[Component.id] = Component:getContent()
if Dina.translator then
if not Dina.currentlanguage then
Dina.currentlanguage = Dina.translator:getFirstLanguage()
end
local content = Component:getContent()
local key = Dina.translations[Component.id]
local translation = Dina.translator:getTranslation(Dina.currentlanguage, key)
if translation ~= nil and translation ~= content then
Component:setContent(translation)
end
end
end
end
return Component
end
print("ERROR: Component '"..tostring(ComponentType).."' not found.")
return nil
end
local function Initialize(self)
self.path = CurrentFolder
InitializeGlobalFunctions()
self.components = {}
self.states = {}
self.loadfcts = {}
self.statecomponents = {}
self.datas = {}
self.translations = {}
self.width = love.graphics.getWidth()
self.height = love.graphics.getHeight()
end
local function ToString()
return Dina._TITLE .. "\n" .. Dina._VERSION .. "\n" .. Dina._URL .. "\n" .. Dina._LICENSE
end
local function SearchDirItem(Item, Path)
local paths = love.filesystem.getDirectoryItems(Path)
for _, name in pairs(paths) do
if string.lower(name) == string.lower(Item) then
return Path .. "/" .. name
end
local filepath = SearchDirItem(Item, Path .. "/" .. name)
if filepath then
return filepath
end
end
return nil
end
--[[
proto Dina:draw(WithState)
.D This function starts the "draw" function of the current state or of each of the loaded components.
.P WithState
Indicates whether to take into account the current state (true by default) or not.
]]--
function Dina:draw(WithState)
if WithState == nil then
WithState = true
end
if WithState and self.state then
if self.states[self.state] then
if self.states[self.state].draw then
self.states[self.state]:draw()
end
end
else
for _, component in pairs(self.components) do
if component.draw then
component:draw()
end
end
end
end
--[[
proto Dina:update(dt, WithState)
.D This function starts the update function of the current state (if it has been defined). Otherwise, this function calls the update function of all loaded components.
.D For the translation:
.D If a language has been defined, the content of the Text components are translated using the datas in the language file if found; otherwise, the content remains the same.
.P dt
Delta time.
.P WithState
Indicates if the framework uses states or not. If true, launch the update function of the given state; otherwise, launch the update of the components.
]]--
local maxdt = 1/60
function Dina:update(dt, WithState)
dt = math.min(dt, maxdt)
local calculate = false
if WithState == nil then
WithState = true
end
if WithState and self.state then
if self.states[self.state] then
if self.states[self.state].update then
self.states[self.state]:update(dt)
end
end
else
for i = 1, #self.components do
local component = self.components[i]
if component then
if component.callbackZOrder then
component:callbackZOrder()
elseif component.isZOrderChanged then
if component:isZOrderChanged() == true then
calculate = true
end
end
if self.translator and component.__name == "Text" then
if not self.currentlanguage then
self.currentlanguage = self.translator:getFirstLanguage()
end
local content = component:getContent()
local key = self.translations[component.id]
local translation = self.translator:getTranslation(self.currentlanguage, key)
if translation ~= nil and translation ~= content then
component:setContent(translation)
end
end
if component.update then
component:update(dt)
end
end
end
end
if calculate then
SortTableByZOrder(self.components)
end
self:removeComponents()
if self.controller then
if self.controller.update then
self.controller:update(dt)
end
end
end
--[[
proto Dina:removeComponent(Component)
.D This function deletes the given component.
.P Component
Component to remove
]]--
function Dina:removeComponent(Component)
if Component == nil or type(Component) ~= "table" then return end
for item = #self.components, 1, -1 do
if self.components[item].id == Component.id then
table.remove(self.components, item)
end
end
end
--[[
proto Dina:removeComponents()
.D This function removes all components tag for removed.
]]--
function Dina:removeComponents()
for i = #self.components, 1, -1 do
if self.components[i].remove then
table.remove(self.components, i)
end
end
end
--[[
proto Dina:setGlobalValue(Name, Data)
.D This function allows to create a global data which can be used everywhere in the code. If the data already exists, it will be overwritten.
.P Name
Name of the data
.P Data
Value of the data
]]--
function Dina:setGlobalValue(Name, Data)
assert(IsStringValid(string.lower(Name)), "ERROR: Name must not be empty.")
self.datas[string.lower(Name)] = Data
end
--[[
proto Dina:getGlobalValue(Name)
.D This function allows to retrieve the value of a global data.
.P Name
Name of the data
.R Returns the value of the data if it exists; otherwise returns nil.
]]--
function Dina:getGlobalValue(Name)
return self.datas[string.lower(Name)]
end
--[[
proto Dina:addState(State, File, Load)
.D This function adds a new report with the supplied file. If the report already exists, no changes are made.
.P State
Name of the state.
.P File
Path and file name without extension.
.P Load
Name of the function to run when the state is loaded (by default, "load").
]]--
function Dina:addState(State, File, Load)
self:loadController()
if not self:isValidState(State) then
self.statecomponents[State] = {}
self.states[State] = require(File)
if Load == nil or Load == "" then Load = "load" end
local LoadFct = self.states[State][Load]
if LoadFct and type(LoadFct) == "function" then
self.loadfcts[State] = Load
end
end
end
--[[
proto Dina:removeState(State)
.D This function allows to remove a given state.
.P State
State to remove.
]]--
function Dina:removeState(State)
if self:isValidState(State) then
self.states[State] = nil
end
end
--[[
proto Dina:setState(State)
.D This function sets the current state and starts the stored Load function.
.P State
New state to define
]]--
function Dina:setState(State)
if self:isValidState(State) then
if self.controller then
self.controller:dissociate()
end
if self.state and self.state ~= self.oldstate then
for _, component in pairs(self.statecomponents[self.state]) do
self:removeComponent(component)
end
self.statecomponents[self.state] = {}
self.oldstate = self.state
end
self.state = State
self.loadingstate = State
local LoadFct = self.loadfcts[self.state]
self.states[State][LoadFct]()
self.loadingstate = nil
else
print(string.format("ERROR: Invalid state '%s'", State))
end
end
--[[
proto Dina:isValidState(State)
.D This function checks if the given state has already been defined (true) or not (false).
.P State
State to be checked.
.R Returns true if the state already exists; false otherwise.
]]--
function Dina:isValidState(State)
return self.states[State] and true or false
end
--[[
proto Dina:loadTranslator()
.D This function allows you to initialize the management of the translation.
]]--
function Dina:loadTranslator()
if not self.translator then
self.translator = CreateComponent(Dina, "TranslatorManager")
end
end
--[[
proto Dina:addLanguage(Language, File)
.D This function allows you to add a given language. The translation is in the given file.
.P Language
Language for the translation.
.P File
File containing the translations.
]]--
function Dina:addLanguage(Language, File)
if not self.translator then
self:loadTranslator()
end
self.translator:loadFile(Language, File)
end
--[[
proto Dina:setLanguage(Language)
.D This function sets the given language as the current language.
.P Language
Language to use for the translation.
]]--
function Dina:setLanguage(Language)
if self.translator:setLanguage(Language) then
self.currentlanguage = Language
else
print(string.format("ERROR: Invalid language '%s'", Language))
end
end
--[[
proto Dina:getCurrentLanguage()
.D This function returns the current language or nil if no language has been added.
.R Returns the current language (could be nil).
]]--
function Dina:getCurrentLanguage()
return self.currentlanguage
end
--[[
proto Dina:getNextLanguage()
.D This function returns the next language or nil if no language has been added.
.R Returns the next language (could be nil).
]]--
function Dina:getNextLanguage()
return self.translator:getNextLanguage()
end
--[[
proto Dina:translate(KeyText)
.D This function returns translation of the given key in the current language.
.R Returns the translation of the given key in the current language.
]]--
function Dina:translate(KeyText)
return self.translator:getTranslation(self.currentlanguage, KeyText)
end
--[[
proto Dina:loadController()
.D This function allows you to initialize the management of the controllers.
]]--
function Dina:loadController()
if not self.controller then
self.controller = CreateComponent(Dina, "Controller")
end
end
--[[
proto Dina:setActionKeys(Object, FctName, State, ...)
.D This function allows you to associate one or more keys with the given function.
.P Object
Object that must contain the function to be executed.
.P FctName
Name of the function to be executed.
.P Mode
Mode: 'pressed', 'released' or 'continuous'.
.P ...
List of keys that should trigger the execution of the function (see tutorials or examples for more details).
]]--
function Dina:setActionKeys(Object, FctName, Mode, ...)
assert(type(Object) == "table", "ERROR: invalid Object parameter.")
assert(type(Object[FctName]) == "function", "ERROR: invalid FctName parameter.")
Mode = string.lower(Mode)
assert(Mode == "pressed" or Mode == "released" or Mode == "continuous", "ERROR: invalid Mode parameter; must be 'pressed', 'released' or 'continuous'.")
if self.controller == nil then
self:loadController()
end
local UID = self.controller:associate(Object, FctName, Mode)
self.controller:setActionKeys(UID, ...)
end
--[[
proto Dina:resetActionKeys()
.D This function removes all action keys previously defined by setActionKeys.
]]--
function Dina:resetActionKeys()
if self.controller then
self.controller:dissociate()
end
end
-- System function
function Dina:require(Component)
local path = SearchDirItem(Component..".lua", self.path)
if path then
path = path:gsub('%.lua$', '')
return require(path)
end
return nil
end
Initialize(Dina)
Dina = setmetatable(Dina, Dina) -- DO NOT REMOVE
Dina.__call = function(...) return CreateComponent(...) end
Dina.__tostring = function() return ToString() end
return Dina