feat(lua): add 'align' and 'opacity' properties for some LVGL widget objects (#5654)

This commit is contained in:
philmoz
2024-11-09 12:26:47 +10:00
committed by GitHub
parent 314e93a381
commit 50cfd4d1f7
2 changed files with 78 additions and 16 deletions
+67 -16
View File
@@ -244,6 +244,12 @@ void LvglWidgetObjectBase::parseParam(lua_State *L, const char *key)
} else { } else {
color = luaL_checkunsigned(L, -1); 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")) { } else if (!strcmp(key, "visible")) {
getVisibleFunction = luaL_ref(L, LUA_REGISTRYINDEX); getVisibleFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (!strcmp(key, "size")) { } else if (!strcmp(key, "size")) {
@@ -267,6 +273,9 @@ bool LvglWidgetObjectBase::callRefs(lua_State *L)
if (!pcallUpdate1Int(L, getColorFunction, if (!pcallUpdate1Int(L, getColorFunction,
[=](int color) { setColor(color); })) [=](int color) { setColor(color); }))
return false; return false;
if (!pcallUpdate1Int(L, getOpacityFunction,
[=](int opa) { setOpacity(opa); }))
return false;
if (!pcallUpdateBool(L, getVisibleFunction, if (!pcallUpdateBool(L, getVisibleFunction,
[=](bool visible) { if (visible) show(); else hide(); })) [=](bool visible) { if (visible) show(); else hide(); }))
return false; return false;
@@ -313,6 +322,7 @@ void LvglWidgetObjectBase::clearRefs(lua_State *L)
{ {
clearRef(L, luaRef); clearRef(L, luaRef);
clearRef(L, getColorFunction); clearRef(L, getColorFunction);
clearRef(L, getOpacityFunction);
clearRef(L, getVisibleFunction); clearRef(L, getVisibleFunction);
clearRef(L, getSizeFunction); clearRef(L, getSizeFunction);
clearRef(L, getPosFunction); clearRef(L, getPosFunction);
@@ -327,6 +337,7 @@ void LvglWidgetObjectBase::refresh()
setPos(x, y); setPos(x, y);
setSize(w, h); setSize(w, h);
setColor(color); setColor(color);
setOpacity(opacity);
} }
void LvglWidgetObjectBase::update(lua_State *L) void LvglWidgetObjectBase::update(lua_State *L)
@@ -387,6 +398,11 @@ void LvglWidgetLabel::parseParam(lua_State *L, const char *key)
getFontFunction = luaL_ref(L, LUA_REGISTRYINDEX); getFontFunction = luaL_ref(L, LUA_REGISTRYINDEX);
else else
font = luaL_checkunsigned(L, -1); 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 { } else {
LvglSimpleWidgetObject::parseParam(L, key); LvglSimpleWidgetObject::parseParam(L, key);
} }
@@ -406,6 +422,8 @@ bool LvglWidgetLabel::callRefs(lua_State *L)
} }
if (!pcallUpdate1Int(L, getFontFunction, [=](int val) { setFont(val); })) if (!pcallUpdate1Int(L, getFontFunction, [=](int val) { setFont(val); }))
return false; return false;
if (!pcallUpdate1Int(L, getAlignFunction, [=](int val) { setAlign(val); }))
return false;
return LvglSimpleWidgetObject::callRefs(L); return LvglSimpleWidgetObject::callRefs(L);
} }
@@ -413,6 +431,7 @@ void LvglWidgetLabel::clearRefs(lua_State *L)
{ {
clearRef(L, getTextFunction); clearRef(L, getTextFunction);
clearRef(L, getFontFunction); clearRef(L, getFontFunction);
clearRef(L, getAlignFunction);
LvglSimpleWidgetObject::clearRefs(L); LvglSimpleWidgetObject::clearRefs(L);
} }
@@ -443,15 +462,22 @@ void LvglWidgetLabel::setFont(LcdFlags font)
{ {
if (lvobj) { if (lvobj) {
this->font = font; 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_align(lvobj, LV_ALIGN_LEFT_MID, 0, 0);
} }
lv_obj_set_style_text_align(lvobj, lv_obj_set_style_text_align(lvobj,
(font & RIGHT) ? LV_TEXT_ALIGN_RIGHT (align & RIGHT) ? LV_TEXT_ALIGN_RIGHT
: (font & CENTERED) ? LV_TEXT_ALIGN_CENTER : (align & CENTERED) ? LV_TEXT_ALIGN_CENTER
: LV_TEXT_ALIGN_LEFT, : LV_TEXT_ALIGN_LEFT,
LV_PART_MAIN); LV_PART_MAIN);
lv_obj_set_style_text_font(lvobj, getFont(font), LV_PART_MAIN);
} }
} }
@@ -462,7 +488,9 @@ void LvglWidgetLabel::build(lua_State *L)
setSize(w, h); setSize(w, h);
setText(txt); setText(txt);
setColor(color); setColor(color);
setOpacity(opacity);
setFont(font); setFont(font);
setAlign(align);
callRefs(L); callRefs(L);
} }
@@ -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) void LvglWidgetLineBase::setPos(coord_t x, coord_t y)
{ {
this->x = x; this->x = x;
@@ -507,13 +541,13 @@ void LvglWidgetLineBase::setSize(coord_t w, coord_t h)
void LvglWidgetLineBase::build(lua_State* L) void LvglWidgetLineBase::build(lua_State* L)
{ {
lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj()); lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj());
lv_obj_set_style_line_opa(lvobj, LV_OPA_COVER, LV_PART_MAIN);
refresh(); refresh();
} }
void LvglWidgetLineBase::refresh() void LvglWidgetLineBase::refresh()
{ {
setColor(color); setColor(color);
setOpacity(opacity);
setLine(); setLine();
lv_obj_set_style_line_rounded(lvobj, rounded, LV_PART_MAIN); lv_obj_set_style_line_rounded(lvobj, rounded, LV_PART_MAIN);
} }
@@ -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) void LvglWidgetLine::setPos(coord_t x, coord_t y)
{ {
if (pts) { if (pts) {
@@ -629,7 +669,6 @@ void LvglWidgetLine::setLine()
void LvglWidgetLine::build(lua_State *L) void LvglWidgetLine::build(lua_State *L)
{ {
lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj()); lvobj = lv_line_create(lvglManager->getCurrentParent()->getLvObj());
lv_obj_set_style_line_opa(lvobj, LV_OPA_COVER, LV_PART_MAIN);
refresh(); refresh();
callRefs(L); callRefs(L);
} }
@@ -637,6 +676,7 @@ void LvglWidgetLine::build(lua_State *L)
void LvglWidgetLine::refresh() void LvglWidgetLine::refresh()
{ {
setColor(color); setColor(color);
setOpacity(opacity);
setLine(); setLine();
} }
@@ -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) 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 // Swap two bytes
@@ -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) void LvglWidgetBorderedObject::build(lua_State *L)
{ {
window = window =
@@ -996,12 +1047,7 @@ void LvglWidgetBorderedObject::build(lua_State *L)
etx_scrollbar(window->getLvObj()); etx_scrollbar(window->getLvObj());
} }
setColor(color); setColor(color);
if (filled) { setOpacity(opacity);
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);
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -1078,7 +1124,7 @@ void LvglWidgetCircle::build(lua_State *L)
setPos(x, y); setPos(x, y);
// Set width & height // Set width & height
setRadius(radius); setRadius(radius);
LvglWidgetBorderedObject::build(L); LvglWidgetRoundObject::build(L);
lv_obj_set_style_radius(window->getLvObj(), LV_RADIUS_CIRCLE, LV_PART_MAIN); lv_obj_set_style_radius(window->getLvObj(), LV_RADIUS_CIRCLE, LV_PART_MAIN);
callRefs(L); callRefs(L);
} }
@@ -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) void LvglWidgetArc::setStartAngle(coord_t angle)
{ {
lv_arc_set_start_angle(window->getLvObj(), angle); lv_arc_set_start_angle(window->getLvObj(), angle);
@@ -1161,9 +1213,8 @@ void LvglWidgetArc::build(lua_State *L)
setEndAngle(endAngle); setEndAngle(endAngle);
lv_obj_remove_style(window->getLvObj(), NULL, LV_PART_KNOB); 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_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); lv_obj_set_style_arc_width(window->getLvObj(), thickness, LV_PART_INDICATOR);
setOpacity(opacity);
callRefs(L); callRefs(L);
} }
+11
View File
@@ -47,6 +47,7 @@ class LvglWidgetObjectBase
virtual void hide() = 0; virtual void hide() = 0;
virtual void setColor(LcdFlags newColor) {} virtual void setColor(LcdFlags newColor) {}
virtual void setOpacity(uint8_t newOpa) {}
virtual void setPos(coord_t x, coord_t y) {} virtual void setPos(coord_t x, coord_t y) {}
virtual void setSize(coord_t w, coord_t h) {} virtual void setSize(coord_t w, coord_t h) {}
@@ -67,7 +68,9 @@ class LvglWidgetObjectBase
coord_t x = 0, y = 0, w = LV_SIZE_CONTENT, h = LV_SIZE_CONTENT; coord_t x = 0, y = 0, w = LV_SIZE_CONTENT, h = LV_SIZE_CONTENT;
LcdFlags color = COLOR2FLAGS(COLOR_THEME_SECONDARY1_INDEX); LcdFlags color = COLOR2FLAGS(COLOR_THEME_SECONDARY1_INDEX);
LcdFlags currentColor = -1; LcdFlags currentColor = -1;
uint8_t opacity = LV_OPA_COVER;
int getColorFunction = LUA_REFNIL; int getColorFunction = LUA_REFNIL;
int getOpacityFunction = LUA_REFNIL;
int getVisibleFunction = LUA_REFNIL; int getVisibleFunction = LUA_REFNIL;
int getSizeFunction = LUA_REFNIL; int getSizeFunction = LUA_REFNIL;
int getPosFunction = LUA_REFNIL; int getPosFunction = LUA_REFNIL;
@@ -120,6 +123,7 @@ class LvglWidgetLabel : public LvglSimpleWidgetObject
void setText(const char *s); void setText(const char *s);
void setColor(LcdFlags newColor) override; void setColor(LcdFlags newColor) override;
void setFont(LcdFlags font); void setFont(LcdFlags font);
void setAlign(LcdFlags align);
void build(lua_State *L) override; void build(lua_State *L) override;
bool callRefs(lua_State *L) override; bool callRefs(lua_State *L) override;
@@ -130,14 +134,17 @@ class LvglWidgetLabel : public LvglSimpleWidgetObject
const char *txt = ""; const char *txt = "";
LcdFlags font = FONT(STD); LcdFlags font = FONT(STD);
LcdFlags align = LEFT;
int getTextFunction = LUA_REFNIL; int getTextFunction = LUA_REFNIL;
int getFontFunction = LUA_REFNIL; int getFontFunction = LUA_REFNIL;
int getAlignFunction = LUA_REFNIL;
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;
void refresh() override void refresh() override
{ {
setText(txt); setText(txt);
setFont(font); setFont(font);
setAlign(align);
LvglSimpleWidgetObject::refresh(); LvglSimpleWidgetObject::refresh();
} }
}; };
@@ -150,6 +157,7 @@ class LvglWidgetLineBase : public LvglSimpleWidgetObject
LvglWidgetLineBase() : LvglSimpleWidgetObject() {} LvglWidgetLineBase() : LvglSimpleWidgetObject() {}
void setColor(LcdFlags newColor) override; void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setPos(coord_t x, coord_t y) override; void setPos(coord_t x, coord_t y) override;
void setSize(coord_t w, coord_t h) override; void setSize(coord_t w, coord_t h) override;
@@ -195,6 +203,7 @@ class LvglWidgetLine : public LvglSimpleWidgetObject
LvglWidgetLine() : LvglSimpleWidgetObject() {} LvglWidgetLine() : LvglSimpleWidgetObject() {}
void setColor(LcdFlags newColor) override; void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setPos(coord_t x, coord_t y) override; void setPos(coord_t x, coord_t y) override;
void setSize(coord_t w, coord_t h) override; void setSize(coord_t w, coord_t h) override;
@@ -298,6 +307,7 @@ class LvglWidgetBorderedObject : public LvglWidgetObject
LvglWidgetBorderedObject() : LvglWidgetObject() {} LvglWidgetBorderedObject() : LvglWidgetObject() {}
void setColor(LcdFlags newColor) override; void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void build(lua_State *L) override; void build(lua_State *L) override;
@@ -369,6 +379,7 @@ class LvglWidgetArc : public LvglWidgetRoundObject
LvglWidgetArc() : LvglWidgetRoundObject() {} LvglWidgetArc() : LvglWidgetRoundObject() {}
void setColor(LcdFlags newColor) override; void setColor(LcdFlags newColor) override;
void setOpacity(uint8_t newOpa) override;
void setStartAngle(coord_t angle); void setStartAngle(coord_t angle);
void setEndAngle(coord_t angle); void setEndAngle(coord_t angle);