Thursday, 3 May 2012

Panel Group Selection

Now we have the panel displaying some icons, it's time to get back to the functions that deal with pointer selection, so we can change the displayed group.

Game input is handled by the checkGamePointer() function, let's review what it does as a reminder.

An outer structure checks the state of the pointer, Just Pressed, Just Released or Held.

Within the first part - getPointerPressed() - the Y position of the pointer is checked against two sets of conditions.
  • If it's over the panel text object, the panel state is changed between opened and closed.
  • If Y is less than this, the work area is processed.
We now want to add the third possibility - that is it over the panel - using the else command.  With a few comments added, this makes the start of that function look like this.
function checkGamePointer()
   if getPointerPressed() > 0
      if PointDY() >= getTextY( panelText() ) and PointDY() < getSpriteY( panelSprite() )
         // Pointer is over Panel Control Text
         ToggleTrayOpen()
      elseif PointDY() < getTextY( panelText() )
         // Pointer is over Work Area
         touch.spritePicked = getSpriteHit( PointWX() , pointWY() )
         if SpritePicked() > 0
            touch.objectPicked = FindWorkObjectBySprite( SpritePicked() )
            if ObjectPicked() > 0
               touch.pickOffsetX# = PointWX() - getSpriteXByOffset( SpritePicked() )
               touch.pickOffsetY# = PointWY() - getSpriteYByOffset( SpritePicked() )
            endif
         endif
      else
         // Pointer is over Panel

      endif
   elseif getPointerReleased() > 0
We will now add the code to the new block indicated by // Pointer is over Panel.

The code almost the same as the work area code, but will be dealing with display coordinates rather than world coordinates.
// Pointer is over Panel
touch.spritePicked = getSpriteHit( PointDX() , pointDY() )
if SpritePicked() > 0
   touch.objectPicked = FindObjectByPanelSprite( SpritePicked() )
   if ObjectPicked() > 0
      if ObjectisGroup( ObjectPicked() ) = 0
         // Object is not a group object (so can be dragged)
         touch.pickOffsetX# = PointDX() - getSpriteXByOffset( SpritePicked() )
         touch.pickOffsetY# = PointDY() - getSpriteYByOffset( SpritePicked() )
         // Create work object for dragging

      else
         // Object is a group object
         if ObjectPicked() <> PanelGroup()
            panel.group = ObjectPicked()
            SetPanelIcons()
         endif
         touch.objectPicked = 0
      endif
   endif
endif
As before, the .objectPicked field of the touch global is used to store the object and .pickOffsetX# and .pickOffsetY#, the offset of the pointer from the middle of that object.  However in this case, we check if the object is a group object before storing these values.

This also calls a new function FindObjectByPanelSprite(), where the work area used FindWorkObjectBySprite(), because panel icons are not work objects and so a different routine is needed.
function FindObjectByPanelSprite( thisSprite )
   thisObject = 0
   thisFrame = getSpriteCurrentFrame( thisSprite )
   thisString$ = GameGroups()
   if PanelGroup() > 0 then thisString$ = thisString$ + ObjectDetail( PanelGroup() )
   for i = 1 to len( thisString$ ) / 2
      thisCode$ = mid( thisString$ , i * 2 - 1 , 2 )
      thisObject = Base96Decode( thisCode$ )
      if thisObject > 0
         if ObjectIcon( thisObject ) = thisFrame then exit
         thisObject = 0
      endif
   next i
endfunction thisObject
This uses the getSpriteCurrentFrame() command to get the current frame of the sprite provided.  This corresponds to the .icon field of the object array so can be used to locate the correct object.

We don't need to check the full list of objects, just the ones currently shown.

Next, it creates a string of Base96 codes by combining the string of groups with members - GameGroups(), with the codes for the member objects - ObjectDetail(), of the currently selected group - PanelGroup().

The resulting string contains Base96 codes for all the icons currently shown in the panel.

This list is processed in the normal manner, extracting two characters, decoding them and then checking to see if the icon for that object matches the frame we are looking for.

Back with the pointer code, once the object is identified as a group object, then it is checked to make sure it is different from the currently selected group.

If so, the current group is changed and the panel redrawn.

Finally, the selected object is reset to zero so the user cant drag the group icon by keeping the pointer pressed.

Now the two groups can be selected on the panel and the correct icons will be shown.

No comments:

Post a Comment