#Include "fbgfx.bi"
Using fb

Type menu
	As Any Ptr imagemenu(1 To 12)'массив для буфера изображений
	event As EVENT 'буфер события меню
	As Integer w=516 'ширина кнопки изображения
	As Integer h=86 'высота кнопки изображения
	As Integer scrW=800 'ширина экрана
	As Integer scrH=600 'высота экрана
	As Integer indentation=20 'отступ между кнопками
	As Integer x,y 'координаты вывода кнопок
	As Integer Mx,My 'координаты курсора
	As Integer temp 'буфер для получения номера кнопки
	As Byte EVENT_PRESS ' флаг нажатия на кнопку
	Declare Constructor()'инициализация
	Declare Destructor() 'освобождение ресурсов меню
	Declare Sub DrawMenu() 'рисование меню
	Declare Sub LoadMenu() 'загрузка меню
	Declare Function TestClickMenu() As Integer
	Declare Function EventMenu() As Integer 'события меню
End Type

Dim m As menu

Constructor menu()
	ScreenRes scrW,scrH,32,2
	ScreenSet 1
	Color &hFFFFFF,&h9EE297
	Cls
	For a As Integer = 1 To 100
		Circle (Rnd*scrW, Rnd*scrH), Rnd*15, &hffffff
	Next
	
	x=(scrW-w)\2
	y=(scrH-(h*4+indentation*3))\2
	For i As Integer = 1 To 12
		imagemenu(i)=ImageCreate(w,h)
		BLoad("img\" & i & ".bmp",imagemenu(i))
	Next
	LoadMenu()
End Constructor

Destructor menu()
	For i As Integer = 1 To 12
		ImageDestroy(imagemenu(i))
	Next
End Destructor

Sub menu.LoadMenu()
	Do
		Select Case EventMenu()
			Case 1
				'Новая игра
			Case 2
				'Настройки
			Case 3
				'Информация
			Case 4
				Exit Sub
		End Select
	Loop
End Sub

Sub menu.DrawMenu()
	temp = TestClickMenu()
	Select Case Temp
		Case 1 To 4
			If EVENT_PRESS = 1 Then
				Put(x,y+(h*(Temp-1))+(Temp-1)*indentation),imagemenu(Temp+8),Alpha
			Else
				Put(x,y+(h*(Temp-1))+(Temp-1)*indentation),imagemenu(Temp+4),Alpha
			EndIf
		Case Else
			For i As Integer = 1 To 4
				Put(x,y+(h*(i-1))+(i-1)*indentation),imagemenu(i),Alpha
			Next
	End Select
End Sub

Function menu.TestClickMenu() As Integer
	Select Case Mx
		Case x To x+w
			Select Case My
				Case y To y+h
					Return 1
				Case y+h+indentation To y+h*2+indentation
					Return 2
				Case y+h*2+indentation*2 To y+h*3+indentation*2
					Return 3
				Case y+h*3+indentation*3 To y+h*4+indentation*3
					Return 4
			End Select
	End Select
End Function

Function menu.EventMenu() As Integer
	Do
		If (ScreenEvent(@event)) Then
			Select Case event.type
				Case EVENT_KEY_PRESS
					If (event.scancode = SC_ESCAPE) Then Return 4
				Case EVENT_MOUSE_MOVE
					Mx=event.x : My=event.y
				Case EVENT_MOUSE_BUTTON_PRESS
					EVENT_PRESS=1
					DrawMenu()
				Case EVENT_MOUSE_BUTTON_RELEASE
					EVENT_PRESS=0
					Return TestClickMenu()
				Case EVENT_WINDOW_CLOSE
					Return 4
			End Select
		End If
		DrawMenu()
		PCopy
	Loop
End Function