Monday, 16 April 2012

Global Touch

Currently the functions that deal with the user touching the screen are slightly fragmented.

We have the pointer fields in the display UDT
pointDX#
pointDY#
pointWX#
pointWY#
and we have the test variables, which keep track of what has been selected.
thisSpritePicked = 0
thisObjectPicked = 0
thisPickOffsetX# = 0
thisPickOffsetY# = 0
It would be better if these were all in the same place.

We could add the variables to the display global, but that is really intended for display related information.

Also, in the case of a phone or tablet, unless it has a separate mouse and pointer feature, the program doesn't know where the pointer is until the user touches the screen.

A better solution would be to use a separate global for all the touch related information.

For this, a new UDT is created called touchType.
type touchType
   pointDX#
   pointDY#
   pointWX#
   pointWY#
   spritePicked
   objectPicked
   pickOffsetX#
   pickOffsetY#
endtype
The "picked" code previously at the start of the program is now removed and instead we use fields in a new global variable called touch which is set up in the Initialise() function.
global touch as touchType
   touch.pointDX# = 0.0
   touch.pointDY# = 0.0
   touch.pointWX# = 0.0
   touch.pointWY# = 0.0
   touch.spritePicked = 0
   touch.objectPicked = 0
   touch.pickOffsetX# = 0.0
   touch.pickOffsetY# = 0.0
Now all references to the touch fields in the display UDT are changed to use the touch UDT.

The following code in LoopStart().
   display.pointDX# = getPointerX()
   display.pointDY# = getPointerY()
   display.pointWX# = ScreentoWorldX(PointDX())
   display.pointWY# = ScreentoWorldY(PointDY())
Now becomes
   touch.pointDX# = getPointerX()
   touch.pointDY# = getPointerY()
   touch.pointWX# = ScreentoWorldX(PointDX())
   touch.pointWY# = ScreentoWorldY(PointDY())
And the existing return functions
function PointDX()
endfunction display.pointDX#
function PointDY()
endfunction display.pointDY#
function PointWX()
endfunction display.pointWX#
function PointWY()
endfunction display.pointWY#
Retain their names, but now return values from the touch global variable, rather than the display one.
function PointDX()
endfunction touch.pointDX#
function PointDY()
endfunction touch.pointDY#
function PointWX()
endfunction touch.pointWX#
function PointWY()
endfunction touch.pointWY#
Hopefully this demonstrates the benefit of using such functions.  All existing calls remain unaltered, while the workings under the hood - so to speak - are re-jigged.

Of course we need some more return functions for the new fields, these are.
function SpritePicked()
endfunction touch.spritePicked
function ObjectPicked()
endfunction touch.objectPicked
function PickOffsetX()
endfunction touch.pickOffsetX#
function PickOffsetY()
endfunction touch.pickOffsetY#
Any existing references to the old variables are changed to access the new fields (if writing) or these functions (if reading).

The following code in the elseif portion of the getPointerPressed() loop.
thisSpritePicked = getSpriteHit( PointWX() , PointWY() )
if thisSpritePicked > 0
   thisObjectPicked = FindWorkObjectBySprite( thisSpritePicked )
   if thisObjectPicked > 0
      thisPickOffsetX# = PointWX() - getSpriteXByOffset( thisSpritePicked )
      thisPickOffsetY# = PointWY() - getSpriteYByOffset( thisSpritePicked )
   endif
endif
Now becomes
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
The following code in the getPointerReleased() loop.
if thisObjectPicked > 0
   thisX# = PointWX() - thisPickOffsetX#
   thisY# = PointWY() - thisPickOffsetY#
   HideHighLight()
   if WorldToScreenY( thisY#) >= getTextY( panelText() )
      DelWorkObject( thisObjectPicked )
   else
      setSpritePositionByOffset( thisSpritePicked , thisX# , thisY# )
      setSpriteDepth( thisSpritePicked , DEPTH_WORK - 1 )
      thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
      if thisObjectUnder > 0
         // This is where the merge objects code goes
      endif
   endif
endif
thisSpritePicked = 0
thisObjectPicked = 0
thisPickOffsetX# = 0.0
thisPickOffsetY# = 0.0
Becomes;
if ObjectPicked() > 0
   thisX# = PointWX() - PickOffsetX()
   thisY# = PointWY() - PickOffsetY()
   HideHighLight()
   if WorldToScreenY( thisY#) >= getTextY( panelText() )
      DelWorkObject( ObjectPicked() )
   else
      setSpritePositionByOffset( SpritePicked() , thisX# , thisY# )
      setSpriteDepth( SpritePicked() , DEPTH_WORK - 1 )
      thisObjectUnder = WorkObjectIsOver( ObjectPicked() )
      if thisObjectUnder > 0
         // This is where the merge objects code goes
      endif
   endif
endif
touch.spritePicked = 0
touch.objectPicked = 0
touch.pickOffsetX# = 0.0
touch.pickOffsetY# = 0.0
And the following code in the getPointerState() portion.
if thisObjectPicked > 0
   thisX# = PointWX() - thisPickOffsetX#
   thisY# = PointWY() - thisPickOffsetY#
   setSpritePositionByOffset( thisSpritePicked , thisX# , thisY# )
   setSpriteDepth( thisSpritePicked , DEPTH_PANEL - 5 )
   if WorldToScreenY( thisY#) >= getTextY( panelText() )
      HighlightSprite( thisSpritePicked , FRAME_TRASHCAN )
   else
      thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
      if thisObjectUnder > 0
         HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT )
      else
         HideHighLight()
      endif
   endif
endif
Becomes;
if thisObjectPicked > 0
   thisX# = PointWX() - PickOffsetX()
   thisY# = PointWY() - PickOffsetY()
   setSpritePositionByOffset( SpritePicked() , thisX# , thisY# )
   setSpriteDepth( SpritePicked() , DEPTH_PANEL - 5 )
   if WorldToScreenY( thisY#) >= getTextY( panelText() )
      HighlightSprite( SpritePicked() , FRAME_TRASHCAN )
   else
      thisObjectUnder = WorkObjectIsOver( ObjectPicked() )
      if thisObjectUnder > 0
         HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT )
      else
         HideHighLight()
      endif
   endif
endif
Of course our test code needs to be updated by changing.
printc( "Sprite Picked: ")
printc( str( thisSpritePicked ))
printc( ", Object Picked: ")
printc( str( thisObjectPicked ))
printc( ", Over : " )
print( str( WorkObjectIsOver( thisObjectPicked ) ))
To
printc( "Sprite Picked: ")
printc( str( SpritePicked() ))
printc( ", Object Picked: ")
printc( str( ObjectPicked() ))
printc( ", Over : " )
print( str( WorkObjectIsOver( ObjectPicked() ) ))
And everything is back in working order.

Shame we removed our test sprites.

No comments:

Post a Comment