Implement menu rendering (very basic without selection)

This commit is contained in:
Jan-Henrik 2020-02-04 23:19:53 +01:00
parent e54318126a
commit 85d1144261
4 changed files with 36 additions and 7 deletions

View file

@ -1,18 +1,18 @@
# Copyright 2013 Emilie Gillet. # Copyright 2013 Emilie Gillet.
# #
# Author: Emilie Gillet (emilie.o.gillet@gmail.com) # Author: Emilie Gillet (emilie.o.gillet@gmail.com)
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights # in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is # copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions: # furnished to do so, subject to the following conditions:
# #
# The above copyright notice and this permission notice shall be included in # The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software. # all copies or substantial portions of the Software.
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # 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, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
# #
# See http://creativecommons.org/licenses/MIT/ for more information. # See http://creativecommons.org/licenses/MIT/ for more information.
# System specifications # System specifications
@ -40,8 +40,9 @@ 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
TOOLCHAIN_PATH ?= /usr/local/arm-4.8.3/ TOOLCHAIN_PATH ?= /usr/local/arm-4.8.3/

View file

@ -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());
}
} }

View file

@ -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;

View file

@ -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();