;==================================================================
;Project Title: CRYSTALIUM
;Author:        Vladislav Borzunov aka ObelardO
;Email:         Obelardos@gmail.com 
;Version:       0.9 Alpha
;Date:          03.03.13
;Notes:         Easy menu system
;==================================================================

; GUI Screen and font
Global MENU_WIDTH  = 800
Global MENU_HEIGHT = 600
Global MENU_FONT
Global MENU_CURSOS

; Objects
Type TYPE_MENU
	Field Name$
End Type

Type TYPE_LABEL
	Field Parent$
	Field Caption$
	Field X
	Field Y
	Field Spasing
	Field Length
End Type

Type TYPE_FRAME
	Field Parent$
	Field X
	Field Y
	Field Width
	Field Height
	Field ClrR
	Field ClrG
	Field ClrB
	Field Alpha
	Field Clear
End Type

Type TYPE_BUTTON
	Field Parent$
	Field X
	Field Y
	Field Width
	Field Height
	Field Caption$
	Field Command$
	Field Frame
	Field LabelX
	Field LabelY
End Type

Type TYPE_IMAGE
	Field Parent$
	Field Image
	Field X
	Field Y
	Field Width
	Field Height
	Field Frame
	Field FrameCount
	Field Animated
End Type

; Create new menu
Function CreateMenu(Name$)
	For MENU.TYPE_MENU = Each TYPE_MENU
		If Name = MENU\NAME Detected = True
	Next
	If Not Detected
		MenuFile$ = OpenFile("data\menus\"+GAME_LANGUAGE+"\"+Name+".cmn")
		If MenuFile
			MENU.TYPE_MENU = New TYPE_MENU
			MENU\Name = Name
			GAME_PAUSED = True
			While Not Eof(MenuFile)
				MenuLine$ = ReadLine(MenuFile)
				Select StrCut(MenuLine,1)
					Case "label"
						MenuLine = Replace(MenuLine,"label ","")
						LABEL.TYPE_LABEL = New TYPE_LABEL
						LABEL\Parent   = MENU\NAME
						LABEL\Caption  = StrCut(MenuLine,3,";")
						LABEL\Spasing  = StrCut(MenuLine,4,";")
						LABEL\Length   = Len(LABEL\Caption) * LABEL\Spasing
						PositionX$     = StrCut(MenuLine,1,";")
						PositionY$     = StrCut(MenuLine,2,";")
						LABEL\X        = PositionXConversion(PositionX, LABEL\Length, LABEL\Spasing)
						LABEL\Y	   = PositionYConversion(PositionY)
					Case "frame"
						MenuLine = Replace(MenuLine,"frame ","")
						FRAME.TYPE_FRAME = New TYPE_FRAME
						FRAME\Parent   = MENU\NAME
						FRAME\Width    = StrToInt(StrCut(MenuLine,3,";"))
						FRAME\Height   = StrToInt(StrCut(MenuLine,4,";"))
						FRAME\X        = PositionXConversion(strCut(MenuLine,1,";"), FRAME\Width)
						FRAME\Y        = PositionYConversion(strCut(MenuLine,2,";"))
						FRAME\ClrR     = StrToInt(StrCut(MenuLine,5,";"))
						FRAME\ClrG     = StrToInt(StrCut(MenuLine,6,";"))
						FRAME\ClrB     = StrToInt(StrCut(MenuLine,7,";"))
						FRAME\Alpha    = StrToInt(StrCut(MenuLine,8,";"))
						FRAME\Clear    = StrToInt(StrCut(MenuLine,9,";"))
					Case "button"
						MenuLine = Replace(MenuLine,"button ","")
						BUTTON.TYPE_BUTTON = New TYPE_BUTTON
						BUTTON\Parent  = MENU\NAME
						BUTTON\Width   = StrToInt(StrCut(MenuLine,3,";"))
						BUTTON\Height  = StrToInt(StrCut(MenuLine,4,";"))
						BUTTON\x       = PositionXConversion(StrCut(MenuLine,1,";"), BUTTON\Width)
						BUTTON\Y       = PositionYConversion(strCut(MenuLine,2,";"))
						BUTTON\Frame   = StrToInt(StrCut(MenuLine,7,";"))
						BUTTON\Caption = StrCut(MenuLine,5,";")
						BUTTON\Command = StrCut(MenuLine,6,";")
						BUTTON\LabelX  = BUTTON\X + BUTTON\Width * 0.5 - Len(BUTTON\Caption) * 0.5 * 20
						BUTTON\LabelY  = BUTTON\Y + BUTTON\Height * 0.5 - 10
					Case "image"
						MenuLine = Replace(MenuLine,"image ","")
						IMAGE.TYPE_IMAGE = New TYPE_IMAGE
						IMAGE\Parent     = MENU\NAME
						IMAGE\FrameCount = StrToInt(StrCut(MenuLine,7,";"))
						IMAGE\Width      = StrToInt(StrCut(MenuLine,3,";"))
						IMAGE\Height     = StrToInt(StrCut(MenuLine,4,";"))
						IMAGE\x          = PositionXConversion(StrCut(MenuLine,1,";"), IMAGE\Width)
						IMAGE\Y          = PositionYConversion(strCut(MenuLine,2,";"))
						IMAGE\Image      = xLoadAnimImage("data\"+StrCut(MenuLine,5,";"), IMAGE\Width, IMAGE\Height, IMAGE\Frame, IMAGE\FrameCount)
						IMAGE\Frame	     = StrToInt(StrCut(MenuLine,6,";"))
						IMAGE\Animated   = StrToInt(StrCut(MenuLine,8,";"))
						
				End Select
			Wend
			CloseFile(MenuFile)
			xPlaySound SOUND_MENU_OPEN
			;xMoveMouse WINDOW_WIDTH * 0.5, WINDOW_HEIGHT * 0.5
		Else
			Loggin("[ERROR] Menu "+StrQuote(Name)+" not found.")
		End If
	End If
End Function

; Conversion tegs
Function PositionYConversion(Position$)
	Select Position
		Case "center"   :Return (WINDOW_HEIGHT * 0.5)
		Case "down"     :Return (WINDOW_HEIGHT - MENU_HEIGHT) * 0.5 + MENU_HEIGHT - 32
		Case "top"      :Return (WINDOW_HEIGHT - MENU_HEIGHT) * 0.5 
		Default         :Return (WINDOW_HEIGHT - MENU_HEIGHT) * 0.5 + StrToInt(Position)
	End Select
End Function

; Conversion tegs
Function PositionXConversion(Position$, Length = 0, Spasing = 0)
	Select Position
		Case "center"   :Return (WINDOW_WIDTH - Length + Spasing * 0.5) * 0.5 
		Case "right"    :Return (WINDOW_WIDTH - MENU_WIDTH) * 0.5 + (MENU_WIDTH * 0.5 - Length) + MENU_WIDTH * 0.5
		Case "left"     :Return (WINDOW_WIDTH - MENU_WIDTH) * 0.5
		Default         :Return (WINDOW_WIDTH - MENU_WIDTH) * 0.5 + StrToInt(Position)
	End Select
End Function

; delete menu
Function DeleteMenu(Name$)
	For MENU.TYPE_MENU = Each TYPE_MENU
		If Name = MENU\Name
			For FRAME.TYPE_FRAME = Each TYPE_FRAME
				If FRAME\Parent = MENU\NAME
					FRAME\Parent$ = 0
					FRAME\X       = 0
					FRAME\Y       = 0
					FRAME\Width   = 0
					FRAME\Height  = 0
					FRAME\ClrR    = 0
					FRAME\ClrG    = 0
					FRAME\ClrB    = 0
					FRAME\Alpha   = 0
					FRAME\Clear   = 0
					Delete FRAME
				End If
			Next

			For LABEL.TYPE_LABEL = Each TYPE_LABEL
				If LABEL\Parent = MENU\NAME
					LABEL\Parent$  = 0
					LABEL\Caption$ = 0
					LABEL\X        = 0
					LABEL\Y        = 0
					LABEL\Spasing  = 0
					LABEL\Length   = 0
					Delete LABEL
				End If
			Next

			For BUTTON.TYPE_BUTTON = Each TYPE_BUTTON
				If BUTTON\Parent = MENU\NAME
					BUTTON\Parent$  = 0
					BUTTON\X        = 0
					BUTTON\Y        = 0
					BUTTON\Width    = 0
					BUTTON\Height   = 0
					BUTTON\Caption$ = 0
					BUTTON\Command$ = 0
					BUTTON\Frame    = 0
					BUTTON\LabelX   = 0
					BUTTON\LabelY   = 0
					Delete BUTTON
				End If
			Next

			For IMAGE.TYPE_IMAGE = Each TYPE_IMAGE
				If IMAGE\Parent = MENU\NAME
					IMAGE\Parent     = 0
					IMAGE\Width      = 0
					IMAGE\Height     = 0
					IMAGE\x          = 0
					IMAGE\Y          = 0
					IMAGE\FrameCount = 0
					IMAGE\Frame	     = 0
					IMAGE\Animated   = 0
					If IMAGE\Image xFreeImage IMAGE\Image
					Delete IMAGE
				End If
			Next

			MENU\Name = ""
			Delete MENU
			GAME_PAUSED = 0
		End If
	Next
End Function

; Update menus
Function UpdateMenus()
	CursorX = xMouseX():CursorY = xMouseY()

	; Default fon
	xColor 235, 235, 202, 255
	xLine  0, WINDOW_HEIGHT * 0.1 + 1, WINDOW_WIDTH, WINDOW_HEIGHT * 0.1 + 1
	xLine  0, WINDOW_HEIGHT - WINDOW_HEIGHT * 0.1 - 1, WINDOW_WIDTH, WINDOW_HEIGHT - WINDOW_HEIGHT * 0.1 - 1
	xColor 0, 0, 0, 128 :xRect 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 1
	xColor 0, 0, 0, 255 :xRect 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT * 0.1, 1
	xRect 0, WINDOW_HEIGHT - WINDOW_HEIGHT * 0.1, WINDOW_WIDTH, WINDOW_HEIGHT * 0.1, 1

	; debug screen
	If DEBUG_MODE 
		xColor 0, 0, 255, 32
		xRect (WINDOW_WIDTH - MENU_WIDTH) * 0.5, (WINDOW_HEIGHT - MENU_HEIGHT) * 0.5, MENU_WIDTH, MENU_HEIGHT, 1
	End If

	; frames
	For FRAME.TYPE_FRAME = Each TYPE_FRAME
		xColor FRAME\ClrR, FRAME\ClrG, FRAME\ClrB, FRAME\Alpha
		xRect FRAME\X, FRAME\Y, FRAME\Width, FRAME\Height, FRAME\Clear
	Next

	; images
	For IMAGE.TYPE_IMAGE = Each TYPE_IMAGE
		If IMAGE\Image
			If IMAGE\Animated 
				IMAGE\Frame = MilliSecs() / 25 Mod IMAGE\FrameCount
				xDrawImage IMAGE\Image, IMAGE\X, IMAGE\Y, IMAGE\Frame
			Else
				xDrawImage IMAGE\Image, IMAGE\X, IMAGE\Y, IMAGE\Frame
			End If
		End If
	Next

	; buttons
	For BUTTON.TYPE_BUTTON = Each TYPE_BUTTON
		If BUTTON\Frame
			xColor 235, 235, 202, 255 :xRect BUTTON\X, BUTTON\Y, BUTTON\Width, BUTTON\Height, 0
			xColor   0,   0,   0, 128 :xRect BUTTON\X, BUTTON\Y, BUTTON\Width, BUTTON\Height, 1
		End If
		TextDraw(BUTTON\LabelX, BUTTON\LabelY, BUTTON\Caption, 20)

		
		If CursorX > BUTTON\X And CursorX < BUTTON\X + BUTTON\Width
		If CursorY > BUTTON\Y And CursorY < BUTTON\Y + BUTTON\Height
			If BUTTON\Frame
				xColor 235,235,202,255
				xRect BUTTON\X, BUTTON\Y, BUTTON\Width, BUTTON\Height, 0
			Else
				TextDraw(BUTTON\LabelX - 40, BUTTON\LabelY, "> "+BUTTON\Caption+ " <", 20)
			End If
			If xMouseHit(1) = 1
				Command$ = BUTTON\Command
				If Command = "null" xPlaySound SOUND_MENU_OPEN
				DeleteMenu(BUTTON\Parent)
				GAME_PAUSED = 0
				InGameCommand(Command)
			End If
		End If
		End If
	Next

	; labels
	For LABEL.TYPE_LABEL = Each TYPE_LABEL
		TextDraw(LABEL\X, LABEL\Y, LABEL\Caption, LABEL\Spasing)
	Next

	xDrawImage MENU_CURSOS, CursorX, CursorY
End Function

; Drawing text
Function TextDraw(x = 0, y = 0, InputString$, spacing = 20)
	For i = 1 To Len(InputString)
		Char = Asc(Mid(Inputstring, i, 1))
		xDrawImage MENU_FONT, x  + (i - 1) * spacing, y, Char
	Next
End Function