diff --git a/src/constants.c b/src/constants.c index 6de2730..143821e 100644 --- a/src/constants.c +++ b/src/constants.c @@ -576,6 +576,23 @@ static const rotable_Reg key_const_table[] = { {0, 0 }, }; +static const rotable_Reg color_format_const_table[] = { + {.name = "UNKNOWN", .integer = LV_COLOR_FORMAT_UNKNOWN }, + {.name = "RAW", .integer = LV_COLOR_FORMAT_RAW }, + {.name = "RAW_ALPHA", .integer = LV_COLOR_FORMAT_RAW_ALPHA}, + {.name = "L8", .integer = LV_COLOR_FORMAT_L8 }, + {.name = "I1", .integer = LV_COLOR_FORMAT_I1 }, + {.name = "I2", .integer = LV_COLOR_FORMAT_I2 }, + {.name = "I4", .integer = LV_COLOR_FORMAT_I4 }, + {.name = "I8", .integer = LV_COLOR_FORMAT_I8 }, + {.name = "A8", .integer = LV_COLOR_FORMAT_A8 }, + {.name = "RGB565", .integer = LV_COLOR_FORMAT_RGB565 }, + {.name = "RGB888", .integer = LV_COLOR_FORMAT_RGB888 }, + {.name = "XRGB8888", .integer = LV_COLOR_FORMAT_XRGB8888 }, + {.name = "ARGB8888", .integer = LV_COLOR_FORMAT_ARGB8888 }, + {0, 0 }, +}; + static int luavgl_LV_PCT(lua_State *L) { int pct = lua_tointeger(L, 1); @@ -634,6 +651,7 @@ static void luavgl_constants_init(lua_State *L) rotable_setfiled(L, -2, "ROLLER_MODE", roller_mode_const_table); #endif rotable_setfiled(L, -2, "KEY", key_const_table); + rotable_setfiled(L, -2, "COLOR_FORMAT", color_format_const_table); /* miscellaneous. */ lua_pushinteger(L, LV_ANIM_REPEAT_INFINITE); diff --git a/src/draw_buf.c b/src/draw_buf.c new file mode 100644 index 0000000..871fb9e --- /dev/null +++ b/src/draw_buf.c @@ -0,0 +1,34 @@ +#include "luavgl.h" +#include "private.h" +#include "rotable.h" + +typedef struct luavgl_draw_buf_s { + lv_draw_buf_t *buf; +} luavgl_draw_buf_t; + +static int luavgl_draw_buf_gc(lua_State *L) +{ + luavgl_draw_buf_t *db = luaL_checkudata(L, 1, "lv_draw_buf"); + lv_draw_buf_destroy(db->buf); + return 0; +} + +static int luavgl_draw_buf_create(lua_State *L, lv_draw_buf_t *buf) +{ + luavgl_draw_buf_t *db = lua_newuserdata(L, sizeof(luavgl_draw_buf_t)); + db->buf = buf; + luaL_setmetatable(L, "lv_draw_buf"); + return 1; +} + +static const luaL_Reg draw_buf_meta[] = { + {"__gc", luavgl_draw_buf_gc}, + {}, +}; + +static void luavgl_draw_buf_init(lua_State *L) +{ + luaL_newmetatable(L, "lv_draw_buf"); + luaL_setfuncs(L, draw_buf_meta, 0); + lua_pop(L, 1); +} diff --git a/src/luavgl.c b/src/luavgl.c index 4a16a5b..32acb3d 100644 --- a/src/luavgl.c +++ b/src/luavgl.c @@ -4,6 +4,7 @@ #include "anim.c" #include "constants.c" #include "disp.c" +#include "draw_buf.c" #include "font.c" #include "fs.c" #include "group.c" @@ -147,6 +148,7 @@ LUALIB_API int luaopen_lvgl(lua_State *L) luavgl_disp_init(L); luavgl_palette_init(L); luavgl_constants_init(L); + luavgl_draw_buf_init(L); /* Methods to create widget locate in widgets table, check `luavgl_obj_init` */ diff --git a/src/obj.c b/src/obj.c index 621d975..7067822 100644 --- a/src/obj.c +++ b/src/obj.c @@ -7,6 +7,10 @@ #include "style.c" #include "widgets/widgets.c" +#if LV_USE_SNAPSHOT +#include "snapshot.c" +#endif + static int luavgl_anim_create(lua_State *L); static int luavgl_obj_delete(lua_State *L); static int luavgl_obj_clean(lua_State *L); @@ -936,6 +940,9 @@ static const rotable_Reg luavgl_obj_methods[] = { {"Anim", LUA_TFUNCTION, {luavgl_anim_create} }, {"remove_all_anim", LUA_TFUNCTION, {luavgl_obj_remove_anim_all} }, {"__property", LUA_TLIGHTUSERDATA, {.ptr = &luavgl_obj_property_table} }, +#if LV_USE_SNAPSHOT + {"snapshot", LUA_TFUNCTION, {luavgl_obj_snapshot} }, +#endif {0, 0, {0} }, }; diff --git a/src/snapshot.c b/src/snapshot.c new file mode 100644 index 0000000..7241c0e --- /dev/null +++ b/src/snapshot.c @@ -0,0 +1,15 @@ +#include "luavgl.h" + +static int luavgl_draw_buf_create(lua_State *L, lv_draw_buf_t *buf); + +static int luavgl_obj_snapshot(lua_State *L) +{ + luavgl_obj_t *lobj = luavgl_to_lobj(L, 1); + + lv_color_format_t cf = LV_COLOR_FORMAT_ARGB8888; + if (!lua_isnoneornil(L, 2)) { + cf = (lv_color_format_t)lua_tointeger(L, 2); + } + lv_draw_buf_t *buf = lv_snapshot_take(lobj->obj, cf); + return luavgl_draw_buf_create(L, buf); +} diff --git a/src/widgets/img.c b/src/widgets/img.c index be5c8c5..1fc9266 100644 --- a/src/widgets/img.c +++ b/src/widgets/img.c @@ -1,5 +1,4 @@ #include "luavgl.h" -#include "private.h" #include "rotable.h" static int luavgl_img_create(lua_State *L) @@ -41,7 +40,23 @@ static const luavgl_table_t img_property_table = { static int luavgl_img_set_src(lua_State *L) { lv_obj_t *obj = luavgl_to_obj(L, 1); - const char *src = luavgl_toimgsrc(L, 2); + + const void *src = NULL; + luavgl_draw_buf_t *db = luaL_checkudata(L, 2, "lv_draw_buf"); + if (db) { + src = db->buf; + luaL_getmetatable(L, "lv_img_src"); + lua_pushvalue(L, 1); + lua_pushvalue(L, 2); + lua_rawset(L, -3); + } else { + src = luavgl_toimgsrc(L, 2); + luaL_getmetatable(L, "lv_img_src"); + lua_pushvalue(L, 1); + lua_pushnil(L); + lua_rawset(L, -3); + } + if (src != NULL) { lv_image_set_src(obj, src); } @@ -144,4 +159,11 @@ static void luavgl_img_init(lua_State *L) { luavgl_obj_newmetatable(L, &lv_image_class, "lv_img", luavgl_img_methods); lua_pop(L, 1); + + luaL_newmetatable(L, "lv_img_src"); + lua_newtable(L); + lua_pushstring(L, "k"); + lua_setfield(L, -2, "__mode"); + lua_setmetatable(L, -2); + lua_pop(L, 1); }