Skip to content

Commit

Permalink
Add 'align' property for labels. Add 'opacity' property for lines, re…
Browse files Browse the repository at this point in the history
…ctangles, circles, etc.
  • Loading branch information
philmoz committed Nov 6, 2024
1 parent 366df60 commit 60e29f7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
83 changes: 67 additions & 16 deletions radio/src/lua/lua_lvgl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ void LvglWidgetObjectBase::parseParam(lua_State *L, const char *key)
} else {
color = luaL_checkunsigned(L, -1);
}
} else if (!strcmp(key, "opacity")) {
if (lua_isfunction(L, -1)) {
getOpacityFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
opacity = luaL_checkunsigned(L, -1);
}
} else if (!strcmp(key, "visible")) {
getVisibleFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (!strcmp(key, "size")) {
Expand All @@ -267,6 +273,9 @@ bool LvglWidgetObjectBase::callRefs(lua_State *L)
if (!pcallUpdate1Int(L, getColorFunction,
[=](int color) { setColor(color); }))
return false;
if (!pcallUpdate1Int(L, getOpacityFunction,
[=](int opa) { setOpacity(opa); }))
return false;
if (!pcallUpdateBool(L, getVisibleFunction,
[=](bool visible) { if (visible) show(); else hide(); }))
return false;
Expand Down Expand Up @@ -313,6 +322,7 @@ void LvglWidgetObjectBase::clearRefs(lua_State *L)
{
clearRef(L, luaRef);
clearRef(L, getColorFunction);
clearRef(L, getOpacityFunction);
clearRef(L, getVisibleFunction);
clearRef(L, getSizeFunction);
clearRef(L, getPosFunction);
Expand All @@ -327,6 +337,7 @@ void LvglWidgetObjectBase::refresh()
setPos(x, y);
setSize(w, h);
setColor(color);
setOpacity(opacity);
}

void LvglWidgetObjectBase::update(lua_State *L)
Expand Down Expand Up @@ -387,6 +398,11 @@ void LvglWidgetLabel::parseParam(lua_State *L, const char *key)
getFontFunction = luaL_ref(L, LUA_REGISTRYINDEX);
else
font = luaL_checkunsigned(L, -1);
} else if (!strcmp(key, "align")) {
if (lua_isfunction(L, -1))
getAlignFunction = luaL_ref(L, LUA_REGISTRYINDEX);
else
align = luaL_checkunsigned(L, -1);
} else {
LvglSimpleWidgetObject::parseParam(L, key);
}
Expand All @@ -406,13 +422,16 @@ bool LvglWidgetLabel::callRefs(lua_State *L)
}
if (!pcallUpdate1Int(L, getFontFunction, [=](int val) { setFont(val); }))
return false;
if (!pcallUpdate1Int(L, getAlignFunction, [=](int val) { setAlign(val); }))
return false;
return LvglSimpleWidgetObject::callRefs(L);
}

void LvglWidgetLabel::clearRefs(lua_State *L)
{
clearRef(L, getTextFunction);
clearRef(L, getFontFunction);
clearRef(L, getAlignFunction);
LvglSimpleWidgetObject::clearRefs(L);
}

Expand Down Expand Up @@ -443,15 +462,22 @@ void LvglWidgetLabel::setFont(LcdFlags font)
{
if (lvobj) {
this->font = font;
if (font & VCENTERED) {
lv_obj_set_style_text_font(lvobj, getFont(font), LV_PART_MAIN);
}
}

void LvglWidgetLabel::setAlign(LcdFlags align)
{
if (lvobj) {
this->align = align;
if (align & VCENTERED) {
lv_obj_align(lvobj, LV_ALIGN_LEFT_MID, 0, 0);
}
lv_obj_set_style_text_align(lvobj,
(font & RIGHT) ? LV_TEXT_ALIGN_RIGHT
: (font & CENTERED) ? LV_TEXT_ALIGN_CENTER
(align & RIGHT) ? LV_TEXT_ALIGN_RIGHT
: (align & CENTERED) ? LV_TEXT_ALIGN_CENTER
: LV_TEXT_ALIGN_LEFT,
LV_PART_MAIN);
lv_obj_set_style_text_font(lvobj, getFont(font), LV_PART_MAIN);
}
}

Expand All @@ -462,7 +488,9 @@ void LvglWidgetLabel::build(lua_State *L)
setSize(w, h);
setText(txt);
setColor(color);
setOpacity(opacity);
setFont(font);
setAlign(align);
callRefs(L);
}

