Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions src/draw_buf.c
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions src/luavgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`
*/
Expand Down
7 changes: 7 additions & 0 deletions src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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} },
};

Expand Down
15 changes: 15 additions & 0 deletions src/snapshot.c
Original file line number Diff line number Diff line change
@@ -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);
}
26 changes: 24 additions & 2 deletions src/widgets/img.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

static int luavgl_img_create(lua_State *L)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}