implementation of choice and filechoice using lvgl

This commit is contained in:
kkoenig1@optonline.com
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent b823150158
commit 3dd0a72ed8
5 changed files with 74 additions and 15 deletions
+60 -12
View File
@@ -21,14 +21,62 @@
#include "menu.h" #include "menu.h"
#include "theme.h" #include "theme.h"
void choicePaintCallback(lv_event_t *e)
{
auto code = lv_event_get_code(e);
if (code == LV_EVENT_DRAW_MAIN_BEGIN) {
lv_obj_t *target = lv_event_get_target(e);
if (target != nullptr) {
ChoiceBase *cb = (ChoiceBase*)lv_obj_get_user_data(target);
std::string text = cb->getLabelText();
lv_label_set_text(cb->label, text.c_str());
}
}
}
ChoiceBase::ChoiceBase(FormGroup * parent, const rect_t & rect, ChoiceType type, WindowFlags windowFlags): ChoiceBase::ChoiceBase(FormGroup * parent, const rect_t & rect, ChoiceType type, WindowFlags windowFlags):
FormField(parent, rect, windowFlags), FormField(parent, rect, windowFlags),
type(type) type(type)
{ {
lv_obj_add_event_cb(lvobj, choicePaintCallback, LV_EVENT_DRAW_MAIN_BEGIN, lvobj);
label = lv_label_create(lvobj);
// Normal font and background color
lv_obj_set_style_bg_color(lvobj, makeLvColor(COLOR_THEME_PRIMARY2), LV_PART_MAIN);
lv_obj_set_style_bg_opa(lvobj, LV_OPA_100, LV_PART_MAIN);
lv_obj_set_style_pad_right(lvobj, 5, LV_PART_MAIN);
lv_obj_set_style_border_width(lvobj, 1, LV_PART_MAIN);
lv_obj_set_style_border_color(lvobj, makeLvColor(COLOR_THEME_SECONDARY2), LV_PART_MAIN);
lv_obj_set_style_text_color(lvobj, makeLvColor(COLOR_THEME_SECONDARY1), LV_PART_MAIN);
lv_obj_set_style_text_font(lvobj, &lv_font_roboto_13, LV_PART_MAIN);
// focused
lv_obj_set_style_bg_color(lvobj, makeLvColor(COLOR_THEME_FOCUS), LV_PART_MAIN | LV_STATE_FOCUSED);
lv_obj_set_style_text_color(lvobj, makeLvColor(COLOR_THEME_PRIMARY2), LV_PART_MAIN | LV_STATE_FOCUSED);
lv_obj_set_width(label, lv_pct(80));
lv_obj_set_style_pad_left(label, FIELD_PADDING_LEFT, LV_PART_MAIN);
lv_obj_set_style_pad_top(label, FIELD_PADDING_TOP, LV_PART_MAIN);
// add the image
lv_obj_t *img = lv_img_create(lvobj);
lv_img_set_src(img, type == CHOICE_TYPE_DROPOWN ? LV_SYMBOL_DOWN : LV_SYMBOL_DIRECTORY);
lv_obj_set_align(img, LV_ALIGN_RIGHT_MID);
} }
std::string Choice::getLabelText()
{
std::string text = "";
if (textHandler != nullptr)
text = textHandler(getValue());
else if (getValue() < values.size())
text = values[getValue()];
return text;
}
Choice::Choice(FormGroup * parent, const rect_t & rect, int vmin, int vmax, Choice::Choice(FormGroup * parent, const rect_t & rect, int vmin, int vmax,
std::function<int()> getValue, std::function<void(int)> setValue, WindowFlags windowFlags) : std::function<int()> getValue, std::function<void(int)> setValue, WindowFlags windowFlags) :
ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags), ChoiceBase(parent, rect, CHOICE_TYPE_DROPOWN, windowFlags),
@@ -111,20 +159,20 @@ void Choice::setValues(const char * const values[])
void Choice::paint(BitmapBuffer * dc) void Choice::paint(BitmapBuffer * dc)
{ {
FormField::paint(dc); // FormField::paint(dc);
auto val = getValue(); // auto val = getValue();
std::string str = ""; // std::string str = "";
if (textHandler) { // if (textHandler) {
str = textHandler(val); // str = textHandler(val);
} else { // } else {
val -= vmin; // val -= vmin;
if (val >= 0 && val < (int)values.size()) { // if (val >= 0 && val < (int)values.size()) {
str = values[val]; // str = values[val];
} // }
} // }
theme->drawChoice(dc, this, str.c_str()); // theme->drawChoice(dc, this, str.c_str());
} }
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
+4
View File
@@ -40,7 +40,10 @@ class ChoiceBase : public FormField
} }
protected: protected:
friend void choicePaintCallback(lv_event_t *e);
ChoiceType type; ChoiceType type;
lv_obj_t *label;
virtual std::string getLabelText() { return ""; };
}; };
class Choice: public ChoiceBase { class Choice: public ChoiceBase {
@@ -150,6 +153,7 @@ class Choice: public ChoiceBase {
} }
protected: protected:
std::string getLabelText() override;
std::vector<std::string> values; std::vector<std::string> values;
int vmin = 0; int vmin = 0;
int vmax = 0; int vmax = 0;
+8 -2
View File
@@ -48,10 +48,16 @@ FileChoice::FileChoice(FormGroup *parent, const rect_t &rect,
{ {
} }
std::string FileChoice::getLabelText()
{
return getValue();
}
void FileChoice::paint(BitmapBuffer * dc) void FileChoice::paint(BitmapBuffer * dc)
{ {
FormField::paint(dc); // FormField::paint(dc);
theme->drawChoice(dc, this, getValue().c_str()); // theme->drawChoice(dc, this, getValue().c_str());
} }
bool FileChoice::openMenu() bool FileChoice::openMenu()
+1
View File
@@ -46,6 +46,7 @@ public:
#endif #endif
protected: protected:
std::string getLabelText() override;
std::string folder; std::string folder;
const char* extension; const char* extension;
int maxlen; int maxlen;
+1 -1
View File
@@ -102,7 +102,7 @@ void MenuBody::setIndex(int index)
btn = lv_obj_get_child(lvobj, selectedIndex); btn = lv_obj_get_child(lvobj, selectedIndex);
if (btn != nullptr) { if (btn != nullptr) {
lv_obj_add_state(btn, LV_STATE_CHECKED); lv_obj_add_state(btn, LV_STATE_CHECKED);
lv_obj_scroll_to_view(btn, LV_ANIM_ON); lv_obj_scroll_to_view(btn, LV_ANIM_OFF);
} }
} }
} }