menu and modal almost working
This commit is contained in:
committed by
Raphael Coeffic
parent
cab656b5d2
commit
66c500799c
+103
-50
@@ -21,6 +21,62 @@
|
|||||||
#include "font.h"
|
#include "font.h"
|
||||||
#include "theme.h"
|
#include "theme.h"
|
||||||
|
|
||||||
|
LvglWidgetFactory menuBodyWidgetFactory = { [] (lv_obj_t *parent) {
|
||||||
|
return lv_list_create(parent);
|
||||||
|
}, nullptr};
|
||||||
|
|
||||||
|
void menuBodyEventCallback(lv_event_t *e)
|
||||||
|
{
|
||||||
|
auto code = lv_event_get_code(e);
|
||||||
|
if (code == LV_EVENT_CLICKED) {
|
||||||
|
lv_obj_t *target = lv_event_get_target(e);
|
||||||
|
if (target != nullptr) {
|
||||||
|
long index = (long)lv_event_get_user_data(e);
|
||||||
|
MenuBody *mb = (MenuBody *) lv_obj_get_user_data(lv_obj_get_parent(target));
|
||||||
|
Menu *menu = mb->getParentMenu();
|
||||||
|
if (index < (int)mb->lines.size()) {
|
||||||
|
onKeyPress();
|
||||||
|
if (menu->multiple) {
|
||||||
|
if (mb->selectedIndex == index)
|
||||||
|
mb->lines[index].onPress();
|
||||||
|
else
|
||||||
|
mb->setIndex(index);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mb->setIndex(index);
|
||||||
|
menu->deleteLater();
|
||||||
|
mb->lines[index].onPress();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuBody::MenuBody(Window * parent, const rect_t & rect):
|
||||||
|
Window(parent, rect, OPAQUE, 0, &menuBodyWidgetFactory)
|
||||||
|
{
|
||||||
|
setPageHeight(MENUS_LINE_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MenuBody::addLine(const std::string & text, std::function<void()> onPress, std::function<bool()> isChecked)
|
||||||
|
{
|
||||||
|
lines.emplace_back(text, std::move(onPress), std::move(isChecked));
|
||||||
|
lv_obj_t *btn = lv_list_add_btn(lvobj, nullptr, text.c_str());
|
||||||
|
lv_obj_set_style_bg_color(btn, makeLvColor(COLOR_THEME_SECONDARY2), LV_PART_MAIN | LV_STATE_CHECKED);
|
||||||
|
lv_obj_set_style_bg_color(btn, makeLvColor(COLOR_THEME_SECONDARY2), LV_PART_MAIN | LV_STATE_CHECKED | LV_STATE_FOCUSED);
|
||||||
|
lv_obj_set_style_bg_opa(btn, LV_OPA_100, LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_bg_opa(btn, LV_OPA_100, LV_PART_MAIN | LV_STATE_CHECKED);
|
||||||
|
lv_obj_set_style_bg_opa(btn, LV_OPA_100, LV_PART_MAIN | LV_STATE_CHECKED | LV_STATE_FOCUSED);
|
||||||
|
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
|
||||||
|
lv_obj_add_event_cb(btn, menuBodyEventCallback, LV_EVENT_CLICKED, (void *) (lines.size() - 1));
|
||||||
|
lv_obj_set_style_text_color(lvobj, makeLvColor(COLOR_THEME_PRIMARY2), LV_PART_MAIN | LV_STATE_FOCUSED);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MenuBody::removeLines()
|
||||||
|
{
|
||||||
|
lines.clear();
|
||||||
|
lv_obj_clean(lvobj);
|
||||||
|
}
|
||||||
|
|
||||||
// ensure index is in range and also handle wrapping index
|
// ensure index is in range and also handle wrapping index
|
||||||
int MenuBody::rangeCheck(int index)
|
int MenuBody::rangeCheck(int index)
|
||||||
@@ -35,17 +91,14 @@ int MenuBody::rangeCheck(int index)
|
|||||||
|
|
||||||
void MenuBody::setIndex(int index)
|
void MenuBody::setIndex(int index)
|
||||||
{
|
{
|
||||||
selectedIndex = index;
|
if (index < (int)lines.size()) {
|
||||||
// calculate the correct scroll postion based on there being separators
|
selectedIndex = index;
|
||||||
int scrollY = 0;
|
|
||||||
for (int i = 0; i < selectedIndex; i++)
|
|
||||||
scrollY += lines[i].lineHeight();
|
|
||||||
|
|
||||||
if (innerHeight > height()) {
|
lv_obj_t *btn = lv_obj_get_child(lvobj, selectedIndex);
|
||||||
lv_obj_scroll_to_y(lvobj, scrollY - 3 * MENUS_LINE_HEIGHT, LV_ANIM_OFF);
|
if (btn != nullptr) {
|
||||||
|
lv_obj_add_state(btn, LV_STATE_CHECKED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MenuBody::select(int index)
|
void MenuBody::select(int index)
|
||||||
@@ -166,42 +219,42 @@ bool MenuBody::onTouchEnd(coord_t /*x*/, coord_t y)
|
|||||||
|
|
||||||
void MenuBody::paint(BitmapBuffer * dc)
|
void MenuBody::paint(BitmapBuffer * dc)
|
||||||
{
|
{
|
||||||
dc->clear(COLOR_THEME_PRIMARY2);
|
// dc->clear(COLOR_THEME_PRIMARY2);
|
||||||
|
|
||||||
int y = 0;
|
// int y = 0;
|
||||||
for (int i = 0; i < (signed)lines.size(); i++) {
|
// for (int i = 0; i < (signed)lines.size(); i++) {
|
||||||
auto& line = lines[i];
|
// auto& line = lines[i];
|
||||||
LcdFlags flags = COLOR_THEME_PRIMARY3 | MENU_FONT;
|
// LcdFlags flags = COLOR_THEME_PRIMARY3 | MENU_FONT;
|
||||||
|
|
||||||
// draw selection if appropriate
|
// // draw selection if appropriate
|
||||||
if (selectedIndex == i && ! line.isSeparator) {
|
// 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, y, width(),
|
// dc->drawSolidFilledRect(0, y, width(),
|
||||||
MENUS_LINE_HEIGHT, COLOR_THEME_FOCUS);
|
// MENUS_LINE_HEIGHT, COLOR_THEME_FOCUS);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (line.isSeparator) {
|
// if (line.isSeparator) {
|
||||||
dc->drawHorizontalLine(5, y + MENUS_SEPARATOR_HEIGHT / 2, width() - 10, 255, COLOR_THEME_SECONDARY1);
|
// dc->drawHorizontalLine(5, y + MENUS_SEPARATOR_HEIGHT / 2, width() - 10, 255, COLOR_THEME_SECONDARY1);
|
||||||
} else if (line.drawLine) {
|
// } else if (line.drawLine) {
|
||||||
line.drawLine(dc, 0, y, COLOR_THEME_PRIMARY3);
|
// line.drawLine(dc, 0, y, COLOR_THEME_PRIMARY3);
|
||||||
} else {
|
// } else {
|
||||||
const char* text = line.text.data();
|
// const char* text = line.text.data();
|
||||||
dc->drawText(10,
|
// dc->drawText(10,
|
||||||
y + (MENUS_LINE_HEIGHT - getFontHeight(MENU_FONT)) / 2,
|
// y + (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,
|
||||||
y + (MENUS_LINE_HEIGHT - 20) / 2,
|
// y + (MENUS_LINE_HEIGHT - 20) / 2,
|
||||||
0);
|
// 0);
|
||||||
}
|
// }
|
||||||
|
|
||||||
y += line.lineHeight();
|
// y += line.lineHeight();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuWindowContent::MenuWindowContent(Menu* parent) :
|
MenuWindowContent::MenuWindowContent(Menu* parent) :
|
||||||
@@ -214,17 +267,17 @@ MenuWindowContent::MenuWindowContent(Menu* parent) :
|
|||||||
|
|
||||||
void MenuWindowContent::paint(BitmapBuffer * dc)
|
void MenuWindowContent::paint(BitmapBuffer * dc)
|
||||||
{
|
{
|
||||||
// the background
|
// // the background
|
||||||
dc->clear(COLOR_THEME_SECONDARY1);
|
// dc->clear(COLOR_THEME_SECONDARY1);
|
||||||
|
|
||||||
// the title
|
// // the title
|
||||||
if (!title.empty()) {
|
// if (!title.empty()) {
|
||||||
dc->drawText(MENUS_WIDTH / 2,
|
// dc->drawText(MENUS_WIDTH / 2,
|
||||||
(POPUP_HEADER_HEIGHT - getFontHeight(MENU_HEADER_FONT)) / 2,
|
// (POPUP_HEADER_HEIGHT - getFontHeight(MENU_HEADER_FONT)) / 2,
|
||||||
title.c_str(), CENTERED | MENU_HEADER_FONT | COLOR_THEME_PRIMARY2);
|
// title.c_str(), CENTERED | MENU_HEADER_FONT | COLOR_THEME_PRIMARY2);
|
||||||
dc->drawSolidHorizontalLine(0, POPUP_HEADER_HEIGHT - 1, MENUS_WIDTH,
|
// dc->drawSolidHorizontalLine(0, POPUP_HEADER_HEIGHT - 1, MENUS_WIDTH,
|
||||||
COLOR_THEME_SECONDARY2);
|
// COLOR_THEME_SECONDARY2);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
Menu::Menu(Window * parent, bool multiple):
|
Menu::Menu(Window * parent, bool multiple):
|
||||||
|
|||||||
+6
-15
@@ -69,6 +69,7 @@ class MenuBody: public Window
|
|||||||
MenuLine(MenuLine &&) = default;
|
MenuLine(MenuLine &&) = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend void menuBodyEventCallback(lv_event_t *e);
|
||||||
bool isSeparator = false;
|
bool isSeparator = false;
|
||||||
coord_t height = MENUS_LINE_HEIGHT;
|
coord_t height = MENUS_LINE_HEIGHT;
|
||||||
std::string text;
|
std::string text;
|
||||||
@@ -78,11 +79,7 @@ class MenuBody: public Window
|
|||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MenuBody(Window * parent, const rect_t & rect):
|
MenuBody(Window * parent, const rect_t & rect);
|
||||||
Window(parent, rect, OPAQUE)
|
|
||||||
{
|
|
||||||
setPageHeight(MENUS_LINE_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(DEBUG_WINDOWS)
|
#if defined(DEBUG_WINDOWS)
|
||||||
std::string getName() const override
|
std::string getName() const override
|
||||||
@@ -116,11 +113,7 @@ class MenuBody: public Window
|
|||||||
bool onTouchEnd(coord_t x, coord_t y) override;
|
bool onTouchEnd(coord_t x, coord_t y) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void addLine(const std::string & text, std::function<void()> onPress, std::function<bool()> isChecked)
|
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)
|
void addCustomLine(std::function<void(BitmapBuffer * /*dc*/, coord_t /*x*/, coord_t /*y*/, LcdFlags /*flags*/)> drawLine, std::function<void()> onPress, std::function<bool()> isChecked)
|
||||||
{
|
{
|
||||||
@@ -133,11 +126,7 @@ class MenuBody: public Window
|
|||||||
lines.emplace_back(true);
|
lines.emplace_back(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeLines()
|
void removeLines();
|
||||||
{
|
|
||||||
lines.clear();
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setCancelHandler(std::function<void()> handler)
|
void setCancelHandler(std::function<void()> handler)
|
||||||
{
|
{
|
||||||
@@ -147,6 +136,7 @@ class MenuBody: public Window
|
|||||||
void paint(BitmapBuffer * dc) override;
|
void paint(BitmapBuffer * dc) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend void menuBodyEventCallback(lv_event_t *);
|
||||||
void selectNext(MENU_DIRECTION direction);
|
void selectNext(MENU_DIRECTION direction);
|
||||||
int rangeCheck(int);
|
int rangeCheck(int);
|
||||||
void setIndex(int index);
|
void setIndex(int index);
|
||||||
@@ -264,6 +254,7 @@ class Menu: public ModalWindow
|
|||||||
void paint(BitmapBuffer * dc) override;
|
void paint(BitmapBuffer * dc) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend void menuBodyEventCallback(lv_event_t *);
|
||||||
MenuWindowContent * content;
|
MenuWindowContent * content;
|
||||||
bool multiple;
|
bool multiple;
|
||||||
Window * toolbar = nullptr;
|
Window * toolbar = nullptr;
|
||||||
|
|||||||
+22
-5
@@ -26,6 +26,10 @@ ModalWindow::ModalWindow(Window * parent, bool closeWhenClickOutside):
|
|||||||
Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}),
|
Window(parent->getFullScreenWindow(), {0, 0, LCD_W, LCD_H}),
|
||||||
closeWhenClickOutside(closeWhenClickOutside)
|
closeWhenClickOutside(closeWhenClickOutside)
|
||||||
{
|
{
|
||||||
|
lv_obj_set_parent(lvobj, lv_layer_top());
|
||||||
|
lv_obj_set_style_bg_color(lvobj, lv_color_black(), LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_bg_opa(lvobj, LV_OPA_50, LV_PART_MAIN);
|
||||||
|
lv_obj_add_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
|
||||||
Layer::push(this);
|
Layer::push(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,20 +37,33 @@ void ModalWindow::deleteLater(bool detach, bool trash)
|
|||||||
{
|
{
|
||||||
if (_deleted)
|
if (_deleted)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
|
||||||
Layer::pop(this);
|
Layer::pop(this);
|
||||||
|
|
||||||
Window::deleteLater(detach, trash);
|
Window::deleteLater(detach, trash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(HARDWARE_TOUCH)
|
||||||
|
bool ModalWindow::onTouchEnd(coord_t x, coord_t y)
|
||||||
|
{
|
||||||
|
if (closeWhenClickOutside) {
|
||||||
|
onKeyPress();
|
||||||
|
deleteLater();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void ModalWindow::paint(BitmapBuffer * dc)
|
void ModalWindow::paint(BitmapBuffer * dc)
|
||||||
{
|
{
|
||||||
dc->drawFilledRect(0, 0, width(), height(), SOLID, BLACK, OPACITY(6));
|
// dc->drawFilledRect(0, 0, width(), height(), SOLID, BLACK, OPACITY(6));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModalWindowContent::paint(BitmapBuffer * dc)
|
void ModalWindowContent::paint(BitmapBuffer * dc)
|
||||||
{
|
{
|
||||||
dc->drawSolidFilledRect(0, 0, width(), POPUP_HEADER_HEIGHT, COLOR_THEME_SECONDARY1);
|
// dc->drawSolidFilledRect(0, 0, width(), POPUP_HEADER_HEIGHT, COLOR_THEME_SECONDARY1);
|
||||||
dc->drawText(FIELD_PADDING_LEFT, (POPUP_HEADER_HEIGHT - getFontHeight(FONT(STD))) / 2, title.c_str(), COLOR_THEME_PRIMARY2);
|
// dc->drawText(FIELD_PADDING_LEFT, (POPUP_HEADER_HEIGHT - getFontHeight(FONT(STD))) / 2, title.c_str(), COLOR_THEME_PRIMARY2);
|
||||||
dc->drawSolidFilledRect(0, POPUP_HEADER_HEIGHT, width(), height() - POPUP_HEADER_HEIGHT, COLOR_THEME_SECONDARY3);
|
// dc->drawSolidFilledRect(0, POPUP_HEADER_HEIGHT, width(), height() - POPUP_HEADER_HEIGHT, COLOR_THEME_SECONDARY3);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-19
@@ -38,25 +38,7 @@ class ModalWindow: public Window
|
|||||||
void paint(BitmapBuffer * dc) override;
|
void paint(BitmapBuffer * dc) override;
|
||||||
|
|
||||||
#if defined(HARDWARE_TOUCH)
|
#if defined(HARDWARE_TOUCH)
|
||||||
bool onTouchStart(coord_t /*x*/, coord_t /*y*/) override
|
bool onTouchEnd(coord_t x, coord_t y) override;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool onTouchEnd(coord_t x, coord_t y) override
|
|
||||||
{
|
|
||||||
if (!Window::onTouchEnd(x, y) && closeWhenClickOutside) {
|
|
||||||
onKeyPress();
|
|
||||||
deleteLater();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY) override
|
|
||||||
{
|
|
||||||
Window::onTouchSlide(x, y, startX, startY, slideX, slideY);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setCloseWhenClickOutside(bool value = true)
|
void setCloseWhenClickOutside(bool value = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user