FryGUI

BlitzMax Forums/BlitzMax Programming/FryGUI

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Well here is my Messagebox....

SuperStrict

Import fry.frygui

Global Width:Int = 800
Global Height:Int = 600

Graphics Width,Height,0
' Set up screens For GUI
	
' Load a skin
fry_LoadSkin("Skin")

' Add fonts
fry_AddFont("Default" , "vinque.ttf" , 16)
fry_AddFont("Large" , "vinque.ttf" , 16 * 1.5)
	
' Create the screen
Local MainScreen:fry_TScreen = fry_CreateScreen("MainScreen")

fry_SetScreen ("MainScreen")

Local icon:String = "Images/buttons/Apoth.png"

If icon = Null Then DebugLog "Error loading image"

Local message:TList = CreateList()
message.AddFirst("Ooops!")
'message.AddLast("This is an error message:")
'message.AddLast("An error has occurred!")
'message.AddLast("What a shame that this has happened!")
'message.AddLast("I mean what are the chances of that occuring?")
'message.Addlast("Do you want to continue ?")

Local buttons:TList = CreateList()
buttons.AddLast("OK")
'buttons.AddLast("Yes")
'buttons.AddLast("No")
'buttons.AddLast("Cancel")

Local error:String = Messagebox(Mainscreen, icon, 128, "Testing", message, buttons,"0000BB","EEEEEE","DDDDDD","000000","Large","Default")

DebugLog "Result was: " + error

Repeat
	
Until KeyHit(key_escape)

