-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
314 lines (250 loc) · 7.18 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
-----------------------
--- 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(data.updatespeed) end
end
end
---------------
--- ON LOAD ---
---------------
function love.load()
-- Set user-input to false, for duration of loading
love.keyboard.setTextInput(false)
MIDI = require('midi/MIDI')
socket = require('socket')
vstruct = require('vstruct')
pack = vstruct.pack
upack = vstruct.unpack
datafuncs = require('funcs/data-funcs')
filefuncs = require('funcs/file-funcs')
generatorfuncs = require('funcs/generator-funcs')
guigridfuncs = require('gui/gui-grid-funcs')
guiloadingfuncs = require('gui/gui-loading-funcs')
guimiscfuncs = require('gui/gui-misc-funcs')
guinotefuncs = require('gui/gui-note-funcs')
guisidebarfuncs = require('gui/gui-sidebar-funcs')
keyfuncs = require('funcs/key-funcs')
modefuncs = require('funcs/mode-funcs')
mousefuncs = require('funcs/mouse-funcs')
notefuncs = require('funcs/note-funcs')
oscfuncs = require('funcs/osc-funcs')
playfuncs = require('funcs/play-funcs')
pointerfuncs = require('funcs/pointer-funcs')
selectfuncs = require('funcs/select-funcs')
undofuncs = require('funcs/undo-funcs')
utilfuncs = require('funcs/util-funcs')
wheelfuncs = require('funcs/wheel-funcs')
data = require('data/data-table')
utilfuncs.tableToNewContext(
_G,
datafuncs,
filefuncs,
generatorfuncs,
guigridfuncs,
guiloadingfuncs,
guimiscfuncs,
guinotefuncs,
guisidebarfuncs,
keyfuncs,
modefuncs,
mousefuncs,
notefuncs,
oscfuncs,
playfuncs,
pointerfuncs,
selectfuncs,
undofuncs,
utilfuncs,
wheelfuncs
)
local defaultprefs, _ = love.filesystem.read('prefs-table.lua')
-- If the userprefs file doesn't exist, create it in the savefile folder,
-- require it like a regular module, and then add it to data-table context.
if not love.filesystem.exists("userprefs.lua") then
local uf = love.filesystem.newFile("userprefs.lua")
uf:open('w')
uf:write(defaultprefs)
uf:close()
prefs = require('prefs-table')
else -- If userprefs exist, simply require them.
prefs = require('userprefs')
end
-- Put the prefs into the data object
tableToNewContext(data, prefs)
-- If the savepath exists, enable saving
local pathf = io.open(data.savepath .. "sect_filepath_test.txt", "w")
if pathf ~= nil then
data.saveok = true
pathf:close()
end
-- Preload all complex GUI elements
preloadFonts()
preloadImages()
preloadCursors()
-- Load the mousemove-inactive cursor
love.mouse.setCursor(data.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(
data.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 data..."},
}
)
else -- Else, if combinatoric tables exist, load them
tableCombine(
data.loadcmds,
{
{{"loadScalesAndWheels"}, "Loading scale and wheel data..."},
}
)
end
-- Enable keyboard commands after completing all other load-funcs
tableCombine(
data.loadcmds,
{
{{"setupUDP"}, "Setting up OSC-UDP apparatus..."},
{{"buttonsToPianoKeys", data.pianokeys}, "Assigning computer-piano keys..."},
{{"buildHotseatCommands"}, "Building hotseat commands..."},
{{"sortKeyComboTables"}, "Sorting key-command tables..."},
}
)
end
-----------------
--- ON UPDATE ---
-----------------
function love.update(dt)
-- If Play Mode is active, iterate by one tick
if data.playing then
iteratePlayMode(dt)
end
end
---------------
--- ON DRAW ---
---------------
function love.draw()
-- Get window dimensions
local width, height = love.graphics.getDimensions()
-- If still loading, render a loading screen
if data.loading then
executeLoadingFuncAndDraw(canvas, width, height)
return nil
end
-- Update the piano-bar width, based on window width
data.pianowidth = data.size.piano.basewidth + (width / 50)
-- If the mouse is being dragged, check drag boundaries
if data.dragging then
-- Get positioning vars
local left = data.size.sidebar.width
local top = 0
local pianoleft = left + (data.pianowidth / 2)
local middle = height - data.size.botbar.height
-- Get the mouse's concrete position
local x, y = love.mouse.getPosition()
-- Check the cursor for dragging behavior
checkMouseDrag(
pianoleft, top,
width, middle,
x - pianoleft, y - top
)
end
-- Build the GUI
buildGUI(width, height)
end
----------------------
--- ON MOUSE PRESS ---
----------------------
function love.mousepressed(x, y, button)
-- 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)
-- 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 data.loading then
return nil
end
key = tostring(key)
addKeystroke(key, isrepeat)
end
----------------------
--- ON KEY RELEASE ---
----------------------
function love.keyreleased(key)
removeKeystroke(key)
end