;*** setup apparence of the menu font/cursor ***
.MENU_SetSkin
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	
	ld A,D
	or E
	ret z
	
	ld C,(HL)
	inc HL
	ld B,(HL)
	inc HL
	
	ex DE,HL
	ld (HL),C
	inc HL
	ld (HL),B
	ex DE,HL
	
	jr MENU_SetSkin


;menu colors and cursor table
.MENU_OPTION_SKIN
	dw	MENU_SelectedColor,color_table_options_red
	dw	MENU_SelectedFont,FONT_DrawStringEX	
	dw	MENU_CurrentCursor,menu_cursor
	dw	MENU_NotSelectedColor,color_table_options_normal
	dw	MENU_NotSelectedFont,FONT_DrawStringEX	
	dw	0
.MENU_TITLE_SKIN
	dw	MENU_SelectedColor,color_table_title_bold
	dw	MENU_SelectedFont,FONT_DrawStringEX	
	dw	MENU_CurrentCursor,menu_nocursor	
	dw	MENU_NotSelectedColor,color_table_options_normal
	dw	MENU_NotSelectedFont,FONT_DrawString		
	dw	0



;**********************
;*** menu subsystem ***
;**********************

; draw menu and options
; HL = Menu address
;
MENU_Init:
	ld (current_menu),HL
	push HL
	pop IX
	
	;reset pos
	ld (IX+0),0
	
	ld B,(IX+1)
	
	ld DE,4
	add IX,DE
	
	xor A

	
.MENU_Init_loop
	ex AF,AF'
	push BC
	
	ld L,(IX+0)
	ld H,(IX+1)
	call MENU_Init_Sub1
	
	inc IX
	inc IX
	
	pop BC
	ex AF,AF'
	inc A
	cp B
	jr c,MENU_Init_loop
	
	ret
	
;sub that draws all	
	
.MENU_Init_Sub1
	
;get in IY , easier
	push HL
	pop IY

;first draw menu name	
	ld H,(IY+2)
	ld L,(IY+3)
	push HL ;save pos for later
;get address from pos
	call GFX_GetCharAddress	
	ex DE,HL
	ld L,(IY+4)
	ld H,(IY+5)
	ld BC,color_table_options_normal
	call FONT_DrawStringEX

;draw current item
;get address from pos	
	ld H,(IY+6)
	ld L,(IY+7)
	call GFX_GetCharAddress
	push HL
	
;get current idem	
	ld A,(IY)
	add A	
;multiplies by 2 and store in DE	
	ld E,A
	ld D,0	
	add IY,DE
	ld L,(IY+8)
	ld H,(IY+9)
	pop DE

;check if selected and use red font	
	ld BC,(current_menu)
	ld A,(BC)
	ld B,A
	ex AF,AF'
	cp B	
	
	jr nz,MENU_Init_Sub1_NotSelected
MENU_SelectedColor	EQU $+1
	ld BC,color_table_options_red
	ex AF,AF'
MENU_SelectedFont	EQU $+1
	call FONT_DrawStringEX
MENU_CurrentCursor	EQU $+1
	ld DE,menu_cursor
	jr MENU_Init_Sub1_Done
	
.MENU_Init_Sub1_NotSelected	
MENU_NotSelectedColor	EQU $+1
	ld BC,color_table_options_normal
	ex AF,AF'
MENU_NotSelectedFont	EQU $+1
	call FONT_DrawStringEX
	ld DE,menu_nocursor	

.MENU_Init_Sub1_Done
	pop HL
	push DE
	dec H
	dec H
	call GFX_GetCharAddress
	ex DE,HL
	pop HL
	call FONT_DrawString
	
	ret	

;update item 
MENU_UpdateItem:
	ld HL,(current_menu)
	
	repeat 4
	inc HL
	rend
	
	ld E,A
	ex AF,AF'
	ld A,E
	ex AF,AF'
	
	add A
	ld E,A
	ld D,0
	add HL,DE
	
	ld E,(HL)
	inc HL
	ld D,(HL)
	ex DE,HL
	
	call MENU_Init_Sub1
	ret
.menu_cursor
	STR "#"
.menu_nocursor
	STR " "

;menu with timer , HL = frames to stay 
.MENU_RunWithTimer
	ld (MENU_Timer),HL
	ld A,1
	jr MENU_RunEntry
;menu loop
MENU_Run:
	xor A
.MENU_RunEntry
	ld (MENU_Run_IsTimer),A

