Wednesday, 18 April 2012

Menu Clean up

There are just a few clean up tasks now to complete the menu.

First we add constant to replace the 255 value used for the highlighted text.
#constant MENU_GREENHI    255
While putting this constant in place, I realised that the line within if getPointerPressed().
if thisMenuSelected > 0 then setTextColorGreen( menuText[ thisMenuSelected ] , MENU_GREENHI )
Is not actually needed.  so I removed it.

The highlighting of the text will be picked up in the following loop by getPointerState(), and the program operates so fast, this will not be noticeable.

This opens up the possibility of having a highlight version of all the colours, which offers much more flexibility. So we can have
#constant MENU_REDHI 255
#constant MENU_GREENHI 255
#constant MENU_BLUEHI 0
And use them within the getPointerState() portion.
elseif getPointerState() > 0
   // PointerHeld
   if MenuPicked() > 0
      if thisMenuSelected = MenuPicked()
         setTextColorRed( menuText[ MenuPicked() ] , MENU_REDHI )
         setTextColorGreen( menuText[ MenuPicked() ] , MENU_GREENHI )
         setTextColorBlue( menuText[ MenuPicked() ] , MENU_BLUENHI )
      else
         setTextColorRed( menuText[ MenuPicked() ] , MENU_RED )
         setTextColorGreen( menuText[ MenuPicked() ] , MENU_GREEN )
         setTextColorBlue( menuText[ MenuPicked() ] , MENU_BLUE )
      endif
   endif
endif
Next, we move the pointer code into a function called checkMenuPointer().
function checkMenuPointer()
   thisMenuSelected = 0
   for i=1 to MENU_ITEMS
      if getTextHitTest( menuText[ i ] , PointDX() , PointDY() ) > 0
         thisMenuSelected = i
         exit
      endif
   next i
   if getPointerPressed() > 0
      // Pointer Pressed
      touch.menuPicked = thisMenuSelected
   elseif getPointerReleased() > 0
      // Pointer Released
      if thisMenuSelected > 0 and thisMenuSelected = MenuPicked() then StartTransition( TRANS_FADEOUT , MENU_FADE )
   elseif getPointerState() > 0
      // PointerHeld
      if MenuPicked() > 0
         if thisMenuSelected = MenuPicked()
            setTextColorRed( menuText[ MenuPicked() ] , MENU_REDHI )
            setTextColorGreen( menuText[ MenuPicked() ] , MENU_GREENHI )
            setTextColorBlue( menuText[ MenuPicked() ] , MENU_BLUENHI )
         else
            setTextColorRed( menuText[ MenuPicked() ] , MENU_RED )
            setTextColorGreen( menuText[ MenuPicked() ] , MENU_GREEN )
            setTextColorBlue( menuText[ MenuPicked() ] , MENU_BLUE )
         endif
      endif
   endif
endfunction
And replace it with a call to this function.
checkMenuPointer()
Now the main loop is reduced in size again.

We add a print line after the line that starts print(", Step: " +... in the PrintDiagnostics() function to display the current game state.
print("Game State : " + str( GameState() ) + chr(10))
We also need to add a case statement to pick up STATE_EXIT in the Game state select structure .
case default
   exit
endcase
This uses the default case, which is called when no other case block matches the value returned by GameState(), and exits the main loop when the state is not recognised.

To exit the program, this needs the following line after the loop command.
end
Which instructs the program to end.

I think we need to reduce the fade time of the menu, which is a little long at the moment.
#constant MENU_FADE 1.0
This is now just 1 second, which I think this looks much better.

Finally, we need to put the menu back the way we came to it, that is we need to make the menu text invisible, so that when we return to the menu, the fade in transition will activate.

This requires the following code adding within the block which performs the menu select block (just after the line if Transition() = TRANS_FADEOUT).
for i=1 to MENU_ITEMS
   setTextVisible( menuText[ i ] , 0 )
next i
That just about does it for the menu for now.

Here's how the main program looks now - functions not included.
// Constants must go at start of program
#constant PANEL_ROWS   3
#constant PANEL_COLS   4

#constant DEPTH_MENU   10
#constant DEPTH_SCORE   20
#constant DEPTH_PANEL   30
#constant DEPTH_WORK   40

#constant MAX_WORKOBJECTS   100

#constant FRAME_HIGHLIGHT   251
#constant FRAME_TRASHCAN      252

#constant STATE_SETUP   0
#constant STATE_INTRO   1
#constant STATE_DEMO   2
#constant STATE_MENU   3
#constant STATE_RUNNING   4
#constant STATE_EXIT   99

#constant MENU_FADE      1.0
#constant MENU_ITEMS      2

#constant MENU_DIVTOP   0.25
#constant MENU_DIVSIZE   0.10

#constant MENU_RED      255
#constant MENU_GREEN   165
#constant MENU_BLUE      0
#constant MENU_REDHI   255
#constant MENU_GREENHI   255
#constant MENU_BLUEHI   0

#constant TRANS_NONE   0
#constant TRANS_FADEIN   1
#constant TRANS_FADEOUT   2

// User Defined Types - custom data structures

type displayType
   physWidth#
   physHeight#
   virtWidth#
   virtHeight#
   iconSize#
   textHeight#
   lastFPS#
   iconImage
   highSprite
   transType
   transCount#
   transStep#
endtype

type touchType
   pointDX#
   pointDY#
   pointWX#
   pointWY#
   spritePicked
   objectPicked
   menuPicked
   pickOffsetX#
   pickOffsetY#
endtype

type panelType
   height#
   speed#
   isOpen
   image
   sprite
   text
   sound
   iconMax
endtype

type WorkObjectType
   isFree
   objectID
   sprite
endtype

type GameType
   state
   level
endtype

// I must remember to put this somewhere

global numWorkObjects = 0

Initialise()

// Temporary Objects

imageRef = SafeloadImage( "finger.png" )
spriteRef = createSprite( imageRef )

// Main Loop
do
   LoopStart()

   select GameState()
      case STATE_SETUP
         // Setup Code
         // Workaround to ensure frame rate ok before menu is called
         if timer() > 2.0 then setGameState( STATE_MENU )
      endcase
      case STATE_INTRO
         // Intro Code
      endcase
      case STATE_DEMO
         // Demo Code
      endcase
      case STATE_MENU
         // Menu Code
         if getTextVisible( menuText[ 1 ]) = 0
            // Menu starting - 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 )
               setTextColor( menuText[ i ] , MENU_RED , MENU_GREEN, MENU_BLUE , 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
               if Transition() = TRANS_FADEOUT
                  // Menu Exiting
                  for i=1 to MENU_ITEMS
                     setTextVisible( menuText[ i ] , 0 )
                  next i
                  select MenuPicked()
                     case 1
                        // Menu Option 1
                        setGameState( STATE_RUNNING )
                     endcase
                     case 2
                        // Menu Option 2
                        setGameState( STATE_EXIT )
                     endcase
                  endselect
               endif
               StopTransition()
            endif
         endif
         checkMenuPointer()
      endcase
      case STATE_RUNNING
         // Running Code
         checkGamePointer()
      endcase
      case default
         // Picks up unknown states - including STATE_EXIT
         exit
      endcase
   endselect

   // Position temporary finger sprite
   setSpritePosition( spriteRef , PointDX() , PointDY() )

   PrintDiagnostics()
   sync()
loop

end


No comments:

Post a Comment