Expand Down Expand Up @@ -490,6 +518,12 @@ void LvglWidgetLineBase::setColor(LcdFlags newColor)
}
}

void LvglWidgetLineBase::setOpacity(uint8_t newOpa)
{
opacity = newOpa;
lv_obj_set_style_line_opa(lvobj, opacity, LV_PART_MAIN);
}

void LvglWidgetLineBase::setPos(coord_t x, coord_t y)
{
this->x = x;
Expand All @@ -507,13 +541,13 @@ void LvglWidgetLineBase::setSize(coord_t w, coord_t h)
void LvglWidgetLineBase::build(lua_State* L)
{
lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj());
lv_obj_set_style_line_opa(lvobj, LV_OPA_COVER, LV_PART_MAIN);
refresh();
}

void LvglWidgetLineBase::refresh()
{
setColor(color);
setOpacity(opacity);
setLine();
lv_obj_set_style_line_rounded(lvobj, rounded, LV_PART_MAIN);
}
Expand Down Expand Up @@ -593,6 +627,12 @@ void LvglWidgetLine::setColor(LcdFlags newColor)
}
}

void LvglWidgetLine::setOpacity(uint8_t newOpa)
{
opacity = newOpa;
lv_obj_set_style_line_opa(lvobj, opacity, LV_PART_MAIN);
}

void LvglWidgetLine::setPos(coord_t x, coord_t y)
{
if (pts) {
Expand Down Expand Up @@ -629,14 +669,14 @@ void LvglWidgetLine::setLine()
void LvglWidgetLine::build(lua_State *L)
{
lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj());
lv_obj_set_style_line_opa(lvobj, LV_OPA_COVER, LV_PART_MAIN);
refresh();
callRefs(L);
}

void LvglWidgetLine::refresh()
{
setColor(color);
setOpacity(opacity);
setLine();
}

Expand Down Expand Up @@ -694,7 +734,7 @@ void LvglWidgetTriangle::setSize(coord_t w, coord_t h)

void LvglWidgetTriangle::fillLine(coord_t x1, coord_t x2, coord_t y)
{
memset(&mask->data[y * w + x1], 255, x2 - x1 + 1);
memset(&mask->data[y * w + x1], opacity, x2 - x1 + 1);
}

// Swap two bytes
Expand Down Expand Up @@ -985,6 +1025,17 @@ void LvglWidgetBorderedObject::setColor(LcdFlags newColor)
}
}

void LvglWidgetBorderedObject::setOpacity(uint8_t newOpa)
{
opacity = newOpa;
if (filled) {
lv_obj_set_style_bg_opa(window->getLvObj(), opacity, LV_PART_MAIN);
} else {
lv_obj_set_style_border_opa(window->getLvObj(), opacity, LV_PART_MAIN);
lv_obj_set_style_border_width(window->getLvObj(), thickness, LV_PART_MAIN);
}
}

void LvglWidgetBorderedObject::build(lua_State *L)
{
window =
Expand All @@ -996,12 +1047,7 @@ void LvglWidgetBorderedObject::build(lua_State *L)
etx_scrollbar(window->getLvObj());
}
setColor(color);
if (filled) {
lv_obj_set_style_bg_opa(window->getLvObj(), LV_OPA_COVER, LV_PART_MAIN);
} else {
lv_obj_set_style_border_opa(window->getLvObj(), LV_OPA_COVER, LV_PART_MAIN);
lv_obj_set_style_border_width(window->getLvObj(), thickness, LV_PART_MAIN);
}
setOpacity(opacity);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1078,7 +1124,7 @@ void LvglWidgetCircle::build(lua_State *L)
setPos(x, y);
// Set width & height
setRadius(radius);
LvglWidgetBorderedObject::build(L);
LvglWidgetRoundObject::build(L);
lv_obj_set_style_radius(window->getLvObj(), LV_RADIUS_CIRCLE, LV_PART_MAIN);
callRefs(L);
}
Expand Down Expand Up @@ -1115,6 +1161,12 @@ void LvglWidgetArc::setColor(LcdFlags newColor)
}
}

