-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
458 lines (368 loc) · 10.7 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
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
-----------------------
--- CUSTOM RUN-LOOP ---
-----------------------
function love.run()
if love.math then
love.math.setRandomSeed(os.time())
end
if love.event then
love.event.pump()
end
if love.load then love.load(arg) end
if love.timer then love.timer.step() end
local dt = 0
while true do
if love.event then
love.event.pump()
for e,a,b,c,d in love.event.poll() do
if e == "quit" then
if not love.quit or not love.quit() then
if love.audio then
love.audio.stop()
end
return
end
end
love.handlers[e](a,b,c,d)
end
end
if love.timer then
love.timer.step()
dt = love.timer.getDelta()
end
if love.update then love.update(dt) end
if love.window and love.graphics and love.window.isCreated() then
love.graphics.clear()
love.graphics.origin()
if love.draw then love.draw() end
love.graphics.present()
end
if love.timer then love.timer.sleep(D.updatespeed) end
end
end
---------------
--- ON LOAD ---
---------------
function love.load()
-- Set text-input commands to false
love.keyboard.setTextInput(false)
MIDI = require('midi/MIDI')
socket = require('socket')
cmdfuncs = require('funcs/cmd-funcs')
datafuncs = require('funcs/data-funcs')
factorfuncs = require('funcs/factor-funcs')
filefuncs = require('funcs/file-funcs')
generatorfuncs = require('funcs/generator-funcs')
guiloadingfuncs = require('gui/gui-loading-funcs')
guimiscfuncs = require('gui/gui-misc-funcs')
guinotefuncs = require('gui/gui-note-funcs')
guipianofuncs = require('gui/gui-piano-funcs')
guireticulefuncs = require('gui/gui-reticule-funcs')
guisaveloadfuncs = require('gui/gui-saveload-funcs')
guiselectfuncs = require('gui/gui-select-funcs')
guiseqfuncs = require('gui/gui-seq-funcs')
guisidebarfuncs = require('gui/gui-sidebar-funcs')
guitrackfuncs = require('gui/gui-track-funcs')
indexfuncs = require('funcs/index-funcs')
keyfuncs = require('funcs/key-funcs')
modefuncs = require('funcs/mode-funcs')
modifyfuncs = require('funcs/modify-funcs')
mousefuncs = require('funcs/mouse-funcs')
notefuncs = require('funcs/note-funcs')
playfuncs = require('funcs/play-funcs')
pointerfuncs = require('funcs/pointer-funcs')
selectfuncs = require('funcs/select-funcs')
socketfuncs = require('funcs/socket-funcs')
undofuncs = require('funcs/undo-funcs')
utilfuncs = require('funcs/util-funcs')
wheelfuncs = require('funcs/wheel-funcs')
D = require('data/data-table')
utilfuncs.tableToNewContext(
_G,
cmdfuncs,
datafuncs,
factorfuncs,
filefuncs,
generatorfuncs,
guiloadingfuncs,
guimiscfuncs,
guinotefuncs,
guipianofuncs,
guireticulefuncs,
guisaveloadfuncs,
guiselectfuncs,
guiseqfuncs,
guisidebarfuncs,
guitrackfuncs,
indexfuncs,
keyfuncs,
modefuncs,
modifyfuncs,
mousefuncs,
notefuncs,
playfuncs,
pointerfuncs,
selectfuncs,
socketfuncs,
undofuncs,
utilfuncs,
wheelfuncs
)
-- If a version.lua exists, grab its version-number contents.
-- Else, if there's no version.lua, create one with the current version.
local version = false
if love.filesystem.exists("version.lua") then
local v = require('version')
version = v.version
else
local vf = love.filesystem.newFile("version.lua")
vf:open('w')
vf:write("return { version = \"" .. D.version .. "\" }")
vf:close()
end
-- Get the default prefs data, and set it as a global table
prefs = require('prefs-table')
local preftext, _ = love.filesystem.read('prefs-table.lua')
-- If the userprefs file doesn't exist, create it in the savefile folder
if not love.filesystem.exists("userprefs.lua") then
-- Write this version's default prefs to userprefs.lua
local uf = love.filesystem.newFile("userprefs.lua")
uf:open('w')
uf:write(preftext)
uf:close()
else -- If userprefs exist, require them, and compare versions.
local userprefs = require('userprefs')
local uptext, _ = love.filesystem.read('userprefs.lua')
-- If userprefs contain no version number,
-- or are from an old version, then replace them.
if (not version) or (D.version ~= version) then
-- Write outdated prefs to an "oldprefs.lua" file, with version number
local oldf = love.filesystem.newFile("oldprefs-" .. (version or "old") .. ".lua")
oldf:open('w')
oldf:write(uptext)
oldf:close()
-- Update the local version.lua with the current version number
local vf = love.filesystem.newFile("version.lua")
vf:open('w')
vf:write("return { version = \"" .. D.version .. "\" }")
vf:close()
-- Write this version's default prefs to userprefs.lua
local uf = love.filesystem.newFile("userprefs.lua")
uf:open('w')
uf:write(preftext)
uf:close()
else -- Else if userprefs were the right version, overwrite prefs with them
prefs = deepCopy(userprefs)
end
end
-- Put the prefs into the data object
tableToNewContext(D, prefs)
-- Check whether the savepath exists
checkUserSavePath()
-- Initialize a global Love canvas
local width, height = love.graphics.getDimensions()
canvas = love.graphics.newCanvas(width, height)
love.graphics.setCanvas(canvas)
-- Preload all complex GUI elements
preloadFonts()
preloadImages()
preloadCursors()
preloadGradients()
-- Load the mousemove-inactive cursor
love.mouse.setCursor(D.cursor.default.c)
-- Get a new time-based random-seed for the entire session
math.randomseed(os.time())
-- Initialize GUI miscellany
love.graphics.setLineStyle("rough")
love.graphics.setLineWidth(1)
love.keyboard.setKeyRepeat(true)
-- If combinatoric data tables don't exist, generate and store them
if (not love.filesystem.exists("scales.lua"))
or (not love.filesystem.exists("wheels.lua"))
then
-- Append combinatoric generation commands to loading-cmd table
tableCombine(
D.loadcmds,
{
{{"buildDataScales"}, "Building scales..."},
{{"anonymizeScaleKeys"}, "Indexing scales..."},
{{"purgeIdenticalScales"}, "Purging identical scales..."},
{{"rotateScalesToFilledPosition"}, "Rotating scales..."},
{{"buildIntervalSpectrum"}, "Building interval spectra..."},
{{"indexScalesByNoteQuantity"}, "Indexing scales by k-species..."},
{{"purgeEmptyScales"}, "Purging empty scales..."},
{{"buildConsonanceRatings"}, "Building consonance ratings..."},
{{"buildWheels"}, "Building wheels..."},
{{"saveScalesAndWheels"}, "Saving scale and wheel D..."},
}
)
else -- Else, if combinatoric tables exist, load them
tableCombine(
D.loadcmds,
{
{{"loadScalesAndWheels"}, "Loading scale and wheel D..."},
}
)
end
-- Enable keyboard commands after completing all other load-funcs
tableCombine(
D.loadcmds,
{
{{"setupUDP"}, "Setting up MIDI-over-UDP apparatus..."},
{{"buildPianoKeyCommands", D.pianokeys}, "Assigning computer-piano keys..."},
{{"buildHotseatCommands"}, "Building hotseat commands..."},
{{"sortKeyComboTables"}, "Sorting key-command tables..."},
}
)
end
-----------------
--- ON UPDATE ---
-----------------
function love.update(dt)
-- If still loading, abort function
if D.loading then
return nil
end
-- Check for incoming MIDI commands
repeat
local d, msg = D.udpin:receive()
if d then
getMidiMessage(d) -- Send incoming MIDI to a parsing function
end
until not d
-- If Play Mode is active, iterate by one tick
if D.playing then
iteratePlayMode(dt)
end
-- If rebuild has been flagged, rebuild the visible GUI elements accordingly, to be rendered on-draw-frame
if D.rebuild then
D.rebuild = false
D.redraw = true
buildGUI()
end
end
---------------
--- ON DRAW ---
---------------
function love.draw()
-- Get window dimensions
local width, height = love.graphics.getDimensions()
-- If still loading, render a loading screen
if D.loading then
executeLoadingFuncAndDraw(width, height)
return nil
end
-- If width or height has been resized, reset width/height/canvas, and flag a redraw
if (width ~= D.width) or (height ~= D.height) then
D.rebuild = true
D.width = width
D.height = height
canvas = love.graphics.newCanvas(D.width, D.height)
end
-- If the mouse is being dragged, check drag boundaries
if D.dragging then
-- Get positioning vars
local left = D.size.sidebar.width
local top = 0
local middle = height - D.size.track.height
-- Get the mouse's concrete position
local x, y = love.mouse.getPosition()
-- Check the cursor for dragging behavior
checkMouseDrag(
left, top,
width, middle,
x - left, y - top
)
end
-- If redraw is flagged, redraw the pre-built GUI items to canvas
if D.redraw then
D.redraw = false
drawGUI()
end
-- Draw canvas to screen
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(canvas, 0, 0)
degradeSavePopup() -- Gradually degrade any active save-popup's activity
drawSavePopup() -- Redraw the save-popup on top of itself
end
----------------------
--- ON MOUSE PRESS ---
----------------------
function love.mousepressed(x, y, button)
-- Ignore mouse activity in Saveload Mode
if D.cmdmode == "saveload" then
return nil
end
-- Get window dimensions
local width, height = love.graphics.getDimensions()
mouseCursorChange(button, true)
if (button == 'l')
or (button == 'r')
then -- Call the mouse-picking function
mousePick(x, y, width, height, button)
elseif button == 'wd' then -- Shift tick-zoom down
shiftInternalValue("cellwidth", false, -0.25)
elseif button == 'wu' then -- Shit tick-zoom up
shiftInternalValue("cellwidth", false, 0.25)
end
end
------------------------
--- ON MOUSE RELEASE ---
------------------------
function love.mousereleased(x, y, button)
-- Ignore mouse activity in Saveload Mode
if D.cmdmode == "saveload" then
return nil
end
-- Set the mouse cursor back to its default appearance
if (button == 'l') or (button == 'r') then
mouseCursorChange(button, false)
end
end
--------------------
--- ON KEY PRESS ---
--------------------
function love.keypressed(key, isrepeat)
-- If still on the loading screen, do nothing
if D.loading then
return nil
end
key = tostring(key)
addKeystroke(key, isrepeat)
end
----------------------
--- ON KEY RELEASE ---
----------------------
function love.keyreleased(key)
-- If still on the loading screen, do nothing
if D.loading then
return nil
end
removeKeystroke(key)
end
---------------------
--- ON TEXT-INPUT ---
---------------------
function love.textinput(t)
-- If any commands are being chorded with ctrl or tab, abort function
for k, v in pairs(D.keys) do
if (v == "ctrl") or (v == "tab") then
return nil
end
end
D.rebuild = true
if t == "backspace" then
removeSaveChar(-1)
elseif t == "delete" then
removeSaveChar(1)
else
addSaveChar(t)
end
end
-----------------------
--- ON FOCUS CHANGE ---
-----------------------
function love.focus(f)
-- If any keys are held down during focus-change, remove their keystrokes
removeAllKeystrokes()
end