AirsoftTimer software based on mbed
Dependencies: mbed TextLCD keypad
games/GameSelector.cpp@21:f4e556dc9885, 2015-05-24 (annotated)
- Committer:
- sillevl
- Date:
- Sun May 24 19:29:28 2015 +0000
- Revision:
- 21:f4e556dc9885
- Parent:
- 19:2eba101d9c2c
Gameselector print list on keyEvent
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 10:afc22465169e | 1 | #include "GameSelector.h" |
sillevl | 10:afc22465169e | 2 | |
sillevl | 10:afc22465169e | 3 | GameSelector::GameSelector(Board* board){ |
sillevl | 10:afc22465169e | 4 | this->board = board; |
sillevl | 19:2eba101d9c2c | 5 | this->board->attach(this, &GameSelector::buttonEvent); |
sillevl | 10:afc22465169e | 6 | titles[0] = "Hold it"; |
sillevl | 10:afc22465169e | 7 | titles[1] = "Capture the bomb"; |
sillevl | 10:afc22465169e | 8 | titles[2] = "Hurry up"; |
sillevl | 10:afc22465169e | 9 | titles[3] = "Search & destroy"; |
sillevl | 10:afc22465169e | 10 | titles[4] = "Simple timer"; |
sillevl | 10:afc22465169e | 11 | titles[5] = "DummyGame 6"; |
sillevl | 10:afc22465169e | 12 | titles[6] = "DummyGame 7"; |
sillevl | 10:afc22465169e | 13 | total_selections = 7; |
sillevl | 10:afc22465169e | 14 | current_selection = 0; |
sillevl | 10:afc22465169e | 15 | start_position = 0; |
sillevl | 17:19dbb1dbb640 | 16 | selected_game = -1; |
sillevl | 19:2eba101d9c2c | 17 | |
sillevl | 19:2eba101d9c2c | 18 | update_screen = false; |
sillevl | 17:19dbb1dbb640 | 19 | } |
sillevl | 17:19dbb1dbb640 | 20 | |
sillevl | 17:19dbb1dbb640 | 21 | int GameSelector::select(){ |
sillevl | 17:19dbb1dbb640 | 22 | print_list(); |
sillevl | 21:f4e556dc9885 | 23 | while(selected_game == -1){} // wait until selection is done |
sillevl | 17:19dbb1dbb640 | 24 | return selected_game; |
sillevl | 10:afc22465169e | 25 | } |
sillevl | 10:afc22465169e | 26 | |
sillevl | 19:2eba101d9c2c | 27 | uint32_t GameSelector::buttonEvent(uint32_t c){ |
sillevl | 17:19dbb1dbb640 | 28 | switch(c){ |
sillevl | 17:19dbb1dbb640 | 29 | case '2': |
sillevl | 17:19dbb1dbb640 | 30 | go_up(); |
sillevl | 17:19dbb1dbb640 | 31 | break; |
sillevl | 17:19dbb1dbb640 | 32 | case '5': |
sillevl | 17:19dbb1dbb640 | 33 | selected_game = current_selection; |
sillevl | 17:19dbb1dbb640 | 34 | break; |
sillevl | 17:19dbb1dbb640 | 35 | case '8': |
sillevl | 17:19dbb1dbb640 | 36 | go_down(); |
sillevl | 17:19dbb1dbb640 | 37 | break; |
sillevl | 17:19dbb1dbb640 | 38 | default: |
sillevl | 17:19dbb1dbb640 | 39 | break; |
sillevl | 17:19dbb1dbb640 | 40 | } |
sillevl | 18:abcebc4d0da0 | 41 | return 0; |
sillevl | 19:2eba101d9c2c | 42 | } |
sillevl | 10:afc22465169e | 43 | |
sillevl | 10:afc22465169e | 44 | void GameSelector::print_list(){ |
sillevl | 10:afc22465169e | 45 | board->lcd->cls(); |
sillevl | 10:afc22465169e | 46 | for(int i = 0; i < 4; i++){ |
sillevl | 10:afc22465169e | 47 | board->lcd->locate(2,i); |
sillevl | 10:afc22465169e | 48 | board->lcd->printf(titles[i+start_position]); |
sillevl | 10:afc22465169e | 49 | } |
sillevl | 10:afc22465169e | 50 | print_selection_arrow(); |
sillevl | 10:afc22465169e | 51 | print_up_down_arrows(); |
sillevl | 19:2eba101d9c2c | 52 | |
sillevl | 19:2eba101d9c2c | 53 | update_screen = false; |
sillevl | 10:afc22465169e | 54 | } |
sillevl | 10:afc22465169e | 55 | |
sillevl | 10:afc22465169e | 56 | void GameSelector::print_up_down_arrows(){ |
sillevl | 10:afc22465169e | 57 | if(start_position != 0){ |
sillevl | 10:afc22465169e | 58 | board->lcd->locate(19,0); |
sillevl | 10:afc22465169e | 59 | board->lcd->putc('\x02'); |
sillevl | 10:afc22465169e | 60 | } |
sillevl | 10:afc22465169e | 61 | if(start_position < total_selections - 4){ |
sillevl | 10:afc22465169e | 62 | board->lcd->locate(19,3); |
sillevl | 10:afc22465169e | 63 | board->lcd->putc('\x03'); |
sillevl | 10:afc22465169e | 64 | } |
sillevl | 10:afc22465169e | 65 | } |
sillevl | 10:afc22465169e | 66 | |
sillevl | 10:afc22465169e | 67 | void GameSelector::print_selection_arrow(){ |
sillevl | 10:afc22465169e | 68 | int line = current_selection - start_position; |
sillevl | 10:afc22465169e | 69 | board->lcd->locate(0,line); |
sillevl | 10:afc22465169e | 70 | board->lcd->putc('\x00'); |
sillevl | 10:afc22465169e | 71 | } |
sillevl | 10:afc22465169e | 72 | |
sillevl | 10:afc22465169e | 73 | void GameSelector::go_down(){ |
sillevl | 10:afc22465169e | 74 | if(current_selection < (total_selections - 1)){ |
sillevl | 10:afc22465169e | 75 | current_selection++; |
sillevl | 10:afc22465169e | 76 | } |
sillevl | 10:afc22465169e | 77 | if(current_selection - start_position >= 4){ |
sillevl | 10:afc22465169e | 78 | start_position = current_selection - 3; |
sillevl | 10:afc22465169e | 79 | } |
sillevl | 21:f4e556dc9885 | 80 | print_list(); |
sillevl | 10:afc22465169e | 81 | } |
sillevl | 10:afc22465169e | 82 | |
sillevl | 10:afc22465169e | 83 | void GameSelector::go_up(){ |
sillevl | 10:afc22465169e | 84 | if(current_selection > 0){ |
sillevl | 10:afc22465169e | 85 | current_selection--; |
sillevl | 10:afc22465169e | 86 | } |
sillevl | 10:afc22465169e | 87 | if(current_selection - start_position < 0){ |
sillevl | 10:afc22465169e | 88 | start_position = current_selection; |
sillevl | 10:afc22465169e | 89 | } |
sillevl | 21:f4e556dc9885 | 90 | print_list(); |
sillevl | 10:afc22465169e | 91 | } |