Code archives/Miscellaneous/Icon Type

This code has been declared by its author to be Public Domain code.

Download source code

Icon Type by TomToad2007
This type will help create and manage icons for your BlitzMAX MAX2D program. You can use this code to test it with.
Just find three images at 32x32 for the Duck.png, Goose.png, and Chicken.png images.
Strict

Include "IconType.bmx"

Graphics 800,600

Local Choice:Int = 0

'Calls TIcon.Create() for each icon
TIcon.Create(LoadImage("Duck.png"),10,10,1)
TIcon.Create(LoadImage("Goose.png"),52,10,2)
TIcon.Create(LoadImage("Chicken.png"),94,10,3)

While Not KeyDown(KEY_ESCAPE)
	Cls
	
	' Draw the icons to the screen
	TIcon.Draw()
	If MouseHit(1)
		'If the Mouse button is pressed, check to see if it is over an icon
		' and store the ID in the Choice variable
		Choice:Int = TIcon.Collide(MouseX(),MouseY())
		
	End If
	
	'Print which icon was just clicked
	Select Choice
		Case 1
			DrawText "Duck",10,52
		Case 2 
			DrawText "Goose",10,52
		Case 3
			DrawText "Chicken",10,52
	End Select

	Flip
Wend
'IconType.bmx
Type TIcon
	Global IconList:TList = Null
	Field ID:Int
	Field X:Int
	Field Y:Int
	Field Width:Int
	Field Height:Int
	Field Image:TImage
	
	'Call this function for each icon.  Passing it an image, x and y location, and a unique ID
	Function create(Image:TImage,X:Int,Y:Int,ID:Int)
		Local Icon:TIcon = New TIcon
		
		If IconList = Null Then IconList = CreateList()
		Icon.X = X
		Icon.Y = Y
		Icon.Image = Image
		Icon.Width = ImageWidth(Image)
		Icon.Height = ImageHeight(Image)
		Icon.ID = ID
		ListAddLast(IconList,Icon)
	End Function
	
	'Call this function in the main loop to draw the Icons to the screen
	Function Draw()
		If IconList = Null Then Return
		
		For Local Icon:TIcon = EachIn IconList
			DrawImage Icon.Image,Icon.X,Icon.Y
		Next
	End Function
	
	'This function will return the ID of the icon located at X and Y. 
	' Usually you would pass the mouse pointer's X and Y location to the function
	' Returns 0 if no icon is selected.
	Function Collide:Int(X:Int,Y:Int)
		If IconList = Null Then Return 0
		
		For Local Icon:TIcon = EachIn IconList
			If X >= Icon.X And X < Icon.X+Icon.Width And Y >= Icon.Y And Y < Icon.Y+Icon.Width
				Return Icon.ID
			End If
		Next
		
		Return 0
	End Function
End Type

Comments

None.

Code Archives Forum