;license:MIT ;(c) 2018-9 by 4am ; ; Search Mode - main UI ; ; Public functions ; - SearchMode ; !zone { ;------------------------------------------------------------------------------ ; SearchMode ; main entry point for Search Mode, which allows the user to search the game ; catalog and launch games ; ; in: none ; out: never returns to caller (may JMP to other major modes) ;------------------------------------------------------------------------------ SearchMode +READ_RAM1_WRITE_RAM1 ldx #$FF txs stx SelectedIndex ; no game selected inx stx OffscreenPage jsr Home ; clear screen jsr .OnInputChanged ; draw UI on HGR page 1 +HGR_MODE ; show HGR bit CLEARKBD jsr ResetInputTimeout .SearchModeInputLoop lda KBD bmi @gotKey inc RNDSEED+1 ; these are only ever incremented, never bne + ; reset (may be used as a pseudorandom inc RNDSEED ; seed) + dec Timeout ; these are a 3-byte timeout counter bne .SearchModeInputLoop ; that counts down from a number set dec Timeout+1 ; in ResetInputTimeout and reset bne .SearchModeInputLoop ; on every keypress (whether or not dec Timeout+2 ; the key leads to an action) bne .SearchModeInputLoop jsr .CoverFade ; no input for ~30 seconds, switch to jmp MegaAttractMode ; mega-attract mode @gotKey cmp #$88 ; left arrow switches to browse mode beq @arrow cmp #$8B ; also right arrow beq @arrow cmp #$95 ; also up arrow beq @arrow cmp #$8A ; also down arrow bne @notArrow @arrow ldx #kInputBrowse bne @InputDispatch ; always branches @notArrow bit CLEARKBD jsr ResetInputTimeout cmp #$9B ; Esc clears the input buffer (if any) bne + ; or switches to mega attract mode ldx #kInputClear bne @InputDispatch ; always branches + cmp #$8D ; ENTER launches the current game (if any) bne + ldx #kInputLaunch bne @InputDispatch ; always branches + cmp #$FF ; delete key bne + ldx #kInputBack bne @InputDispatch ; always branches + cmp #$89 ; TAB switches to mini attract mode bne + ; if there is a game selected ldx #kInputTab bne @InputDispatch ; always branches + and #$7F ; strip high bit for search characters cmp #$30 ; control keys and punctuation ignored bcc @badkey cmp #$3A ; numbers are good input bcc @goodkey cmp #$41 ; more punctuation (also ignored) bcc @badkey cmp #$5B ; uppercase letters are good input bcs + ora #$20 ; convert uppercase letters to lowercase bne @goodkey ; always branches + cmp #$61 ; more punctuation (also ignored) bcc @badkey cmp #$7B ; lowercase letters are good input bcc @goodkey @badkey jsr SoftBell ; beep on invalid input jmp .SearchModeInputLoop ; and start over @goodkey ldx #kInputSearch ; execution falls through here @InputDispatch pha ; save key pressed txa asl tax lda .InputDispatchTable,x sta @j+1 lda .InputDispatchTable+1,x sta @j+2 pla ; restore key pressed @j jsr $FDFD ; SMC bcs + jmp .SearchModeInputLoop ; if carry is clear, we're done + jmp SearchMode ; if carry is set, force full redraw .OnClear ldx InputLength bne + jsr .CoverFade ; Esc with no input switches to jmp MegaAttractMode ; mega-attract mode + ldx #0 ; Esc with input clears the input stx InputLength jmp .OnInputChanged .OnBack ldx InputLength bne + jsr SoftBell rts + dec InputLength jmp .OnInputChanged .OnTab ldx SelectedIndex cpx #$FF beq + jsr MiniAttractMode cmp #$8D ; if we exited mini attract mode beq .OnLaunch ; by pressing Enter, launch the game sec ; tell caller to redraw UI rts + clc rts .OnLaunch ldx SelectedIndex cpx #$FF beq + jsr PlayGameFromSearch sec ; tell caller to redraw UI rts + clc rts .OnSearch ldx InputLength cpx #MaxInputLength bne + jsr SoftBell clc rts + sta InputBuffer,x inc InputLength ; execution falls through here .OnInputChanged lda InputLength bne @findMatchingTitle ; no input, reset params and UI lda #$FF sta SelectedIndex ; no game selected ldx #40 ; reset visible line lda #0 - sta UILine1-1,x dex bne - ldy #MaxInputLength ; clear visible search bar lda #" " - sta UILine2+1,y dey bne - lda #$7F sta UILine2+1 jsr .LoadTitleOffscreen jsr DrawSearchBarOffscreen jsr ShowOtherPage clc rts @findMatchingTitle jsr ResetTextRank jsr okvs_iter_values ; iterate through all game titles !word gGamesListStore ; and rank them for the best match !word TextRankCallback ; to the current input buffer lda MatchCount ; any matches at all? bne + jsr SoftBell ; no matches for this input buffer, beep dec InputLength ; and ignore the last key typed clc rts + lda BestMatchIndex ; check if the new best match is the same cmp SelectedIndex ; as the current best match php ; (we'll use this later to skip reloading) sta SelectedIndex sta @index jsr okvs_nth ; get the name of the new best match !word gGamesListStore @index !byte $FD +STAY @key plp bne + jsr ToggleOffscreenPage ; Since we're not loading a new screenshot ; we fake switching the 'offscreen' page ; in order to draw on the visible page. bpl @skipload ; always branches + jsr GetOffscreenAddress ; we have a new best match, so load the sta + ; new title screenshot (offscreen) +LDADDR kHGRTitleDirectory jsr SetPath +LDAY @key jsr AddToPath jsr LoadFileAt !byte $00 + !byte $FD ; SMC @skipload jsr okvs_get !word gGamesListStore @key !word $FDFD +STAY SRC ; A/Y points to game title (in OKVS) ldy #0 ; copy game title into search bar buffer lda (SRC),y sta SAVE ; game title length inc SAVE - iny cpy SAVE bcc @printTitleChar beq @printCursor lda #" " +HIDE_NEXT_2_BYTES @printCursor lda #$7F +HIDE_NEXT_2_BYTES @printTitleChar lda (SRC),y ; copy game title to search UI sta UILine2,y cpy #MaxInputLength+1 bcc - ldx #40 lda #0 - sta UILine1-1,x ; reset search bar dex bne - tay - lda (SRC),y +LOW_ASCII_TO_LOWER cmp InputBuffer,x bne + lda #$0B ; add dots to highlight matched characters sta UILine1,y inx cpx InputLength beq @doneHighlight + inc HTAB iny cpy #40 bne - @doneHighlight jsr DrawSearchBarOffscreen; actually draw the search UI (offscreen) jmp ShowOtherPage ; now show everything at once ;------------------------------------------------------------------------------ .LoadTitleOffscreen ; [private] clobbers all +LDADDR @TitleFile bne + ; Always branches, because Y is loaded ; last with the high byte of the address, ; which is never 0. I miss my 65c02. .LoadCoverOffscreen ; [private] clobbers all +LDADDR @CoverFile + jsr SetPath jsr GetOffscreenAddress sta + jsr LoadFileAt !byte $00 + !byte $FD ; SMC rts @TitleFile !byte 5 !text "TITLE" @CoverFile !byte 5 !text "COVER" .CoverFade ; [private] clobbers all jsr .LoadCoverOffscreen jsr ShowOtherPage lda OffscreenPage bne + jsr .LoadCoverOffscreen jsr ShowOtherPage + ; load transition effect code at $6000 +LDADDR kFXDirectory jsr SetPath +LDADDR @CoverFadeFile jsr AddToPath jsr LoadFile jmp $6000 ; exit via loaded code @CoverFadeFile !byte 9 !text "COVERFADE" ; indices into InputDispatchTable kInputSearch = 0 kInputClear = 1 kInputBack = 2 kInputBrowse = 3 kInputTab = 4 kInputLaunch = 5 .InputDispatchTable !word .OnSearch !word .OnClear !word .OnBack !word BrowseMode !word .OnTab !word .OnLaunch }