OptionsMenu:
	ld hl, hInMenu
	ld a, [hl]
	push af
	ld [hl], $1
	call ClearBGPalettes
	hlcoord 0, 0
	lb bc, 16, 18
	call TextBox
	hlcoord 2, 2
	ld de, .options_string
	call PlaceText
	xor a
	ld [wJumptableIndex], a
	ld c, 6 ; number of items on the menu minus 1 (for cancel)

.print_text_loop ; this next will display the settings of each option when the menu is opened
	push bc
	xor a
	ld [hJoyLast], a
	call GetOptionPointer
	pop bc
	ld hl, wJumptableIndex
	inc [hl]
	dec c
	jr nz, .print_text_loop

	call UpdateFrame
	xor a
	ld [wJumptableIndex], a
	inc a
	ld [hBGMapMode], a
	call ApplyTilemapInVBlank
	ld b, SCGB_SCROLLINGMENU
	predef GetSGBLayout
	call SetPalettes

.joypad_loop
	call JoyTextDelay
	ld a, [hJoyPressed]
	and START | B_BUTTON
	jr nz, .ExitOptions
	call OptionsControl
	jr c, .dpad
	call GetOptionPointer
	jr c, .ExitOptions

.dpad
	call Options_UpdateCursorPosition
	call Delay2
	jr .joypad_loop

.ExitOptions
	ld de, SFX_TRANSACTION
	call PlayWaitSFX
	pop af
	ld [hInMenu], a
	ret

.options_string
	db TX_COMPRESSED, $28, $e9, $db, $8a, $04, $19
	db $e4, $4d, $60, $72, $49, $24, $8e
	db $22, $6b, $03, $12, $db, $be, $9c, $ac, $83, $7f, $6c, $61, $22
	db $04, $d6, $07, $24, $92, $48
	db $e2, $26, $b0, $31, $2d, $bb, $e9, $c5, $16, $0b
	db $a6, $26, $b0, $39, $24, $92, $47
	db $11, $35, $81, $a8, $0b, $44
	db $c8, $9a, $c0, $e4, $92, $49, $1c
	db $44, $d6, $07, $c2, $b1, $92, $cd, $71, $41, $dd, $8c, $9c
	db $89, $ac, $0e, $49, $24, $91
	db $c4, $4d, $60, $65, $34, $b2, $0c, $9c, $c5, $02, $0c
	db $f2, $26, $b0, $39, $24, $92, $47
	db $11, $35, $81, $94, $74, $ed, $c5, $35, $eb
	db $e6, $26, $b0, $39, $24, $92, $47, $12, $81, $08
	db $31, $35, $81, $f0, $04
	db $87, $4d

GetOptionPointer:
	call RunAnonymousJumptable

.Pointers
	dw Options_TextSpeed
	dw Options_BattleScene
	dw Options_BattleStyle
	dw Options_Sound
	dw Options_Print
	dw Options_TurningSpeed
	dw Options_Frame
	dw Options_Cancel

Options_TextSpeed:
	ld a, [wOptions]
	ld c, a
	ld a, [hJoyPressed]
	bit D_LEFT_F, a
	jr nz, .LeftPressed
	bit D_RIGHT_F, a
	jr z, .NonePressed
	inc c
	inc c

.LeftPressed
	dec c

.Save
	ld a, c
	and %11
	ld b, a
	ld a, [wOptions]
	and %11111100
	or b
	ld [wOptions], a

.NonePressed
	ld a, c
	and %11
	ld bc, 5
	ld hl, .Inst
	rst AddNTimes
	ld d, h
	ld e, l
	hlcoord 11, 3
	call PlaceString
	and a
	ret

.Inst
	db "Inst@"
.Fast
	db "Fast@"
.Mid
	db "Mid @"
.Slow
	db "Slow@"

Options_BattleScene:
	ld hl, wOptions
	ld a, [hJoyPressed]
	and D_RIGHT | D_LEFT
	ld a, [hl]
	jr nz, .switchBattleScene
	and 1 << BATTLE_SCENE
	jr .getString
.switchBattleScene
	xor 1 << BATTLE_SCENE
	ld [hl], a
.getString
	ld de, .On
	jr z, .gotString
	ld de, .Off
.gotString
	hlcoord 11, 5
	call PlaceString
	and a
	ret

.On
	db "On @"
.Off
	db "Off@"

Options_BattleStyle:
	ld hl, wOptions
	ld a, [hJoyPressed]
	and D_RIGHT | D_LEFT
	ld a, [hl]
	jr nz, .switchBattleStyle
	and 1 << BATTLE_SHIFT
	jr .getString
.switchBattleStyle
	xor 1 << BATTLE_SHIFT
	ld [hl], a
.getString
	ld de, .Shift
	jr z, .gotString
	ld de, .Set
