checkbox implementation. almost right. it still needs some styling of the checkmark color. cant figure out how to make it happen

This commit is contained in:
kevinkoenig
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent a363e89fba
commit ce8e7b67d0
2 changed files with 51 additions and 31 deletions
+45 -16
View File
@@ -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<uint8_t()> getValue, std::function<void(uint8_t)> 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
+6 -15
View File
@@ -24,15 +24,7 @@
class CheckBox : public FormField {
public:
CheckBox(Window * parent, const rect_t & rect, std::function<uint8_t()> getValue, std::function<void(uint8_t)> 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<uint8_t()> getValue, std::function<void(uint8_t)> 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<void(uint8_t)> handler)
{
_setValue = std::move(handler);