-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
408 lines (352 loc) · 14.1 KB
/
main.lua
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
-- Imports
local nuklear = require "nuklear"
log = require "libs/log"
-- Love2d callback: runs once when the project is loaded
function love.load()
log.info("Starting love2d window...")
love.window.setMode(960, 540)
icon = love.image.newImageData("icon.png")
love.window.setIcon(icon)
love.window.setTitle("Lovely Engine by Vinny v0.2")
log.info("Love2d window created successfully")
log.info("Checking if projects folder exists...")
os.execute("mkdir -p ./projects")
log.info("Starting nuklear...")
ui = nuklear.newUI()
log.info("Nuklear started successfully")
welcome = true
new = false
closed = true
projects = love.filesystem.getDirectoryItems("projects")
plugins = love.filesystem.getDirectoryItems("plugins")
newProject = {value = ""}
command = {value = ""}
projectDirectory = {}
openProject = ""
thumbnail = ""
output = ""
currentDirectory = ""
end
-- Function to refresh the projects table
function updateProjects()
log.info("Updating projects list...")
projects = love.filesystem.getDirectoryItems("projects")
projectDirectory = love.filesystem.getDirectoryItems("projects/" .. openProject)
log.info("Projects list updated")
end
-- Function to execute system commands and format the output
function execute(cmd, raw)
log.info("Executing system command...")
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
log.info("System command executed successfully")
return s
end
-- Function to create exported versions of the game using the installed modules
function package()
-- Create releases folder
log.info("Checking if releases folder exists...")
os.execute("mkdir -p ./projects/" .. openProject .. "/releases")
-- Create .love file
log.info("Creating game.love file...")
os.execute("cd ./projects/" .. openProject .. " && zip -9 -r game.love . > /dev/null && mv game.love ../../")
log.info("File game.love created successfully")
-- Create win32 release
if love.filesystem.getInfo("export/win32") ~= nil then
log.info("Found win32 export template, packaging...")
os.execute("cat ./export/win32/love.exe game.love > game.exe")
os.execute("cp -r ./export/win32/template win32")
os.execute("mv game.exe win32")
os.execute("zip -9 -r win32.zip win32 > /dev/null")
os.execute("rm -rf win32")
os.execute("mv win32.zip ./projects/" .. openProject .. "/releases")
log.info("Exported win32 and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: win32")
end
-- Create win64 release
if love.filesystem.getInfo("export/win64") ~= nil then
log.info("Found win64 export template, packaging...")
os.execute("cat ./export/win64/love.exe game.love > game.exe")
os.execute("cp -r ./export/win64/template win64")
os.execute("mv game.exe win64")
os.execute("zip -9 -r win64.zip win64 > /dev/null")
os.execute("rm -rf win64")
os.execute("mv win64.zip ./projects/" .. openProject .. "/releases")
log.info("Exported win64 and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: win64")
end
-- Create macos release
if love.filesystem.getInfo("export/macos") ~= nil then
log.info("Found macos export template, packaging...")
os.execute("cp -r ./export/macos/template game.app")
os.execute("cp game.love ./game.app/Contents/Resources")
os.execute("zip -9 -r -y macos.zip game.app > /dev/null")
os.execute("rm -rf game.app")
os.execute("mv macos.zip ./projects/" .. openProject .. "/releases")
log.info("Exported macos and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: macos")
end
-- Create linux32 release
if love.filesystem.getInfo("export/linux32") ~= nil then
log.info("Found linux32 export template, packaging...")
os.execute("cp -r ./export/linux32 linux32")
os.execute("cp game.love linux32")
os.execute("zip -9 -r linux32.zip linux32 > /dev/null")
os.execute("rm -rf linux32")
os.execute("mv linux32.zip ./projects/" .. openProject .. "/releases")
log.info("Exported linux32 and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: linux32")
end
-- Create linux64 release
if love.filesystem.getInfo("export/linux64") ~= nil then
log.info("Found linux64 export template, packaging...")
os.execute("cp -r ./export/linux64 linux64")
os.execute("cp game.love linux64")
os.execute("zip -9 -r linux64.zip linux64 > /dev/null")
os.execute("rm -rf linux64")
os.execute("mv linux64.zip ./projects/" .. openProject .. "/releases")
log.info("Exported linux64 and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: linux64")
end
-- Create web release
if love.filesystem.getInfo("export/web") ~= nil then
log.info("Found web export template, packaging...")
os.execute("./export/web/node ./export/web/index.js game.love game -c -t game")
os.execute("zip -9 -r web.zip game > /dev/null")
os.execute("rm -rf game")
os.execute("mv web.zip ./projects/" .. openProject .. "/releases")
log.info("Exported web and placed it in ./projects/" .. openProject .. "/releases")
else
log.warn("Export template not found: web")
end
-- Move game.love in releases folder
os.execute("mv game.love ./projects/" .. openProject .. "/releases")
log.info("Placed game.love in ./projects/" .. openProject .. "/releases")
end
-- Love2d callback: runs every frame
function love.update(dt)
ui:frameBegin()
-- Projects window
if ui:windowBegin("Projects", 0, 0, 200, 540, "border", "title", "scrollbar", "scroll auto hide") then
ui:layoutRow("dynamic", 50, 2)
if ui:button("New") then
new = true
log.info("Requested new project panel...")
end
if ui:button("Refresh") then
updateProjects()
end
if new == true then
ui:layoutRow("dynamic", 50, 1)
ui:spacing(1)
ui:label("Enter name:")
ui:edit("simple", newProject)
if ui:button("Create") then
os.execute("cp -r template ./projects/" .. newProject["value"])
log.info("Created new project named " .. newProject["value"])
newProject["value"] = ""
new = false
updateProjects()
log.info("Closed new project panel")
end
if ui:button("Cancel") then
new = false
newProject["value"] = ""
log.info("Closed new project panel")
end
end
ui:spacing(1)
ui:layoutRow("dynamic", 50, 1)
ui:label("Existing projects:")
for k, v in pairs(projects) do
if ui:button(v) then
openProject = v
if love.filesystem.getInfo("projects/" .. openProject .. "/thumbnail.png") ~= nil then
thumbnail = love.graphics.newImage("projects/" .. openProject .. "/thumbnail.png")
log.info("Thumbnail found")
else
if love.filesystem.getInfo("projects/" .. openProject .. "/default.png") ~= nil then
thumbnail = love.graphics.newImage("projects/" .. openProject .. "/default.png")
log.warn("Thumbnail not found, loaded default one")
else
log.error("Thumbnail default.png not found")
end
end
projectDirectory = love.filesystem.getDirectoryItems("projects/" .. openProject)
log.info("Opened project " .. v)
end
end
end
ui:windowEnd()
-- Check if the welcome page needs to be displayed
if openProject == "" then
welcome = true
else
welcome = false
end
-- If yes create welcome window, else create the project window
if welcome == true then
if ui:windowBegin("Welcome", 200, 0, 560, 540, "border", "background") then
ui:layoutRow("dynamic", 50, 1)
ui:spacing(4)
ui:label("Welcome to Lovely Engine! To start open or create a new project :)", "centered")
end
ui:windowEnd()
else
if ui:windowBegin("Project", 200, 0, 560, 540, "border", "background", "scrollbar", "scroll auto hide") then
ui:layoutRow("dynamic", 50, 1)
ui:label(openProject, "centered")
if thumbnail ~= "" then
ui:layoutRow("dynamic", 300, 1)
ui:image(thumbnail)
end
ui:layoutRow("dynamic", 50, 2)
ui:spacing(1)
ui:spacing(1)
if ui:button("Run") then
log.info("Running " .. openProject .. "...")
os.execute("./love ./projects/" .. openProject)
log.info("Finished running " .. openProject)
end
if ui:button("Delete") then
os.execute("rm -rf ./projects/" .. openProject)
openProject = ""
updateProjects()
ui:windowSetScroll(0, 0)
projectDirectory = {}
log.info("Deleted " .. openProject)
end
if ui:button("Package") then
log.info("Starting packaging routine...")
package()
log.info("Finished packaging")
end
if ui:button("Close") then
log.info("Closed " .. openProject)
openProject = ""
ui:windowSetScroll(0, 0)
projectDirectory = {}
end
ui:spacing(1)
ui:spacing(1)
end
ui:windowEnd()
end
-- Project Directory window
if ui:windowBegin("Project Directory", 760, 0, 200, 270, "border", "title", "scrollbar", "scroll auto hide") then
ui:layoutRow("dynamic", 25, 1)
if ui:button("Back to top") then
currentDirectory = ""
projectDirectory = love.filesystem.getDirectoryItems("projects/" .. openProject)
log.info("Reset directory path")
end
ui:spacing(1)
for k, v in pairs(projectDirectory) do
if ui:button(v) then
currentDirectory = currentDirectory .. v .. "/"
projectDirectory = love.filesystem.getDirectoryItems("projects/" .. openProject .. "/" .. currentDirectory)
log.info("Opened directory projects/" .. openProject .. "/" .. currentDirectory)
end
end
end
ui:windowEnd()
-- Plugins window
if ui:windowBegin("Plugins", 760, 270, 200, 270, "border", "title", "scrollbar", "scroll auto hide") then
ui:layoutRow("dynamic", 50, 1)
for k, v in pairs(plugins) do
filename = v:gsub("%.lua", "")
if ui:button(filename) then
require("plugins/" .. filename).start()
log.info("Executed plugin " .. v)
end
end
end
ui:windowEnd()
-- If closed is true create open terminal window, else create the terminal and input windows
if closed == true then
if ui:windowBegin("Open Terminal", 200, 478, 560, 62, "border") then
ui:layoutRow("dynamic", 50, 8)
ui:label("Terminal")
ui:spacing(1)
ui:spacing(1)
ui:spacing(1)
ui:spacing(1)
ui:spacing(1)
ui:spacing(1)
if ui:button("+") then
closed = false
log.info("Opened terminal")
end
end
ui:windowEnd()
else
if ui:windowBegin("Terminal", 200, 340, 560, 138, "border", "title") then
ui:layoutRow("dynamic", 50, 1)
ui:label(output)
end
ui:windowEnd()
if ui:windowBegin("Input", 200, 478, 560, 62, "border") then
ui:layoutRow("dynamic", 50, 4)
if ui:button("Close") then
closed = true
log.info("Closed terminal")
end
if ui:button("Clear") then
output = ""
log.info("Cleared terminal output")
end
ui:edit("simple", command)
if ui:button("Enter") then
output = execute(command["value"], false)
if output == "" then
output = "No ouput for this command"
end
log.info("Executed command " .. command["value"])
command["value"] = ""
end
end
ui:windowEnd()
end
ui:frameEnd()
end
-- Love2d callback: runs every frame and is used to draw things on screen
function love.draw()
ui:draw()
end
-- Love2d callbacks used by nuklear to handle input
function love.keypressed(key, scancode, isrepeat)
ui:keypressed(key, scancode, isrepeat)
log.debug("NUKLEAR: Key " .. key .. " pressed")
end
function love.keyreleased(key, scancode)
ui:keyreleased(key, scancode)
log.debug("NUKLEAR: Key " .. key .. " released")
end
function love.mousepressed(x, y, button, istouch, presses)
ui:mousepressed(x, y, button, istouch, presses)
log.debug("NUKLEAR: Mouse button " .. button .. " pressed")
end
function love.mousereleased(x, y, button, istouch, presses)
ui:mousereleased(x, y, button, istouch, presses)
log.debug("NUKLEAR: Mouse button " .. button .. " released")
end
function love.mousemoved(x, y, dx, dy, istouch)
ui:mousemoved(x, y, dx, dy, istouch)
end
function love.textinput(text)
ui:textinput(text)
log.debug("NUKLEAR: Text input")
end
function love.wheelmoved(x, y)
ui:wheelmoved(x, y)
end