rusefi-1/firmware/controllers/gauges/lcd_menu_tree.cpp

95 lines
2.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file lcd_menu_tree.cpp
*
* @date Jan 6, 2015
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
#include "pch.h"
2015-07-10 06:01:56 -07:00
#include "stddef.h"
#include "lcd_menu_tree.h"
MenuTree::MenuTree(MenuItem *root) {
this->root = root;
}
void MenuTree::init(MenuItem *first, int linesCount) {
this->linesCount = linesCount;
current = first;
topVisible = first;
}
void MenuTree::enterSubMenu(void) {
if (current->firstChild != NULL) {
current = topVisible = current->firstChild;
} else if (current->callback != NULL) {
VoidCallback cb = current->callback;
cb();
}
}
void MenuTree::back(void) {
if (current->parent == root)
return; // we are on the top level already
current = topVisible = current->parent->topOfTheList;
}
void MenuTree::nextItem(void) {
if (!current->next) {
2015-07-10 06:01:56 -07:00
current = topVisible = current->topOfTheList;
return;
}
current = current->next;
if (current->index - topVisible->index == linesCount)
topVisible = topVisible->next;
}
2016-04-23 20:07:45 -07:00
/**
* This constructor created a menu item and associates a callback with it
*/
2015-07-10 06:01:56 -07:00
MenuItem::MenuItem(MenuItem * parent, const char *text, VoidCallback callback) {
baseConstructor(parent, LL_STRING, text, callback);
}
2016-04-23 20:07:45 -07:00
/**
* Looks like this constructor is used to create
*/
2015-07-10 06:01:56 -07:00
MenuItem::MenuItem(MenuItem * parent, const char *text) {
baseConstructor(parent, LL_STRING, text, NULL);
}
2016-04-23 20:07:45 -07:00
/**
* This constructor is used for lines with dynamic content
*/
2015-07-10 06:01:56 -07:00
MenuItem::MenuItem(MenuItem * parent, lcd_line_e lcdLine) {
baseConstructor(parent, lcdLine, NULL, NULL);
}
void MenuItem::baseConstructor(MenuItem * parent, lcd_line_e lcdLine, const char *text, VoidCallback callback) {
this->parent = parent;
this->lcdLine = lcdLine;
this->text = text;
this->callback = callback;
2019-10-07 22:26:35 -07:00
firstChild = nullptr;
lastChild = nullptr;
topOfTheList = nullptr;
next = nullptr;
2015-07-10 06:01:56 -07:00
index = 0;
// root element has NULL parent
if (parent != NULL) {
if (!parent->firstChild) {
2015-07-10 06:01:56 -07:00
topOfTheList = this;
parent->firstChild = this;
}
if (parent->lastChild != NULL) {
index = parent->lastChild->index + 1;
topOfTheList = parent->lastChild->topOfTheList;
parent->lastChild->next = this;
}
parent->lastChild = this;
}
}