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:
committed by
Raphael Coeffic
parent
a363e89fba
commit
ce8e7b67d0
+45
-16
@@ -20,9 +20,46 @@
|
|||||||
#include "checkbox.h"
|
#include "checkbox.h"
|
||||||
#include "theme.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)
|
#if defined(HARDWARE_KEYS)
|
||||||
@@ -32,8 +69,12 @@ void CheckBox::onEvent(event_t event)
|
|||||||
|
|
||||||
if (enabled && event == EVT_KEY_BREAK(KEY_ENTER)) {
|
if (enabled && event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||||
onKeyPress();
|
onKeyPress();
|
||||||
_setValue(!getValue());
|
if (lv_obj_get_state(lvobj) & LV_STATE_CHECKED)
|
||||||
invalidate();
|
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 {
|
else {
|
||||||
FormField::onEvent(event);
|
FormField::onEvent(event);
|
||||||
@@ -41,15 +82,3 @@ void CheckBox::onEvent(event_t event)
|
|||||||
}
|
}
|
||||||
#endif
|
#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
@@ -24,15 +24,7 @@
|
|||||||
|
|
||||||
class CheckBox : public FormField {
|
class CheckBox : public FormField {
|
||||||
public:
|
public:
|
||||||
CheckBox(Window * parent, const rect_t & rect, std::function<uint8_t()> getValue, std::function<void(uint8_t)> setValue, WindowFlags flags = 0) :
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(DEBUG_WINDOWS)
|
#if defined(DEBUG_WINDOWS)
|
||||||
std::string getName() const override
|
std::string getName() const override
|
||||||
@@ -51,21 +43,20 @@ class CheckBox : public FormField {
|
|||||||
label = std::move(newLabel);
|
label = std::move(newLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
void paint(BitmapBuffer * dc) override;
|
|
||||||
|
|
||||||
#if defined(HARDWARE_KEYS)
|
#if defined(HARDWARE_KEYS)
|
||||||
void onEvent(event_t event) override;
|
void onEvent(event_t event) override;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(HARDWARE_TOUCH)
|
|
||||||
bool onTouchEnd(coord_t x, coord_t y) override;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t getValue() const
|
uint8_t getValue() const
|
||||||
{
|
{
|
||||||
return _getValue();
|
return _getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setValue(uint8_t value)
|
||||||
|
{
|
||||||
|
_setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
void setSetValueHandler(std::function<void(uint8_t)> handler)
|
void setSetValueHandler(std::function<void(uint8_t)> handler)
|
||||||
{
|
{
|
||||||
_setValue = std::move(handler);
|
_setValue = std::move(handler);
|
||||||
|
|||||||
Reference in New Issue
Block a user