Thursday, 3 May 2012

Panel Icon Text

The depth and position of the sprite is set in the PositionPanelIcons() function, so the depth and position of the text should also be set there.
setSpritePosition( panelIconSprite( icon ) , x , y )
setSpriteDepth( panelIconSprite( icon ) , iconDepth )
thisX# = x + IconSize() / 2.0
thisY# = y + IconSize() - getTextTotalHeight( panelIconNameText( icon ) )
setTextPosition( panelIconShadText( icon ) , thisX# , thisY# )
setTextPosition( panelIconNameText( icon ) , thisX# - 1 , thisY# - 1 )
setTextDepth( panelIconShadText( icon ) , iconDepth - 1 )
setTextDepth( panelIconNameText( icon ) , iconDepth - 2 )
The text is centrally aligned so the X position is middle of the icon. The Y position is the bottom of the icon less the height of the text object.  The shadow is set to the resulting position and the name is positioned 1 pixel up and left.

The depth of the text items needs to be above the icon image with the shadow first, then the text on top.

The "drawing" of the icon images is done in the SetPanelSprite() function, this is where we will set the text for the icons too. Since this is called by both the group and object routines, this will display text for both.

This (with comments added) is what we have so far.
function SetPanelSprite( thisPos , thisFrame )
   thisSprite = panelIconSprite( thisPos )
   if thisFrame = 0
      // No Icon - Clear Image and Reset Animation
      setSpriteImage( thisSprite , 0 )
      clearSpriteAnimationFrames( thisSprite )
      // Clear Text

   else
      // Icon - Set Image and Animation Frame
      setSpriteImage( thisSprite , IconImage() )
      setSpriteAnimation( thisSprite , 64 , 64 , 252 )
      setSpriteFrame( thisSprite , thisFrame )
      // Set Text

   endif
endfunction
The // Clear Text and // Set Text comments show where the text changes go.

At the start of the function, where we get the sprite reference, we also need a few more bits of information.
thisNameText = panelIconNameText( thisPos )
thisShadText = panelIconShadText( thisPos )
This gets the references for both text items.

The code to clear the text is simply.
// Clear Text
setTextString( thisNameText , "" )
setTextString( thisShadText , "" )
Since a null string won't show, we don't need to do anything extra here.

If there is a frame, then we need to find the object it belongs to.
// Set Text
thisObject = FindPanelObjectByFrame( thisFrame )
This looks like a new function, and it is in a way, but we only need a slight change from what we have in the FindObjectByPanelSprite() function, which starts by getting the frame of the sprite. If we could jump in here - as we have the frame - we could use the existing code.
function FindObjectByPanelSprite( thisSprite )
   thisObject = 0
   thisFrame = getSpriteCurrentFrame( thisSprite )
   thisString$ = GameGroups()
   if PanelGroup() > 0 then thisString$ = thisString$ + ObjectDetail( PanelGroup() )
...
To do this we split the function at the point we want to enter from - after the frame has been obtained - and put our new function name in there.

Above it we will add some code to link the old call to the new one.

In practical - less editing - terms, we insert a block of code immediately after the old function first line.
   thisFrame = getSpriteCurrentFrame( thisSprite )
   thisObject = FindPanelObjectByFrame( thisFrame )
endfunction thisObject 

function FindPanelObjectByFrame( thisFrame )
This creates a smaller version of the FindObjectByPanelSprite() function and starts a new function FindPanelObjectByFrame() to pick up the old code.

Delete the now duplicate line from the old code.
thisFrame = getSpriteCurrentFrame( thisSprite )
and the result should look like this.
function FindObjectByPanelSprite( thisSprite )
   thisFrame = getSpriteCurrentFrame( thisSprite )
   thisObject = FindPanelObjectByFrame( thisFrame )
endfunction thisObject

function FindPanelObjectByFrame( thisFrame )
   thisObject = 0
   thisString$ = GameGroups()
   if PanelGroup() > 0 then thisString$ = thisString$ + ObjectDetail( PanelGroup() )
...
This may all be changed back when the finished routine is optimised.

Back at the SetPanelSprite() function, we now use the name of the retrieved object to set the text of the icon.
// Set Text
thisObject = FindPanelObjectByFrame( thisFrame )
setTextString( thisNameText , ObjectName( thisObject ) )
setTextString( thisShadText , ObjectName( thisObject ) )
This just dumps it in as is for now.

Finally we set the size of the text to an arbitrary value of 15 to give an idea of how it looks.
setTextSize( thisNameText , 15 )
setTextSize( thisShadText , 15 )
Now a test run gives us our first look at the icon text.
A few issues to deal with, but nothing unexpected.

No comments:

Post a Comment