Lists of==multiple Types

BlitzMax Forums/BlitzMax Programming/Lists of==multiple Types

Ant(Posted 2005) [#1]
Hi - I'm trying to create a UI system that will allow me to create screens and buttons etc. I'm doing this the following way using my new OOP knowledge!


You see in the UICreateScreen function that it creates a list of buttons.



What I'd like to do is have the functionality to add buttons to the list I have created (within TScreen), and then use a for eachin loop to cycle through the buttons in the list and any that exist will be drawn.

Problem is I've tried putting an addButton method function into the TScreen type, but I'm confused as to how to access the list I have already created in the UICreateScreen function . And I also dont know how to access the buttons to draw them ;-(

Can anyone help?
thanks


klepto2(Posted 2005) [#2]
Type TScreen
	Field sName:String
	Field background:TImage 
	
	' Create a list of buttons belonging to the screen
	Field buttonList: TList
			
	'Creates a list of screens
	Global ScreenList: TList	
	
	Function UICreateScreen:TScreen(name:String, image:String, totalButtons:Int)
		Local NewScreen:	TScreen
		NewScreen	=	New TScreen
		NewScreen.sName	=	name$
		NewScreen.background	=	LoadImage(image$)

		' Verfiy files exist and are of correct size
		Assert NewScreen.background Else "Cannot find file '"+image$+ "'."
		Assert NewScreen.background.width=iScreenWidth Else "Background: "+image$+" size does Not match screen width."
		Assert NewScreen.background.height=iScreenHeight Else "Background: "+image$+" size does Not match screen height."
		If Not screenlist Then ScreenList:Tlist = CreateList()
	
		' Once all checks are done add screen to list
		ScreenList.addlast ( NewScreen)
		
		
		'Create a list of buttons which will be a part of this screen
		If Not NewScreen.buttonList Then NewScreen.buttonList:Tlist= CreateList()
		For Local temp=1 To totalButtons
			NewScreen.buttonList.addlast( New TButton)
		Next
		
		'This should return the object just created so we can access it at a later date
		Return NewScreen
	EndFunction
	
	Method AddButton(name:String, x:Int,y:Int, image:String, buttonType:Int)
		
		Local NewButton:TButton
		NewButton= New Tbutton
		NewButton.sName$=name$
	
		NewButton.x=x
		NewButton.y=y
				SetMaskColor (0,0,0)
		If buttonType=STATIC
			NewButton.normal = LoadImage(image$)
		Else
			NewButton.normal = LoadAnimImage(image$,NewButton.iCellWidth,NewButton.iCellHeight,0,NewButton.iTotalFrames+1,DYNAMICIMAGE|MASKEDIMAGE)
		EndIf
		Assert NewButton.normal Else "Cannot find file '"+image$+ "'."
		
		buttonList.addlast ( NewButton ) 'You needn't secify a screen as a method always belongs to the created Screen
	End Method
	
	Method DrawButtoScreen()
		DrawImage background,0,0
		'Same here 
		For Local Button = EachIn ButtonList 
			DrawImage Button.Normal,Button.x,Button.y
		Next
	EndMethod
	
EndType



This should work (not tested)


Ant(Posted 2005) [#3]
ok thanks I'll give it a blast. Mind bending stuff ;-)


Ant(Posted 2005) [#4]
Nope ;-(

DrawImage Button.normal,Button.x,Button.y

Complains of an Unhandled Exception:attempt to access field or method of null object


Who was John Galt?(Posted 2005) [#5]
Just make the list global. You'll be able to access it from anywhere.


klepto2(Posted 2005) [#6]
Sorry the Line : For Local Button = EachIn ButtonList
should be For Local Button:TButton = eachin ButtonList

And delete the Totalbutton things, because of this you ciycle through all available Buttons, but the first ones doesn't have
any Images attached.


Ant(Posted 2005) [#7]
Ok great it all seems to be working! Thanks so much for your help - one final question though, is there any way to actually see the contents of the list in the debugger ? My debugger tells me that Mainmenu is of type TScreen and has all the fieids associated with it including the button list - but if I expand the tree for the buttonlist, there is no reference to the buttons I have added (although the program is behaving howI expect it to). I guess because I added them like this:

MainMenu.AddButton("Options", 300,200, "click.png",
STATIC)

and draw them like this:

MainMenu.DrawButtonScreen()

they are definately part of the TScreen type?

Thanks again forall the help!