forked from cadin/panels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Panels.lua
1122 lines (953 loc) · 27.6 KB
/
Panels.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
-- Panels version 1.7.3
-- https://cadin.github.io/panels/
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "CoreLibs/animation"
local gfx <const> = playdate.graphics
local ScreenHeight <const> = playdate.display.getHeight()
local ScreenWidth <const> = playdate.display.getWidth()
Panels = {}
Panels.comicData = {}
Panels.credits = {}
Panels.vars = {}
import "./modules/Font"
import "./modules/Audio"
import "./modules/Settings"
import "./modules/ScrollConstants"
import "./modules/ButtonIndicator"
import "./modules/Color"
import "./modules/Effect"
import "./modules/Input"
import "./modules/Image"
import "./modules/Menus"
import "./modules/Alert"
import "./modules/Panel"
import "./modules/Layer"
import "./modules/TextAlignment"
import "./modules/Utils"
import "./modules/Credits"
-- PD function shortcuts
local pdUpdateTimers = playdate.timer.updateTimers
local pdEaseInOutQuad = playdate.easingFunctions.inOutQuad
local pdButtonJustPressed = playdate.buttonJustPressed
local sequenceDidStart = false
local sequenceIsFinishing = false
local currentSeqIndex = 1
local sequences = nil
local sequence = {}
local panels = {}
local scrollPos = 0
local scrollAcceleration = 0.25
local maxScrollVelocity = 8
local scrollVelocity = 0
local maxScroll = 0
local snapStrength = 1.5
local panelBoundaries = {}
local transitionOutAnimator = nil
local transitionInAnimator = nil
local buttonIndicator = nil
local numMenusOpen = 0
local numMenusFullScreen = 0
local menusAreFullScreen = false
local chapterDidSelect = false
local panelTransitionAnimator = nil
local previousBGColor = nil
local transitionFader = nil
local shouldFadeBG = false
Panels.unlockedSequences = {}
local gameDidFinish = false
local alert = nil
local isCutscene = false
local cutsceneFinishCallback = nil
local targetSequence = nil
local mainCanvas = gfx.image.new(ScreenWidth, ScreenHeight, gfx.kColorBlack)
local function setUpPanels(seq)
panels = {}
local pos = 0
local j = 1
if seq.panels == nil then
printError(seq.title or "Untitled sequence", "No panel data found in sequence:")
end
local list = table.shallowcopy(seq.panels)
if seq.scrollingIsReversed then
reverseTable(list)
end
for i, panel in ipairs(list) do
if panel.frame == nil then
panel.frame = table.shallowcopy(seq.defaultFrame)
end
panel.axis = seq.axis
panel.scrollingIsReversed = seq.scrollingIsReversed or false
panel.direction = seq.direction
if panel.advanceControl == nil then
if panel.advanceControlSequence and #panel.advanceControlSequence == 1 then
panel.advanceControl = panel.advanceControlSequence[1]
else
panel.advanceControl = sequence.advanceControl
end
end
if panel.advanceControlSequence == nil then
panel.advanceControlSequence = { panel.advanceControl }
end
if panel.backControl == nil then
panel.backControl = sequence.backControl
end
if panel.preventBacktracking == nil then
panel.preventBacktracking = sequence.preventBacktracking or false
end
if sequence.font and panel.font == nil then
panel.font = sequence.font
end
if sequence.fontFamily and panel.fontFamily == nil then
panel.fontFamily = sequence.fontFamily
end
local p = Panels.Panel.new(panel)
if p.frame.margin then
pos = pos + p.frame.margin
end
if p.frame.gap and i > 1 then
pos = pos + p.frame.gap
end
if seq.axis == Panels.ScrollAxis.VERTICAL then
p.frame.y = pos
pos = pos + p.frame.height
maxScroll = pos - ScreenHeight + p.frame.margin
if i > 1 then
panelBoundaries[j] = -(p.frame.y - p.frame.margin)
j = j + 1
end
if p.frame.height > ScreenHeight then
panelBoundaries[j] = -(p.frame.y + p.frame.height - ScreenHeight + p.frame.margin)
j = j + 1
end
else
p.frame.x = pos
pos = pos + p.frame.width
maxScroll = pos - ScreenWidth + p.frame.margin
if i > 1 then
panelBoundaries[j] = -(p.frame.x - p.frame.margin)
j = j + 1
end
if p.frame.width > ScreenWidth then
panelBoundaries[j] = -(p.frame.x + p.frame.width - ScreenWidth + p.frame.margin)
j = j + 1
end
end
panels[i] = p
end
end
local function lastPanelIsShowing()
local threshold = 24
if (sequence.scrollingIsReversed and scrollPos >= -threshold)
or (not sequence.scrollingIsReversed and scrollPos <= -(maxScroll - threshold)) then
return true
end
return false
end
-- -------------------------------------------------
-- BUTTON INDICATOR
local function createButtonIndicators()
buttonIndicators = {}
if sequence.advanceControls == nil then
buttonIndicators = { Panels.ButtonIndicator.new() }
else
for i, value in ipairs(sequence.advanceControls) do
buttonIndicators[i] = Panels.ButtonIndicator.new()
end
end
end
local function drawButtonIndicators(offset)
if transitionOutAnimator == nil then
if lastPanelIsShowing() and sequenceDidStart and not sequenceIsFinishing then
for key, button in pairs(buttonIndicators) do
button:show()
end
else
for key, button in pairs(buttonIndicators) do
button:hide()
end
end
end
if sequence.showAdvanceControls and sequenceDidStart then
for i, button in ipairs(buttonIndicators) do
if sequence.advanceControls[i].anchor then
local lastPanel = panels[#panels]
button:draw(button.x + lastPanel.frame.x + offset.x , button.y + lastPanel.frame.y + offset.y)
else
button:draw()
end
end
end
end
local function getAdvanceControlForScrollDirection(dir)
if dir == Panels.ScrollDirection.LEFT_TO_RIGHT then
return Panels.Input.RIGHT
elseif dir == Panels.ScrollDirection.TOP_DOWN then
return Panels.Input.DOWN
elseif dir == Panels.ScrollDirection.BOTTOM_UP then
return Panels.Input.UP
elseif dir == Panels.ScrollDirection.NONE then
return nil
else
return Panels.Input.LEFT
end
end
local function getBackControlForScrollDirection(dir)
if dir == Panels.ScrollDirection.LEFT_TO_RIGHT then
return Panels.Input.LEFT
elseif dir == Panels.ScrollDirection.TOP_DOWN then
return Panels.Input.UP
elseif dir == Panels.ScrollDirection.BOTTOM_UP then
return Panels.Input.DOWN
elseif dir == Panels.ScrollDirection.NONE then
return nil
else
return Panels.Input.RIGHT
end
end
-- -------------------------------------------------
-- SCROLLING
local function prepareScrolling(reversed)
if reversed then
panelNum = #panels
scrollPos = -maxScroll
else
scrollPos = 0
panelNum = 1
end
end
local function snapScrollToPanel()
for i, b in ipairs(panelBoundaries) do
if scrollPos > b - 20 and scrollPos < b + 20 then
local diff = scrollPos - b
scrollPos = round(scrollPos - (diff - (diff / 1.25)), 2)
end
end
end
local function updateScroll()
if panelTransitionAnimator then
scrollPos = panelTransitionAnimator:currentValue()
else
if scrollPos > 0 then
scrollPos = math.floor(scrollPos / snapStrength)
elseif scrollPos < -maxScroll then
local diff = scrollPos + maxScroll
scrollPos = math.floor(scrollPos - (diff - (diff / snapStrength)))
end
if Panels.Settings.snapToPanels then snapScrollToPanel() end
end
end
-- -------------------------------------------------
-- PANEL TRANSITIONS
local function isLastPanel(num)
if (num == #panels and not sequence.scrollingIsReversed) or (sequence.scrollingIsReversed and num <= 1) then
return true
else
return false
end
end
local function isFirstPanel(num)
if (num == 1 and not sequence.scrollingIsReversed) or (sequence.scrollingIsReversed and num == #panels) then
return true
else
return false
end
end
function getPanelScrollLocation(panel, isTrailingEdge)
if sequence.axis == Panels.ScrollAxis.VERTICAL then
if isTrailingEdge == true then
return (panel.frame.y + panel.frame.margin + panel.frame.height - ScreenHeight) * -1
else
return (panel.frame.y - panel.frame.margin) * -1
end
else
if isTrailingEdge == true then
return (panel.frame.x + panel.frame.margin + panel.frame.width - ScreenWidth) * -1
else
return (panel.frame.x - panel.frame.margin) * -1
end
end
end
local function scrollToNextPanel()
if not isLastPanel(panelNum) then
if panelTransitionAnimator and panelTransitionAnimator:progress() < 1 then return end
local p = panels[panelNum]
p.buttonsPressed = {}
local target = 0
if p.frame.height > ScreenHeight and scrollPos > p.frame.y * -1 then
target = getPanelScrollLocation(p, true)
elseif p.frame.width > ScreenWidth and scrollPos > p.frame.x * -1 then
target = getPanelScrollLocation(p, true)
else
if sequence.scrollingIsReversed then
panelNum = panelNum - 1
else
panelNum = panelNum + 1
end
target = getPanelScrollLocation(panels[panelNum])
end
if sequence.direction == Panels.ScrollDirection.NONE then
scrollPos = target
else
panelTransitionAnimator = gfx.animator.new(500, scrollPos, target, pdEaseInOutQuad)
end
end
end
local function scrollToPreviousPanel()
if not isFirstPanel(panelNum) then
local p = panels[panelNum]
local target = 0
if p.frame.height > ScreenHeight and scrollPos < p.frame.y * -1 then
target = getPanelScrollLocation(p)
elseif p.frame.width > ScreenWidth and scrollPos < p.frame.x * -1 then
target = getPanelScrollLocation(p)
else
if sequence.scrollingIsReversed then
panelNum = panelNum + 1
else
panelNum = panelNum - 1
end
target = getPanelScrollLocation(panels[panelNum], true)
end
panelTransitionAnimator = gfx.animator.new(500, scrollPos, target, pdEaseInOutQuad)
end
end
-- -------------------------------------------------
-- SEQUENCE TRANSITIONS
local function startTransitionIn(direction, delay)
local target = scrollPos
local start
if direction == Panels.ScrollDirection.BOTTOM_UP then
start = scrollPos - ScreenHeight
elseif direction == Panels.ScrollDirection.TOP_DOWN then
start = scrollPos + ScreenHeight
elseif direction == Panels.ScrollDirection.LEFT_TO_RIGHT then
start = scrollPos + ScreenWidth
elseif direction == Panels.ScrollDirection.NONE then
start = scrollPos
else
start = scrollPos - ScreenWidth
end
scrollPos = start
-- make a dummy animator to hold scroll pos until delayed transition starts
transitionInAnimator = playdate.graphics.animator.new(math.max(delay * 2, 2000), start, start)
if previousBGColor then
gfx.lockFocus(transitionFader)
gfx.setColor(previousBGColor)
gfx.fillRect(0, 0, ScreenWidth, ScreenHeight)
gfx.unlockFocus()
end
shouldFadeBG = previousBGColor ~= nil and previousBGColor ~= sequence.backgroundColor
local function delayedStart()
transitionInAnimator = playdate.graphics.animator.new(
Panels.Settings.sequenceTransitionDuration, start, target, playdate.easingFunctions.inOutQuart)
end
playdate.timer.performAfterDelay(delay, delayedStart)
end
local function startTransitionOut(direction)
local target
local start = scrollPos
local duration = Panels.Settings.sequenceTransitionDuration
if direction == Panels.ScrollDirection.TOP_DOWN then
target = -maxScroll - ScreenHeight
elseif direction == Panels.ScrollDirection.BOTTOM_UP then
target = maxScroll + ScreenHeight
elseif direction == Panels.ScrollDirection.RIGHT_TO_LEFT then
target = maxScroll + ScreenWidth
elseif direction == Panels.ScrollDirection.NONE then
target = scrollPos
duration = 200
else
target = -maxScroll - ScreenWidth
end
transitionOutAnimator = playdate.graphics.animator.new(
duration, start, target, playdate.easingFunctions.inOutQuart)
end
local function getAxisForScrollDirection(dir)
if dir == Panels.ScrollDirection.TOP_TO_BOTTOM or dir == Panels.ScrollDirection.BOTTOM_UP then
return Panels.ScrollAxis.VERTICAL
else
return Panels.ScrollAxis.HORIZONTAL
end
end
-- -------------------------------------------------
-- SEQUENCE LIFECYCLE
local function setSequenceScrollDirection()
if sequence.axis == nil and sequence.direction == nil then
sequence.axis = Panels.ScrollAxis.HORIZONTAL
end
if sequence.axis == nil then
sequence.axis = getAxisForScrollDirection(sequence.direction)
end
if sequence.direction == nil then
if sequence.axis == Panels.ScrollAxis.VERTICAL then
sequence.direction = Panels.ScrollDirection.TOP_DOWN
else
sequence.direction = Panels.ScrollDirection.LEFT_TO_RIGHT
end
elseif sequence.direction == Panels.ScrollDirection.NONE then
sequence.axis = Panels.ScrollAxis.HORIZONTAL
elseif sequence.direction == Panels.ScrollDirection.RIGHT_TO_LEFT
or sequence.direction == Panels.ScrollDirection.BOTTOM_UP then
sequence.scrollingIsReversed = true
end
end
local function setSequenceColors()
if sequence.backgroundColor == nil then
if sequence.foregroundColor then
sequence.backgroundColor = Panels.Color.invert(sequence.foregroundColor)
else
sequence.foregroundColor = Panels.Color.BLACK
sequence.backgroundColor = Panels.Color.WHITE
end
else
if sequence.foregroundColor == nil then
sequence.foregroundColor = Panels.Color.invert(sequence.backgroundColor)
end
end
end
local function unlockSequence(num)
for i = 1, num, 1 do
if not Panels.unlockedSequences[i] then
Panels.unlockedSequences[i] = false
end
end
Panels.unlockedSequences[num] = true
end
local function loadSequence(num)
currentSeqIndex = num
sequence = sequences[num]
createButtonIndicators()
unlockSequence(num)
-- set default scroll direction for each axis if not specified
setSequenceScrollDirection()
setSequenceColors()
if sequence.scrollType == nil then
sequence.scrollType = Panels.ScrollType.MANUAL
end
if sequence.defaultFrame == nil then
sequence.defaultFrame = Panels.Settings.defaultFrame
end
if sequence.advanceControls == nil then
local control
if sequence.advanceControl == nil then
local _input = getAdvanceControlForScrollDirection(sequence.direction)
control = {input = _input}
sequence.advanceControl = _input
else
control = {input = sequence.advanceControl}
end
if sequence.advanceControlPosition == nil then
local x, y = Panels.ButtonIndicator.getPosititonForScrollDirection(sequence.direction)
control.x = x
control.y = y
else
control.x = sequence.advanceControlPosition.x
control.y = sequence.advanceControlPosition.y
end
sequence.advanceControls = { control }
end
if sequence.showAdvanceControls == nil then
if sequence.showAdvanceControl == nil then
sequence.showAdvanceControls = true
else
sequence.showAdvanceControls = sequence.showAdvanceControl
end
end
if sequence.backControl == nil then
sequence.backControl = getBackControlForScrollDirection(sequence.direction)
end
if sequence.audio then
if sequence.audio.continuePrevious and Panels.Audio.bgAudioIsPlaying() then
else
if sequence.audio.file then
Panels.Audio.startBGAudio(
Panels.Settings.audioFolder .. sequence.audio.file,
sequence.audio.loop or false,
sequence.audio.volume or 1
)
else
Panels.Audio.killBGAudio()
end
end
else
Panels.Audio.killBGAudio()
end
setUpPanels(sequence)
prepareScrolling(sequence.scrollingIsReversed)
for i, control in ipairs(sequence.advanceControls) do
buttonIndicators[i]:setButton(control.input)
if sequence.showAdvanceControls and (control.x == nil or control.y == nil) then
local err = sequence.title or "Untitled sequence (" .. num .. ")"
printError(err, "Invalid position for advance control")
end
buttonIndicators[i]:setPosition(control.x or (i-1) * 40, control.y or 0)
end
startTransitionIn(sequence.direction, sequence.delay or 0)
end
local function unloadSequence()
for i, p in ipairs(panels) do
p:killTypingEffects()
if p.layers then
for j, l in ipairs(p.layers) do
if l.timer then
l.timer:remove()
l.timer = nil
end
if l.animationLoop then
l.animationLoop = nil
end
end
end
if p.wasOnScreen then
p:reset()
end
end
panelTransitionAnimator = nil
Panels.Image.clearCache()
sequence.didFinish = false
previousBGColor = sequence.backgroundColor
end
local function nextSequence()
local isDeadEnd = sequence.endSequence or false
unloadSequence()
if isCutscene then
playdate.inputHandlers.pop()
gameDidFinish = true
cutsceneFinishCallback(targetSequence)
Panels.Audio.killBGAudio()
elseif targetSequence then
loadSequence(targetSequence)
targetSequence = nil
updateMenuData(sequences, gameDidFinish)
elseif currentSeqIndex < #sequences and not isDeadEnd then
currentSeqIndex = currentSeqIndex + 1
loadSequence(currentSeqIndex)
updateMenuData(sequences, gameDidFinish)
else
gameDidFinish = true
updateMenuData(sequences, gameDidFinish)
menusAreFullScreen = true
if Panels.Settings.resetVarsOnGameOver then
Panels.vars = {}
end
Panels.Audio.killBGAudio()
Panels.mainMenu:show()
end
end
local function updateSequenceTransition()
if transitionOutAnimator then
scrollPos = transitionOutAnimator:currentValue()
if transitionOutAnimator:ended() then
transitionOutAnimator = nil
sequenceDidStart = false
playdate.timer.performAfterDelay(1, nextSequence) -- prevent flash before transition in
end
elseif transitionInAnimator then
scrollPos = transitionInAnimator:currentValue()
if transitionInAnimator:ended() then
sequenceDidStart = true
sequenceIsFinishing = false
transitionInAnimator = nil
shouldFadeBG = false
end
end
end
local function finishSequence()
if not sequence.didFinish then
sequence.didFinish = true
startTransitionOut(sequence.direction)
end
end
-- -------------------------------------------------
-- INPUTS
local function shouldGoBack(panel)
local should = true
if panel.preventBacktracking then
if panel.frame.height > ScreenHeight and scrollPos < panel.frame.y * -1 or
panel.frame.width > ScreenWidth and scrollPos < panel.frame.x * -1 then
-- same frame, allow it
should = true
else
should = false
end
end
return should
end
function Panels.cranked(change, accChange)
if sequence.scrollType == Panels.ScrollType.MANUAL then
if sequence.axis == Panels.ScrollAxis.VERTICAL and sequence.scrollingIsReversed then
scrollPos = scrollPos + change
else
scrollPos = scrollPos - change
end
end
end
local function hideOtherAdvanceControls(pressedIndex)
for i, button in ipairs(buttonIndicators) do
if i ~= pressedIndex then
button:hide()
end
end
end
local function checkInputs()
local p = panels[panelNum]
if sequenceIsFinishing then return end
if lastPanelIsShowing() then
if p.advanceFunction == nil then
for i, button in ipairs(buttonIndicators) do
if pdButtonJustPressed(sequence.advanceControls[i].input) then
if sequence.advanceControls[i].target then
targetSequence = sequence.advanceControls[i].target
end
button:press()
hideOtherAdvanceControls(i)
sequenceIsFinishing = true
if p.advanceDelay then
p:exit()
playdate.timer.performAfterDelay(p.advanceDelay, finishSequence)
else
finishSequence()
end
end
end
end
end
if sequence.scrollType == Panels.ScrollType.AUTO then
if p.advanceFunction == nil then
if p.advanceControlSequence then
local trigger = p.advanceControlSequence[#p.buttonsPressed + 1]
if pdButtonJustPressed(trigger) then
p.buttonsPressed[#p.buttonsPressed + 1] = trigger
if #p.buttonsPressed == #p.advanceControlSequence then
if p.advanceDelay then
p:exit()
playdate.timer.performAfterDelay(p.advanceDelay, scrollToNextPanel)
else
scrollToNextPanel()
end
end
end
else
if pdButtonJustPressed(p.advanceControl) then
scrollToNextPanel()
end
end
end
if pdButtonJustPressed(p.backControl) then
if shouldGoBack(p) then
scrollToPreviousPanel()
end
end
end
end
local function updateArrowControls()
if (sequence.axis == Panels.ScrollAxis.VERTICAL
and playdate.buttonIsPressed(Panels.Input.UP))
or (sequence.axis == Panels.ScrollAxis.HORIZONTAL
and playdate.buttonIsPressed(Panels.Input.LEFT)) then
scrollVelocity = scrollVelocity + scrollAcceleration
elseif (sequence.axis == Panels.ScrollAxis.VERTICAL
and playdate.buttonIsPressed(Panels.Input.DOWN))
or (sequence.axis == Panels.ScrollAxis.HORIZONTAL
and playdate.buttonIsPressed(Panels.Input.RIGHT)) then
scrollVelocity = scrollVelocity - scrollAcceleration
else
scrollVelocity = scrollVelocity / 2
end
if scrollVelocity > maxScrollVelocity then
scrollVelocity = maxScrollVelocity
elseif scrollVelocity < -maxScrollVelocity then
scrollVelocity = -maxScrollVelocity
end
scrollPos = scrollPos + scrollVelocity
end
-- -------------------------------------------------
-- GAME LOOP
local function getScrollOffset()
local offset = { x = 0, y = 0 }
if sequence.axis == Panels.ScrollAxis.HORIZONTAL then
offset.x = scrollPos
else
offset.y = scrollPos
end
return offset
end
local function updateComic(offset)
if transitionInAnimator or transitionOutAnimator then
updateSequenceTransition()
else
if panels and #panels < 1 then
printError("`panels` table is empty", "This sequence has invalid panel definitions.")
end
if panels and panels[panelNum]:shouldAutoAdvance() then
if not isLastPanel(panelNum) then
scrollToNextPanel()
else
finishSequence()
end
end
updateScroll()
if sequence.scrollType == Panels.ScrollType.MANUAL then
updateArrowControls()
end
checkInputs()
end
end
local function drawComic(offset)
gfx.pushContext(mainCanvas)
gfx.clear(sequence.backgroundColor)
if shouldFadeBG then
local pct = 1 -
(transitionInAnimator:currentValue() - transitionInAnimator.startValue) /
(transitionInAnimator.endValue - transitionInAnimator.startValue)
transitionFader:drawFaded(0, 0, pct, gfx.image.kDitherTypeBayer8x8)
end
for i, panel in ipairs(panels) do
if (panel:isOnScreen(offset)) then
panel:render(offset, sequence.foregroundColor, sequence.backgroundColor)
elseif panel.wasOnScreen then
if panel.targetSequenceFunction then
targetSequence = panel.targetSequenceFunction()
end
panel:reset()
panel.wasOnScreen = false
end
end
gfx.popContext()
mainCanvas:draw(0, 0)
if Panels.Settings.showFPS then
playdate.drawFPS(0,0)
end
end
-- Playdate update loop
function Panels.update()
if not menusAreFullScreen then
local offset = getScrollOffset()
updateComic(offset)
drawComic(offset)
drawButtonIndicators(offset)
end
if numMenusOpen > 0 then
updateMenus()
end
if alert.isActive then
alert:udpate()
end
pdUpdateTimers()
end
-- -------------------------------------------------
-- SAVE & LOAD GAME PROGRESS
local function loadGameData()
local data = playdate.datastore.read()
if data then
currentSeqIndex = data.sequence
Panels.unlockedSequences = data.unlockedSequences or {}
gameDidFinish = data.gameDidFinish
Panels.vars = data.vars or {}
end
end
local function saveGameData()
playdate.datastore.write({ sequence = currentSeqIndex, unlockedSequences = Panels.unlockedSequences, gameDidFinish = gameDidFinish, vars = Panels.vars })
end
function playdate.gameWillTerminate()
saveGameData()
end
function playdate.deviceWillSleep()
saveGameData()
end
function playdate.deviceWillLock()
saveGameData()
end
-- -------------------------------------------------
-- MENU HANDLERS
function Panels.onChapterSelected(chapter)
chapterDidSelect = true
Panels.Audio.stopBGAudio()
unloadSequence()
currentSeqIndex = chapter
loadSequence(currentSeqIndex)
end
function Panels.onMenuWillShow(menu)
numMenusOpen = numMenusOpen + 1
Panels.Audio.pauseBGAudio()
Panels.Audio.muteTypingSounds()
if panels then
for i, p in ipairs(panels) do
if p.wasOnScreen then
p:pauseSounds()
end
end
end
end
function Panels.onMenuDidShow()
menusAreFullScreen = true
numMenusFullScreen = numMenusFullScreen + 1
end
function Panels.onMenuWillHide(menu)
if menu == Panels.mainMenu then
if not chapterDidSelect then
Panels.Audio.unmuteTypingSounds()
loadSequence(currentSeqIndex)
end
end
numMenusFullScreen = numMenusFullScreen - 1
if numMenusFullScreen < 1 then
menusAreFullScreen = false
end
end
function Panels.onMenuDidHide(menu)
numMenusOpen = numMenusOpen - 1
if numMenusOpen < 1 then
Panels.Audio.resumeBGAudio()
Panels.Audio.unmuteTypingSounds()
if panels then
for i, p in ipairs(panels) do
if p.wasOnScreen then
p:unPauseSounds()
end
end
end
chapterDidSelect = false
end
end
function Panels.onMenuDidStartOver()
alert:show()
end
function onAlertDidStartOver()
Panels.Audio.stopBGAudio()
Panels.unlockedSequences = {}
gameDidFinish = false
saveGameData()
unloadSequence()
currentSeqIndex = 1
Panels.vars = {}
Panels.mainMenu:hide()
createMenus(sequences, gameDidFinish, currentSeqIndex > 1)
end
function onAlertDidHide()
if alert.selection == 2 then
onAlertDidStartOver()
end
end
function shouldShowMainMenu()
local should = false
if Panels.Settings.showMenuOnLaunch then
if currentSeqIndex > 1 or Panels.Settings.skipMenuOnFirstLaunch == false then
should = true
end
end
if gameDidFinish then should = true end
return should
end
-- -------------------------------------------------
-- START GAME
local function updateSystemMenu()
local sysMenu = playdate.getSystemMenu()
if Panels.Settings.useChapterMenu then
local chaptersMenuItem, error = sysMenu:addMenuItem("Chapters",
function()
Panels.creditsMenu:hide()
Panels.chapterMenu:show()
end