Optimize update_visible_items for higher efficiency

This commit is contained in:
Jan-Henrik 2020-02-26 19:53:16 +01:00
parent 602faa5c8f
commit c55c16ae38

View file

@ -43,6 +43,8 @@ void Menu::render(U8G2* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, ui
void Menu::update_visible_items() void Menu::update_visible_items()
{ {
AbstractMenuItem* currentlySelectedItem = this->visibleItems[this->selectedVisibleItem];
// set all visibleItems to null // set all visibleItems to null
this->visibleItemCount = 0; this->visibleItemCount = 0;
for (size_t i = 0; i < MAXIMUM_MENU_ITEM_COUNT; i++) { for (size_t i = 0; i < MAXIMUM_MENU_ITEM_COUNT; i++) {
@ -55,33 +57,31 @@ void Menu::update_visible_items()
this->visibleItems[this->visibleItemCount++] = this->items[i]; this->visibleItems[this->visibleItemCount++] = this->items[i];
} }
} }
}
void Menu::up() // if our visibleitem changed, chances are that the index of the selected item has changed:
{ if (currentlySelectedItem && this->visibleItems[this->selectedVisibleItem] != currentlySelectedItem) {
if (this->currentEditingVisibleItem >= 0) {
this->update_visible_items();
AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem];
item->decrease();
this->update_visible_items();
if (this->visibleItems[this->selectedVisibleItem] != item) {
int8_t delta = 0; int8_t delta = 0;
// the index of our visible item has changed. // the index of our visible item has changed.
// go through the list of visible items and find it! // go through the list of visible items and find it!
for (size_t i = 0; i < this->visibleItemCount; i++) { for (size_t i = 0; i < this->visibleItemCount; i++) {
if (this->visibleItems[i] == item) { if (this->visibleItems[i] == currentlySelectedItem) {
delta = this->selectedVisibleItem - i; delta = this->selectedVisibleItem - i;
this->selectedVisibleItem = this->currentEditingVisibleItem = i; this->selectedVisibleItem = this->currentEditingVisibleItem = i;
break; break;
} }
} }
this->currentVisibleScrollStart -= delta; this->currentVisibleScrollStart -= delta;
CONSTRAIN(this->currentVisibleScrollStart, 0, this->visibleItemCount);
CONSTRAIN(this->currentEditingVisibleItem, 0, this->visibleItemCount); CONSTRAIN(this->currentEditingVisibleItem, 0, this->visibleItemCount);
} }
}
void Menu::up()
{
if (this->currentEditingVisibleItem >= 0) {
AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem];
if (item)
item->decrease();
} else if (this->selectedVisibleItem > 0) { } else if (this->selectedVisibleItem > 0) {
uint8_t newItem = this->selectedVisibleItem - 1; uint8_t newItem = this->selectedVisibleItem - 1;
@ -95,35 +95,15 @@ void Menu::up()
this->currentVisibleScrollStart = 0; this->currentVisibleScrollStart = 0;
} }
} }
this->update_visible_items();
} }
void Menu::down() void Menu::down()
{ {
this->update_visible_items();
if (this->currentEditingVisibleItem >= 0) { if (this->currentEditingVisibleItem >= 0) {
this->update_visible_items();
AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem]; AbstractMenuItem* item = this->visibleItems[this->selectedVisibleItem];
if (item)
item->increase(); item->increase();
this->update_visible_items();
if (this->visibleItems[this->selectedVisibleItem] != item) {
int8_t delta = 0;
// the index of our visible item has changed.
// go through the list of visible items and find it!
for (size_t i = 0; i < this->visibleItemCount; i++) {
if (this->visibleItems[i] == item) {
delta = this->selectedVisibleItem - i;
this->selectedVisibleItem = this->currentEditingVisibleItem = i;
break;
}
}
this->currentVisibleScrollStart -= delta;
CONSTRAIN(this->currentEditingVisibleItem, 0, this->visibleItemCount);
}
} else { } else {
uint8_t maxVisibleItems = height / kMenuItemHeight; uint8_t maxVisibleItems = height / kMenuItemHeight;
if (this->selectedVisibleItem < this->visibleItemCount - 1) { if (this->selectedVisibleItem < this->visibleItemCount - 1) {