Custom menu

Fork of Menu by Peihsun Yeh

Revision:
3:3fceb6ff921e
Parent:
2:2654dc659298
Child:
4:396eff5be1a1
diff -r 2654dc659298 -r 3fceb6ff921e Navigator.cpp
--- a/Navigator.cpp	Tue Mar 05 21:24:37 2013 +0000
+++ b/Navigator.cpp	Thu Feb 26 11:57:11 2015 +0000
@@ -1,101 +1,75 @@
 #include "Navigator.h"
 
-Navigator::Navigator(Menu *root, RPG &rpg, TextLCD *lcd) : activeMenu(root), rpg(rpg), lcd(lcd) 
-{
-    bottom = root->selections.size();
+Navigator::Navigator(Menu *root, TextLCD *lcd) :
+        root(root), activeMenu(root), _lcd(lcd) {
     cursorPos = 0;
-    cursorLine = 1;
-    button = 0;
-    lastButton = 0;
-    
     printMenu();
-    printCursor();
+}
+
+void Navigator::printMenu() {
+    _lcd->cls();
+    _lcd->printf("%s\n", activeMenu->menuID);
+    _lcd->printf(">%s\n", activeMenu->selections[cursorPos].selText);
 }
 
-void Navigator::printMenu()
-{ 
-    lcd->cls();
-    if(bottom == 1){ // the current Menu only has one selection
-        lcd->printf("%s\n", activeMenu->selections[0].selText);
-    } else {
-        if(cursorLine == 2){ // if we're at the bottom
-            lcd->printf("%s\n", activeMenu->selections[cursorPos-1].selText);
-            lcd->printf("%s\n", activeMenu->selections[cursorPos].selText);
-        } else {
-            lcd->printf("%s\n", activeMenu->selections[cursorPos].selText);
-            lcd->printf("%s\n", activeMenu->selections[cursorPos+1].selText);
-        }
+void Navigator::actionNone() {
+    if (_lastAction != none) {
+
     }
+    _lastAction = none;
 }
 
-void Navigator::printCursor()
-{   
-    if(activeMenu->selections[cursorPos].childMenu == NULL) printf("No child menu\n");
-    else printf("child menu: %s\n", activeMenu->selections[cursorPos].childMenu->menuID);
-     
-    lcd->locate(0,0);
-    if(cursorLine == 1){
-        lcd->putc('>');
-        lcd->locate(0,1);
-        lcd->putc(' ');
-    } else if(cursorLine == 2){
-        lcd->putc(' ');
-        lcd->locate(0,1);
-        lcd->putc('>');
+void Navigator::actionUp() {
+    if (_lastAction != up) {
+        if (cursorPos > 0) {
+            cursorPos--;
+            printMenu();
+        }
     }
+    _lastAction = up;
 }
 
-void Navigator::poll()
-{
-    if((direction = rpg.dir())!=0){ //Get Dir
-        wait(0.2); 
-        if(direction == 1) moveDown();
-        else if(direction == -1) moveUp();
-    }
-       
-    if ((button = rpg.pb()) && !lastButton){ //prevents multiple selections when button is held down
-        wait(0.2);
-        if(activeMenu->selections[cursorPos].fun != NULL){
-            (activeMenu->selections[cursorPos].fun)();
-        }
-        if(activeMenu->selections[cursorPos].childMenu != NULL){
-            activeMenu = activeMenu->selections[cursorPos].childMenu;
-            bottom = activeMenu->selections.size();
-            cursorPos = 0;
-            cursorLine = 1;
+void Navigator::actionDown() {
+    if (_lastAction != down) {
+        if (cursorPos < (activeMenu->selections.size() - 1)) {
+            cursorPos++;
             printMenu();
-            printCursor();
         }
     }
-    lastButton = button;
+    _lastAction = down;
+}
+
+void Navigator::actionBack() {
+    if (_lastAction != back) {
+        if (activeMenu->parent != NULL) {
+            activeMenu = activeMenu->parent;
+            cursorPos = 0;
+            printMenu();
+        }
+    }
+    _lastAction = back;
 }
 
-void Navigator::moveUp()
-{
-    if(cursorLine == 1){
-        printMenu();
-    } else if(cursorLine == 2){
-        cursorLine = 1;
+void Navigator::actionEnter() {
+    if (_lastAction != enter) {
+        if (activeMenu->selections[cursorPos].fun != NULL) {
+            (activeMenu->selections[cursorPos].fun)();
+            printMenu();
+        }
+        if (activeMenu->selections[cursorPos].childMenu != NULL) {
+            activeMenu = activeMenu->selections[cursorPos].childMenu;
+            cursorPos = 0;
+            printMenu();
+        }
     }
-    
-    if(cursorPos != 0){
-        cursorPos--;
-        printMenu();
-    }
-    printCursor();
+    _lastAction = enter;
 }
 
-void Navigator::moveDown()
-{
-    if(cursorLine == 1){
-        cursorLine = 2;
-    } else if(cursorLine == 2){
+void Navigator::actionHome() {
+    if (_lastAction != home) {
+        activeMenu = root;
+        cursorPos = 0;
         printMenu();
     }
-    
-    if(cursorPos != (bottom-1)){
-        cursorPos++;
-        printMenu();
-    }
-    printCursor();
-}
\ No newline at end of file
+    _lastAction = home;
+}