Refactor menu system: Add constants and deduplicate input logic

This commit is contained in:
scawful
2025-11-22 19:03:56 -05:00
parent 3ceab24c7c
commit 52a5ed4b02
2 changed files with 106 additions and 213 deletions

View File

@@ -35,6 +35,26 @@ incsrc "menu_gfx_table.asm"
incsrc "menu_text.asm" incsrc "menu_text.asm"
incsrc "menu_palette.asm" incsrc "menu_palette.asm"
; =========================================================
; CONSTANTS
!MENU_STATE_INIT_GRAPHICS = $00
!MENU_STATE_UPLOAD_RIGHT = $01
!MENU_STATE_UPLOAD_LEFT = $02
!MENU_STATE_SCROLL_DOWN = $03
!MENU_STATE_ITEM_SCREEN = $04
!MENU_STATE_SCROLL_TO = $05
!MENU_STATE_STATS_SCREEN = $06
!MENU_STATE_SCROLL_FROM = $07
!MENU_STATE_SCROLL_UP = $08
!MENU_STATE_RING_BOX = $09
!MENU_STATE_EXIT = $0A
!MENU_STATE_INIT_SCROLL_DOWN = $0B
!MENU_STATE_MAGIC_BAG = $0C
!MENU_STATE_SONG_MENU = $0D
!MENU_STATE_JOURNAL = $0E
!MENU_STATE_SUBMENU_RETURN = $0F
; ========================================================= ; =========================================================
; SUBROUTINE TABLE ; SUBROUTINE TABLE
@@ -125,8 +145,8 @@ Menu_UploadRight:
JSR DrawLocationName JSR DrawLocationName
SEP #$30 SEP #$30
LDA.b #$22 : STA.w $0116 LDA.b #$23 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
INC.w $0200 INC.w $0200
RTS RTS
} }
@@ -154,7 +174,7 @@ Menu_UploadLeft:
;----------------------- ;-----------------------
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 : STA.b $15 ; added for palette LDA.b #$01 : STA.b $17 : STA.b $15 ; added for palette
INC.w $0200 INC.w $0200
RTS RTS
} }
@@ -193,25 +213,25 @@ incsrc "menu_select_item.asm"
Menu_CheckForSpecialMenus: Menu_CheckForSpecialMenus:
{ {
LDA.w $0202 : CMP.b #$05 : BNE + LDA.w $0202 : CMP.b #!MENU_STATE_SCROLL_TO : BNE +
LDA.b $F6 : BIT.b #$80 : BEQ + LDA.b $F6 : BIT.b #$80 : BEQ +
STZ.w $020B STZ.w $020B
LDA.b #$0C : STA.w $0200 ; Magic Bag LDA.b #!MENU_STATE_MAGIC_BAG : STA.w $0200 ; Magic Bag
JSR Menu_DeleteCursor ; Ensure cursor is deleted JSR Menu_DeleteCursor ; Ensure cursor is deleted
SEC : RTS ; Return Carry Set SEC : RTS ; Return Carry Set
+ +
LDA.w $0202 : CMP.b #$0D : BNE ++ LDA.w $0202 : CMP.b #!MENU_STATE_SONG_MENU : BNE ++
LDA.b $F6 : BIT.b #$80 : BEQ ++ LDA.b $F6 : BIT.b #$80 : BEQ ++
LDA.b #$0D : STA.w $0200 LDA.b #!MENU_STATE_SONG_MENU : STA.w $0200
LDA.b #$01 : STA.w CurrentSong ; Initialize song selection LDA.b #$01 : STA.w CurrentSong ; Initialize song selection
JSR Menu_DeleteCursor JSR Menu_DeleteCursor
JSR Menu_DrawSongMenu JSR Menu_DrawSongMenu
SEP #$30 SEP #$30
SEC : RTS ; Return Carry Set SEC : RTS ; Return Carry Set
++ ++
LDA.w $0202 : CMP.b #$0E : BNE ++ LDA.w $0202 : CMP.b #!MENU_STATE_JOURNAL : BNE ++
LDA.b $F6 : BIT.b #$80 : BEQ ++ LDA.b $F6 : BIT.b #$80 : BEQ ++
LDA.b #$0E : STA.w $0200 LDA.b #!MENU_STATE_JOURNAL : STA.w $0200
JSR Menu_DeleteCursor JSR Menu_DeleteCursor
JSL Menu_DrawJournal JSL Menu_DrawJournal
SEP #$30 SEP #$30
@@ -222,7 +242,7 @@ Menu_CheckForSpecialMenus:
JSR Menu_DeleteCursor JSR Menu_DeleteCursor
JSR Menu_DrawRingBox JSR Menu_DrawRingBox
STZ.w $020B STZ.w $020B
LDA.b #$09 : STA.w $0200 ; Ring Box LDA.b #!MENU_STATE_RING_BOX : STA.w $0200 ; Ring Box
SEC : RTS ; Return Carry Set SEC : RTS ; Return Carry Set
+++ +++
@@ -281,7 +301,7 @@ Menu_ItemScreen:
SEP #$20 SEP #$20
.exit .exit
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
RTS RTS
} }
@@ -336,7 +356,7 @@ Menu_ScrollUp:
LDA.w Menu_Scroll, X LDA.w Menu_Scroll, X
STA.b $EA : BNE .notDoneScrolling STA.b $EA : BNE .notDoneScrolling
STZ.b $E4 STZ.b $E4
LDA.w #$000A : STA.w $0200 LDA.w #!MENU_STATE_EXIT : STA.w $0200
RTS RTS
.notDoneScrolling .notDoneScrolling
@@ -353,7 +373,7 @@ Menu_CheckBottle:
LDA.b #$0001 : JMP .prepare_bottle LDA.b #$0001 : JMP .prepare_bottle
.not_first .not_first
LDA.w $0202 : CMP.b #$0C : BNE .not_second LDA.w $0202 : CMP.b #!MENU_STATE_MAGIC_BAG : BNE .not_second
LDA.b #$0002 : JMP .prepare_bottle LDA.b #$0002 : JMP .prepare_bottle
.not_second .not_second
@@ -514,13 +534,68 @@ Menu_InitiateScrollDown:
DEX : BNE .loop4 DEX : BNE .loop4
LDA.b #$24 : STA.w $0116 LDA.b #$24 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
LDA.b #$08 : STA.w $0200 LDA.b #!MENU_STATE_SCROLL_UP : STA.w $0200
LDA.b #$12 : STA.w $012F ; play menu exit sound effect LDA.b #$12 : STA.w $012F ; play menu exit sound effect
RTS RTS
} }
; =========================================================
; SUBMENU GRID NAVIGATION
; Inputs:
; X = Address of Cursor Position Table
; Y = Max Index (Exclusive)
; Uses:
; $00-$01 (Scratch for table pointer)
; $020B (Cursor Index)
; =========================================================
Menu_HandleGridNavigation:
{
STX $00 ; Save Table Pointer
TYA ; Max Index to A
PHA ; Save Max
LDA.b $F4
LSR : BCS .move_right
LSR : BCS .move_left
LSR : BCS .move_down
LSR : BCS .move_up
PLA ; Clean stack
CLC : RTS
.move_up
.move_right
JSR .delete_cursor
PLA ; Get Max
STA $02 ; Store Max
INC.w $020B
LDA.w $020B : CMP $02 : BCC .done
STZ.w $020B
BRA .done
.move_down
.move_left
JSR .delete_cursor
PLA ; Clean stack
LDA.w $020B : BEQ .done
DEC.w $020B
BRA .done
.delete_cursor
REP #$30
LDA.w $020B : ASL : TAY
LDA ($00), Y : TAX
JSR Menu_DeleteCursor_AltEntry
SEP #$20
RTS
.done
SEC : RTS
}
; ========================================================= ; =========================================================
; 0C MENU MAGIC BAG ; 0C MENU MAGIC BAG
@@ -531,38 +606,11 @@ Menu_MagicBag:
SEP #$30 SEP #$30
INC $0207 INC $0207
LDA.b $F4
LSR : BCS .move_right
LSR : BCS .move_left
LSR : BCS .move_down
LSR : BCS .move_up
BRA .continue
.move_up LDX.w #Menu_MagicBagCursorPositions
.move_right LDY.w #$0006
REP #$30 JSR Menu_HandleGridNavigation
LDA.w $020B : ASL : TAY ; Y = current index * 2
LDX.w Menu_MagicBagCursorPositions, Y
JSR Menu_DeleteCursor_AltEntry
SEP #$20 ; Back to 8-bit A after JSR
INC.w $020B
LDA.w $020B : CMP.b #$06 : BCC .continue
STZ.w $020B ; Wrap to 0
BRA .continue
.move_down
.move_left
REP #$30
LDA.w $020B : ASL : TAY ; Y = current index * 2
LDX.w Menu_MagicBagCursorPositions, Y
JSR Menu_DeleteCursor_AltEntry
SEP #$20 ; Back to 8-bit A after JSR
LDA.w $020B : BEQ .continue ; Already at 0, don't decrement
DEC.w $020B
BRA .continue
.zero
STZ.w $020B
.continue
JSR DrawCollectibleNamesAndCount JSR DrawCollectibleNamesAndCount
LDA.w $020B LDA.w $020B
ASL : TAY ASL : TAY
@@ -575,7 +623,7 @@ Menu_MagicBag:
; Trigger VRAM tilemap upload ; Trigger VRAM tilemap upload
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
RTS RTS
} }
@@ -717,7 +765,7 @@ Menu_SongMenu:
SEP #$20 SEP #$20
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
RTS RTS
} }
@@ -737,32 +785,9 @@ Menu_RingBox:
JSR Menu_DrawMagicRingsInBox JSR Menu_DrawMagicRingsInBox
INC $0207 INC $0207
LDA.b $F4 LDX.w #Menu_RingIconCursorPositions
LSR : BCS .move_right LDY.w #$0006
LSR : BCS .move_left JSR Menu_HandleGridNavigation
LSR : BCS .move_down
LSR : BCS .move_up
BRA .continue
.move_up
.move_right
REP #$30
LDX.w Menu_RingIconCursorPositions-2, Y
JSR Menu_DeleteCursor_AltEntry
INC.w $020B
LDA.w $020B : CMP.b #$06 : BCS .zero
BRA .continue
.move_left
.move_down
REP #$30
LDX.w Menu_RingIconCursorPositions-2, Y
JSR Menu_DeleteCursor_AltEntry
LDA.w $020B : CMP.b #$00 : BEQ .continue
DEC.w $020B
BRA .continue
.zero
STZ.w $020B
.continue
JSR DrawMagicRingNames JSR DrawMagicRingNames
LDA.w $020B : ASL : TAY LDA.w $020B : ASL : TAY
@@ -773,7 +798,7 @@ Menu_RingBox:
SEP #$20 SEP #$20
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
RTS RTS
} }
@@ -840,12 +865,12 @@ RingMenu_Controls:
; Return to item menu if player presses X ; Return to item menu if player presses X
LDA.b $F6 : BIT.b #$40 : BEQ + LDA.b $F6 : BIT.b #$40 : BEQ +
LDA.b #$0F : STA.w $0200 LDA.b #!MENU_STATE_SUBMENU_RETURN : STA.w $0200
+ +
; Close the menu if the player presses start ; Close the menu if the player presses start
LDA.b $F4 : BIT.b #$10 : BEQ + LDA.b $F4 : BIT.b #$10 : BEQ +
LDA.b #$08 : STA.w $0200 LDA.b #!MENU_STATE_SCROLL_UP : STA.w $0200
+ +
RTS RTS
@@ -860,7 +885,7 @@ Menu_Journal:
JSR Submenu_Return JSR Submenu_Return
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 LDA.b #$01 : STA.b $17
RTS RTS
} }
@@ -869,8 +894,8 @@ Menu_SubmenuReturn:
STZ.w $0116 ; Clear VRAM flag to prevent partial upload STZ.w $0116 ; Clear VRAM flag to prevent partial upload
JSR Menu_RefreshInventoryScreen JSR Menu_RefreshInventoryScreen
LDA.b #$22 : STA.w $0116 LDA.b #$22 : STA.w $0116
LDA.b #$01 : STA.b $17 : STA.b $15 : STA.b $15 LDA.b #$01 : STA.b $17 : STA.b $15
LDA.b #$04 : STA.w $0200 ; Set state to Item Screen LDA.b #!MENU_STATE_ITEM_SCREEN : STA.w $0200 ; Set state to Item Screen
RTS RTS
} }
@@ -878,12 +903,12 @@ Submenu_Return:
{ {
; Return to the item menu if they press A ; Return to the item menu if they press A
LDA.b $F6 : BIT.b #$80 : BEQ + LDA.b $F6 : BIT.b #$80 : BEQ +
LDA.b #$0F : STA.w $0200 LDA.b #!MENU_STATE_SUBMENU_RETURN : STA.w $0200
+ +
; Close the menu if the player presses start ; Close the menu if the player presses start
LDA.b $F4 : BIT.b #$10 : BEQ + LDA.b $F4 : BIT.b #$10 : BEQ +
LDA.b #$08 : STA.w $0200 LDA.b #!MENU_STATE_SCROLL_UP : STA.w $0200
+ +
RTS RTS
} }

