mirror of
https://github.com/jhbruhn/eurorack.git
synced 2025-03-15 02:55:49 +00:00
Implement menu rendering (very basic without selection)
This commit is contained in:
parent
e54318126a
commit
85d1144261
4 changed files with 36 additions and 7 deletions
|
@ -40,6 +40,7 @@ UPLOAD_COMMAND = upload_combo_jtag
|
||||||
TARGET = midi2cv
|
TARGET = midi2cv
|
||||||
PACKAGES = midi2cv \
|
PACKAGES = midi2cv \
|
||||||
midi2cv/drivers \
|
midi2cv/drivers \
|
||||||
|
midi2cv/menu \
|
||||||
stmlib/utils \
|
stmlib/utils \
|
||||||
stmlib/system
|
stmlib/system
|
||||||
RESOURCES = midi2cv/resources
|
RESOURCES = midi2cv/resources
|
||||||
|
|
|
@ -1,7 +1,31 @@
|
||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <u8g2.h>
|
||||||
|
|
||||||
#define MENU_ITEM_HEIGHT 8
|
#define MENU_ITEM_HEIGHT 8
|
||||||
|
|
||||||
void Menu::render(u8g2_t* u8g2_, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
|
void Menu::render(u8g2_t* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t height)
|
||||||
|
{
|
||||||
|
uint8_t maxVisibleItems = height / MENU_ITEM_HEIGHT;
|
||||||
|
|
||||||
|
uint8_t itemsToRender = std::min(maxVisibleItems, uint8_t(this->itemCount - currentScrollStart));
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < itemsToRender; i++) {
|
||||||
|
bool selected = this->selectedItem == (i + this->currentScrollStart);
|
||||||
|
uint8_t yPosition = yStart + i * MENU_ITEM_HEIGHT;
|
||||||
|
|
||||||
|
AbstractMenuItem* item = this->items[i + this->currentScrollStart];
|
||||||
|
|
||||||
|
u8g2_SetDrawColor(u8g2_, selected ? 1 : 0);
|
||||||
|
|
||||||
|
if (selected) {
|
||||||
|
u8g2_DrawBox(u8g2_, xStart, yPosition, width, MENU_ITEM_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8g2_SetDrawColor(u8g2_, !selected ? 1 : 0);
|
||||||
|
u8g2_DrawStr(u8g2_, xStart + 0, yPosition + MENU_ITEM_HEIGHT, item->get_label());
|
||||||
|
|
||||||
|
uint8_t valueStringWidth = u8g2_GetStrWidth(u8g2_, item->get_string_representation());
|
||||||
|
u8g2_DrawStr(u8g2_, xStart + width - valueStringWidth, yPosition + MENU_ITEM_HEIGHT, item->get_string_representation());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ template <class T>
|
||||||
class MenuItem : public AbstractMenuItem {
|
class MenuItem : public AbstractMenuItem {
|
||||||
private:
|
private:
|
||||||
const char* label;
|
const char* label;
|
||||||
|
bool visible;
|
||||||
T value;
|
T value;
|
||||||
T step;
|
T step;
|
||||||
char stringRepresentation[24];
|
char stringRepresentation[24];
|
||||||
|
@ -21,6 +22,7 @@ class MenuItem : public AbstractMenuItem {
|
||||||
protected:
|
protected:
|
||||||
MenuItem(const char* _label, T _initialValue)
|
MenuItem(const char* _label, T _initialValue)
|
||||||
: label(_label)
|
: label(_label)
|
||||||
|
, visible(true)
|
||||||
, value(_initialValue) {};
|
, value(_initialValue) {};
|
||||||
|
|
||||||
virtual void to_string(char* buf) = 0;
|
virtual void to_string(char* buf) = 0;
|
||||||
|
|
|
@ -129,6 +129,8 @@ int main(void)
|
||||||
menu.add_item(&item);
|
menu.add_item(&item);
|
||||||
menu.add_item(&item2);
|
menu.add_item(&item2);
|
||||||
menu.add_item(&item3);
|
menu.add_item(&item3);
|
||||||
|
menu.render(0, 0, 0, 0, 0);
|
||||||
|
|
||||||
item.increase();
|
item.increase();
|
||||||
item2.increase();
|
item2.increase();
|
||||||
item3.increase();
|
item3.increase();
|
||||||
|
|
Loading…
Reference in a new issue