.gotString
	hlcoord 11, 7
	call PlaceString
	and a
	ret

.Shift
	db "Shift@"
.Set
	db "Set  @"

Options_Sound:
	ld hl, wOptions
	ld a, [hJoyPressed]
	and D_LEFT | D_RIGHT
	ld a, [hl]
	jr nz, .switchSpeakers
	and 1 << STEREO
	jr .getString
.switchSpeakers
	xor 1 << STEREO
	ld [hl], a
.getString
	ld de, .Mono
	jr z, .gotString
	ld de, .Stereo
.gotString
	hlcoord 11, 9
	call PlaceString
	and a
	ret

.Mono
	db "Mono  @"
.Stereo
	db "Stereo@"

Options_Print:
	call GetPrinterSetting
	ld a, [hJoyPressed]
	bit D_LEFT_F, a
	jr nz, .LeftPressed
	bit D_RIGHT_F, a
	jr z, .NonePressed
	ld a, c
	cp 4
	jr c, .Increase
	ld c, -1

.Increase
	inc c
	ld a, e
	jr .Save

.LeftPressed
	ld a, c
	and a
	jr nz, .Decrease
	ld c, 5

.Decrease
	dec c
	ld a, d

.Save
	ld b, a
	ld [GBPrinter], a

.NonePressed
	ld a, c
	ld bc, 9
	ld hl, .Lightest
	rst AddNTimes
	ld d, h
	ld e, l
	hlcoord 11, 11
	call PlaceString
	and a
	ret

.Lightest
	db "Lightest@"
.Lighter
	db "Lighter @"
.Normal
	db "Normal  @"
.Darker
	db "Darker  @"
.Darkest
	db "Darkest @"

GetPrinterSetting:
	ld a, [GBPrinter] ; converts from the stored printer setting to 0,1,2,3,4
	and a
	ld c, a
	lb de, PRINT_DARKEST, PRINT_LIGHTER ; the 2 values next to this setting
	ret z
	ld c, 1
	cp PRINT_LIGHTER
	lb de, PRINT_LIGHTEST, PRINT_NORMAL ; the 2 values next to this setting
	ret z
	ld c, 3
	cp PRINT_DARKER
	lb de, PRINT_NORMAL, PRINT_DARKEST ; the 2 values next to this setting
	ret z
	inc c
	cp PRINT_DARKEST
	lb de, PRINT_DARKER, PRINT_LIGHTEST ; the 2 values next to this setting
	ret z
	ld c, 2 ; normal if none of the above
	lb de, PRINT_LIGHTER, PRINT_DARKER ; the 2 values next to this setting
	ret

Options_Frame:
	ld hl, TextBoxFrame
	ld a, [hJoyPressed]
	bit D_LEFT_F, a
	jr nz, .LeftPressed
	bit D_RIGHT_F, a
	jr nz, .RightPressed
	and a
	ret

.RightPressed
	ld a, [hl]
	inc a
	jr .Save

.LeftPressed
	ld a, [hl]
	dec a

.Save
	and $7
	ld [hl], a
UpdateFrame:
	ld a, [TextBoxFrame]
	add "1"
	ldcoord_a 16, 15
	call LoadFontsExtra
	and a
	ret

Options_TurningSpeed:
	ld hl, wOptions
	ld a, [hJoyPressed]
	and D_LEFT | D_RIGHT
	ld a, [hl]
	jr nz, .switchTurningSpeed
	and 1 << TURNING_SPEED
	jr .getString
.switchTurningSpeed
	xor 1 << TURNING_SPEED
	ld [hl], a
.getString
	ld de, .Slow
	jr z, .gotString
	ld de, .Fast
.gotString
	hlcoord 11, 13
	call PlaceString
	and a
	ret

.Fast
	db "Fast@"
.Slow
	db "Slow@"

Options_Cancel:
	ld a, [hJoyPressed]
	and A_BUTTON
	ret z
	scf
	ret

OptionsControl:
	ld hl, wJumptableIndex
	ld b, [hl]
	ld a, [hJoyLast]
	cp D_DOWN
	jr z, .DownPressed
	cp D_UP
	jr z, .UpPressed
	and a
	ret

.DownPressed
	inc b
	inc b
.UpPressed
	dec b
	ld a, b
	and 7
	ld [hl], a
	scf
	ret

Options_UpdateCursorPosition:
	hlcoord 1, 1
	ld de, SCREEN_WIDTH
	ld c, $10
.loop
	ld [hl], " "
	add hl, de
	dec c
	jr nz, .loop
	hlcoord 1, 2
	ld bc, 2 * SCREEN_WIDTH
	ld a, [wJumptableIndex]
	rst AddNTimes
	ld [hl], "▶"
	ret