void LvglWidgetArc::setOpacity(uint8_t newOpa)
{
opacity = newOpa;
lv_obj_set_style_arc_opa(window->getLvObj(), opacity, LV_PART_INDICATOR);
}

void LvglWidgetArc::setStartAngle(coord_t angle)
{
lv_arc_set_start_angle(window->getLvObj(), angle);
Expand Down Expand Up @@ -1161,9 +1213,8 @@ void LvglWidgetArc::build(lua_State *L)
setEndAngle(endAngle);
lv_obj_remove_style(window->getLvObj(), NULL, LV_PART_KNOB);
lv_obj_set_style_arc_opa(window->getLvObj(), LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_arc_width(window->getLvObj(), thickness, LV_PART_MAIN);
lv_obj_set_style_arc_opa(window->getLvObj(), LV_OPA_COVER, LV_PART_INDICATOR);
lv_obj_set_style_arc_width(window->getLvObj(), thickness, LV_PART_INDICATOR);
setOpacity(opacity);
callRefs(L);
}

Expand Down
11 changes: 11 additions & 0 deletions radio/src/lua/lua_lvgl_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LvglWidgetObjectBase
virtual void hide() = 0;

virtual void setColor(LcdFlags newColor) {}
virtual void setOpacity(uint8_t newOpa) {}
virtual void setPos(coord_t x, coord_t y) {}
virtual void setSize(coord_t w, coord_t h) {}

Expand All @@ -67,7 +68,9 @@ class LvglWidgetObjectBase
coord_t x = 0, y = 0, w = LV_SIZE_CONTENT, h = LV_SIZE_CONTENT;
LcdFlags color = COLOR2FLAGS(COLOR_THEME_SECONDARY1_INDEX);
LcdFlags currentColor = -1;
uint8_t opacity = LV_OPA_COVER;
int getColorFunction = LUA_REFNIL;
int getOpacityFunction = LUA_REFNIL;
int getVisibleFunction = LUA_REFNIL;
int getSizeFunction = LUA_REFNIL;
int getPosFunction = LUA_REFNIL;
Expand Down Expand Up @@ -120,6 +123,7 @@ class LvglWidgetLabel : public LvglSimpleWidgetObject
void setText(const char *s);
void setColor(LcdFlags newColor) override;
void setFont(LcdFlags font);
void setAlign(LcdFlags align);

void build(lua_State *L) override;
bool callRefs(lua_State *L) override;
Expand All @@ -130,14 +134,17 @@ class LvglWidgetLabel : public LvglSimpleWidgetObject

const char *txt = "";
LcdFlags font = FONT(STD);
LcdFlags align = LEFT;
int getTextFunction = LUA_REFNIL;
int getFontFunction = LUA_REFNIL;
int getAlignFunction = LUA_REFNIL;

void parseParam(lua_State *L, const char *key) override;
void refresh() override
{
setText(txt);
setFont(font);
setAlign(align);
LvglSimpleWidgetObject::refresh();
}
};
Expand All @@ -150,6 +157,7 @@ class LvglWidgetLineBase : public LvglSimpleWidgetObject
LvglWidgetLineBase() : LvglSimpleWidgetObject() {}

void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setPos(coord_t x, coord_t y) override;
void setSize(coord_t w, coord_t h) override;

Expand Down Expand Up @@ -195,6 +203,7 @@ class LvglWidgetLine : public LvglSimpleWidgetObject
LvglWidgetLine() : LvglSimpleWidgetObject() {}

void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setPos(coord_t x, coord_t y) override;
void setSize(coord_t w, coord_t h) override;

Expand Down Expand Up @@ -298,6 +307,7 @@ class LvglWidgetBorderedObject : public LvglWidgetObject
LvglWidgetBorderedObject() : LvglWidgetObject() {}

void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;

void build(lua_State *L) override;

Expand Down Expand Up @@ -369,6 +379,7 @@ class LvglWidgetArc : public LvglWidgetRoundObject
LvglWidgetArc() : LvglWidgetRoundObject() {}

void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setStartAngle(coord_t angle);
void setEndAngle(coord_t angle);

Expand Down

0 comments on commit 60e29f7

Please sign in to comment.