Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Menu/Menu.cpp@17:2a909f7da973, 2018-05-08 (annotated)
- Committer:
- Andrew_M
- Date:
- Tue May 08 14:52:29 2018 +0000
- Revision:
- 17:2a909f7da973
- Parent:
- 15:130900e5c268
More comments and debugging tools added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Andrew_M | 8:9d01fd4a63ad | 1 | #include "Menu.h" |
Andrew_M | 8:9d01fd4a63ad | 2 | |
Andrew_M | 8:9d01fd4a63ad | 3 | // nothing doing in the constructor and destructor |
Andrew_M | 8:9d01fd4a63ad | 4 | Menu::Menu() |
Andrew_M | 8:9d01fd4a63ad | 5 | { |
Andrew_M | 8:9d01fd4a63ad | 6 | |
Andrew_M | 8:9d01fd4a63ad | 7 | } |
Andrew_M | 8:9d01fd4a63ad | 8 | |
Andrew_M | 8:9d01fd4a63ad | 9 | Menu::~Menu() |
Andrew_M | 8:9d01fd4a63ad | 10 | { |
Andrew_M | 8:9d01fd4a63ad | 11 | |
Andrew_M | 8:9d01fd4a63ad | 12 | } |
Andrew_M | 8:9d01fd4a63ad | 13 | |
Andrew_M | 8:9d01fd4a63ad | 14 | void Menu::init() |
Andrew_M | 8:9d01fd4a63ad | 15 | { |
Andrew_M | 15:130900e5c268 | 16 | // initialises the default values |
Andrew_M | 17:2a909f7da973 | 17 | |
Andrew_M | 9:fe86ddbf7799 | 18 | _start = false; |
Andrew_M | 8:9d01fd4a63ad | 19 | |
Andrew_M | 10:279d3775d52c | 20 | _mainSelection = 1; |
Andrew_M | 10:279d3775d52c | 21 | |
Andrew_M | 10:279d3775d52c | 22 | _difficulty = 2; |
Andrew_M | 10:279d3775d52c | 23 | |
Andrew_M | 10:279d3775d52c | 24 | _difSelection = _difficulty; |
Andrew_M | 10:279d3775d52c | 25 | |
Andrew_M | 10:279d3775d52c | 26 | _level = 1; |
Andrew_M | 10:279d3775d52c | 27 | |
Andrew_M | 10:279d3775d52c | 28 | _lvlSelection = _level; |
Andrew_M | 10:279d3775d52c | 29 | |
Andrew_M | 10:279d3775d52c | 30 | _buttonPressed = false; |
Andrew_M | 10:279d3775d52c | 31 | |
Andrew_M | 10:279d3775d52c | 32 | _menuScreen = "main"; |
Andrew_M | 17:2a909f7da973 | 33 | |
Andrew_M | 17:2a909f7da973 | 34 | printf("Menu initialised\n"); |
Andrew_M | 9:fe86ddbf7799 | 35 | } |
Andrew_M | 8:9d01fd4a63ad | 36 | |
Andrew_M | 9:fe86ddbf7799 | 37 | void Menu::update() |
Andrew_M | 9:fe86ddbf7799 | 38 | { |
Andrew_M | 10:279d3775d52c | 39 | //moves arrow around menus |
Andrew_M | 14:a57a40ff9430 | 40 | moveArrow(); |
Andrew_M | 14:a57a40ff9430 | 41 | |
Andrew_M | 15:130900e5c268 | 42 | //checks if a button has been pressed then checks what should be altered depending on the currently selected item |
Andrew_M | 14:a57a40ff9430 | 43 | if (_buttonPressed) { |
Andrew_M | 14:a57a40ff9430 | 44 | selectItem(); |
Andrew_M | 14:a57a40ff9430 | 45 | } |
Andrew_M | 14:a57a40ff9430 | 46 | } |
Andrew_M | 14:a57a40ff9430 | 47 | |
Andrew_M | 14:a57a40ff9430 | 48 | void Menu::moveArrow() |
Andrew_M | 14:a57a40ff9430 | 49 | { |
Andrew_M | 15:130900e5c268 | 50 | //each part of the menu has different selection tracker, this means that it will remember what was selected before moving between menu screens |
Andrew_M | 17:2a909f7da973 | 51 | |
Andrew_M | 10:279d3775d52c | 52 | if (_menuScreen == "main" ) { |
Andrew_M | 15:130900e5c268 | 53 | if (_d == N && _mainSelection > 1) { //checks the menu arrow location and direction wanting to be moved |
Andrew_M | 15:130900e5c268 | 54 | _mainSelection -= 1; //if there is still a postion to be moved to in that direction, the currently selected menu item changes |
Andrew_M | 17:2a909f7da973 | 55 | //all the menu navigation functions in the same way |
Andrew_M | 10:279d3775d52c | 56 | } else if (_d == S && _mainSelection < 3) { |
Andrew_M | 10:279d3775d52c | 57 | _mainSelection += 1; |
Andrew_M | 10:279d3775d52c | 58 | } |
Andrew_M | 10:279d3775d52c | 59 | } else if (_menuScreen == "dif" ) { |
Andrew_M | 10:279d3775d52c | 60 | if (_d == N && _difSelection > 1) { |
Andrew_M | 10:279d3775d52c | 61 | _difSelection -= 1; |
Andrew_M | 11:b25874e7efe4 | 62 | } else if (_d == S && _difSelection < 4) { |
Andrew_M | 10:279d3775d52c | 63 | _difSelection += 1; |
Andrew_M | 10:279d3775d52c | 64 | } |
Andrew_M | 10:279d3775d52c | 65 | } else if (_menuScreen == "lvl" ) { |
Andrew_M | 10:279d3775d52c | 66 | if (_d == N && _lvlSelection > 1) { |
Andrew_M | 10:279d3775d52c | 67 | _lvlSelection -= 1; |
Andrew_M | 10:279d3775d52c | 68 | } else if (_d == S && _lvlSelection < 3) { |
Andrew_M | 10:279d3775d52c | 69 | _lvlSelection += 1; |
Andrew_M | 10:279d3775d52c | 70 | } |
Andrew_M | 9:fe86ddbf7799 | 71 | } |
Andrew_M | 14:a57a40ff9430 | 72 | } |
Andrew_M | 8:9d01fd4a63ad | 73 | |
Andrew_M | 14:a57a40ff9430 | 74 | void Menu::selectItem() |
Andrew_M | 14:a57a40ff9430 | 75 | { |
Andrew_M | 15:130900e5c268 | 76 | if (_menuScreen == "main" ) { //checks the menu screen |
Andrew_M | 15:130900e5c268 | 77 | if (_mainSelection == 1) { //checks what the current selection is |
Andrew_M | 15:130900e5c268 | 78 | _start = true; //applies the needed change depending on the selection |
Andrew_M | 17:2a909f7da973 | 79 | printf("Game Start\n"); |
Andrew_M | 14:a57a40ff9430 | 80 | } else if (_mainSelection == 2) { |
Andrew_M | 14:a57a40ff9430 | 81 | _menuScreen = "lvl"; |
Andrew_M | 17:2a909f7da973 | 82 | printf("Menu Screen Changed\n"); |
Andrew_M | 14:a57a40ff9430 | 83 | } else if (_mainSelection == 3) { |
Andrew_M | 14:a57a40ff9430 | 84 | _menuScreen = "dif"; |
Andrew_M | 17:2a909f7da973 | 85 | printf("Menu Screen Changed\n"); |
Andrew_M | 14:a57a40ff9430 | 86 | } |
Andrew_M | 15:130900e5c268 | 87 | } else { //an else is used as the other two menu screen MUST return to the main menu once a selection is made |
Andrew_M | 14:a57a40ff9430 | 88 | if (_menuScreen == "dif" ) { |
Andrew_M | 14:a57a40ff9430 | 89 | if (_difSelection == 1) { |
Andrew_M | 14:a57a40ff9430 | 90 | _difficulty = 1; |
Andrew_M | 14:a57a40ff9430 | 91 | } else if (_difSelection == 2) { |
Andrew_M | 14:a57a40ff9430 | 92 | _difficulty = 2; |
Andrew_M | 14:a57a40ff9430 | 93 | } else if (_difSelection == 3) { |
Andrew_M | 14:a57a40ff9430 | 94 | _difficulty = 3; |
Andrew_M | 14:a57a40ff9430 | 95 | } else if (_difSelection == 4) { |
Andrew_M | 14:a57a40ff9430 | 96 | _difficulty = 4; |
Andrew_M | 10:279d3775d52c | 97 | } |
Andrew_M | 14:a57a40ff9430 | 98 | } else if (_menuScreen == "lvl" ) { |
Andrew_M | 14:a57a40ff9430 | 99 | if (_lvlSelection == 1) { |
Andrew_M | 14:a57a40ff9430 | 100 | _level = 1; |
Andrew_M | 14:a57a40ff9430 | 101 | } else if (_lvlSelection == 2) { |
Andrew_M | 14:a57a40ff9430 | 102 | _level = 2; |
Andrew_M | 14:a57a40ff9430 | 103 | } else if (_lvlSelection == 3) { |
Andrew_M | 14:a57a40ff9430 | 104 | _level = 3; |
Andrew_M | 10:279d3775d52c | 105 | } |
Andrew_M | 14:a57a40ff9430 | 106 | } |
Andrew_M | 9:fe86ddbf7799 | 107 | |
Andrew_M | 15:130900e5c268 | 108 | _menuScreen = "main"; //returns to the main menu |
Andrew_M | 17:2a909f7da973 | 109 | printf("Menu Screen Changed\n"); |
Andrew_M | 10:279d3775d52c | 110 | } |
Andrew_M | 8:9d01fd4a63ad | 111 | } |
Andrew_M | 8:9d01fd4a63ad | 112 | |
Andrew_M | 9:fe86ddbf7799 | 113 | void Menu::read_input(Gamepad &pad) |
Andrew_M | 9:fe86ddbf7799 | 114 | { |
Andrew_M | 10:279d3775d52c | 115 | _buttonPressed = false; |
Andrew_M | 10:279d3775d52c | 116 | |
Andrew_M | 15:130900e5c268 | 117 | |
Andrew_M | 15:130900e5c268 | 118 | //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 |
Andrew_M | 11:b25874e7efe4 | 119 | 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)) { |
Andrew_M | 10:279d3775d52c | 120 | _buttonPressed = true; |
Andrew_M | 17:2a909f7da973 | 121 | |
Andrew_M | 17:2a909f7da973 | 122 | printf("Button Pressed\n"); |
Andrew_M | 17:2a909f7da973 | 123 | |
Andrew_M | 15:130900e5c268 | 124 | pad.tone(750.0,0.1); //plays a noise to show the user that the item has been selected |
Andrew_M | 17:2a909f7da973 | 125 | wait(0.1); //waits to reduce 'ghosting' of inputs |
Andrew_M | 17:2a909f7da973 | 126 | |
Andrew_M | 10:279d3775d52c | 127 | } |
Andrew_M | 15:130900e5c268 | 128 | _d = pad.get_direction(); //gets the current direction of the joystick for later use |
Andrew_M | 10:279d3775d52c | 129 | |
Andrew_M | 9:fe86ddbf7799 | 130 | } |
Andrew_M | 9:fe86ddbf7799 | 131 | |
Andrew_M | 9:fe86ddbf7799 | 132 | void Menu::draw(N5110 &lcd) |
Andrew_M | 9:fe86ddbf7799 | 133 | { |
Andrew_M | 15:130900e5c268 | 134 | lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws an outline |
Andrew_M | 15:130900e5c268 | 135 | if (_menuScreen == "main" ) { //checks the currently selected screen |
Andrew_M | 17:2a909f7da973 | 136 | |
Andrew_M | 15:130900e5c268 | 137 | lcd.printString(" Start",2,1); //draws the related elements |
Andrew_M | 11:b25874e7efe4 | 138 | lcd.printString(" Level Select",2,2); |
Andrew_M | 11:b25874e7efe4 | 139 | lcd.printString(" Difficulty",2,3); |
Andrew_M | 10:279d3775d52c | 140 | |
Andrew_M | 15:130900e5c268 | 141 | lcd.printString(">",2,_mainSelection); //draws the selection arrow |
Andrew_M | 10:279d3775d52c | 142 | |
Andrew_M | 15:130900e5c268 | 143 | int _snake[8][40] = { //makes a snake sprite to be drawn |
Andrew_M | 12:d3eef5ea3f43 | 144 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 145 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 146 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 147 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 148 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 149 | { 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}, |
Andrew_M | 12:d3eef5ea3f43 | 150 | { 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}, |
Andrew_M | 14:a57a40ff9430 | 151 | { 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} |
Andrew_M | 12:d3eef5ea3f43 | 152 | |
Andrew_M | 12:d3eef5ea3f43 | 153 | }; |
Andrew_M | 14:a57a40ff9430 | 154 | |
Andrew_M | 15:130900e5c268 | 155 | lcd.drawSprite(10,35,8,40,(int *)_snake); //draws the snake sprite bellow the menu elements for decoration |
Andrew_M | 12:d3eef5ea3f43 | 156 | |
Andrew_M | 10:279d3775d52c | 157 | } else if ( _menuScreen == "dif") { |
Andrew_M | 9:fe86ddbf7799 | 158 | |
Andrew_M | 11:b25874e7efe4 | 159 | lcd.printString(" Easy",2,1); |
Andrew_M | 11:b25874e7efe4 | 160 | lcd.printString(" Normal",2,2); |
Andrew_M | 11:b25874e7efe4 | 161 | lcd.printString(" Hard",2,3); |
Andrew_M | 11:b25874e7efe4 | 162 | lcd.printString(" Adaptive",2,4); |
Andrew_M | 10:279d3775d52c | 163 | |
Andrew_M | 11:b25874e7efe4 | 164 | lcd.printString(">",2,_difSelection); |
Andrew_M | 10:279d3775d52c | 165 | |
Andrew_M | 10:279d3775d52c | 166 | } else if ( _menuScreen == "lvl") { |
Andrew_M | 10:279d3775d52c | 167 | |
Andrew_M | 11:b25874e7efe4 | 168 | lcd.printString(" 1",2,1); |
Andrew_M | 11:b25874e7efe4 | 169 | lcd.printString(" 2",2,2); |
Andrew_M | 11:b25874e7efe4 | 170 | lcd.printString(" 3",2,3); |
Andrew_M | 10:279d3775d52c | 171 | |
Andrew_M | 11:b25874e7efe4 | 172 | lcd.printString(">",2,_lvlSelection); |
Andrew_M | 10:279d3775d52c | 173 | } |
Andrew_M | 9:fe86ddbf7799 | 174 | } |
Andrew_M | 9:fe86ddbf7799 | 175 | |
Andrew_M | 9:fe86ddbf7799 | 176 | bool Menu::started() |
Andrew_M | 9:fe86ddbf7799 | 177 | { |
Andrew_M | 9:fe86ddbf7799 | 178 | return _start; |
Andrew_M | 9:fe86ddbf7799 | 179 | } |
Andrew_M | 10:279d3775d52c | 180 | |
Andrew_M | 10:279d3775d52c | 181 | int Menu::getDif() |
Andrew_M | 10:279d3775d52c | 182 | { |
Andrew_M | 10:279d3775d52c | 183 | return _difficulty; |
Andrew_M | 10:279d3775d52c | 184 | } |
Andrew_M | 12:d3eef5ea3f43 | 185 | |
Andrew_M | 12:d3eef5ea3f43 | 186 | int Menu::getLvl() |
Andrew_M | 12:d3eef5ea3f43 | 187 | { |
Andrew_M | 12:d3eef5ea3f43 | 188 | return _level; |
Andrew_M | 12:d3eef5ea3f43 | 189 | } |
Andrew_M | 17:2a909f7da973 | 190 | |
Andrew_M | 17:2a909f7da973 | 191 |