ELEC2645 (2017/18) / Mbed OS el16ajm
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Menu.cpp Source File

Menu.cpp

00001 #include "Menu.h"
00002 
00003 // nothing doing in the constructor and destructor
00004 Menu::Menu()
00005 {
00006 
00007 }
00008 
00009 Menu::~Menu()
00010 {
00011 
00012 }
00013 
00014 void Menu::init()
00015 {
00016     // initialises the default values
00017 
00018     _start = false;
00019 
00020     _mainSelection = 1;
00021 
00022     _difficulty = 2;
00023 
00024     _difSelection = _difficulty;
00025 
00026     _level = 1;
00027 
00028     _lvlSelection = _level;
00029 
00030     _buttonPressed = false;
00031 
00032     _menuScreen = "main";
00033 
00034     printf("Menu initialised\n");
00035 }
00036 
00037 void Menu::update()
00038 {
00039     //moves arrow around menus
00040     moveArrow();
00041 
00042     //checks if a button has been pressed then checks what should be altered depending on the currently selected item
00043     if (_buttonPressed) {
00044         selectItem();
00045     }
00046 }
00047 
00048 void Menu::moveArrow()
00049 {
00050     //each part of the menu has different selection tracker, this means that it will remember what was selected before moving between menu screens
00051 
00052     if (_menuScreen == "main" ) {
00053         if (_d == N && _mainSelection > 1) {    //checks the menu arrow location and direction wanting to be moved
00054             _mainSelection -= 1;                //if there is still a postion to be moved to in that direction, the currently selected menu item changes
00055             //all the menu navigation functions in the same way
00056         } else if (_d == S && _mainSelection < 3) {
00057             _mainSelection += 1;
00058         }
00059     } else if (_menuScreen == "dif" ) {
00060         if (_d == N && _difSelection > 1) {
00061             _difSelection -= 1;
00062         } else if (_d == S && _difSelection < 4) {
00063             _difSelection += 1;
00064         }
00065     } else if (_menuScreen == "lvl" ) {
00066         if (_d == N && _lvlSelection > 1) {
00067             _lvlSelection -= 1;
00068         } else if (_d == S && _lvlSelection < 3) {
00069             _lvlSelection += 1;
00070         }
00071     }
00072 }
00073 
00074 void Menu::selectItem()
00075 {
00076     if (_menuScreen == "main" ) {           //checks the menu screen
00077         if (_mainSelection == 1) {          //checks what the current selection is
00078             _start = true;                  //applies the needed change depending on the selection
00079             printf("Game Start\n");
00080         } else if (_mainSelection == 2) {
00081             _menuScreen = "lvl";
00082             printf("Menu Screen Changed\n");
00083         } else if (_mainSelection == 3) {
00084             _menuScreen = "dif";
00085             printf("Menu Screen Changed\n");
00086         }
00087     } else {                                //an else is used as the other two menu screen MUST return to the main menu once a selection is made
00088         if (_menuScreen == "dif" ) {
00089             if (_difSelection == 1) {
00090                 _difficulty = 1;
00091             } else if (_difSelection == 2) {
00092                 _difficulty = 2;
00093             } else if (_difSelection == 3) {
00094                 _difficulty = 3;
00095             } else if (_difSelection == 4) {
00096                 _difficulty = 4;
00097             }
00098         } else if (_menuScreen == "lvl" ) {
00099             if (_lvlSelection == 1) {
00100                 _level = 1;
00101             } else if (_lvlSelection == 2) {
00102                 _level = 2;
00103             } else if (_lvlSelection == 3) {
00104                 _level = 3;
00105             }
00106         }
00107 
00108         _menuScreen = "main";               //returns to the main menu
00109         printf("Menu Screen Changed\n");
00110     }
00111 }
00112 
00113 void Menu::read_input(Gamepad &pad)
00114 {
00115     _buttonPressed = false;
00116 
00117 
00118     //checks if any button on the top of the gamepad has been pressed, there is no need to limit the user to one type of button
00119     if ( pad.check_event(Gamepad::START_PRESSED) || pad.check_event(Gamepad::A_PRESSED) || pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::X_PRESSED) || pad.check_event(Gamepad::Y_PRESSED) ||pad.check_event(Gamepad::BACK_PRESSED)) {
00120         _buttonPressed = true;
00121         
00122         printf("Button Pressed\n");
00123         
00124         pad.tone(750.0,0.1);    //plays a noise to show the user that the item has been selected
00125         wait(0.1);          //waits to reduce 'ghosting' of inputs
00126         
00127     }
00128     _d = pad.get_direction();   //gets the current direction of the joystick for later use
00129 
00130 }
00131 
00132 void Menu::draw(N5110 &lcd)
00133 {
00134     lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);       //draws an outline
00135     if (_menuScreen == "main" ) {                   //checks the currently selected screen
00136 
00137         lcd.printString(" Start",2,1);              //draws the related elements
00138         lcd.printString(" Level Select",2,2);
00139         lcd.printString(" Difficulty",2,3);
00140 
00141         lcd.printString(">",2,_mainSelection);      //draws the selection arrow
00142 
00143         int _snake[8][40] =   {                     //makes a snake sprite to be drawn
00144             { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
00145             { 0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
00146             { 0,0,0,0,1,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
00147             { 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0},
00148             { 0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
00149             { 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0},
00150             { 0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
00151             { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
00152 
00153         };
00154 
00155         lcd.drawSprite(10,35,8,40,(int *)_snake);   //draws the snake sprite bellow the menu elements for decoration
00156 
00157     } else if ( _menuScreen == "dif") {
00158 
00159         lcd.printString(" Easy",2,1);
00160         lcd.printString(" Normal",2,2);
00161         lcd.printString(" Hard",2,3);
00162         lcd.printString(" Adaptive",2,4);
00163 
00164         lcd.printString(">",2,_difSelection);
00165 
00166     }   else if ( _menuScreen == "lvl") {
00167 
00168         lcd.printString(" 1",2,1);
00169         lcd.printString(" 2",2,2);
00170         lcd.printString(" 3",2,3);
00171 
00172         lcd.printString(">",2,_lvlSelection);
00173     }
00174 }
00175 
00176 bool Menu::started()
00177 {
00178     return _start;
00179 }
00180 
00181 int Menu::getDif()
00182 {
00183     return _difficulty;
00184 }
00185 
00186 int Menu::getLvl()
00187 {
00188     return _level;
00189 }
00190 
00191