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
|
_2DMenu_::
xor a
ldh [hBGMapMode], a
call MenuBox
call Place2DMenuItemStrings
call UpdateSprites
call ApplyTilemap
call Init2DMenuCursorPosition
call StaticMenuJoypad
call MenuClickSound
ld a, [wMenuDataFlags]
bit 1, a
jr z, .skip
call GetMenuJoypad
bit SELECT_F, a
jr nz, .quit1
.skip
ld a, [wMenuDataFlags]
bit 0, a
jr nz, .skip2
call GetMenuJoypad
bit B_BUTTON_F, a
jr nz, .quit2
.skip2
ld a, [w2DMenuNumCols]
ld c, a
ld a, [wMenuCursorY]
dec a
call SimpleMultiply
ld c, a
ld a, [wMenuCursorX]
add c
ld [wMenuCursorPosition], a
and a
ret
.quit1
scf
ret
.quit2
scf
ret
Get2DMenuNumberOfColumns:
ld a, [wMenuData_2DMenuDimensions]
and $f
ret
Get2DMenuNumberOfRows:
ld a, [wMenuData_2DMenuDimensions]
swap a
and $f
ret
Place2DMenuItemStrings:
ld hl, wMenuData_2DMenuItemStringsAddr
ld e, [hl]
inc hl
ld d, [hl]
call GetMenuTextStartCoord
call Coord2Tile
call Get2DMenuNumberOfRows
ld b, a
.row
push bc
push hl
call Get2DMenuNumberOfColumns
ld c, a
.col
push bc
ld a, [wMenuData_2DMenuItemStringsBank]
call Place2DMenuItemName
inc de
ld a, [wMenuData_2DMenuSpacing]
ld c, a
ld b, 0
add hl, bc
pop bc
dec c
jr nz, .col
pop hl
ld bc, 2 * SCREEN_WIDTH
add hl, bc
pop bc
dec b
jr nz, .row
ld hl, wMenuData_2DMenuFunctionAddr
ld a, [hli]
ld h, [hl]
ld l, a
or h
ret z
ld a, [wMenuData_2DMenuFunctionBank]
rst FarCall
ret
Init2DMenuCursorPosition:
call GetMenuTextStartCoord
ld a, b
ld [w2DMenuCursorInitY], a
dec c
ld a, c
ld [w2DMenuCursorInitX], a
call Get2DMenuNumberOfRows
ld [w2DMenuNumRows], a
call Get2DMenuNumberOfColumns
ld [w2DMenuNumCols], a
call .InitFlags_a
call .InitFlags_b
call .InitFlags_c
ld a, [w2DMenuNumCols]
ld e, a
ld a, [wMenuCursorPosition]
ld b, a
xor a
ld d, 0
.loop
inc d
add e
cp b
jr c, .loop
sub e
ld c, a
ld a, b
sub c
and a
jr z, .reset1
cp e
jr z, .okay1
jr c, .okay1
.reset1
ld a, 1
.okay1
ld [wMenuCursorX], a
ld a, [w2DMenuNumRows]
ld e, a
ld a, d
and a
jr z, .reset2
cp e
jr z, .okay2
jr c, .okay2
.reset2
ld a, 1
.okay2
ld [wMenuCursorY], a
xor a
ld [wCursorOffCharacter], a
ld [wCursorCurrentTile], a
ld [wCursorCurrentTile + 1], a
ret
.InitFlags_a:
xor a
ld hl, w2DMenuFlags1
ld [hli], a
ld [hld], a
ld a, [wMenuDataFlags]
bit 5, a
ret z
set 5, [hl]
set 4, [hl]
ret
.InitFlags_b:
ld a, [wMenuData_2DMenuSpacing]
or $20
ld [w2DMenuCursorOffsets], a
ret
.InitFlags_c:
ld hl, wMenuDataFlags
ld a, A_BUTTON
bit 0, [hl]
jr nz, .skip
or B_BUTTON
.skip
bit 1, [hl]
jr z, .skip2
or SELECT
.skip2
ld [wMenuJoypadFilter], a
ret
_StaticMenuJoypad::
call Place2DMenuCursor
_ScrollingMenuJoypad::
ld hl, w2DMenuFlags2
res 7, [hl]
ldh a, [hBGMapMode]
push af
.menu_joypad_loop
call Move2DMenuCursor
ldh a, [hOAMUpdate]
push af
ld a, $1
ldh [hOAMUpdate], a
call WaitBGMap
pop af
ldh [hOAMUpdate], a
xor a
ldh [hBGMapMode], a
.loopRTC
call UpdateTimeAndPals
call Menu_WasButtonPressed
jr c, .pressed
ld a, [w2DMenuFlags1]
bit 7, a
jp nz, .done
jr .loopRTC
.pressed
vc_hook print_forbid_3
call _2DMenuInterpretJoypad
jp c, .done
ld a, [w2DMenuFlags1]
bit 7, a
jr nz, .done
call GetMenuJoypad
ld b, a
ld a, [wMenuJoypadFilter]
and b
jp z, .menu_joypad_loop
.done
pop af
ldh [hBGMapMode], a
call GetMenuJoypad
ret
Menu_WasButtonPressed:
ld a, [w2DMenuFlags1]
bit 6, a
jr z, .skip_to_joypad
callfar PlaySpriteAnimationsAndDelayFrame
.skip_to_joypad
call JoyTextDelay
call GetMenuJoypad
and a
ret z
scf
vc_hook print_forbid_2
ret
_2DMenuInterpretJoypad:
call GetMenuJoypad
bit A_BUTTON_F, a
jp nz, .a_b_start_select
bit B_BUTTON_F, a
jp nz, .a_b_start_select
bit SELECT_F, a
jp nz, .a_b_start_select
bit START_F, a
jp nz, .a_b_start_select
bit D_RIGHT_F, a
jr nz, .d_right
bit D_LEFT_F, a
jr nz, .d_left
bit D_UP_F, a
jr nz, .d_up
bit D_DOWN_F, a
jr nz, .d_down
and a
ret
.set_bit_7
ld hl, w2DMenuFlags2
set 7, [hl]
scf
ret
.d_down
ld hl, wMenuCursorY
ld a, [w2DMenuNumRows]
cp [hl]
jr z, .check_wrap_around_down
inc [hl]
xor a
ret
.check_wrap_around_down
ld a, [w2DMenuFlags1]
bit 5, a
jr nz, .wrap_around_down
bit 3, a
jp nz, .set_bit_7
xor a
ret
.wrap_around_down
ld [hl], $1
xor a
ret
.d_up
ld hl, wMenuCursorY
ld a, [hl]
dec a
jr z, .check_wrap_around_up
ld [hl], a
xor a
ret
.check_wrap_around_up
ld a, [w2DMenuFlags1]
bit 5, a
jr nz, .wrap_around_up
bit 2, a
jp nz, .set_bit_7
xor a
ret
.wrap_around_up
ld a, [w2DMenuNumRows]
ld [hl], a
xor a
ret
.d_left
ld hl, wMenuCursorX
ld a, [hl]
dec a
jr z, .check_wrap_around_left
ld [hl], a
xor a
ret
.check_wrap_around_left
ld a, [w2DMenuFlags1]
bit 4, a
jr nz, .wrap_around_left
bit 1, a
jp nz, .set_bit_7
xor a
ret
.wrap_around_left
ld a, [w2DMenuNumCols]
ld [hl], a
xor a
ret
.d_right
ld hl, wMenuCursorX
ld a, [w2DMenuNumCols]
cp [hl]
jr z, .check_wrap_around_right
inc [hl]
xor a
ret
.check_wrap_around_right
ld a, [w2DMenuFlags1]
bit 4, a
jr nz, .wrap_around_right
bit 0, a
jp nz, .set_bit_7
xor a
ret
.wrap_around_right
ld [hl], $1
xor a
ret
.a_b_start_select
xor a
ret
Move2DMenuCursor:
ld hl, wCursorCurrentTile
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [hl]
cp "▶"
jr nz, Place2DMenuCursor
ld a, [wCursorOffCharacter]
ld [hl], a
Place2DMenuCursor:
ld a, [w2DMenuCursorInitY]
ld b, a
ld a, [w2DMenuCursorInitX]
ld c, a
call Coord2Tile
ld a, [w2DMenuCursorOffsets]
swap a
and $f
ld c, a
ld a, [wMenuCursorY]
ld b, a
xor a
dec b
jr z, .got_row
.row_loop
add c
dec b
jr nz, .row_loop
.got_row
ld c, SCREEN_WIDTH
call AddNTimes
ld a, [w2DMenuCursorOffsets]
and $f
ld c, a
ld a, [wMenuCursorX]
ld b, a
xor a
dec b
jr z, .got_col
.col_loop
add c
dec b
jr nz, .col_loop
.got_col
ld c, a
add hl, bc
ld a, [hl]
cp "▶"
jr z, .cursor_on
ld [wCursorOffCharacter], a
ld [hl], "▶"
.cursor_on
ld a, l
ld [wCursorCurrentTile], a
ld a, h
ld [wCursorCurrentTile + 1], a
ret
_PushWindow::
xor a ; BANK(sWindowStack)
call OpenSRAM
ld hl, wWindowStackPointer
ld e, [hl]
inc hl
ld d, [hl]
push de
ld b, wMenuHeaderEnd - wMenuHeader
ld hl, wMenuHeader
.loop
ld a, [hli]
ld [de], a
dec de
dec b
jr nz, .loop
; If bit 6 or 7 of the menu flags is set, set bit 0 of the address
; at 7:[wWindowStackPointer], and draw the menu using the coordinates from the header.
; Otherwise, reset bit 0 of 7:[wWindowStackPointer].
ld a, [wMenuFlags]
bit 6, a
jr nz, .bit_6
bit 7, a
jr z, .not_bit_7
.bit_6
ld hl, wWindowStackPointer
ld a, [hli]
ld h, [hl]
ld l, a
set 0, [hl]
call MenuBoxCoord2Tile
call GetMenuBoxDims
inc b
inc c
call .ret ; empty function
.row
push bc
push hl
.col
ld a, [hli]
ld [de], a
dec de
dec c
jr nz, .col
pop hl
ld bc, SCREEN_WIDTH
add hl, bc
pop bc
dec b
jr nz, .row
jr .done
.not_bit_7
pop hl ; last-pushed register was de
push hl
ld a, [hld]
ld l, [hl]
ld h, a
res 0, [hl]
.done
pop hl
call .ret ; empty function
ld a, h
ld [de], a
dec de
ld a, l
ld [de], a
dec de
ld hl, wWindowStackPointer
ld [hl], e
inc hl
ld [hl], d
call CloseSRAM
ld hl, wWindowStackSize
inc [hl]
ret
.ret
ret
_ExitMenu::
xor a
ldh [hBGMapMode], a
xor a ; BANK(sWindowStack)
call OpenSRAM
call GetWindowStackTop
ld a, l
or h
jp z, Error_Cant_ExitMenu
ld a, l
ld [wWindowStackPointer], a
ld a, h
ld [wWindowStackPointer + 1], a
call PopWindow
ld a, [wMenuFlags]
bit 0, a
jr z, .loop
ld d, h
ld e, l
call RestoreTileBackup
.loop
call GetWindowStackTop
ld a, h
or l
jr z, .done
call PopWindow
.done
call CloseSRAM
ld hl, wWindowStackSize
dec [hl]
call RestoreOverworldMapTiles
ld a, [wSpriteUpdatesEnabled]
cp 0
ret z
call ReloadPalettes
ret
RestoreOverworldMapTiles:
ld a, [wVramState]
bit 0, a
ret z
xor a ; sScratch
call OpenSRAM
hlcoord 0, 0
ld de, sScratch
ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
call CopyBytes
call CloseSRAM
call OverworldTextModeSwitch
xor a ; sScratch
call OpenSRAM
ld hl, sScratch
decoord 0, 0
ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
.loop
ld a, [hl]
cp $61
jr c, .next
ld [de], a
.next
inc hl
inc de
dec bc
ld a, c
or b
jr nz, .loop
call CloseSRAM
ret
Error_Cant_ExitMenu:
ld hl, .WindowPoppingErrorText
call PrintText
call WaitBGMap
.infinite_loop
jr .infinite_loop
.WindowPoppingErrorText:
text_far _WindowPoppingErrorText
text_end
_InitVerticalMenuCursor::
ld a, [wMenuDataFlags]
ld b, a
ld hl, w2DMenuCursorInitY
ld a, [wMenuBorderTopCoord]
inc a
bit 6, b
jr nz, .skip_offset
inc a
.skip_offset
ld [hli], a
; w2DMenuCursorInitX
ld a, [wMenuBorderLeftCoord]
inc a
ld [hli], a
; w2DMenuNumRows
ld a, [wMenuDataItems]
ld [hli], a
; w2DMenuNumCols
ld a, 1
ld [hli], a
; w2DMenuFlags1
ld [hl], $0
bit 5, b
jr z, .skip_bit_5
set 5, [hl]
.skip_bit_5
ld a, [wMenuFlags]
bit 4, a
jr z, .skip_bit_6
set 6, [hl]
.skip_bit_6
inc hl
; w2DMenuFlags2
xor a
ld [hli], a
; w2DMenuCursorOffsets
ln a, 2, 0
ld [hli], a
; wMenuJoypadFilter
ld a, A_BUTTON
bit 0, b
jr nz, .skip_bit_1
add B_BUTTON
.skip_bit_1
ld [hli], a
; wMenuCursorY
ld a, [wMenuCursorPosition]
and a
jr z, .load_at_the_top
ld c, a
ld a, [wMenuDataItems]
cp c
jr nc, .load_position
.load_at_the_top
ld c, 1
.load_position
ld [hl], c
inc hl
; wMenuCursorX
ld a, 1
ld [hli], a
; wCursorOffCharacter, wCursorCurrentTile
xor a
ld [hli], a
ld [hli], a
ld [hli], a
ret
|