View File

@@ -329,135 +329,3 @@ assert pc() <= $0DE3C7
pullpc pullpc
; =========================================================
; Stats Screen Selector Logic
; =========================================================
Menu_StatsCursorPositions:
; Row 14: Sword, Shield, Tunic
dw menu_offset(14,2) ; 0: Sword
dw menu_offset(14,5) ; 1: Shield
dw menu_offset(14,8) ; 2: Tunic
dw menu_offset(14,8) ; 3: Placeholder (maps to Tunic)
; Row 17: Glove, Flippers, Boots, Pearl
dw menu_offset(17,2) ; 4: Glove
dw menu_offset(17,5) ; 5: Flippers
dw menu_offset(17,8) ; 6: Boots
dw menu_offset(17,11) ; 7: Pearl
Stats_Nav_Right:
db $01, $02, $00, $00, $05, $06, $07, $04
Stats_Nav_Left:
db $02, $00, $01, $01, $07, $04, $05, $06
Stats_Nav_Down:
db $04, $05, $06, $07, $00, $01, $02, $02
Stats_Nav_Up:
db $04, $05, $06, $07, $00, $01, $02, $02
Menu_DrawStatsCursor:
{
LDA.b #$20 : BIT.w $0207
REP #$20
BEQ .no_delete
; Delete cursor
LDA.w #$20F5
STA.w $1108, X : STA.w $1148, X
STA.w $114E, X : STA.w $110E, X
STA.w $11C8, X : STA.w $1188, X
STA.w $118E, X : STA.w $11CE, X
BRA .done
.no_delete
; We reuse the Item Menu cursor graphics
LDA.w #$3060 : STA.w $1108, X ; corner
LDA.w #$3070 : STA.w $1148, X
LDA.w #$7060 : STA.w $110E, X ; corner
LDA.w #$7070 : STA.w $114E, X
LDA.w #$3070 : STA.w $1188, X
LDA.w #$B060 : STA.w $11C8, X ; corner
LDA.w #$7070 : STA.w $118E, X
LDA.w #$F060 : STA.w $11CE, X ; corner
.done
SEP #$20
RTS
}
Menu_DeleteStatsCursor:
{
REP #$30
; Use $020C for index
LDA.w $020C : AND.w #$00FF : ASL : TAY
LDX.w Menu_StatsCursorPositions, Y
LDA.w #$20F5
STA.w $1108, X : STA.w $1148, X
STA.w $114E, X : STA.w $110E, X
STA.w $11C8, X : STA.w $1188, X
STA.w $118E, X : STA.w $11CE, X
SEP #$30
STZ $0207
RTS
}
Menu_StatsScreen_Input:
{
INC $0207
; Safety check for uninitialized index
LDA.w $020C : AND #$F8 : BEQ .safe_index
STZ.w $020C
.safe_index
LDA.b $F4 ; Joypad
LSR : BCS .move_right
LSR : BCS .move_left
LSR : BCS .move_down
LSR : BCS .move_up
BRA .draw_cursor
.move_right
JSR Menu_DeleteStatsCursor
LDY.w $020C
LDA.w Stats_Nav_Right, Y
STA.w $020C
BRA .play_sound
.move_left
JSR Menu_DeleteStatsCursor
LDY.w $020C
LDA.w Stats_Nav_Left, Y
STA.w $020C
BRA .play_sound
.move_down
JSR Menu_DeleteStatsCursor
LDY.w $020C
LDA.w Stats_Nav_Down, Y
STA.w $020C
BRA .play_sound
.move_up
JSR Menu_DeleteStatsCursor
LDY.w $020C
LDA.w Stats_Nav_Up, Y
STA.w $020C
BRA .play_sound
.play_sound
LDA.b #$20 : STA.w $012F
.draw_cursor
SEP #$30
LDA.w $020C : ASL : TAY
REP #$10
LDX.w Menu_StatsCursorPositions, Y
JSR Menu_DrawStatsCursor
SEP #$20
RTS
}