Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9
Fork of Menu by
Library to display menus on TextLCDs. Interaction with functions Up,Down and Select (Buttons or RPG) Based on menu-library from pyeh9
Navigator.cpp
- Committer:
- charly
- Date:
- 2015-01-12
- Revision:
- 7:12f2b3b7975f
- Parent:
- 6:819049708d51
- Child:
- 8:fbaeab73fe1a
File content as of revision 7:12f2b3b7975f:
#include "Navigator.h"
Navigator::Navigator(Menu *root, TextLCD_Base *lcd) : activeMenu(root), lcd(lcd)
{
_cursorPos = 0;
_cursorLine = 1;
_display_rows = lcd->rows();
printMenu();
printCursor();
}
void Navigator::printMenu()
{
int index =0;
lcd->cls();
for(int row=0; row < _display_rows; row++) {
lcd->locate(0,row);
index = row + _cursorPos - (_cursorLine-1); // index into selection for this line
//should we display a menu on this line?
if (index <= activeMenu->selections.size()-1 ) {
lcd->printf(" %s", activeMenu->selections[index].selText);
}
}
}
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);
for (int row=0; row<_display_rows; row++) {
lcd->locate(0,row);
if (row == _cursorLine-1) {
//we are on Cursor-Line
//print cursor
lcd->putc('>');
} else {
//on other lines print a space
lcd->putc(' ');
}
}
}
void Navigator::poll()
{
// no longer needed
}
void Navigator::select()
{
if(activeMenu->selections[_cursorPos].fun != NULL) {
//execute function
(activeMenu->selections[_cursorPos].fun)();
// refresh the Menu
//printMenu();
//printCursor();
}
if(activeMenu->selections[_cursorPos].childMenu != NULL) {
activeMenu = activeMenu->selections[_cursorPos].childMenu;
_cursorPos = 0;
_cursorLine = 1;
printMenu();
printCursor();
}
}
void Navigator::moveUp()
{
// allready on TOP of Display?
if(_cursorLine > 1) {
// scroll up cursor one line
_cursorLine--;
}
if(_cursorPos > 0) {
//scroll up one item
_cursorPos--;
}
printMenu();
printCursor();
}
void Navigator::moveDown()
{
// allready on last line of display?
if (_cursorPos == activeMenu->selections.size()-1) {
//stay on this line
} else {
// move down
if(_cursorLine < _display_rows) {
// Only move down cursor
_cursorLine++;
_cursorPos++;
} else {
// on last Display-Line scroll down Menu
_cursorPos++;
}
}
printMenu();
printCursor();
}