.MENU_RunLoop
;sync on 50HZ
	ld A,(int_counter)
	cp 5
	jr nz,MENU_RunLoop
	
	di
	call KEY_Update
	ei
	call KEY_UpdateState
	
;up	
	ld A,(keys)
	cp 1
	jr nz,MENU_Run_NoUp
	
	ld IX,(current_menu)
	ld A,(IX)	
	dec A
	cp (IX+1)
	;jr nc,MENU_Run_NoUp
	jr c,MENU_RunUpNoCycle
	
	ld A,(IX+1)
	dec A
	
.MENU_RunUpNoCycle	
	;ld (IX),A
	;push AF
	;call MENU_UpdateItem
	;pop AF
	;inc A
	;call MENU_UpdateItem
	ld B,A
	push BC
	ld A,(IX)
	ld (IX),B
	call MENU_UpdateItem
	pop AF
	call MENU_UpdateItem
	
	ld A,sfx_cursor
	call SND_SfxPlay
	
.MENU_Run_NoUp

;down
	ld A,(keys+1)
	cp 1
	jr nz,MENU_Run_NoDown
	ld IX,(current_menu)
	ld A,(IX)
	inc A
	cp (IX+1)
	;jr nc,MENU_Run_NoDown
	jr c,MENU_RunDownNoCycle
	xor A
	
.MENU_RunDownNoCycle	
	;ld (IX),A
	;push AF
	;call MENU_UpdateItem
	;pop AF
	;dec A
	;call MENU_UpdateItem
	ld B,A
	push BC
	ld A,(IX)
	ld (IX),B
	call MENU_UpdateItem
	pop AF
	call MENU_UpdateItem

	
	ld A,sfx_cursor
	call SND_SfxPlay
.MENU_Run_NoDown

;left
	ld A,(keys+2)
	cp 1
	jr nz,MENU_Run_NoLeft
	
	ld HL,(current_menu)
	ld A,(HL)
	
	push AF
	add A,A
	ld E,A
	ld D,0
	add HL,DE
	repeat 4
	inc HL
	rend
	ld E,(HL)
	inc HL
	ld D,(HL)
	
	ex DE,HL
	
	ld A,(HL)
	dec A
	inc HL
	cp (HL)
	jr c,MENU_Run_LeftNoCycle
	ld A,(HL)
	dec A
.MENU_Run_LeftNoCycle	
	dec HL
	ld (HL),A
.MENU_Run_LeftDone

	ld A,128		;128 = right
	call MENU_CallBackDo
	
	pop AF
	call MENU_UpdateItem
	
.MENU_Run_NoLeft

;right
	ld A,(keys+3)
	cp 1
	jr nz,MENU_Run_NoRight
	
	ld HL,(current_menu)
	ld A,(HL)
	
	push AF
	add A,A
	ld E,A
	ld D,0
	add HL,DE
	repeat 4
	inc HL
	rend
	ld E,(HL)
	inc HL
	ld D,(HL)
	
	ex DE,HL
	ld A,(HL)
	inc A
	inc HL
	cp (HL)
	jr c,MENU_RunRightNoCycle
	xor A
.MENU_RunRightNoCycle
	dec HL
	ld (HL),A
	
.MENU_RunRightDone	
	ld A,64		;64 = left
	call MENU_CallBackDo
	
	pop AF
	call MENU_UpdateItem
	
.MENU_Run_NoRight

;fire
	ld A,(keys+4)
	cp 1
	jr nz,MENU_Run_NoFire
	xor A		;null = fire
	call MENU_CallBackDo
	
.MENU_Run_NoFire

;counter sync
	ld A,(int_counter)
	cp 5
	jr z,MENU_Run_NoFire
	
MENU_Run_IsTimer	EQU $+1
	ld A,0
	or A
	jp z,MENU_RunLoop
	
MENU_Timer	EQU $+1	
	ld HL,0
	dec HL
	ld (MENU_Timer),HL
	
	ld A,L
	or H
	jp nz,MENU_RunLoop
	
	ret

;pass a value to callback
; A = value OR current selected menu
.MENU_CallBackDo
	ld HL,(current_menu)
	or (HL)
	inc HL
	inc HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	ld (MENU_RunCallBack ),DE
	
MENU_RunCallBack EQU $+1
	jp menu_default_callback
	
	
current_menu
	dw 0


.menu_default_callback
	ret
	

	
	