From ce8e7b67d094846ea0a8572e44eb64f622a18e59 Mon Sep 17 00:00:00 2001 From: kevinkoenig Date: Sun, 30 Jan 2022 03:58:59 -0500 Subject: [PATCH] checkbox implementation. almost right. it still needs some styling of the checkmark color. cant figure out how to make it happen --- .../src/thirdparty/libopenui/src/checkbox.cpp | 61 ++++++++++++++----- radio/src/thirdparty/libopenui/src/checkbox.h | 21 ++----- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/radio/src/thirdparty/libopenui/src/checkbox.cpp b/radio/src/thirdparty/libopenui/src/checkbox.cpp index 9dd22bcfd..6e91050c3 100644 --- a/radio/src/thirdparty/libopenui/src/checkbox.cpp +++ b/radio/src/thirdparty/libopenui/src/checkbox.cpp @@ -20,9 +20,46 @@ #include "checkbox.h" #include "theme.h" -void CheckBox::paint(BitmapBuffer * dc) +LvglWidgetFactory checkBoxFactory = { lv_checkbox_create, nullptr }; +lv_style_t style_indicator; +lv_style_t style_edit; + + +static void checkbox_event_handler(lv_event_t * e) { - theme->drawCheckBox(dc, this); + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * target = lv_event_get_target(e); + CheckBox* cb = (CheckBox *)lv_obj_get_user_data(target); + + if(code == LV_EVENT_VALUE_CHANGED) { + onKeyPress(); + cb->setValue(lv_obj_get_state(target) & LV_STATE_CHECKED); + } +} + +CheckBox::CheckBox(Window * parent, const rect_t & rect, std::function getValue, std::function setValue, WindowFlags flags) : + FormField(parent, rect, flags, 0, &checkBoxFactory), + _getValue(std::move(getValue)), + _setValue(std::move(setValue)) +{ + coord_t size = min(rect.w, rect.h); + setWidth(size); + setHeight(size); + + // BORDER + lv_obj_set_style_border_color(lvobj, makeLvColor(COLOR_THEME_SECONDARY2), LV_PART_INDICATOR); + lv_obj_set_style_border_width(lvobj, 1, LV_PART_INDICATOR); + + lv_obj_set_style_border_color(lvobj, makeLvColor(COLOR_THEME_FOCUS), LV_PART_INDICATOR | LV_STATE_FOCUSED); + lv_obj_set_style_border_width(lvobj, 2, LV_PART_INDICATOR | LV_STATE_FOCUSED); + + // UNCHECKED BACKGROUND + lv_obj_set_style_bg_color(lvobj, makeLvColor(COLOR_THEME_PRIMARY2), LV_PART_INDICATOR); + + if (_getValue()) + lv_obj_add_state(lvobj, LV_STATE_CHECKED); + + lv_obj_add_event_cb(lvobj, checkbox_event_handler, LV_EVENT_ALL, this); } #if defined(HARDWARE_KEYS) @@ -32,8 +69,12 @@ void CheckBox::onEvent(event_t event) if (enabled && event == EVT_KEY_BREAK(KEY_ENTER)) { onKeyPress(); - _setValue(!getValue()); - invalidate(); + if (lv_obj_get_state(lvobj) & LV_STATE_CHECKED) + lv_obj_clear_state(lvobj, LV_STATE_CHECKED); + else + lv_obj_add_state(lvobj, LV_STATE_CHECKED); + + _setValue(lv_obj_get_state(lvobj) & LV_STATE_CHECKED); } else { FormField::onEvent(event); @@ -41,15 +82,3 @@ void CheckBox::onEvent(event_t event) } #endif -#if defined(HARDWARE_TOUCH) -bool CheckBox::onTouchEnd(coord_t x, coord_t y) -{ - if (enabled) { - onKeyPress(); - _setValue(!getValue()); - setFocus(SET_FOCUS_DEFAULT); - invalidate(); - } - return true; -} -#endif diff --git a/radio/src/thirdparty/libopenui/src/checkbox.h b/radio/src/thirdparty/libopenui/src/checkbox.h index 8bdd51756..e85ceb13c 100644 --- a/radio/src/thirdparty/libopenui/src/checkbox.h +++ b/radio/src/thirdparty/libopenui/src/checkbox.h @@ -24,15 +24,7 @@ class CheckBox : public FormField { public: - CheckBox(Window * parent, const rect_t & rect, std::function getValue, std::function setValue, WindowFlags flags = 0) : - FormField(parent, rect, flags), - _getValue(std::move(getValue)), - _setValue(std::move(setValue)) - { - coord_t size = min(rect.w, rect.h); - setWidth(size); - setHeight(size); - } + CheckBox(Window * parent, const rect_t & rect, std::function getValue, std::function setValue, WindowFlags flags = 0); #if defined(DEBUG_WINDOWS) std::string getName() const override @@ -51,21 +43,20 @@ class CheckBox : public FormField { label = std::move(newLabel); } - void paint(BitmapBuffer * dc) override; - #if defined(HARDWARE_KEYS) void onEvent(event_t event) override; #endif -#if defined(HARDWARE_TOUCH) - bool onTouchEnd(coord_t x, coord_t y) override; -#endif - uint8_t getValue() const { return _getValue(); } + void setValue(uint8_t value) + { + _setValue(value); + } + void setSetValueHandler(std::function handler) { _setValue = std::move(handler);