Thursday, 3 May 2012

Panel Icon UDT

All the object loaded have names, but these are not currently used anywhere.

To make the objects in the panel more easily identified, we can add the name to the bottom of each icon.

Since we don't know in advance the colour of the icon, we need a way to make the text visible over any colour.

This is done by putting something behind the text that is a contrasting colour, either a box or - in this case - a shadow.

If we make the text a bright colour such as yellow, and put a black shadow behind it, it should be easy against any icon.  If this does not work, we will try something else.

To create the text, we need two text objects - one for the text, the other for it's shadow.  These will be used with the sprite currently stored in the panelIconSprite[] array.

Rather than have more arrays, the best option is to move from a dedicated sprite array to a UDT array for the icon.
type panelIconType
   sprite
   nameText
   shadText
endtype
Now we can create a new array called panelIcon[] in the Initialise() function to replace panelIconSprite[].

The initialise code is changed from.
dim panelIconSprite[ panelIconMax() ]
   for icon = 1 to panelIconMax()
      panelIconSprite[ icon ] = createSprite( 0 )
      setSpriteSize( panelIconSprite[ icon ] , IconSize() - 4, IconSize() - 4)
      setSpriteColor( panelIconSprite[ icon ] , 255 , 255 , 255 , 127 )
   next icon
To this.
dim PanelIcon[ panelIconMax() ] as panelIconType
   for icon = 1 to panelIconMax()
      panelIcon[ icon ].sprite = createSprite( 0 )
      setSpriteSize( panelIconSprite( icon ) , IconSize() - 4, IconSize() - 4)
      setSpriteColor( panelIconSprite( icon ) , 255 , 255 , 255 , 127 )
      panelIcon[ icon ].nameText = createText( "" )
      panelIcon[ icon ].shadText = createText( "" )
      setTextAlignment( PanelIconNameText( icon ) , 1 )
      setTextAlignment( PanelIconShadText( icon ) , 1 )
      setTextColor( PanelIconNameText( icon ) , 255 , 255 , 128 , 255 )
      setTextColor( PanelIconShadText( icon ) , 0 , 0 , 0 , 255 )
   next icon
Which now uses individual fields for all the values needed for this icon. The text alignment and colour is also set here.

A series of return functions are created.
// Return functions - PanelIcon Array

function PanelIconIsValid( thisIcon )
   thisBool = ( thisIcon > 0 and thisIcon <= PanelIconMax() )
endfunction thisBool

function PanelIconSprite( thisIcon )
   if PanelIconIsValid( thisIcon )
      thisSprite = panelIcon[ thisIcon ].sprite
   else
      thisSprite = -1
   endif
endfunction thisSprite

function PanelIconNameText( thisIcon )
   if PanelIconIsValid( thisIcon )
      thisText = panelIcon[ thisIcon ].nameText
   else
      thisText = -1
   endif
endfunction thisText

function PanelIconShadText( thisIcon )
   if PanelIconIsValid( thisIcon )
      thisText = panelIcon[ thisIcon ].shadText
   else
      thisText = -1
   endif
endfunction thisText
Now to keep the old functions working, every use of the panelIconSprite[] array needs to be changed.  There should be.
  • Two in PositionPanelIcons().
  • Three in SetPanelIcons().
  • One in SetPanelSprite().
Where the value is put into the array, this needs changing to panelIcon[].sprite.

Where a value is read from and array (which will be most of them), it is changed to the function call PanelIconSprite().

In both cases, the variables used as the parameter (the thing in square brackets) is kept the same.  See the  initialise code for examples.

Perhaps now it's apparent why so often we copy the sprite references from the arrays into local variables like thisSprite.  It is to reduce changes like this.

No comments:

Post a Comment