eurorack/midi2cv/menu/menu.cc

90 lines
2.5 KiB
C++
Raw Normal View History

2020-01-03 21:09:11 +00:00
#include "menu.h"
#include <algorithm>
#include <u8g2.h>
2020-01-03 21:09:11 +00:00
2020-02-22 23:15:01 +00:00
const int kMenuItemHeight = 12;
2020-01-03 21:09:11 +00:00
2020-02-23 10:15:27 +00:00
void Menu::render(U8G2* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t height)
{
this->width = width;
this->height = height;
2020-02-22 23:15:01 +00:00
uint8_t maxVisibleItems = height / kMenuItemHeight;
2020-01-03 21:09:11 +00:00
uint8_t itemsToRender = std::min(maxVisibleItems, uint8_t(this->itemCount - currentScrollStart));
2020-02-23 10:15:27 +00:00
u8g2_->setFont(u8g2_font_6x10_tf);
for (uint8_t i = 0; i < itemsToRender; i++) {
bool selected = this->selectedItem == (i + this->currentScrollStart);
2020-02-21 00:39:20 +00:00
bool editing = this->currentEditingItem == (i + this->currentScrollStart);
2020-02-22 23:15:01 +00:00
uint8_t yPosition = yStart + i * kMenuItemHeight;
AbstractMenuItem* item = this->items[i + this->currentScrollStart];
2020-02-23 10:15:27 +00:00
u8g2_->setDrawColor(selected ? 1 : 0);
if (editing) {
2020-02-23 10:15:27 +00:00
u8g2_->drawFrame(xStart, yPosition, width, kMenuItemHeight);
2020-02-21 00:39:20 +00:00
} else if (selected) {
2020-02-23 10:15:27 +00:00
u8g2_->drawBox(xStart, yPosition, width, kMenuItemHeight);
}
2020-02-23 10:15:27 +00:00
u8g2_->setDrawColor(editing || !selected ? 1 : 0);
u8g2_->drawStr(xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label());
2020-02-23 10:15:27 +00:00
uint8_t valueStringWidth = u8g2_->getStrWidth(item->get_string_representation());
u8g2_->drawStr(xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation());
}
2020-01-03 21:09:11 +00:00
}
void Menu::up()
{
2020-02-21 00:39:20 +00:00
if (this->currentEditingItem >= 0) {
this->items[this->selectedItem]->decrease();
} else if (this->selectedItem > 0) {
if (this->selectedItem - this->currentScrollStart == 1) { // keep scroll start one up
this->currentScrollStart--;
}
this->selectedItem--;
if (this->selectedItem == 0) {
this->currentScrollStart = 0;
}
}
}
void Menu::down()
{
2020-02-21 00:39:20 +00:00
if (this->currentEditingItem >= 0) {
this->items[this->selectedItem]->increase();
} else {
2020-02-22 23:15:01 +00:00
uint8_t maxVisibleItems = height / kMenuItemHeight;
2020-02-21 00:39:20 +00:00
if (this->selectedItem < this->itemCount - 1) {
if (this->selectedItem - this->currentScrollStart == maxVisibleItems - 2 && this->itemCount - this->currentScrollStart > maxVisibleItems) {
this->currentScrollStart++;
}
this->selectedItem++;
}
2020-02-21 00:39:20 +00:00
}
}
bool Menu::enter()
2020-02-21 00:39:20 +00:00
{
if (this->currentEditingItem >= 0) {
this->currentEditingItem = -1;
return true;
}
2020-02-21 00:39:20 +00:00
this->currentEditingItem = this->selectedItem;
return false;
}
bool Menu::back()
{
if (this->currentEditingItem >= 0) {
this->currentEditingItem = -1;
return false;
}
return true;
}