Custom menu
Fork of Menu by
Navigator.cpp@2:2654dc659298, 2013-03-05 (annotated)
- Committer:
- pyeh9
- Date:
- Tue Mar 05 21:24:37 2013 +0000
- Revision:
- 2:2654dc659298
- Parent:
- 1:84d263c8932d
- Child:
- 3:3fceb6ff921e
Second version - with comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pyeh9 | 1:84d263c8932d | 1 | #include "Navigator.h" |
pyeh9 | 1:84d263c8932d | 2 | |
pyeh9 | 1:84d263c8932d | 3 | Navigator::Navigator(Menu *root, RPG &rpg, TextLCD *lcd) : activeMenu(root), rpg(rpg), lcd(lcd) |
pyeh9 | 1:84d263c8932d | 4 | { |
pyeh9 | 1:84d263c8932d | 5 | bottom = root->selections.size(); |
pyeh9 | 1:84d263c8932d | 6 | cursorPos = 0; |
pyeh9 | 1:84d263c8932d | 7 | cursorLine = 1; |
pyeh9 | 1:84d263c8932d | 8 | button = 0; |
pyeh9 | 1:84d263c8932d | 9 | lastButton = 0; |
pyeh9 | 2:2654dc659298 | 10 | |
pyeh9 | 2:2654dc659298 | 11 | printMenu(); |
pyeh9 | 2:2654dc659298 | 12 | printCursor(); |
pyeh9 | 1:84d263c8932d | 13 | } |
pyeh9 | 1:84d263c8932d | 14 | |
pyeh9 | 1:84d263c8932d | 15 | void Navigator::printMenu() |
pyeh9 | 1:84d263c8932d | 16 | { |
pyeh9 | 1:84d263c8932d | 17 | lcd->cls(); |
pyeh9 | 1:84d263c8932d | 18 | if(bottom == 1){ // the current Menu only has one selection |
pyeh9 | 1:84d263c8932d | 19 | lcd->printf("%s\n", activeMenu->selections[0].selText); |
pyeh9 | 1:84d263c8932d | 20 | } else { |
pyeh9 | 1:84d263c8932d | 21 | if(cursorLine == 2){ // if we're at the bottom |
pyeh9 | 1:84d263c8932d | 22 | lcd->printf("%s\n", activeMenu->selections[cursorPos-1].selText); |
pyeh9 | 1:84d263c8932d | 23 | lcd->printf("%s\n", activeMenu->selections[cursorPos].selText); |
pyeh9 | 1:84d263c8932d | 24 | } else { |
pyeh9 | 1:84d263c8932d | 25 | lcd->printf("%s\n", activeMenu->selections[cursorPos].selText); |
pyeh9 | 1:84d263c8932d | 26 | lcd->printf("%s\n", activeMenu->selections[cursorPos+1].selText); |
pyeh9 | 1:84d263c8932d | 27 | } |
pyeh9 | 1:84d263c8932d | 28 | } |
pyeh9 | 1:84d263c8932d | 29 | } |
pyeh9 | 1:84d263c8932d | 30 | |
pyeh9 | 1:84d263c8932d | 31 | void Navigator::printCursor() |
pyeh9 | 1:84d263c8932d | 32 | { |
pyeh9 | 1:84d263c8932d | 33 | if(activeMenu->selections[cursorPos].childMenu == NULL) printf("No child menu\n"); |
pyeh9 | 1:84d263c8932d | 34 | else printf("child menu: %s\n", activeMenu->selections[cursorPos].childMenu->menuID); |
pyeh9 | 1:84d263c8932d | 35 | |
pyeh9 | 1:84d263c8932d | 36 | lcd->locate(0,0); |
pyeh9 | 1:84d263c8932d | 37 | if(cursorLine == 1){ |
pyeh9 | 1:84d263c8932d | 38 | lcd->putc('>'); |
pyeh9 | 1:84d263c8932d | 39 | lcd->locate(0,1); |
pyeh9 | 1:84d263c8932d | 40 | lcd->putc(' '); |
pyeh9 | 1:84d263c8932d | 41 | } else if(cursorLine == 2){ |
pyeh9 | 1:84d263c8932d | 42 | lcd->putc(' '); |
pyeh9 | 1:84d263c8932d | 43 | lcd->locate(0,1); |
pyeh9 | 1:84d263c8932d | 44 | lcd->putc('>'); |
pyeh9 | 1:84d263c8932d | 45 | } |
pyeh9 | 1:84d263c8932d | 46 | } |
pyeh9 | 1:84d263c8932d | 47 | |
pyeh9 | 1:84d263c8932d | 48 | void Navigator::poll() |
pyeh9 | 1:84d263c8932d | 49 | { |
pyeh9 | 1:84d263c8932d | 50 | if((direction = rpg.dir())!=0){ //Get Dir |
pyeh9 | 1:84d263c8932d | 51 | wait(0.2); |
pyeh9 | 1:84d263c8932d | 52 | if(direction == 1) moveDown(); |
pyeh9 | 1:84d263c8932d | 53 | else if(direction == -1) moveUp(); |
pyeh9 | 1:84d263c8932d | 54 | } |
pyeh9 | 1:84d263c8932d | 55 | |
pyeh9 | 1:84d263c8932d | 56 | if ((button = rpg.pb()) && !lastButton){ //prevents multiple selections when button is held down |
pyeh9 | 1:84d263c8932d | 57 | wait(0.2); |
pyeh9 | 1:84d263c8932d | 58 | if(activeMenu->selections[cursorPos].fun != NULL){ |
pyeh9 | 1:84d263c8932d | 59 | (activeMenu->selections[cursorPos].fun)(); |
pyeh9 | 1:84d263c8932d | 60 | } |
pyeh9 | 1:84d263c8932d | 61 | if(activeMenu->selections[cursorPos].childMenu != NULL){ |
pyeh9 | 1:84d263c8932d | 62 | activeMenu = activeMenu->selections[cursorPos].childMenu; |
pyeh9 | 1:84d263c8932d | 63 | bottom = activeMenu->selections.size(); |
pyeh9 | 1:84d263c8932d | 64 | cursorPos = 0; |
pyeh9 | 1:84d263c8932d | 65 | cursorLine = 1; |
pyeh9 | 1:84d263c8932d | 66 | printMenu(); |
pyeh9 | 1:84d263c8932d | 67 | printCursor(); |
pyeh9 | 1:84d263c8932d | 68 | } |
pyeh9 | 1:84d263c8932d | 69 | } |
pyeh9 | 1:84d263c8932d | 70 | lastButton = button; |
pyeh9 | 1:84d263c8932d | 71 | } |
pyeh9 | 1:84d263c8932d | 72 | |
pyeh9 | 1:84d263c8932d | 73 | void Navigator::moveUp() |
pyeh9 | 1:84d263c8932d | 74 | { |
pyeh9 | 1:84d263c8932d | 75 | if(cursorLine == 1){ |
pyeh9 | 1:84d263c8932d | 76 | printMenu(); |
pyeh9 | 1:84d263c8932d | 77 | } else if(cursorLine == 2){ |
pyeh9 | 1:84d263c8932d | 78 | cursorLine = 1; |
pyeh9 | 1:84d263c8932d | 79 | } |
pyeh9 | 1:84d263c8932d | 80 | |
pyeh9 | 1:84d263c8932d | 81 | if(cursorPos != 0){ |
pyeh9 | 1:84d263c8932d | 82 | cursorPos--; |
pyeh9 | 1:84d263c8932d | 83 | printMenu(); |
pyeh9 | 1:84d263c8932d | 84 | } |
pyeh9 | 1:84d263c8932d | 85 | printCursor(); |
pyeh9 | 1:84d263c8932d | 86 | } |
pyeh9 | 1:84d263c8932d | 87 | |
pyeh9 | 1:84d263c8932d | 88 | void Navigator::moveDown() |
pyeh9 | 1:84d263c8932d | 89 | { |
pyeh9 | 1:84d263c8932d | 90 | if(cursorLine == 1){ |
pyeh9 | 1:84d263c8932d | 91 | cursorLine = 2; |
pyeh9 | 1:84d263c8932d | 92 | } else if(cursorLine == 2){ |
pyeh9 | 1:84d263c8932d | 93 | printMenu(); |
pyeh9 | 1:84d263c8932d | 94 | } |
pyeh9 | 1:84d263c8932d | 95 | |
pyeh9 | 1:84d263c8932d | 96 | if(cursorPos != (bottom-1)){ |
pyeh9 | 1:84d263c8932d | 97 | cursorPos++; |
pyeh9 | 1:84d263c8932d | 98 | printMenu(); |
pyeh9 | 1:84d263c8932d | 99 | } |
pyeh9 | 1:84d263c8932d | 100 | printCursor(); |
pyeh9 | 1:84d263c8932d | 101 | } |