LPMenu.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:7k
源码类别:

J2ME

开发平台:

Java

  1. /**
  2.  * <p>Title: lipeng</p>
  3.  * <p>Description:
  4.  * You cannot remove this copyright and notice.
  5.  * You cannot use this file without the express permission of the author.
  6.  * All Rights Reserved</p>
  7.  * <p>Copyright: lizhenpeng (c) 2004</p>
  8.  * <p>Company: LP&P</p>
  9.  * @author lizhenpeng
  10.  * @version 1.1.1
  11.  * <p>
  12.  * Revise History
  13.  * 2004.07.22 Add a menu entry demoMode V1.1.0
  14.  * 2004.07.22 revise a logic error on init V1.1.1
  15.  * </p>
  16.  */
  17. package lipeng;
  18. import javax.microedition.lcdui.Canvas;
  19. public class LPMenu
  20. {
  21. //method
  22.   public LPMenu(LPIGameManager gm)
  23.   {
  24.     this.gm=gm;
  25.   }
  26.   private void menuSelect(int direct)
  27.   {
  28.     switch(direct)
  29.     {
  30.       case DIRECTION_UP:
  31.         switch(whichActive)
  32.         {
  33.           case MENU_NEW_GAME:
  34.             whichActive=MENU_ABOUT;
  35.             break;
  36.           case MENU_LOAD_GAME:
  37.             whichActive=MENU_NEW_GAME;
  38.             break;
  39.           case MENU_VOL:
  40.             whichActive=MENU_LOAD_GAME;
  41.             break;
  42.           case MENU_EXIT:
  43.             whichActive=MENU_VOL;
  44.             break;
  45.           case MENU_ABOUT:
  46.             whichActive=MENU_EXIT;
  47.             break;
  48.         }
  49.         break;
  50.       case DIRECTION_DOWN:
  51.         switch(whichActive)
  52.         {
  53.           case MENU_NEW_GAME:
  54.             whichActive=MENU_LOAD_GAME;
  55.             break;
  56.           case MENU_LOAD_GAME:
  57.             whichActive=MENU_VOL;
  58.             break;
  59.           case MENU_VOL:
  60.             whichActive=MENU_EXIT;
  61.             break;
  62.           case MENU_EXIT:
  63.             whichActive=MENU_ABOUT;
  64.             break;
  65.           case MENU_ABOUT:
  66.             whichActive=MENU_NEW_GAME;
  67.             break;
  68.         }
  69.         break;
  70.     }
  71.   }
  72.   public void action()
  73.   {
  74.     switch(menuState)
  75.     {
  76.       case MENU_STATE_MAIN:
  77.         if((allAction&LPKeyMask.MASK_KEY_UP_FLAG)!=0)
  78.         {
  79.           menuSelect(DIRECTION_UP);
  80.           allAction&=~LPKeyMask.MASK_KEY_UP_FLAG;
  81.         }
  82.         else if((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)!=0)
  83.         {
  84.           menuSelect(DIRECTION_DOWN);
  85.           allAction&=~LPKeyMask.MASK_KEY_DOWN_FLAG;
  86.         }
  87.         else if((allAction&LPKeyMask.MASK_KEY_OK_FLAG)!=0)
  88.         {
  89.           switch(whichActive)
  90.           {
  91.             case MENU_NEW_GAME:
  92.               gm.menuBeginNewGame();
  93.               break;
  94.             case MENU_LOAD_GAME:
  95.               gm.menuLoadGame();
  96.               break;
  97.             case MENU_VOL:
  98.               isVolOn=!isVolOn;
  99.               gm.menuAudio();
  100.               break;
  101.             case MENU_EXIT:
  102.               gm.menuExitGame();
  103.               break;
  104.             case MENU_ABOUT:
  105.               //about
  106.               menuState=MENU_STATE_ABOUT;
  107.               gm.menuAbout();
  108.               break;
  109.           }
  110.           allAction&=~LPKeyMask.MASK_KEY_OK_FLAG;
  111.         }
  112.         break;
  113.     }
  114.   }
  115.   public void judgeKeyCode(int keyCode,int GameKeyCode)
  116.   {
  117.     switch(GameKeyCode)
  118.     {
  119.       case Canvas.FIRE:
  120.         if(((allAction&LPKeyMask.MASK_KEY_OK)==0)
  121.            &&((allAction&LPKeyMask.MASK_KEY_OK_FLAG)==0))
  122.         {
  123.           allAction|=LPKeyMask.MASK_KEY_OK|LPKeyMask.MASK_KEY_OK_FLAG;
  124.         }
  125.         break;
  126.       case Canvas.UP:
  127.         if(((allAction&LPKeyMask.MASK_KEY_UP)==0)
  128.            &&((allAction&LPKeyMask.MASK_KEY_UP_FLAG)==0))
  129.         {
  130.           allAction|=LPKeyMask.MASK_KEY_UP|LPKeyMask.MASK_KEY_UP_FLAG;
  131.         }
  132.         break;
  133.       case Canvas.DOWN:
  134.         if(((allAction&LPKeyMask.MASK_KEY_DOWN)==0)
  135.            &&((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)==0))
  136.         {
  137.           allAction|=LPKeyMask.MASK_KEY_DOWN|LPKeyMask.MASK_KEY_DOWN_FLAG;
  138.         }
  139.         break;
  140.       case Canvas.LEFT:
  141.         if(((allAction&LPKeyMask.MASK_KEY_LEFT)==0)
  142.            &&((allAction&LPKeyMask.MASK_KEY_LEFT_FLAG)==0))
  143.         {
  144.           allAction|=LPKeyMask.MASK_KEY_LEFT|LPKeyMask.MASK_KEY_LEFT_FLAG;
  145.         }
  146.         break;
  147.       case Canvas.RIGHT:
  148.         if(((allAction&LPKeyMask.MASK_KEY_RIGHT)==0)
  149.            &&((allAction&LPKeyMask.MASK_KEY_RIGHT_FLAG)==0))
  150.         {
  151.           allAction|=LPKeyMask.MASK_KEY_RIGHT|LPKeyMask.MASK_KEY_RIGHT_FLAG;
  152.         }
  153.         break;
  154.       default:
  155.         switch(keyCode)
  156.         {
  157.           case Canvas.KEY_NUM2: //up
  158.             if(((allAction&LPKeyMask.MASK_KEY_UP)==0)
  159.                &&((allAction&LPKeyMask.MASK_KEY_UP_FLAG)==0))
  160.             {
  161.               allAction|=LPKeyMask.MASK_KEY_UP|LPKeyMask.MASK_KEY_UP_FLAG;
  162.             }
  163.             break;
  164.           case Canvas.KEY_NUM8: //down
  165.             if(((allAction&LPKeyMask.MASK_KEY_DOWN)==0)
  166.                &&((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)==0))
  167.             {
  168.               allAction|=LPKeyMask.MASK_KEY_DOWN|LPKeyMask.MASK_KEY_DOWN_FLAG;
  169.             }
  170.             break;
  171.           case Canvas.KEY_NUM4: //left
  172.             if(((allAction&LPKeyMask.MASK_KEY_LEFT)==0)
  173.                &&((allAction&LPKeyMask.MASK_KEY_LEFT_FLAG)==0))
  174.             {
  175.               allAction|=LPKeyMask.MASK_KEY_LEFT|LPKeyMask.MASK_KEY_LEFT_FLAG;
  176.             }
  177.             break;
  178.           case Canvas.KEY_NUM6: //right
  179.             if(((allAction&LPKeyMask.MASK_KEY_RIGHT)==0)
  180.                &&((allAction&LPKeyMask.MASK_KEY_RIGHT_FLAG)==0))
  181.             {
  182.               allAction|=LPKeyMask.MASK_KEY_RIGHT|LPKeyMask.MASK_KEY_RIGHT_FLAG;
  183.             }
  184.             break;
  185.           case Canvas.KEY_NUM5: //ok
  186.             if(((allAction&LPKeyMask.MASK_KEY_OK)==0)
  187.                &&((allAction&LPKeyMask.MASK_KEY_OK_FLAG)==0))
  188.             {
  189.               allAction|=LPKeyMask.MASK_KEY_OK|LPKeyMask.MASK_KEY_OK_FLAG;
  190.             }
  191.             break;
  192.         }
  193.         break;
  194.     }
  195.   }
  196.   public void freeKey(int keyCode,int gameKeyCode)
  197.   {
  198.     switch(gameKeyCode)
  199.     {
  200.       case Canvas.FIRE:
  201.         allAction&=~LPKeyMask.MASK_KEY_OK;
  202.         break;
  203.       case Canvas.UP:
  204.         allAction&=~LPKeyMask.MASK_KEY_UP;
  205.         break;
  206.       case Canvas.DOWN:
  207.         allAction&=~LPKeyMask.MASK_KEY_DOWN;
  208.         break;
  209.       case Canvas.LEFT:
  210.         allAction&=~LPKeyMask.MASK_KEY_LEFT;
  211.         break;
  212.       case Canvas.RIGHT:
  213.         allAction&=~LPKeyMask.MASK_KEY_RIGHT;
  214.         break;
  215.       default:
  216.         switch(keyCode)
  217.         {
  218.           case Canvas.KEY_NUM2: //up
  219.             allAction&=~LPKeyMask.MASK_KEY_UP;
  220.             break;
  221.           case Canvas.KEY_NUM8: //down
  222.             allAction&=~LPKeyMask.MASK_KEY_DOWN;
  223.             break;
  224.           case Canvas.KEY_NUM4: //left
  225.             allAction&=~LPKeyMask.MASK_KEY_LEFT;
  226.             break;
  227.           case Canvas.KEY_NUM6: //right
  228.             allAction&=~LPKeyMask.MASK_KEY_RIGHT;
  229.             break;
  230.           case Canvas.KEY_NUM5:
  231.             allAction&=~LPKeyMask.MASK_KEY_OK;
  232.             break;
  233.         }
  234.         break;
  235.     }
  236.   }
  237.   public void init()
  238.   {
  239.     menuState=MENU_STATE_MAIN;
  240.     whichActive=MENU_NEW_GAME;
  241.     allAction=0;
  242.   }
  243. // property
  244. //public
  245.   public int whichActive;
  246.   public int menuState;
  247.   public int allAction;
  248.   public boolean isVolOn=true;
  249.   private LPIGameManager gm;
  250. //final
  251.   public static final int MENU_NEW_GAME=0;
  252.   public static final int MENU_LOAD_GAME=1;
  253.   public static final int MENU_VOL=3;
  254.   public static final int MENU_EXIT=4;
  255.   public static final int MENU_ABOUT=5;
  256.   public static final int MENU_STATE_MAIN=0;
  257.   public static final int MENU_STATE_ABOUT=2;
  258.   private static final int DIRECTION_UP=0;
  259.   private static final int DIRECTION_DOWN=1;
  260. }