From 85d1144261b79d7e4ca1705daa3ff214d47ca380 Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Tue, 4 Feb 2020 23:19:53 +0100 Subject: [PATCH] Implement menu rendering (very basic without selection) --- midi2cv/makefile | 13 +++++++------ midi2cv/menu/menu.cc | 26 +++++++++++++++++++++++++- midi2cv/menu/menu_items.h | 2 ++ midi2cv/midi2cv.cc | 2 ++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/midi2cv/makefile b/midi2cv/makefile index 40ab8da..6c5e72d 100644 --- a/midi2cv/makefile +++ b/midi2cv/makefile @@ -1,18 +1,18 @@ # Copyright 2013 Emilie Gillet. -# +# # Author: Emilie Gillet (emilie.o.gillet@gmail.com) -# +# # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -20,7 +20,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -# +# # See http://creativecommons.org/licenses/MIT/ for more information. # System specifications @@ -40,8 +40,9 @@ UPLOAD_COMMAND = upload_combo_jtag TARGET = midi2cv PACKAGES = midi2cv \ midi2cv/drivers \ + midi2cv/menu \ stmlib/utils \ - stmlib/system + stmlib/system RESOURCES = midi2cv/resources TOOLCHAIN_PATH ?= /usr/local/arm-4.8.3/ diff --git a/midi2cv/menu/menu.cc b/midi2cv/menu/menu.cc index 97e793a..20d7aa3 100644 --- a/midi2cv/menu/menu.cc +++ b/midi2cv/menu/menu.cc @@ -1,7 +1,31 @@ #include "menu.h" +#include +#include #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()); + } } diff --git a/midi2cv/menu/menu_items.h b/midi2cv/menu/menu_items.h index ec63522..76ace14 100644 --- a/midi2cv/menu/menu_items.h +++ b/midi2cv/menu/menu_items.h @@ -14,6 +14,7 @@ template class MenuItem : public AbstractMenuItem { private: const char* label; + bool visible; T value; T step; char stringRepresentation[24]; @@ -21,6 +22,7 @@ class MenuItem : public AbstractMenuItem { protected: MenuItem(const char* _label, T _initialValue) : label(_label) + , visible(true) , value(_initialValue) {}; virtual void to_string(char* buf) = 0; diff --git a/midi2cv/midi2cv.cc b/midi2cv/midi2cv.cc index 77a9aef..4f0c1f8 100644 --- a/midi2cv/midi2cv.cc +++ b/midi2cv/midi2cv.cc @@ -129,6 +129,8 @@ int main(void) menu.add_item(&item); menu.add_item(&item2); menu.add_item(&item3); + menu.render(0, 0, 0, 0, 0); + item.increase(); item2.increase(); item3.increase();