fix(color): Lua LVGL object does not draw correctly when using a function (#6193)
This commit is contained in:
@@ -736,6 +736,29 @@ void LvglWidgetLine::getPt(lua_State* L, int n)
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
|
||||
uint32_t LvglWidgetLine::getPts(lua_State* L)
|
||||
{
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
size_t newPtCnt = lua_rawlen(L, -1);
|
||||
if (newPtCnt > 1) {
|
||||
ptCnt = newPtCnt;
|
||||
if (pts && ptCnt > ptAlloc) {
|
||||
delete pts;
|
||||
pts = nullptr;
|
||||
}
|
||||
if (!pts) {
|
||||
pts = new lv_point_t[ptCnt];
|
||||
ptAlloc = ptCnt;
|
||||
}
|
||||
for (size_t i = 0; i < ptCnt; i += 1)
|
||||
getPt(L, i);
|
||||
return hash(pts, sizeof(ptCnt * sizeof(lv_point_t)));
|
||||
} else {
|
||||
ptCnt = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void LvglWidgetLine::parseParam(lua_State *L, const char *key)
|
||||
{
|
||||
if (!strcmp(key, "thickness")) {
|
||||
@@ -746,17 +769,7 @@ void LvglWidgetLine::parseParam(lua_State *L, const char *key)
|
||||
if (lua_isfunction(L, -1)) {
|
||||
getPointsFunction = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
} else {
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
ptCnt = lua_rawlen(L, -1);
|
||||
if (pts) delete pts;
|
||||
if (ptCnt > 1) {
|
||||
pts = new lv_point_t[ptCnt];
|
||||
for (size_t i = 0; i < ptCnt; i += 1)
|
||||
getPt(L, i);
|
||||
} else {
|
||||
pts = nullptr;
|
||||
ptCnt = 0;
|
||||
}
|
||||
ptsHash = getPts(L);
|
||||
}
|
||||
} else {
|
||||
LvglSimpleWidgetObject::parseParam(L, key);
|
||||
@@ -768,21 +781,10 @@ bool LvglWidgetLine::callRefs(lua_State *L)
|
||||
int t = lua_gettop(L);
|
||||
if (getPointsFunction != LUA_REFNIL) {
|
||||
if (pcallFunc(L, getPointsFunction, 1)) {
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
ptCnt = lua_rawlen(L, -1);
|
||||
if (pts) delete pts;
|
||||
if (ptCnt > 1) {
|
||||
pts = new lv_point_t[ptCnt];
|
||||
for (size_t i = 0; i < ptCnt; i += 1)
|
||||
getPt(L, i);
|
||||
uint32_t h = hash(pts, sizeof(ptCnt * sizeof(lv_point_t)));
|
||||
if (h != ptsHash) {
|
||||
ptsHash = h;
|
||||
refresh();
|
||||
}
|
||||
} else {
|
||||
pts = nullptr;
|
||||
ptCnt = 0;
|
||||
uint32_t h = getPts(L);
|
||||
if (h != ptsHash) {
|
||||
ptsHash = h;
|
||||
setLine();
|
||||
}
|
||||
lua_settop(L, t);
|
||||
} else {
|
||||
@@ -830,7 +832,10 @@ void LvglWidgetLine::setSize(coord_t w, coord_t h)
|
||||
|
||||
void LvglWidgetLine::setLine()
|
||||
{
|
||||
if (lvobj && pts) {
|
||||
if (pts) {
|
||||
if (!lvobj)
|
||||
lvobj = lv_line_create(parent);
|
||||
|
||||
x = pts[0].x;
|
||||
y = pts[0].y;
|
||||
for (size_t i = 1; i < ptCnt; i += 1) {
|
||||
@@ -847,7 +852,6 @@ void LvglWidgetLine::setLine()
|
||||
void LvglWidgetLine::build(lua_State *L)
|
||||
{
|
||||
if (pts) {
|
||||
lvobj = lv_line_create(parent);
|
||||
setColor(color.flags);
|
||||
setOpacity(opacity.value);
|
||||
setLine();
|
||||
@@ -856,10 +860,6 @@ void LvglWidgetLine::build(lua_State *L)
|
||||
|
||||
void LvglWidgetLine::refresh()
|
||||
{
|
||||
if (lvobj) {
|
||||
lv_obj_del(lvobj);
|
||||
lvobj = nullptr;
|
||||
}
|
||||
color.currVal = -1;
|
||||
build(nullptr);
|
||||
}
|
||||
|
||||
@@ -243,6 +243,7 @@ class LvglWidgetLine : public LvglSimpleWidgetObject
|
||||
coord_t thickness = 1;
|
||||
bool rounded = false;
|
||||
size_t ptCnt = 0;
|
||||
size_t ptAlloc = 0;
|
||||
lv_point_t* pts = nullptr;
|
||||
uint32_t ptsHash = -1;
|
||||
lv_obj_t* parent = nullptr;
|
||||
@@ -250,6 +251,7 @@ class LvglWidgetLine : public LvglSimpleWidgetObject
|
||||
|
||||
void setLine();
|
||||
|
||||
uint32_t getPts(lua_State* L);
|
||||
void getPt(lua_State* L, int n);
|
||||
void build(lua_State *L) override;
|
||||
void parseParam(lua_State *L, const char *key) override;
|
||||
|
||||
Reference in New Issue
Block a user