''
''this module contain types and functions
''responding for handing Pseudo GUI elements/objects in game



Type Rect_moje
	X1 As Integer
	Y1 As Integer
	X2 As Integer
	Y2 As Integer
End Type


''type describing element of Pseudo GUI -> PG
Type Struktura_PG
	'Rect
	Position As Point_2D	''position of element
	Rect_se As Rect_moje	''rect start - end
	Size As Point_2D	''size of element
	Rodzaj As UByte	''type of element
	Style As UByte
	description1 As String
	Text As String
	Value As Integer		''value
	reserved_1 As UByte	''reserved value, used as a function id
	reserved_2 As UByte	''reserved value
	curr_color As UInteger	''current color
	def_color As UInteger	''default color
	curr_bord_color As UInteger	''current border color
	def_bord_color As UInteger
	
End Type




Declare Sub Rysuj_PG()
Declare Sub Dodaj_przycisk_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
Declare Sub Dodaj_pole_tekstowe_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
Declare Sub Dodaj_etykiete_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
Declare Sub Wyczysc_PG()
Declare Function Podstaw_Point2D(ByVal X As Integer, ByVal Y As Integer) As Point_2D
Declare Function Podstaw_Point2DS(ByVal X As Single, ByVal Y As Single) As Point_2DS
Declare Sub Sprawdz_zaznaczenie()
Declare Sub Sprawdz_klikniecie()
Declare Sub Wczytaj_dane_czcionki(ByVal nr_czcionki As UByte, ByVal plik_czcionki As String)
Declare Function Podstaw_Rect_moj(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Rect_moje
Declare Sub Wyswietl_napis_graficzny(ByVal Poz_X As Integer, ByVal Poz_Y As Integer, ByVal Tekst_do_wyswietlenia As String, ByVal Rodzaj_czcionki As UByte)
Declare Sub SetCaption(ByVal nr_elementu As Integer, ByVal tekst As String)
Declare Sub SetType(ByVal nr_elementu As Integer, ByVal rodzaj As Integer)




Dim Shared As Point_2D Kursor_myszy
Dim Shared As Integer Przyciski_myszy, Przyciski_myszy_last


Dim Shared As UByte PG_Ilosc_elementow	''element's count
Dim Shared As Struktura_PG PG_element(20)
Dim Shared As UByte PG_aktualny_element, PG_aktualny_element_last	''actual element -> that element has focus
Dim Shared As String PG_tekst_aktualnego_elementu	''text of actual selected element
Dim Shared As UByte PG_aktualny_element_click, PG_aktualny_element_click2



Dim Shared As Integer tab_last_keystate(88)
Dim Shared As Integer Czas_napisu
Dim Shared As Single Sila_wiatru = 0
Dim Shared As Integer nr_wystrzalu

Dim Shared As Integer Numer_rundy = 0
Dim Shared As Integer ostatni_zwyciezca
Dim Shared As UByte tryb_okienkowy


Const As Integer Max_wybuchy = 10
Const As Integer Max_bonusy = 7
Const As Integer Max_chmury = 3
Const As Single Liczba_PI = 3.14
Const As Single Pi_przez_180 = Liczba_PI / 180


''draw_PG -> render/draw Pseudo GUI element's
Public Sub Rysuj_PG()
	Dim As UInteger CZuIL1, CZuIL2
	Dim As UByte CZuB1
	Dim As Integer CZI1, CZI2, CZI3
	
	
	For CZuB1 = 1 To PG_Ilosc_elementow
		
		Select Case As Const PG_element(CZuB1).Rodzaj
			Case 0	''default -> no type
				''draw nothing
			Case 1	''simple rectangle - filled
				''first border - white
				Line (PG_element(CZuB1).Rect_se.X1, PG_element(CZuB1).Rect_se.Y1) - (PG_element(CZuB1).Rect_se.X2, PG_element(CZuB1).Rect_se.Y2), PG_element(CZuB1).curr_bord_color, BF
				''second fill it with .curr_color
				Line (PG_element(CZuB1).Rect_se.X1 + 1, PG_element(CZuB1).Rect_se.Y1 + 1) - (PG_element(CZuB1).Rect_se.X2 - 1, PG_element(CZuB1).Rect_se.Y2 - 1), PG_element(CZuB1).curr_color, BF
				
			Case 3	''rectangle - filled + text
				''first border - white
				Line (PG_element(CZuB1).Rect_se.X1, PG_element(CZuB1).Rect_se.Y1) - (PG_element(CZuB1).Rect_se.X2, PG_element(CZuB1).Rect_se.Y2), PG_element(CZuB1).curr_bord_color, BF
				''second fill it with .curr_color
				Line (PG_element(CZuB1).Rect_se.X1 + 1, PG_element(CZuB1).Rect_se.Y1 + 1) - (PG_element(CZuB1).Rect_se.X2 - 1, PG_element(CZuB1).Rect_se.Y2 - 1), PG_element(CZuB1).curr_color, BF
				''text
				'CZI1 = (PG_element(CZuB1).Size.X - (Len(PG_element(CZuB1).Text) * 16) * 0.5)
				''CZI1 = Len(PG_element(CZuB1).Text) * 2
				CZI1 = 15
				''CZI2 = PG_element(CZuB1).Size.Y - (PG_czcionki(2).char_size(101).Y * 1.75)
				CZI2 = (PG_element(CZuB1).Rect_se.Y2 - PG_element(CZuB1).Rect_se.Y1) Shr 1	- 3	''normal 8
				Wyswietl_napis_graficzny(PG_element(CZuB1).Position.X + CZI1, PG_element(CZuB1).Position.Y + CZI2, PG_element(CZuB1).Text, 2)
				
			Case 4	''simple label
				''first border - white
				Line (PG_element(CZuB1).Rect_se.X1, PG_element(CZuB1).Rect_se.Y1) - (PG_element(CZuB1).Rect_se.X2, PG_element(CZuB1).Rect_se.Y2), PG_element(CZuB1).curr_bord_color, BF
				''second fill it with .curr_color
				Line (PG_element(CZuB1).Rect_se.X1 + 1, PG_element(CZuB1).Rect_se.Y1 + 1) - (PG_element(CZuB1).Rect_se.X2 - 1, PG_element(CZuB1).Rect_se.Y2 - 1), PG_element(CZuB1).curr_color, BF
				CZI1 = 15
				''CZI2 = PG_element(CZuB1).Size.Y - (PG_czcionki(2).char_size(101).Y * 1.75)
				CZI2 = (PG_element(CZuB1).Rect_se.Y2 - PG_element(CZuB1).Rect_se.Y1) Shr 1	- 3
				Wyswietl_napis_graficzny(PG_element(CZuB1).Position.X + CZI1, PG_element(CZuB1).Position.Y + CZI2, PG_element(CZuB1).Text, 2)
				
			Case 5	''advanced label
				''nothing
			Case 6	''simple Edit Box
				''something
				''first border - white
				Line (PG_element(CZuB1).Rect_se.X1, PG_element(CZuB1).Rect_se.Y1) - (PG_element(CZuB1).Rect_se.X2, PG_element(CZuB1).Rect_se.Y2), PG_element(CZuB1).curr_bord_color, BF
				''second fill it with .curr_color
				Line (PG_element(CZuB1).Rect_se.X1 + 1, PG_element(CZuB1).Rect_se.Y1 + 1) - (PG_element(CZuB1).Rect_se.X2 - 1, PG_element(CZuB1).Rect_se.Y2 - 1), PG_element(CZuB1).curr_color, BF
				CZI1 = 15
				''CZI2 = PG_element(CZuB1).Size.Y - (PG_czcionki(2).char_size(101).Y * 1.75)
				CZI2 = (PG_element(CZuB1).Rect_se.Y2 - PG_element(CZuB1).Rect_se.Y1) Shr 1	- 3				
				Wyswietl_napis_graficzny(PG_element(CZuB1).Position.X + CZI1, PG_element(CZuB1).Position.Y + CZI2, PG_element(CZuB1).Text, 2)
			
		End Select
		
	Next
	
	
End Sub


''add new element to PG
Public Sub Dodaj_przycisk_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
	Dim As UByte CZuB1
	
	PG_Ilosc_elementow += 1
	CZuB1 = PG_Ilosc_elementow
	
	PG_element(CZuB1).Position = Pozycja
	PG_element(CZuB1).Size = Wielkosc
	''rect
	PG_element(CZuB1).Rect_se.X1 = Pozycja.X
	PG_element(CZuB1).Rect_se.Y1 = Pozycja.Y
	PG_element(CZuB1).Rect_se.X2 = Pozycja.X + Wielkosc.X - 1
	PG_element(CZuB1).Rect_se.Y2 = Pozycja.Y + Wielkosc.Y - 1
	
	PG_element(CZuB1).Rodzaj = Rodzaj
	PG_element(CZuB1).Style = 0	''default style
	PG_element(CZuB1).Text = Tekst
	PG_element(CZuB1).Value = Wartosc
	PG_element(CZuB1).reserved_1 = res_1
	PG_element(CZuB1).reserved_2 = res_2
	
	PG_element(CZuB1).def_color = RGB(128, 128, 128)
	PG_element(CZuB1).curr_color = PG_element(CZuB1).def_color
	
	PG_element(CZuB1).def_bord_color = RGB(255, 255, 255)
	PG_element(CZuB1).curr_bord_color = PG_element(CZuB1).def_bord_color
	
End Sub


''add new element to PG
Public Sub Dodaj_pole_tekstowe_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
	Dim As UByte CZuB1
	
	PG_Ilosc_elementow += 1
	CZuB1 = PG_Ilosc_elementow
	
	PG_element(CZuB1).Position = Pozycja
	PG_element(CZuB1).Size = Wielkosc
	''rect
	PG_element(CZuB1).Rect_se.X1 = Pozycja.X
	PG_element(CZuB1).Rect_se.Y1 = Pozycja.Y
	PG_element(CZuB1).Rect_se.X2 = Pozycja.X + Wielkosc.X - 1
	PG_element(CZuB1).Rect_se.Y2 = Pozycja.Y + Wielkosc.Y - 1
	
	PG_element(CZuB1).Rodzaj = Rodzaj
	PG_element(CZuB1).Style = 0	''default style
	PG_element(CZuB1).Text = Tekst
	PG_element(CZuB1).Value = Wartosc
	PG_element(CZuB1).reserved_1 = res_1
	PG_element(CZuB1).reserved_2 = res_2
	
	PG_element(CZuB1).def_color = RGB(192, 192, 192)
	PG_element(CZuB1).curr_color = PG_element(CZuB1).def_color
	
	PG_element(CZuB1).def_bord_color = RGB(255, 255, 255)
	PG_element(CZuB1).curr_bord_color = PG_element(CZuB1).def_bord_color
	
End Sub


''add new element to PG
Public Sub Dodaj_etykiete_PG(ByVal Pozycja As Point_2D, ByVal Wielkosc As Point_2D, ByVal Rodzaj As UByte, ByVal Tekst As String, ByVal Wartosc As Integer, ByVal res_1 As UByte, ByVal res_2 As UByte)
	Dim As UByte CZuB1
	
	PG_Ilosc_elementow += 1
	CZuB1 = PG_Ilosc_elementow
	
	PG_element(CZuB1).Position = Pozycja
	PG_element(CZuB1).Size = Wielkosc
	''rect
	PG_element(CZuB1).Rect_se.X1 = Pozycja.X
	PG_element(CZuB1).Rect_se.Y1 = Pozycja.Y
	PG_element(CZuB1).Rect_se.X2 = Pozycja.X + Wielkosc.X - 1
	PG_element(CZuB1).Rect_se.Y2 = Pozycja.Y + Wielkosc.Y - 1
	
	PG_element(CZuB1).Rodzaj = Rodzaj
	PG_element(CZuB1).Style = 0	''default style
	PG_element(CZuB1).Text = Tekst
	PG_element(CZuB1).Value = Wartosc
	PG_element(CZuB1).reserved_1 = res_1
	PG_element(CZuB1).reserved_2 = res_2
	
	PG_element(CZuB1).def_color = RGB(127, 127, 127)
	PG_element(CZuB1).curr_color = PG_element(CZuB1).def_color
	
	''PG_element(CZuB1).def_bord_color = RGB(255, 255, 255)
	PG_element(CZuB1).def_bord_color = RGB(0, 0, 0)
	PG_element(CZuB1).curr_bord_color = PG_element(CZuB1).def_bord_color
	
End Sub


''clear totaly PG elements
Public Sub Wyczysc_PG()
	Dim As UByte CZuB1
	
	PG_Ilosc_elementow = 0
	''Cls
	
End Sub


''helping
Public Function Podstaw_Point2D(ByVal X As Integer, ByVal Y As Integer) As Point_2D
	Dim As Point_2D chwil_pkt
	chwil_pkt.X = X
	chwil_pkt.Y = Y
	
	Return chwil_pkt
	
End Function


Public Function Podstaw_Point2DS(ByVal X As Single, ByVal Y As Single) As Point_2DS
	Dim As Point_2DS chwil_pkt
	chwil_pkt.X = X
	chwil_pkt.Y = Y
	
	Return chwil_pkt
	
End Function


''check element's
Public Sub Sprawdz_zaznaczenie()
	Dim As UByte CZuB1
	
	''reset text of akcual element
	If (PG_element(PG_aktualny_element).Rodzaj = 6) Then
		''If Len(PG_tekst_aktualnego_elementu) > 1 Then	
		''EndIf
		PG_element(PG_aktualny_element).Text = Left(PG_tekst_aktualnego_elementu, Len(PG_tekst_aktualnego_elementu) - 1)
	EndIf
	
	
	PG_aktualny_element = 0	''nothing selected
	PG_tekst_aktualnego_elementu = ""
	''set curr color for all elements
	For CZuB1 = 1 To PG_Ilosc_elementow
		PG_element(CZuB1).curr_color = PG_element(CZuB1).def_color
		PG_element(CZuB1).curr_bord_color = PG_element(CZuB1).def_bord_color
	Next
	
	''search on which element is mouse cursor
	For CZuB1 = 1 To PG_Ilosc_elementow
		If (Kursor_myszy.X > PG_element(CZuB1).Rect_se.X1) Then
			If (Kursor_myszy.X < PG_element(CZuB1).Rect_se.X2) Then
				If (Kursor_myszy.Y > PG_element(CZuB1).Rect_se.Y1) Then
					If (Kursor_myszy.Y < PG_element(CZuB1).Rect_se.Y2) Then
						''activate this element
						PG_element(CZuB1).curr_bord_color = RGB(96, 160, 255)
						PG_aktualny_element = CZuB1
						''If Przyciski_myszy = 1 Then PG_aktualny_element_last = CZuB1
						''WindowTitle(Str(pg_aktualny_element))
						
						Select Case As Const PG_element(CZuB1).Rodzaj
							Case 0, 1, 2, 3	''buttons
								PG_element(CZuB1).curr_color = RGB(192, 128, 128)
							Case 4, 5	''labels
								''PG_element(CZuB1).curr_color = RGB(192, 128, 128)
								''PG_element(CZuB1).curr_color = PG_element(CZuB1).def_color
								PG_element(CZuB1).curr_bord_color = PG_element(CZuB1).def_bord_color
							Case 6	''edit box
								PG_element(CZuB1).curr_color = RGB(128, 192, 128)
								If (Asc(Right(PG_element(CZuB1).Text, 1)) <> 31) Then
									PG_element(CZuB1).Text = PG_element(CZuB1).Text & Chr(31)
									PG_tekst_aktualnego_elementu = PG_element(CZuB1).Text
								EndIf
								
						End Select
						
						Exit For
					EndIf
				EndIf
			EndIf
		EndIf
		
	Next

	If PG_aktualny_element = 0 Then 
		PG_aktualny_element_last = 0
		PG_aktualny_element_click = 0		
		PG_aktualny_element_click2 = 0		
	EndIf
	
	
	
End Sub


''check for mouse clicks
Public Sub Sprawdz_klikniecie()
	Dim As UByte CZuB1
	Dim As Integer czi1
	
	''mouse down
	If Przyciski_myszy = 1 And Przyciski_myszy_last = 0 And PG_aktualny_element > 0 Then
		PG_aktualny_element_click = PG_aktualny_element
	EndIf
	''mouse up
	If Przyciski_myszy = 0 And Przyciski_myszy_last = 1 And PG_aktualny_element > 0 Then
		PG_aktualny_element_click2 = PG_aktualny_element
	EndIf


	''if some element has focus/ is selected
	''If (PG_aktualny_element > 0 And PG_aktualny_element_last > 0) And (Przyciski_myszy = 0 And Przyciski_myszy_last = 1) Then
	If (PG_aktualny_element > 0 And PG_aktualny_element = PG_aktualny_element_click And PG_aktualny_element_click = PG_aktualny_element_click2) And Przyciski_myszy = 0 Then
		Select Case As Const PG_element(PG_aktualny_element).Rodzaj
			Case 0	''default
				''nothing
				Print "qwe"
			Case 1, 2, 3	''simple rectangle
				''something
				''obsluga prostych przyciskow
				Select Case As Const PG_element(PG_aktualny_element).reserved_1
					Case 0	''no function assigned
						''nothing
						Print "qwe"
					Case 1	''set new Tryb_gry
						Ustaw_Tryb_gry(PG_element(PG_aktualny_element).reserved_2)
						''Robic_glowne = 0
					Case 3	''ustawia akltywna czynnosc
						Aktualna_czynnosc = PG_element(PG_aktualny_element).reserved_2
					Case 4	''wysadz wszystkie ludziki
						For czi1 = 0 To map_config_data(0)
							tab_ludziki(czi1).czas_robienia = 0
							tab_ludziki(czi1).czas_detonacji = 6
						Next czi1
					Case 5	''cycle player
						'Gracze_lista(PG_element(PG_aktualny_element).value).rodzaj += 1
						'If Gracze_lista(PG_element(PG_aktualny_element).value).rodzaj >= 4 Then Gracze_lista(PG_element(PG_aktualny_element).value).rodzaj = 1
						'czub1 = Gracze_lista(PG_element(PG_aktualny_element).value).rodzaj
						Select Case As Const czub1
							Case 0
								SetType(PG_element(PG_aktualny_element).reserved_2, 4)
								SetCaption(PG_element(PG_aktualny_element).reserved_2, "none")
								'Gracze_lista(PG_element(PG_aktualny_element).value).nick = "none" 
							Case 1
								SetType(PG_element(PG_aktualny_element).reserved_2, 6)
								SetCaption(PG_element(PG_aktualny_element).reserved_2, "local_player")
								'Gracze_lista(PG_element(PG_aktualny_element).value).nick = "local_player" 
							Case 2
								SetType(PG_element(PG_aktualny_element).reserved_2, 4)
								SetCaption(PG_element(PG_aktualny_element).reserved_2, "CPU")
								'Gracze_lista(PG_element(PG_aktualny_element).value).nick = "CPU" 
							Case 3
								SetType(PG_element(PG_aktualny_element).reserved_2, 4)
								SetCaption(PG_element(PG_aktualny_element).reserved_2, "CPU+")
								'Gracze_lista(PG_element(PG_aktualny_element).value).nick = "CPU+" 
						End Select

				End Select
		End Select
		
	Przyciski_myszy_last = 0
	PG_aktualny_element_last = 0
	PG_aktualny_element_click = 0
	EndIf	
	
	Przyciski_myszy_last = Przyciski_myszy
	
End Sub


''load font data -> rect


''helping function
Public Function Podstaw_Rect_moj(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Rect_moje
''
	Dim As Rect_moje Podstawiane_rect
	
	With Podstawiane_rect
		.x1 = x1
		.x2 = x2
		.y1 = y1
		.y2 = y2
	End With
	
	Return Podstawiane_rect
	
End Function



''draw graphical text
''call Wyswietl_napis_graficzny(Position_x, Position_y, Text)
''graphic file include only BIG / GRAND letters
Public Sub Wyswietl_napis_graficzny(ByVal Poz_X As Integer, ByVal Poz_Y As Integer, ByVal Tekst_do_wyswietlenia As String, ByVal Rodzaj_czcionki As UByte)
	
	Dim As Integer Curr_X, Curr_Y, Roznica_X
	Dim As UByte Poz_w_tekscie, Dlug_tekstu, Kod_ASC
	Dim As String Obrabiany_tekst
	
	Curr_X = Poz_X
	Curr_Y = Poz_y
	If (Len(Tekst_do_wyswietlenia) > 255) Then
		Obrabiany_tekst = Mid(Tekst_do_wyswietlenia, 1, 255)
	Else
		Obrabiany_tekst = Tekst_do_wyswietlenia
	EndIf
	Dlug_tekstu = Len(Obrabiany_tekst)
	
	''po koleji wyswietlanie znakow z tekstu
	/'
	For Poz_w_tekscie = 1 To Dlug_tekstu
		Kod_ASC = Asc(Mid(Obrabiany_tekst, Poz_w_tekscie, 1))
			Put (Curr_X, Curr_Y), font_bitmap, (PG_czcionki(Rodzaj_czcionki).char_rect_se(Kod_ASC).X1, PG_czcionki(Rodzaj_czcionki).char_rect_se(Kod_ASC).Y1) - (PG_czcionki(Rodzaj_czcionki).char_rect_se(Kod_ASC).X2, PG_czcionki(Rodzaj_czcionki).char_rect_se(Kod_ASC).Y2), Trans
			''Roznica_x = Tablica_znakow(Kod_ASC).x2 - Tablica_znakow(Kod_ASC).x1 + 1
			Curr_x += CInt(PG_czcionki(Rodzaj_czcionki).char_size(Kod_ASC).X)
	Next Poz_w_tekscie
	'/
	Draw String (curr_x, curr_y), Obrabiany_tekst
	
End Sub


''set new text/caption on pseudo-gui element
Public Sub SetCaption(ByVal nr_elementu As Integer, ByVal tekst As String)
	PG_element(nr_elementu).Text = tekst
End Sub


Public Sub SetType(ByVal nr_elementu As Integer, ByVal rodzaj As Integer)
	PG_element(nr_elementu).Rodzaj = rodzaj
	Select Case As Const rodzaj
		Case 3	''przycisk
			PG_element(nr_elementu).def_color = RGB(128, 128, 128)
			PG_element(nr_elementu).curr_color = PG_element(nr_elementu).def_color
			PG_element(nr_elementu).def_bord_color = RGB(255, 255, 255)
			PG_element(nr_elementu).curr_bord_color = PG_element(nr_elementu).def_bord_color
		Case 4	''etykieta
			PG_element(nr_elementu).def_color = RGB(127, 127, 127)
			PG_element(nr_elementu).curr_color = PG_element(nr_elementu).def_color
			PG_element(nr_elementu).def_bord_color = RGB(0, 0, 0)
			PG_element(nr_elementu).curr_bord_color = PG_element(nr_elementu).def_bord_color			
		Case 6	''pole tekstowe
			PG_element(nr_elementu).def_color = RGB(192, 192, 192)
			PG_element(nr_elementu).curr_color = PG_element(nr_elementu).def_color
			PG_element(nr_elementu).def_bord_color = RGB(255, 255, 255)
			PG_element(nr_elementu).curr_bord_color = PG_element(nr_elementu).def_bord_color		
	End Select
End Sub
