Tuesday 17 April 2012

Transitions Part 2

With the first of the transitions functions tested and working, the rest of the code can be moved from the menu block and a little optimisation done.

This is the code that updates the counter by the step value, and again, because the work is already done, the new function is very straight forward.
function UpdateTransition()
   if TransitionCount() < 1.0 then display.transCount# = TransitionCount() + TransitionStep()
   if TransitionCount() > 1.0 then display.transCount# = 1.0
endfunction
The step value is only added if the counter has not yet reached 1.0.  If it exceeds 1.0 then it is made to equal 1.0.

Following a call to this function, the count value will always be from 0.0 to 1.0 and no further checks are needed in the main program.

Since this is no longer just used by the menu, the call to this function goes in the loopStart() function.
if Transition() > TRANS_NONE then UpdateTransition()
Which only calls the update function if a transition is in use.

Now the menu functions need only worry about calculating alpha values.

The existing block of code.
thisValue# = TransitionCount() + TransitionStep()
display.transCount# = thisValue#
thisAlpha = 255.0 * thisValue#
if thisAlpha > 255 then thisAlpha = 255
Is replaced with.
thisAlpha = 255.0 * TransitionCount()
if Transition() = TRANS_FADEOUT then thisAlpha = 255 - thisAlpha
Which sets the alpha value based on the counter, then if the transition is fading out the menu, subtracts it from 255.

This means the if Transition() check is done once, rather than once for each menu item.  Because of this the loop code is reduced to.
for i=1 to MENU_ITEMS
   setTextColorAlpha( menuText[ i ] , thisAlpha )
next i
Finally, the code which checks if the transition is finished.
if thisAlpha = 255 then display.transType = TRANS_NONE
Is replaced by a call to a new function to stop the transition.
if TransitionCount() = 1.0 then StopTransition()
This function is very simply.
function StopTransition()
   display.transType = TRANS_NONE
endfunction
Which keeps everything neat and tidy and isolates the transition variables from the main program.

The menu block now looks like this.
case STATE_MENU
   // Menu Code
   if getTextVisible( menuText[ 1 ]) = 0
      // Activate Transition
      StartTransition( TRANS_FADEIN , MENU_FADE )
      thisX# = VirtWidth() / 2.0
      thisY# = VirtHeight() * MENU_DIVTOP
      thisSize# = VirtHeight() * MENU_DIVSIZE
      for i=1 to MENU_ITEMS
         setTextSize( menuText[ i ] , thisSize# )
         setTextAlignment( menuText[ i ] , 1 )
         setTextDepth( menuText[ i ] , DEPTH_MENU )
         setTextPosition( menuText[ i ] , thisX# , thisY# )
         setTextVisible( menuText[ i ] , 1 )
         setTextColorAlpha( menuText[ i ] , 0)
         thisY# = thisY# + getTextTotalHeight( menuText[ i ] ) * 2.0
      next i
   elseif (Transition() = TRANS_FADEIN) or (Transition() = TRANS_FADEOUT)
      thisAlpha = 255.0 * TransitionCount()
      if Transition() = TRANS_FADEOUT then thisAlpha = 255 - thisAlpha
      for i=1 to MENU_ITEMS
         setTextColorAlpha( menuText[ i ] , thisAlpha )
      next i
      if TransitionCount() = 1.0 then StopTransition()
   endif
endcase
The transition functions look like this.
// Transition Functions

function StartTransition( thisType , thisTime# )
   display.transType = thisType
   display.transCount# = 0.0
   display.transStep# = 1.0 / ( lastFPS() * thisTime# )
endfunction

function StopTransition()
   display.transType = TRANS_NONE
endfunction

function UpdateTransition()
   if TransitionCount() < 1.0 then display.transCount# = TransitionCount() + TransitionStep()
   if TransitionCount() > 1.0 then display.transCount# = 1.0
endfunction
and the loopStart() function now looks like this.
function LoopStart()
   display.lastFPS# = screenFPS()
   panel.speed# = panelHeight() / (lastFPS() * 1.25)
   touch.pointDX# = getPointerX()
   touch.pointDY# = getPointerY()
   touch.pointWX# = ScreentoWorldX(PointDX())
   touch.pointWY# = ScreentoWorldY(PointDY())
   if Transition() > TRANS_NONE then UpdateTransition()
endfunction
We can also include transition variables in the PrintDiagnostics() function by adding the following lines.
printc("Transition Type : " + str( Transition() ) )
printc(", Count: " + str( TransitionCount() , 3 ) )
print(", Step: " + str( TransitionStep() , 3 ) + chr(10))
After the line which begins with print( ", @ "....

Now the progress of the transition is shown in numbers for checking.

No comments:

Post a Comment