added menu separators

This commit is contained in:
kkoenig1@optonline.com
2021-10-22 20:36:05 +11:00
committed by Peter Feerick
parent 3ebd4321eb
commit bd3781c95e
2 changed files with 362 additions and 215 deletions
+100 -23
View File
@@ -21,15 +21,59 @@
#include "font.h" #include "font.h"
#include "theme.h" #include "theme.h"
void MenuBody::select(int index)
// ensure index is in range and also handle wrapping index
int MenuBody::rangeCheck(int index)
{
if (index < 0)
index = lines.size() - 1;
else if (index > lines.size() - 1)
index = 0;
return index;
}
void MenuBody::setIndex(int index)
{ {
selectedIndex = index; selectedIndex = index;
// calculate the correct scroll postion based on there being separators
int scrollY = 0;
for (int i = 0; i < selectedIndex; i++)
scrollY += lines[i].lineHeight();
if (innerHeight > height()) { if (innerHeight > height()) {
setScrollPositionY(MENUS_LINE_HEIGHT * index - 3 * MENUS_LINE_HEIGHT); setScrollPositionY(scrollY - 3 * MENUS_LINE_HEIGHT);
} }
invalidate(); invalidate();
} }
void MenuBody::select(int index)
{
// adjust the selection based on separators
for (int i = 0; i <= index; i++) {
if (lines[i].isSeparator)
index++;
index = rangeCheck(index);
}
setIndex(index);
}
void MenuBody::selectNext(MENU_DIRECTION direction)
{
// look for the next non separator line
int index = selectedIndex + direction;
index = rangeCheck(index);
while (lines[index].isSeparator) {
index += direction;
index = rangeCheck(index);
}
setIndex(index);
}
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
void MenuBody::onEvent(event_t event) void MenuBody::onEvent(event_t event)
{ {
@@ -37,13 +81,13 @@ void MenuBody::onEvent(event_t event)
if (event == EVT_ROTARY_RIGHT) { if (event == EVT_ROTARY_RIGHT) {
if (!lines.empty()) { if (!lines.empty()) {
select(int((selectedIndex + 1) % lines.size())); selectNext(DIRECTION_UP);
onKeyPress(); onKeyPress();
} }
} }
else if (event == EVT_ROTARY_LEFT) { else if (event == EVT_ROTARY_LEFT) {
if (!lines.empty()) { if (!lines.empty()) {
select(int(selectedIndex <= 0 ? lines.size() - 1 : selectedIndex - 1)); selectNext(DIRECTION_DOWN);
onKeyPress(); onKeyPress();
} }
} }
@@ -83,22 +127,39 @@ void MenuBody::onEvent(event_t event)
bool MenuBody::onTouchEnd(coord_t /*x*/, coord_t y) bool MenuBody::onTouchEnd(coord_t /*x*/, coord_t y)
{ {
Menu * menu = getParentMenu(); Menu * menu = getParentMenu();
int index = y / MENUS_LINE_HEIGHT;
int index = 0;
// dynamically find the selected element based on variable line heights
coord_t yAccumulator = 0;
for (int i = 0; i < lines.size(); i++) {
if (y >= yAccumulator && y <= yAccumulator + lines[i].lineHeight()) {
index = i;
break;
}
yAccumulator += lines[i].lineHeight();
}
// dont allow selecting separators
if (lines[index].isSeparator)
return false;
if (index < (int)lines.size()) { if (index < (int)lines.size()) {
onKeyPress(); onKeyPress();
if (menu->multiple) { if (menu->multiple) {
if (selectedIndex == index) if (selectedIndex == index)
lines[index].onPress(); lines[index].onPress();
else else
select(index); setIndex(index);
menu->invalidate(); menu->invalidate();
} }
else { else {
select(index); setIndex(index);
menu->deleteLater(); menu->deleteLater();
lines[index].onPress(); lines[index].onPress();
} }
} }
return true; return true;
} }
#endif #endif
@@ -107,37 +168,43 @@ void MenuBody::paint(BitmapBuffer * dc)
{ {
dc->clear(COLOR_THEME_PRIMARY2); dc->clear(COLOR_THEME_PRIMARY2);
for (unsigned i = 0; i < lines.size(); i++) { int y = 0;
for (int i = 0; i < lines.size(); i++) {
auto& line = lines[i]; auto& line = lines[i];
LcdFlags flags = COLOR_THEME_SECONDARY1 | MENU_FONT; LcdFlags flags = COLOR_THEME_SECONDARY1 | MENU_FONT;
if (selectedIndex == (int)i) {
// draw selection if appropriate
if (selectedIndex == i && ! line.isSeparator) {
flags = COLOR_THEME_PRIMARY2 | MENU_FONT; flags = COLOR_THEME_PRIMARY2 | MENU_FONT;
if (COLOR_THEME_FOCUS != COLOR_THEME_PRIMARY2) { if (COLOR_THEME_FOCUS != COLOR_THEME_PRIMARY2) {
dc->drawSolidFilledRect(0, i * MENUS_LINE_HEIGHT, width(), dc->drawSolidFilledRect(0, y, width(),
MENUS_LINE_HEIGHT, COLOR_THEME_FOCUS); MENUS_LINE_HEIGHT, COLOR_THEME_FOCUS);
} }
} }
if (line.drawLine) {
line.drawLine(dc, 0, i * MENUS_LINE_HEIGHT, flags); if (line.isSeparator) {
dc->drawHorizontalLine(5, y + MENUS_SEPARATOR_HEIGHT / 2, width() - 10, 255, COLOR_THEME_SECONDARY2);
} else if (line.drawLine) {
line.drawLine(dc, 0, y, flags);
} else { } else {
const char* text = line.text.data(); const char* text = line.text.data();
dc->drawText(10, dc->drawText(10,
i * MENUS_LINE_HEIGHT + y + (MENUS_LINE_HEIGHT - getFontHeight(MENU_FONT)) / 2,
(MENUS_LINE_HEIGHT - getFontHeight(MENU_FONT)) / 2,
text[0] == '\0' ? "---" : text, flags); text[0] == '\0' ? "---" : text, flags);
} }
Menu* menu = getParentMenu(); Menu* menu = getParentMenu();
if (menu->multiple && line.isChecked) { if (menu->multiple && line.isChecked) {
theme->drawCheckBox(dc, line.isChecked(), width() - 35, theme->drawCheckBox(dc, line.isChecked(), width() - 35,
i * MENUS_LINE_HEIGHT + (MENUS_LINE_HEIGHT - 20) / 2, y + (MENUS_LINE_HEIGHT - 20) / 2,
0); 0);
} }
if (i > 0) { // if (i > 0 && ! lines[i].isSeparator && ! lines[i-1].isSeparator) {
dc->drawSolidHorizontalLine(0, i * MENUS_LINE_HEIGHT, MENUS_WIDTH, // dc->drawSolidHorizontalLine(0, y, MENUS_WIDTH, COLOR_THEME_SECONDARY2);
COLOR_THEME_SECONDARY2); // }
}
y += line.lineHeight();
} }
} }
@@ -173,18 +240,22 @@ Menu::Menu(Window * parent, bool multiple):
void Menu::updatePosition() void Menu::updatePosition()
{ {
// calcualte the correct menu height given that the line heights are variable
coord_t height = 0;
for (int i = 0; i < content->body.lines.size(); i++)
height += content->body.lines[i].lineHeight();
if (!toolbar) { if (!toolbar) {
// there is no navigation bar at the left, we may center the window on screen // there is no navigation bar at the left, we may center the window on screen
auto headerHeight = content->title.empty() ? 0 : POPUP_HEADER_HEIGHT; auto headerHeight = content->title.empty() ? 0 : POPUP_HEADER_HEIGHT;
auto bodyHeight = limit<coord_t>( auto bodyHeight = limit<coord_t>(MENUS_MIN_HEIGHT, height, MENUS_MAX_HEIGHT);
MENUS_MIN_HEIGHT, content->body.lines.size() * MENUS_LINE_HEIGHT,
MENUS_MAX_HEIGHT);
content->setTop((LCD_H - headerHeight - bodyHeight) / 2 + MENUS_OFFSET_TOP); content->setTop((LCD_H - headerHeight - bodyHeight) / 2 + MENUS_OFFSET_TOP);
content->setHeight(headerHeight + bodyHeight); content->setHeight(headerHeight + bodyHeight);
content->body.setTop(headerHeight); content->body.setTop(headerHeight);
content->body.setHeight(bodyHeight); content->body.setHeight(bodyHeight);
} }
content->body.setInnerHeight(content->body.lines.size() * MENUS_LINE_HEIGHT);
content->body.setInnerHeight(height);
} }
void Menu::setTitle(std::string text) void Menu::setTitle(std::string text)
@@ -199,6 +270,12 @@ void Menu::addLine(const std::string & text, std::function<void()> onPress, std:
updatePosition(); updatePosition();
} }
void Menu::addSeparator()
{
content->body.addSeparator();
updatePosition();
}
void Menu::addCustomLine(std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked) void Menu::addCustomLine(std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked)
{ {
content->body.addCustomLine(std::move(drawLine), std::move(onPress), std::move(isChecked)); content->body.addCustomLine(std::move(drawLine), std::move(onPress), std::move(isChecked));
+262 -192
View File
@@ -17,230 +17,300 @@
* Lesser General Public License for more details. * Lesser General Public License for more details.
*/ */
#pragma once #include "menu.h"
#include "font.h"
#include "theme.h"
#include <vector>
#include <functional>
#include <utility>
#include "modal_window.h"
class Menu; // ensure index is in range and also handle wrapping index
class MenuWindowContent; int MenuBody::rangeCheck(int index)
class MenuBody: public Window
{ {
friend class MenuWindowContent; if (index < 0)
friend class Menu; index = lines.size() - 1;
else if (index > lines.size() - 1)
index = 0;
class MenuLine { return index;
friend class MenuBody; }
public: void MenuBody::setIndex(int index)
MenuLine(std::string text, std::function<void()> onPress, std::function<bool()> isChecked): {
text(std::move(text)), selectedIndex = index;
onPress(std::move(onPress)), // calculate the correct scroll postion based on there being separators
isChecked(std::move(isChecked)) int scrollY = 0;
{ for (int i = 0; i < selectedIndex; i++)
} scrollY += lines[i].lineHeight();
MenuLine(std::function<void(BitmapBuffer * /*dc*/, coord_t /*x*/, coord_t /*y*/, LcdFlags /*flags*/)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked): if (innerHeight > height()) {
drawLine(std::move(drawLine)), setScrollPositionY(scrollY - 3 * MENUS_LINE_HEIGHT);
onPress(std::move(onPress)), }
isChecked(std::move(isChecked))
{
}
MenuLine(MenuLine &) = delete; invalidate();
}
MenuLine(MenuLine &&) = default; void MenuBody::select(int index)
{
// adjust the selection based on separators
for (int i = 0; i <= index; i++) {
if (lines[i].isSeparator)
index++;
index = rangeCheck(index);
}
protected: setIndex(index);
std::string text; }
std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine;
std::function<void()> onPress;
std::function<bool()> isChecked;
};
public: void MenuBody::selectNext(MENU_DIRECTION direction)
MenuBody(Window * parent, const rect_t & rect): {
Window(parent, rect, OPAQUE) // look for the next non separator line
{ int index = selectedIndex + direction;
setPageHeight(MENUS_LINE_HEIGHT); index = rangeCheck(index);
} while (lines[index].isSeparator) {
index += direction;
index = rangeCheck(index);
}
#if defined(DEBUG_WINDOWS) setIndex(index);
std::string getName() const override }
{
return "MenuBody";
}
#endif
void select(int index);
int selection() const
{
return selectedIndex;
}
int count() const
{
return lines.size();
}
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
void onEvent(event_t event) override; void MenuBody::onEvent(event_t event)
{
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event);
if (event == EVT_ROTARY_RIGHT) {
if (!lines.empty()) {
selectNext(DIRECTION_UP);
onKeyPress();
}
}
else if (event == EVT_ROTARY_LEFT) {
if (!lines.empty()) {
selectNext(DIRECTION_DOWN);
onKeyPress();
}
}
else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
if (!lines.empty()) {
onKeyPress();
if (selectedIndex < 0) {
select(0);
}
else {
Menu * menu = getParentMenu();
if (menu->multiple) {
lines[selectedIndex].onPress();
menu->invalidate();
}
else {
menu->deleteLater();
lines[selectedIndex].onPress();
}
}
}
}
else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
onKeyPress();
if (onCancel) {
onCancel();
}
Window::onEvent(event);
}
else {
Window::onEvent(event);
}
}
#endif #endif
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
bool onTouchEnd(coord_t x, coord_t y) override; bool MenuBody::onTouchEnd(coord_t /*x*/, coord_t y)
#endif
void addLine(const std::string & text, std::function<void()> onPress, std::function<bool()> isChecked)
{
lines.emplace_back(text, std::move(onPress), std::move(isChecked));
invalidate();
}
void addCustomLine(std::function<void(BitmapBuffer * /*dc*/, coord_t /*x*/, coord_t /*y*/, LcdFlags /*flags*/)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked)
{
lines.emplace_back(std::move(drawLine), std::move(onPress), std::move(isChecked));
invalidate();
}
void removeLines()
{
lines.clear();
invalidate();
}
void setCancelHandler(std::function<void()> handler)
{
onCancel = std::move(handler);
}
void paint(BitmapBuffer * dc) override;
protected:
std::vector<MenuLine> lines;
int selectedIndex = 0;
std::function<void()> onCancel;
inline Menu * getParentMenu();
};
class MenuWindowContent: public ModalWindowContent
{ {
friend class Menu; Menu * menu = getParentMenu();
public: int index = 0;
explicit MenuWindowContent(Menu * parent);
void deleteLater(bool detach = true, bool trash = true) override // dynamically find the selected element based on variable line heights
{ coord_t yAccumulator = 0;
if (_deleted) for (int i = 0; i < lines.size(); i++) {
return; if (y >= yAccumulator && y <= yAccumulator + lines[i].lineHeight()) {
index = i;
body.deleteLater(true, false); break;
ModalWindowContent::deleteLater(detach, trash);
} }
yAccumulator += lines[i].lineHeight();
}
#if defined(DEBUG_WINDOWS) // dont allow selecting separators
std::string getName() const override if (lines[index].isSeparator)
{ return false;
return "MenuWindowContent";
if (index < (int)lines.size()) {
onKeyPress();
if (menu->multiple) {
if (selectedIndex == index)
lines[index].onPress();
else
setIndex(index);
menu->invalidate();
} }
else {
setIndex(index);
menu->deleteLater();
lines[index].onPress();
}
}
return true;
}
#endif #endif
void paint(BitmapBuffer * dc) override; void MenuBody::paint(BitmapBuffer * dc)
protected:
MenuBody body;
};
class Menu: public ModalWindow
{ {
friend class MenuBody; dc->clear(COLOR_THEME_PRIMARY2);
public: int y = 0;
explicit Menu(Window * parent, bool multiple = false); for (int i = 0; i < lines.size(); i++) {
auto& line = lines[i];
LcdFlags flags = COLOR_THEME_SECONDARY1 | MENU_FONT;
#if defined(DEBUG_WINDOWS) // draw selection if appropriate
std::string getName() const override if (selectedIndex == i && ! line.isSeparator) {
{ flags = COLOR_THEME_PRIMARY2 | MENU_FONT;
return "Menu"; if (COLOR_THEME_FOCUS != COLOR_THEME_PRIMARY2) {
} dc->drawSolidFilledRect(0, y, width(),
#endif MENUS_LINE_HEIGHT, COLOR_THEME_FOCUS);
void setCancelHandler(std::function<void()> handler)
{
content->body.setCancelHandler(std::move(handler));
}
void setWaitHandler(std::function<void()> handler)
{
waitHandler = std::move(handler);
}
void setFocusBody(uint8_t flag = SET_FOCUS_DEFAULT)
{
content->body.setFocus(flag);
}
void setToolbar(Window * window)
{
toolbar = window;
content->setLeft(toolbar->right());
content->setTop(toolbar->top());
content->setHeight(toolbar->height());
}
void setTitle(std::string text);
void addLine(const std::string & text, std::function<void()> onPress, std::function<bool()> isChecked = nullptr);
void addCustomLine(std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked = nullptr);
void removeLines();
unsigned count() const
{
return content->body.count();
}
int selection() const
{
return content->body.selection();
}
void select(int index)
{
content->body.select(index);
}
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override;
#endif
void checkEvents() override
{
ModalWindow::checkEvents();
if (waitHandler) {
waitHandler();
} }
} }
void paint(BitmapBuffer * dc) override; if (line.isSeparator) {
dc->drawHorizontalLine(5, y + MENUS_SEPARATOR_HEIGHT / 2, width() - 10, 255, COLOR_THEME_SECONDARY2);
} else if (line.drawLine) {
line.drawLine(dc, 0, y, flags);
} else {
const char* text = line.text.data();
dc->drawText(10,
y + (MENUS_LINE_HEIGHT - getFontHeight(MENU_FONT)) / 2,
text[0] == '\0' ? "---" : text, flags);
}
protected: Menu* menu = getParentMenu();
MenuWindowContent * content; if (menu->multiple && line.isChecked) {
bool multiple; theme->drawCheckBox(dc, line.isChecked(), width() - 35,
Window * toolbar = nullptr; y + (MENUS_LINE_HEIGHT - 20) / 2,
std::function<void()> waitHandler; 0);
void updatePosition(); }
};
Menu * MenuBody::getParentMenu() // if (i > 0 && ! lines[i].isSeparator && ! lines[i-1].isSeparator) {
{ // dc->drawSolidHorizontalLine(0, y, MENUS_WIDTH, COLOR_THEME_SECONDARY2);
return static_cast<Menu *>(getParent()->getParent()); // }
y += line.lineHeight();
}
} }
MenuWindowContent::MenuWindowContent(Menu* parent) :
ModalWindowContent(parent, {(LCD_W - MENUS_WIDTH) / 2,
(LCD_H - MENUS_WIDTH) / 2, MENUS_WIDTH, 0}),
body(this, {0, 0, width(), height()})
{
body.setFocus(SET_FOCUS_DEFAULT);
}
void MenuWindowContent::paint(BitmapBuffer * dc)
{
// the background
dc->clear(COLOR_THEME_PRIMARY2);
// the title
if (!title.empty()) {
dc->drawText(MENUS_WIDTH / 2,
(POPUP_HEADER_HEIGHT - getFontHeight(MENU_HEADER_FONT)) / 2,
title.c_str(), CENTERED | MENU_HEADER_FONT);
dc->drawSolidHorizontalLine(0, POPUP_HEADER_HEIGHT - 1, MENUS_WIDTH,
COLOR_THEME_SECONDARY2);
}
}
Menu::Menu(Window * parent, bool multiple):
ModalWindow(parent, true),
content(createMenuWindow(this)),
multiple(multiple)
{
}
void Menu::updatePosition()
{
// calcualte the correct menu height given that the line heights are variable
coord_t height = 0;
for (int i = 0; i < content->body.lines.size(); i++)
height += content->body.lines[i].lineHeight();
if (!toolbar) {
// there is no navigation bar at the left, we may center the window on screen
auto headerHeight = content->title.empty() ? 0 : POPUP_HEADER_HEIGHT;
auto bodyHeight = limit<coord_t>(MENUS_MIN_HEIGHT, height, MENUS_MAX_HEIGHT);
content->setTop((LCD_H - headerHeight - bodyHeight) / 2 + MENUS_OFFSET_TOP);
content->setHeight(headerHeight + bodyHeight);
content->body.setTop(headerHeight);
content->body.setHeight(bodyHeight);
}
content->body.setInnerHeight(height);
}
void Menu::setTitle(std::string text)
{
content->setTitle(std::move(text));
updatePosition();
}
void Menu::addLine(const std::string & text, std::function<void()> onPress, std::function<bool()> isChecked)
{
content->body.addLine(text, std::move(onPress), std::move(isChecked));
updatePosition();
}
void Menu::addSeparator()
{
content->body.addSeparator();
updatePosition();
}
void Menu::addCustomLine(std::function<void(BitmapBuffer * dc, coord_t x, coord_t y, LcdFlags flags)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked)
{
content->body.addCustomLine(std::move(drawLine), std::move(onPress), std::move(isChecked));
updatePosition();
}
void Menu::removeLines()
{
content->body.removeLines();
updatePosition();
}
#if defined(HARDWARE_KEYS)
void Menu::onEvent(event_t event)
{
if (toolbar && (event == EVT_KEY_BREAK(KEY_PGDN) || event == EVT_KEY_LONG(KEY_PGDN))) {
toolbar->onEvent(event);
}
else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
deleteLater();
}
else if (event == EVT_KEY_BREAK(KEY_ENTER) && !multiple) {
deleteLater();
}
}
#endif
void Menu::paint(BitmapBuffer * dc)
{
ModalWindow::paint(dc);
rect_t r(content->getRect());
if (toolbar) {
r.x = toolbar->left();
r.w += toolbar->width();
}
dc->drawSolidRect(r.x - 1, r.y - 1, r.w + 2, r.h + 2, 1, COLOR_THEME_SECONDARY2);
}