TGC Codebase Backup



Simple Menu (with media) by sneaky smith12

12th Oct 2009 12:44
Summary

A easy method for creating fast and efficient menus. Including rollover and down state images.



Description

A easy method for creating fast and efficient menus. Including rollover and down state images.

Keep media in same folder as source code.

Screen size = menu image size, and in order to customize menus I would suggest photoshop or fireworks png files with preserved transparency. If using photoshop save a template to work from, and that way color changes are as simple as copy and paste, and change the hue.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Credit to Grog Grueslayer and sneaky smith12, but mostly Grueslayer.

set display mode 800,600,32

global CMenu
global nextfreeimage as integer

` Image numbers. Note that over images are followed by their respective down image.
mainmenu = freeImage()
    optiononeover = freeImage()
        optiononedown = freeImage()
    optiontwoover = freeImage()
        optiontwodown = freeImage()
    optionthreeover = freeImage()
        optionthreedown = freeImage()
menutwo = freeImage()

` load menus
load image "Title.png",mainmenu,1
load image "Title2.png",menutwo,1

` Load each selected text
load image "Menu1Select.png",optiononeover,1
load image "Menu2Select.png",optiontwoover,1
load image "Menu3Select.png",optionthreeover,1

`Load down state text
load image "Menu1click.png",optiononedown,1
load image "Menu2click.png",optiontwodown,1
load image "Menu3click.png",optionthreedown,1

sync on
` Set the starting menu
CMenu=1
do
 
 Menu( CMenu, optiononeover, optionthreedown, 316, 201)
 
 sync

loop

function freeImage()

    ` Find the first free image number
    repeat
     ` Go to the next image number  (Starts at 1 to not produce an error)
     inc nextfreeimage
     ` Don't leave the loop till the image doesn't exist
    until image exist(nextfreeimage)=0

endfunction nextfreeimage

function Menu(menuID, mstart, mend, x, y)

    `set local y value to check for clicked images
    local resety as integer : resety = y
    
    ` Show the background
    paste image menuID,0,0
    
    ` Go though each menu selection image
    for t= mstart to mend step 2
        ` Check if the mouse is on the menu selection
        if mousex()=>x and mousey()=>y and mousex()<=x+image width(t) and mousey()<=y+image height(t)
            ` Show the current selected image
            paste image t,x,y,1
			   ` Check for mouse clicks
			   if mouseclick()>0
			   	` Show clicked version of the image
			   	paste image t+1,x,y,1
			   	sync
			   	` Wait for the user to let go of the mouse buttons
			   	MouseWait()
					` Check again to make sure the mouse is still in the right area for a click
					if mousex()=>x and mousey()=>y and mousex()<=x+image width(t) and mousey()<=y+image height(t)
        				` Call the MenuSelect() function
        				MenuSelect(t)
        		   endif
        		endif
        endif
        ` Go to the next menu selection; inc value is about the image size plus space between.
        inc y,88
    next t

endfunction


function MenuSelect(Choice)
 
 ` Selected by mouseover image number
 select Choice
   
  ` Menu Option 1
  case 2
   ` Change to the first menu
   CMenu=1
  endcase

  ` Menu Option 2
  case 4
  endcase
  
  ` Menu Option 3
  case 6
   ` Change to the other menu
   CMenu=8
  endcase

  case default
  endcase

 endselect

endfunction


function MouseWait()

 ` Wait for the user to let go of the mouse buttons
 repeat
 until mouseclick()=0
 
endfunction