This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
code.lua
executable file
·1171 lines (768 loc) · 30 KB
/
code.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Pixel Vision 8 - SFX Tool
Copyright (C) 2017, Pixel Vision 8 (http://pixelvision8.com)
Created by Jesse Freeman (@jessefreeman)
Please do not copy and distribute verbatim copies
of this license document, but modifications without
distributing is allowed.
]]--
-- API Bridge
LoadScript("sb-sprites")
LoadScript("pixel-vision-os-v2")
LoadScript("pixel-vision-os-sound-progress-modal-v1")
local toolName = "Sound Editor"
local success = false
local playSound = false
local originalSounds = {}
local canExport = ProgressModal ~= nil
-- local soundHistory = {}
local knobData = {
{name = "Volume", x = 40, y = 88, propID = 24, range = 1, toolTip = "Volume is set to "},
-- Envelope
{name = "AttackTime", x = 80, y = 88, propID = 2, range = 1, toolTip = "Attack Time is set to ", disabledOffset = 28},
{name = "SustainTime", x = 104, y = 88, propID = 3, range = 1, toolTip = "Sustain Time is set to ", disabledOffset = 28},
{name = "SustainPunch", x = 128, y = 88, propID = 4, range = 1, toolTip = "Sustain Punch is set to ", disabledOffset = 28},
{name = "DecayTime", x = 152, y = 88, propID = 5, range = 1, toolTip = "Decay Time is set to ", disabledOffset = 28},
-- Frequency
{name = "StartFrequency", x = 192, y = 88, propID = 6, range = 1, toolTip = "Start Frequency is set to ", disabledOffset = 28},
{name = "MinFrequency", x = 216, y = 88, propID = 7, range = 1, toolTip = "Minimum Frequency is set to ", disabledOffset = 28},
-- Slide
{name = "Slide", x = 16, y = 128, propID = 8, range = 2, toolTip = "Slide is set to "},
{name = "DeltaSlide", x = 40, y = 128, propID = 9, range = 2, toolTip = "Delta Slide is set to "},
-- Vibrato
{name = "VibratoDepth", x = 72, y = 128, propID = 10, range = 1, toolTip = "Vibrato Depth is set to "},
{name = "VibratoSpeed", x = 96, y = 128, propID = 11, range = 1, toolTip = "Vibrato Speed is set to "},
-- Harmonics
{name = "OverTones", x = 128, y = 128, propID = 12, range = 1, toolTip = "Over Tones is set to "},
{name = "OverTonesFalloff", x = 152, y = 128, propID = 13, range = 1, toolTip = "Over Tones Falloff is set to "},
-- Square Wave
{name = "SquareDuty", x = 192, y = 128, propID = 14, range = 1, toolTip = "Square Duty is set to ", disabledOffset = 32},
{name = "DutySweep", x = 216, y = 128, propID = 15, range = 2, toolTip = "Duty Sweep is set to ", disabledOffset = 32},
-- Repeat
{name = "RepeatSpeed", x = 72, y = 168, propID = 16, range = 1, toolTip = "Repeat Speed is set to "},
-- Phaser
{name = "PhaserOffset", x = 16, y = 168, propID = 17, range = 2, toolTip = "Phaser Offset is set to "},
{name = "PhaserSweep", x = 40, y = 168, propID = 18, range = 2, toolTip = "Phaser Sweep is set to "},
-- LP Filter
{name = "LPFilterCutoff", x = 104, y = 168, propID = 19, range = 1, toolTip = "LP Filter Cutoff is set to "},
{name = "LPFilterCutoffSweep", x = 128, y = 168, propID = 20, range = 2, toolTip = "LP Filter Cutoff Sweep is set to "},
{name = "LPFilterResonance", x = 152, y = 168, propID = 21, range = 1, toolTip = "LP Filter Resonance is set to "},
-- HP Filter
{name = HPFilterCutoff, x = 192, y = 168, propID = 22, range = 1, toolTip = "HP Filter Cutoff is set to "},
{name = HPFilterCutoffSweep, x = 216, y = 168, propID = 23, range = 2, toolTip = "HP Filter Cutoff Sweep is set to "},
}
local sfxButtonData = {
{name = "pickup", spriteName = "sfxbutton1", x = 8, y = 40, toolTip = "Create a randomized 'pickup' or coin sound effect."},
{name = "explosion", spriteName = "sfxbutton2", x = 24, y = 40, toolTip = "Create a randomized 'explosion' sound effect."},
{name = "powerup", spriteName = "sfxbutton3", x = 40, y = 40, toolTip = "Create a randomized 'power-up' sound effect."},
{name = "shoot", spriteName = "sfxbutton4", x = 56, y = 40, toolTip = "Create a randomized 'laser' or 'shoot' sound effect."},
{name = "jump", spriteName = "sfxbutton5", x = 72, y = 40, toolTip = "Create a randomized 'jump' sound effect."},
{name = "hurt", spriteName = "sfxbutton6", x = 88, y = 40, toolTip = "Create a randomized 'hit' or 'hurt' sound effect."},
{name = "select", spriteName = "sfxbutton7", x = 104, y = 40, toolTip = "Create a randomized 'blip' or 'select' sound effect."},
{name = "random", spriteName = "sfxbutton8", x = 120, y = 40, toolTip = "Create a completely random sound effect."},
{name = "melody", spriteName = "instrumentbutton1", x = 8, y = 56, toolTip = "Create a 'melody' instrument sound effect."},
{name = "harmony", spriteName = "instrumentbutton2", x = 24, y = 56, toolTip = "Create a 'harmony' instrument sound effect."},
{name = "bass", spriteName = "instrumentbutton3", x = 40, y = 56, toolTip = "Create a 'bass' instrument sound effect."},
{name = "pad", spriteName = "instrumentbutton4", x = 56, y = 56, toolTip = "Create a 'pad' instrument sound effect."},
{name = "lead", spriteName = "instrumentbutton5", x = 72, y = 56, toolTip = "Create a 'lead' instrument sound effect."},
{name = "drums", spriteName = "instrumentbutton6", x = 88, y = 56, toolTip = "Create a 'drums' instrument sound effect."},
{name = "snare", spriteName = "instrumentbutton7", x = 104, y = 56, toolTip = "Create a 'snare' instrument sound effect."},
{name = "kick", spriteName = "instrumentbutton8", x = 120, y = 56, toolTip = "Create a 'kick' instrument sound effect."}
}
local waveButtonData = {
{name = "Template1", spriteName = "wavebutton0", x = 112 - 24, y = 200, waveID = 0, toolTip = "Wave type square."},
{name = "Template2", spriteName = "wavebutton1", x = 120, y = 200, waveID = 1, toolTip = "Wave type saw."},
{name = "Template3", spriteName = "wavebutton3", x = 152, y = 200, waveID = 3, toolTip = "Wave type noise."},
{name = "Template4", spriteName = "wavebutton4", x = 184, y = 200, waveID = 4, toolTip = "Wave type triangle."},
{name = "Template5", spriteName = "wavebutton5", x = 216, y = 200, waveID = 5, toolTip = "Load wav sample file."},
}
local validWaves = {
"!\"", -- any
"%&", -- square
":;", -- saw tooth
-- "'(", -- sine (Not enabled by default)
")*", -- noise
"<=", -- triangle
"./" -- wave
}
local waveTypeIDs = {
-1,
0,
1,
-- 2,
3,
4,
5
}
local SaveShortcut, UndoShortcut, RedoShortcut, CopyShortcut, PasteShortcut = 4, 9, 10, 11, 12
local currentID = 0
function InvalidateData()
-- Only everything if it needs to be
if(invalid == true)then
return
end
pixelVisionOS:ChangeTitle(toolTitle .."*", "toolbariconfile")
pixelVisionOS:EnableMenuItem(SaveShortcut, true)
invalid = true
end
function ResetDataValidation()
-- Only everything if it needs to be
if(invalid == false)then
return
end
pixelVisionOS:ChangeTitle(toolTitle, "toolbariconfile")
invalid = false
pixelVisionOS:EnableMenuItem(SaveShortcut, false)
end
function Init()
BackgroundColor(5)
-- Disable the back key in this tool
EnableBackKey(false)
-- Create an global instance of the Pixel Vision OS
_G["pixelVisionOS"] = PixelVisionOS:Init()
-- Reset the undo history so it's ready for the tool
-- TODO restart undo history here
-- pixelVisionOS:ResetUndoHistory()
-- Get a reference to the Editor UI
-- editorUI = pixelVisionOS.editorUI
rootDirectory = ReadMetadata("directory", nil)
if(rootDirectory ~= nil) then
-- Load only the game data we really need
success = gameEditor.Load(rootDirectory, {SaveFlags.System, SaveFlags.Sounds})
end
if(success == true) then
local pathSplit = string.split(rootDirectory, "/")
-- Update title with file path
toolTitle = pathSplit[#pathSplit] .. "/sounds.json"
-- Get the game name we are editing
-- pixelVisionOS:ChangeTitle(toolTitle)
local menuOptions =
{
-- About ID 1
{name = "About", action = function() pixelVisionOS:ShowAboutModal(toolName) end, toolTip = "Learn about PV8."},
{divider = true},
{name = "New", action = OnNewSound, key = Keys.N, toolTip = "Revert the sound to empty."}, -- Reset all the values
{name = "Save", action = OnSave, key = Keys.S, toolTip = "Save changes made to the sounds file."}, -- Reset all the values
{name = "Export SFX", action = function() OnExport(currentID, true) end, key = Keys.E, enabled = canExport, toolTip = "Create a wav for the current SFX file."}, -- Reset all the values
{name = "Export All", action = OnExportAll, enabled = canExport, toolTip = "Export all sound effects to wavs."}, -- Reset all the values
{name = "Revert", action = nil, key = Keys.R, enabled = false, toolTip = "Revert the sounds.json file to its previous state."}, -- Reset all the values
{divider = true},
{name = "Undo", action = OnUndo, enabled = false, key = Keys.Z, toolTip = "Undo the last action."}, -- Reset all the values
{name = "Redo", action = OnRedo, enabled = false, key = Keys.Y, toolTip = "Redo the last undo."}, -- Reset all the values
{name = "Copy", action = OnCopySound, key = Keys.C, toolTip = "Copy the currently selected sound."}, -- Reset all the values
{name = "Paste", action = OnPasteSound, key = Keys.V, enabled = false, toolTip = "Paste the last copied sound."}, -- Reset all the values
{name = "Mutate", action = OnMutate, key = Keys.M, toolTip = "Mutate the sound to produce random variations."}, -- Reset all the values
{divider = true},
{name = "Quit", key = Keys.Q, action = OnQuit, toolTip = "Quit the current game."}, -- Quit the current game
}
if(PathExists(NewWorkspacePath(rootDirectory).AppendFile("code.lua"))) then
table.insert(menuOptions, #menuOptions, {name = "Run Game", action = OnRunGame, key = Keys.R, toolTip = "Run the code for this game."})
end
pixelVisionOS:CreateTitleBarMenu(menuOptions, "See menu options for this tool.")
-- Get the total number of songs
totalSounds = gameEditor:TotalSounds()
totalChannels = gameEditor:TotalChannels()
-- TODO need to replace this with the new number stepper component
local textColorOffset = 52
soundIDStepper = editorUI:CreateNumberStepper({x = 32, y = 16}, 16, 0, 0, totalSounds - 1, "top", "Sound ID value.", textColorOffset)
soundIDStepper.onInputAction = OnChangeSoundID
songNameFieldData = editorUI:CreateInputField({x = 88, y = 24, w = 80}, "Untitled", "Change the label of the selected sound.", "name", nil, textColorOffset)
songNameFieldData.onAction = OnChangeName
channelIDStepper = editorUI:CreateNumberStepper({x = 176, y = 16}, 8, - 1, 0, tostring(gameEditor:TotalChannels()) - 1, "top", "The channel sound effects will be previewed on.", textColorOffset)
channelIDStepper.onInputAction = OnChangeChannelID
waveInputField = editorUI:CreateInputField({x = 224, y = 24, w = 16}, validWaves[1], "Wave type.", nil, nil, textColorOffset)
waveInputField.editable = false
-- Create buttons
totalKnobs = #knobData
for i = 1, totalKnobs do
local data = knobData[i]
data.knobUI = editorUI:CreateKnob({x = data.x, y = data.y, w = 24, h = 24}, "knob", "Change the volume.")
data.knobUI.type = data.name
if(data.disabledOffset ~= nil) then
data.knobUI.colorOffsetDisabled = data.disabledOffset
end
data.knobUI.onAction = function(value)
local type = data.name
local propID = data.propID
-- Calculate new value based on range
local newValue = (data.range * value) - (data.range - 1)
UpdateLoadedSFX(propID, newValue)
UpdateKnobTooltip(data, value)
end
end
totalSFXButtons = #sfxButtonData
for i = 1, totalSFXButtons do
local data = sfxButtonData[i]
-- TODO need to build sprite tables for each state
data.buttonUI = editorUI:CreateButton({x = data.x, y = data.y}, data.spriteName, data.toolTip)
data.buttonUI.onAction = function()
-- print("Click")
OnSFXAction(data.name)
end
end
waveGroupData = editorUI:CreateToggleGroup(true)
waveGroupData.onAction = function(value)
-- print("Select Wave Button", value)
OnChangeWave(value)
--TODO refresh wave buttons
-- TODO save wave data
end
totalWaveButtons = #waveButtonData
for i = 1, totalWaveButtons do
local data = waveButtonData[i]
-- TODO need to build sprite tables for each state
editorUI:ToggleGroupButton(waveGroupData, {x = data.x, y = data.y}, data.spriteName, data.toolTip)
end
playButton = editorUI:CreateButton({x = 8, y = 16}, "playbutton", "Play the current sound.")
playButton.onAction = OnPlaySound
-- {name = "Play", spriteName = "playbutton", x = 8, y = 16, toolTip = "Play the current sound.", action = function() OnPlaySound() end}
--
-- totalControlButtonData = #controlButtonData
--
-- for i = 1, totalControlButtonData do
--
-- local data = controlButtonData[i]
--
-- -- TODO need to build sprite tables for each state
-- data.buttonUI = editorUI:CreateButton({x = data.x, y = data.y}, data.spriteName, data.toolTip)
-- data.buttonUI.onAction = data.action
--
-- -- TODO need to add support for hit rect since each button is a different size
--
-- end
-- Adjust hit area for the mutate button
-- controlButtonData[3].hitRect = {x = 5, y = 4, w = 16, h = 16}
-- Look to see if there is a saved ID
if(SessionID() == ReadSaveData("sessionID", "") and rootDirectory == ReadSaveData("rootDirectory", "")) then
currentID = tonumber(ReadSaveData("currentID", "0"))
end
editorUI:ChangeNumberStepperValue(channelIDStepper, 0)
LoadSound(currentID, true, false)
ResetDataValidation()
pixelVisionOS:DisplayMessage(toolName..": Create and manage your game's sound effects.", 5)
else
-- Patch background when loading fails
-- Left panel
DrawRect(104, 24, 88, 8, 0, DrawMode.TilemapCache)
DrawRect(214, 18, 25, 19, BackgroundColor(), DrawMode.TilemapCache)
pixelVisionOS:ChangeTitle(toolName, "toolbaricontool")
pixelVisionOS:ShowMessageModal(toolName .. " Error", "The tool could not load without a reference to a file to edit.", 160, false,
function()
QuitCurrentTool()
end
)
end
end
local currentChannelType = -1
function OnChangeChannelID(value)
currentChannelType = gameEditor:ChannelType(tonumber(value))
-- print("New channel", value, currentChannelType)
-- local value = tonumber(channelIDInputData.inputField.text)
--
-- local type = gameEditor:ChannelType(value)
-- local wavID = table.indexOf(waveTypeIDs, value)
local validWaveID = table.indexOf(waveTypeIDs, currentChannelType)
editorUI:ChangeInputField(waveInputField, validWaves[validWaveID])
UpdatePlayButton()
-- UpdateSoundTemplates()
-- Enable wave buttons based on the channel
-- for i = 1, totalWaveButtons do
-- editorUI:Enable(waveGroupData.buttons[i], currentChannelType < 0)
-- end
end
function UpdatePlayButton()
local enablePlay = true
local isWav = gameEditor:IsWav(currentID)
if(isWav) then --currentChannelType > - 1 or currentChannelType < 5) then
enablePlay = (currentChannelType < 0 or currentChannelType > 4)
elseif(currentChannelType == 5) then
enablePlay = false
end
-- Enable play button
editorUI:Enable(playButton, enablePlay)
end
function UpdateLoadedSFX(propID, value)
soundData[propID] = tostring(value)
if(playButton.enabled) then
playSound = true
end
-- OnPlaySound()
end
function OnChangeWave(value)
currentWaveType = value
-- if(value == 5) then
-- EnableKnobs(false)
-- else
UpdateLoadedSFX(1, waveButtonData[value].waveID)
EnableKnobs(value)
-- EnableKnobs(true)
-- end
-- EnableWavePanel(value == 1)
-- TODO need to
end
function EnableKnobs(waveID)
-- for i = 2, totalKnobs do
-- if(knobData[i] ~= nil) then
-- editorUI:Enable(knobData[i].knobUI, value)
-- end
-- end
-- print("Current Wav", currentWaveType)
local enableSquarePanel = waveID == 1
local isWav = gameEditor:IsWav(currentID)
for i = 2, totalKnobs do
local tmpKnob = knobData[i]
editorUI:Enable(tmpKnob.knobUI, isWav == false)
if(tmpKnob.name == "SquareDuty" or tmpKnob.name == "DutySweep") then
editorUI:Enable(tmpKnob.knobUI, enableSquarePanel)
end
end
-- Update any panels
local spriteData = enableSquarePanel == true and squarewavepanelenabled or squarewavepaneldisabled
if(spriteData ~= nil) then
DrawSprites(spriteData.spriteIDs, 23, 14, spriteData.width, false, false, DrawMode.Tile)
end
end
function OnSFXAction(name)
-- print("OnSFX", name)
if(name == "pickup") then
OnSoundTemplatePress(1)
elseif(name == "explosion") then
OnSoundTemplatePress(3)
elseif(name == "powerup") then
OnSoundTemplatePress(4)
elseif(name == "shoot") then
OnSoundTemplatePress(2)
elseif(name == "jump") then
OnSoundTemplatePress(6)
elseif(name == "hurt") then
OnSoundTemplatePress(5)
elseif(name == "select") then
OnSoundTemplatePress(7)
elseif(name == "random") then
OnSoundTemplatePress(8)
elseif(name == "melody") then
OnInstrumentTemplatePress(1)
elseif(name == "harmony") then
OnInstrumentTemplatePress(2)
elseif(name == "bass") then
OnInstrumentTemplatePress(3)
elseif(name == "drums") then
OnInstrumentTemplatePress(4)
elseif(name == "lead") then
OnInstrumentTemplatePress(5)
elseif(name == "pad") then
OnInstrumentTemplatePress(6)
elseif(name == "snare") then
OnInstrumentTemplatePress(7)
elseif(name == "kick") then
OnInstrumentTemplatePress(8)
end
end
function Update(timeDelta)
-- Convert timeDelta to a float
timeDelta = timeDelta / 1000
-- This needs to be the first call to make sure all of the editor UI is updated first
pixelVisionOS:Update(timeDelta)
-- Only update the tool's UI when the modal isn't active
if(pixelVisionOS:IsModalActive() == false) then
-- If the tool didn't load, don't display any of the UI
if(success == false) then
return
end
if(Key(Keys.Space, InputState.Released)) then
OnPlaySound()
end
-- editorUI:UpdateButton(backBtnData)
-- editorUI:UpdateButton(nextBtnData)
-- editorUI:Enable(controlButtonData[2].buttonUI, gameEditor:IsChannelPlaying())
editorUI:UpdateStepper(soundIDStepper)
editorUI:UpdateStepper(channelIDStepper)
editorUI:UpdateInputField(waveInputField)
editorUI:UpdateInputField(songNameFieldData)
for i = 1, totalKnobs do
local data = knobData[i].knobUI
-- TODO go through and make sure the value is correct, then update
editorUI:UpdateKnob(data)
end
for i = 1, totalSFXButtons do
local data = sfxButtonData[i].buttonUI
editorUI:UpdateButton(data)
end
editorUI:UpdateToggleGroup(waveGroupData)
-- for i = 1, totalControlButtonData do
--
-- local data = playButton
editorUI:UpdateButton(playButton)
-- end
-- TODO this is not working
-- local playing = gameEditor:IsChannelPlaying(0)
-- if(playing) then
-- -- print("Channel Playing", playing)
-- end
-- Only play sounds when the mouse is not down
if(editorUI.collisionManager.mouseDown == false and playSound == true) then
playSound = false
ApplySoundChanges()
end
end
if(installing == true) then
installingTime = installingTime + timeDelta
if(installingTime > installingDelay) then
installingTime = 0
OnInstallNextStep()
if(installingCounter >= installingTotal) then
OnInstallComplete()
end
end
end
end
function ApplySoundChanges(autoPlay, saveHistory)
-- Save sound changes
local settingsString = ""
local total = #soundData
--print("total", total)
for i = 1, total do
local value = soundData[i]
if(value ~= "" or value ~= nil) then
settingsString = settingsString .. soundData[i]
end
if(i < total) then
settingsString = settingsString .. ","
end
end
local id = CurrentSoundID()
gameEditor:Sound(id, settingsString)
InvalidateData()
if(saveHistory ~= false) then
UpdateHistory(settingsString)
end
if(autoPlay ~= false) then
OnPlaySound()
end
end
function Draw()
-- Copy over the screen buffer
RedrawDisplay()
if(gameEditor:IsChannelPlaying()) then
DrawSprites(powerbuttonon.spriteIDs, 16, 88, powerbuttonon.width)
-- playButton.selected = gameEditor:IsChannelPlaying()
-- editorUI:Invalidate(controlButtonData[1])
end
pixelVisionOS:Draw()
end
function OnSave()
-- This will save the system data, the colors and color-map
gameEditor:Save(rootDirectory, {SaveFlags.System, SaveFlags.Sounds})
-- Display a message that everything was saved
pixelVisionOS:DisplayMessage("You're changes have been saved.", 5)
-- Clear the validation
ResetDataValidation()
-- Clear the sound cache
originalSounds = {}
end
function OnPage(value)
activePage = activePanel[value]
activePage:Open()
end
function CurrentSoundID()
return tonumber(soundIDStepper.inputField.text)
end
function OnSoundTemplatePress(value)
gameEditor:GenerateSound(CurrentSoundID(), value)
if(playButton.enabled) then
gameEditor:PlaySound(CurrentSoundID())
end
local id = CurrentSoundID()
-- Reload the sound data
LoadSound(id)
InvalidateData()
end
local playFlag = true
local playDelay = .2
local playTime = 0
function OnPlaySound()
if(playButton.enabled == false) then
return
end
-- print("Play Sound")
gameEditor:StopSound()
gameEditor:PlaySound(CurrentSoundID(), tonumber(channelIDStepper.inputField.text))
end
function OnInstrumentTemplatePress(value)
local template = nil
if(value == 1) then
-- Melody
template = "0,,.2,,.2,.1266,,,,,,,,,,,,,1,,,,,.5"
elseif(value == 2) then
-- Harmony
template = "0,,.01,,.509,.1266,,,,,,,,.31,,,,,1,,,.1,,.5";
elseif(value == 3) then
-- Bass
template = "4,,.01,,.509,.1266,,,,,,,,.31,,,,,1,,,.1,,1";
elseif(value == 4) then
-- Drums
template = "3,,.01,,.209,.1668,,,,,,,,.31,,,,,.3,,,.1,,.5";
elseif(value == 5) then
-- Lead
template = "4,.6,.01,,.609,.1347,,,,,.2,,,.31,,,,,1,,,.1,,.5";
elseif(value == 6) then
-- Pad
template = "4,.5706,.4763,.0767,.8052,.1266,,,-.002,,.1035,.2062,,,-.0038,.8698,-.0032,,.6377,.1076,,.0221,.0164,.5";
elseif(value == 7) then
-- Snare
template = "3,.032,.11,.6905,.4,.1668,.0412,-.2434,.0259,.1296,.4162,.069,.7284,.5,-.213,.0969,-.1699,.8019,.1452,-.0715,.3,.1509,.9632,.5";
elseif(value == 8) then
-- Kick
template = "4,,.2981,.1079,.1122,.1826,.0583,-.2287,.1341,.3666,.0704,.1626,.2816,.0642,.3733,.2103,-.3137,-.3065,.8693,-.3045,.4969,.0218,-.015,.6";
end
if(template ~= nil) then
UpdateSound(template)
end
end
function UpdateSound(settings, autoPlay, addToHistory)
local id = CurrentSoundID()
gameEditor:Sound(id, settings)
if(autoPlay ~= false) then
gameEditor:PlaySound(CurrentSoundID(), tonumber(channelIDStepper.inputField.text))
end
-- Reload the sound data
LoadSound(id, false, addToHistory)
InvalidateData()
end
function OnChangeSoundID(text)
-- convert the text value to a number
local value = tonumber(text)
-- update buttons
-- editorUI:Enable(backBtnData, value > soundIDStepper.inputField.min)
-- editorUI:Enable(nextBtnData, value < soundIDFieldData.inputField.max)
-- Load the sound into the editor
LoadSound(value, true, false)
end
function LoadSound(value, clearHistory, updateHistory)
-- print("Load Sound Clear", clearHistory)
currentID = value
local data = gameEditor:Sound(value)
if(originalSounds[value] == nil) then
-- Make a copy of the sound
originalSounds[value] = data
end
-- Load the current sounds string data so we can edit it
soundData = {}
local tmpValue = ""
for i = 1, #data do
local c = data:sub(i, i)
if(c == ",") then
table.insert(soundData, tmpValue)
tmpValue = ""
else
tmpValue = tmpValue .. c
end
end
-- Always add the last value since it doesn't end in a comma
table.insert(soundData, tmpValue)
Refresh()
local label = gameEditor:SoundLabel(value)
editorUI:ChangeInputField(songNameFieldData, label, false)
editorUI:ChangeNumberStepperValue(soundIDStepper, currentID, false, true)
if(clearHistory == true) then
-- Reset the undo history so it's ready for the tool
-- pixelVisionOS:ResetUndoHistory()
-- UpdateHistoryButtons()
-- TODO Undo logic here too that needs to be restored
end
if(updateHistory ~= false) then
UpdateHistory(data)
end
-- TODO need to refresh the editor panels
end
function Refresh()
for i = 1, totalKnobs do
local knob = knobData[i]
local value = soundData[knob.propID] ~= "" and tonumber(soundData[knob.propID]) or 0
local percent = ((knob.range - 1) + value) / knob.range
UpdateKnobTooltip(knob, percent)
editorUI:ChangeKnob(knob.knobUI, percent, false)
end
UpdateSoundTemplates()
UpdateWaveButtons()
UpdatePlayButton()
end
function UpdateSoundTemplates()
-- Loop through template buttons
totalSFXButtons = #sfxButtonData
local enabled = true
if(gameEditor:IsWav(currentID)) then
enabled = false
end
for i = 1, totalSFXButtons do
local data = sfxButtonData[i].buttonUI
editorUI:Enable(sfxButtonData[i].buttonUI, enabled)
end
end
function UpdateKnobTooltip(knobData, value)
local percentString = string.lpad(tostring(value * 100), 3, "0") .. "%"
knobData.knobUI.toolTip = knobData.toolTip .. percentString .. "."
end
function UpdateWaveButtons()
editorUI:ClearGroupSelections(waveGroupData)
local isWav = gameEditor:IsWav(currentID)
local tmpID = isWav and 5 or 1
local waveID = soundData[1]
for i = 1, totalWaveButtons do
tmpButton = waveGroupData.buttons[i]
local enabled = not isWav
if(i == 5) then
enabled = isWav
end
-- Enable the button
editorUI:Enable(tmpButton, enabled)
if(tonumber(waveID) == waveButtonData[i].waveID) then
tmpID = i
end
end
if(isWav) then
tmpID = 5
end
EnableKnobs(tmpID)
editorUI:SelectToggleButton(waveGroupData, tmpID, false)
end
local enableSquarePanel = nil
function EnableWavePanel(value)
-- if(enableSquarePanel == value) then
-- return
-- end
enableSquarePanel = value
local spriteData = value == true and squarewavepanelenabled or squarewavepaneldisabled
if(spriteData ~= nil) then
DrawSprites(spriteData.spriteIDs, 23, 14, spriteData.width, false, false, DrawMode.Tile)
end
for i = 1, totalKnobs do
local tmpKnob = knobData[i]
if(tmpKnob.name == "SquareDuty" or tmpKnob.name == "DutySweep") then
editorUI:Enable(tmpKnob.knobUI, value)
end
end
end
function OnRevertSound()
local id = CurrentSoundID()
if(originalSounds[id] ~= nil) then
UpdateSound(originalSounds[id])
end
end
function OnChangeName(value)
local id = CurrentSoundID()
local label = gameEditor:SoundLabel(id, value)
Refresh()
InvalidateData()
end
function OnNewSound()
gameEditor:NewSound(CurrentSoundID())
-- Reload the sound
LoadSound(CurrentSoundID())
InvalidateData()