Function Messagebox:String(screen:fry_TScreen, icon:String, iconsize:Int, title:String, message:TList, buttons:TList, tHexColour:String, tHexTextColour:String,mHexColour:String,mHexTextColour:String, titleFont:String="", msgFont:String="")
	Rem
		screen - The screen to add the panels to
		icon	- The url path to the icon to display
		iconsize - 	Size of the icon
		title - Message box title
		message - the Tlist containing the message
		buttons - the Tlist containing the button text (limited to a maximum of 3)
		tHexColour - Title Panel Colour
		tHexTextColour - Title Panel text Colour
		mHexColour - Message panel colour
		mHexTextColour - Message panel text colour
		titleFont - The font to display in the title with.
		msgFont - the font to display the message with.
	EndRem
	
	Local result:String
	
	Local x:Int		' X value of messagebox
	Local y:Int		' Y values of messagebox
	Local w:Int		' width of messagebox
	Local h:Int		' height of messagebox
	Local bx:Int	' X value of button
	Local by:Int	' Y value of button
	Local bw:Int	' button width
	Local bh:Int	' button height
	Local mx:Int	' X value of message text
	Local my:Int	' Y value of message text
	Local mw:Int	' message width
	Local mh:Int	' message height
	Local msgIcon:fry_Timage
	
	' Create a panel
	Local msgBox:fry_TPanel = fry_CreatePanel ("msgBox",x,y,w,h,Null)
	Screen.AddPanel(msgBox)
	
	' create a panel for the title
	Local titleBox:fry_TPanel = fry_CreatePanel ("titleBox",x,y,w,h,Null)
	Screen.AddPanel(titleBox)
	
	' Create a label for the title
	Local lblTitle:fry_TLabel = fry_CreateLabel ("lblTitle",title,x,y,w,h,0,0,titleBox)
	
	If TitleFont
		lblTitle.SetFont(titleFont)
	EndIf
	
	' Set the width to the width of the title
	w = lblTitle.gW
	h = lblTitle.gH
	
	' Create the labels for the message
	
	Local msgLine:fry_TLabel[CountList(message)]
	
	Local count:Int
	For Local text:String = EachIn message
		Local lx:Int 
		
		msgLine[count] = fry_CreateLabel ("msgLine" + count , text , x , y , w , h , 0 , 0 , msgBox)
		If msgFont
			msgLine[count].SetFont(msgFont)
		EndIf
		'msgLine[count].HexTextColour(mHexTextColour)
		'my = msgLine[count].gY + msgLine[count].gH
		
		' find the longest line and set it to mw
		If mw < msgLine[count].gW Then mw = msgLine[count].gW
		DebugLog mw
		count:+ 1
	Next
	
	' Create the buttons
	
	' create a label "offscreen" and use it to get the widths and heights for the buttons
	Local btnlabel:fry_TLabel = fry_CreateLabel ("btnlabel" , "" , -30 , -30 , 0 , 0 , 0 , 0 , msgBox)
	If TitleFont
		btnlabel.SetFont(TitleFont) 
	EndIf
	
	' Loop through all the buttons to find the widest
	For Local btn:String = EachIn buttons
		btnlabel.SetText(btn)
		If bw < btnlabel.gW Then bw = btnlabel.gW
		If bh < btnlabel.gH Then bh = btnlabel.gH
	Next
	
	' Dispose of btnLabel
	btnlabel = Null
	
	' Create the buttons
	Local btnMSG:fry_TButton[CountList(buttons)]
	
	count = 0 
	For Local btn:String = EachIn buttons
		btnMsg[count] = fry_CreateButton ("btnMSG" + count, btn, bx, by, bw, bh, msgBox)
		count:+1
	Next
	
	' Messagebox created now create x,y,w,h settings
	w = lblTitle.gW
	
	For Local count:Int = 0 To msgLine.length -1
		If w < msgLine[count].gW Then 
			w = msgLine[count].gW
		EndIf
	Next
	
	' Ensure that msgBox is wider then bw * # of buttons
	If w < bw * CountList(buttons) Then w = bw * CountList(buttons)
	
	' Create a space either size to centralize the text
	Local oldwidth:Int = w
	w:+ (w /8)
	DebugLog "W:" + w + ", MW:" + mw + ", oldWidth:" + oldwidth
	mx = (w - oldwidth) /2 '((oldwidth - mw) /2)


	' Set a height for the messagebox
	h = msgLine[0].gH * (CountList(message) )' +1)
	
	' If there is an icon leave room for an icon and add it
	If icon Then
		w:+ (iconsize * 1.5)
		mx:+ iconsize + (iconsize / 4)
		
		' Check that the height is big enough for the icon
		If h < iconsize Then h = iconsize
		
		msgIcon = fry_CreateImage ("msgIcon", icon, 0, 0, iconsize, iconsize, msgBox)
	EndIf
	
	' Change the x , y , w amd h settings for message boxes and refresh them
	
	msgBox.gW = w
	msgBox.gH = h
	msgBox.gX = (width - msgBox.gW ) /2
	msgbox.gY = (height - msgBox.gH ) /2
	msgBox.HexColour(mHexColour)
	msgBox.Refresh()
	
	titleBox.gX = (width - w) /2
	titlebox.gY = msgbox.gY - (lblTitle.gH /2)
	titleBox.gW = w
	titleBox.gH = lblTitle.gH
	titleBox.HexColour(tHexColour)
	titleBox.Refresh()
	
	' Adjust the title
	lblTitle.gOrigX = w /2
	lblTitle.gW = w
	lblTitle.SetXAlign(1)
	lblTitle.HexTextColour(tHexTextColour)
	lblTitle.Refresh()
	
	' Adjust the message
	For Local count:Int = 0 To msgLine.length - 1
		msgline[count].gOrigX = mx
		msgLine[count].gOrigY = (count +1) * msgLine[count].gH
		msgLine[count].SetXAlign(0)
		msgLine[count].Refresh()
		msgline[count].HexTextColour(mHexTextColour)
	Next
	
	
	' Adjust the icon (if it exists)
	If icon Then
		msgIcon.gX = iconsize / 4
		msgIcon.gY = msgLine[0].gY
	EndIf	
	msgIcon.refresh()
	
	' Adjust the buttons
	
	' first make some room
	
	Local oldheight:Int = msgBox.gH
	h = oldheight + (bh * 2.5)
	by = oldheight + bh
	msgBox.gH = h
	msgBox.Refresh()
	
	Select btnMSG.Length
		Case 1
			bx = (w - bw) /2
			btnMsg[0].gX = bx
			btnMsg[0].gY = by
			btnMsg[0].Refresh()
		Case 2
			bx = bw /8
			btnMsg[0].gX = bx
			btnMsg[0].gY = by
			btnMsg[0].Refresh()
			bx = w - bw - (bw /8)
			btnMsg[1].gX = bx
			btnMsg[1].gY = by
			btnMsg[1].Refresh()
		Case 3
			bx = bw /8
			btnMsg[0].gX = bx
			btnMsg[0].gY = by
			btnMsg[0].Refresh()
			bx = (w - bw) /2
			btnMsg[1].gX = bx
			btnMsg[1].gY = by
			btnMsg[1].Refresh()
			bx = w - bw - (bw /8)
			btnMsg[2].gX = bx
			btnMsg[2].gY = by
			btnMsg[2].Refresh()
	EndSelect
	
	Repeat
		PollSystem
		
		fry_Refresh()
		
While fry_PollEvent()
		If fry_EventID() = fry_EVENT_GADGETSELECT
			Select fry_EventSourceName()
				Case "btnMSG0"
					result = btnMSG[0].gText					
				Case "btnMSG1"
					result = btnMSG[1].gText
				Case "btnMSG2"
					result = btnMSG[2].gText
			End Select
		EndIf
	Wend		
		Flip
	Until result <> Null 'KeyHit(key_escape) Or AppTerminate()
	
	msgBox.Hide()
	titleBox.Hide()

	Cls
	Flip
	
	Return result
End Function


This code works but I'm sure it could be made neater....

Please play with and let me know of issues, the thing to note is that you are limited to a maximum of three buttons, you can add more to the list but none will be displayed.

The parameters for Messagebox are numerous, but this was to allow maximum customization...