luat_lib_easylvgl_container.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. @module easylvgl.container
  3. @summary EasyLVGL Container 组件 Lua 绑定
  4. @version 0.1.0
  5. @date 2025.12.25
  6. @tag LUAT_USE_EASYLVGL
  7. */
  8. #include "luat_base.h"
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "../inc/luat_easylvgl.h"
  12. #include "../inc/luat_easylvgl_component.h"
  13. #include "../inc/luat_easylvgl_binding.h"
  14. #define EASYLVGL_CONTAINER_MT "easylvgl.container"
  15. /**
  16. * 创建 Container 组件
  17. * @api easylvgl.container(config)
  18. */
  19. static int l_easylvgl_container(lua_State *L) {
  20. easylvgl_ctx_t *ctx = NULL;
  21. lua_getfield(L, LUA_REGISTRYINDEX, "easylvgl_ctx");
  22. if (lua_type(L, -1) == LUA_TLIGHTUSERDATA) {
  23. ctx = (easylvgl_ctx_t *)lua_touserdata(L, -1);
  24. }
  25. lua_pop(L, 1);
  26. if (ctx == NULL) {
  27. luaL_error(L, "easylvgl not initialized, call easylvgl.init() first");
  28. return 0;
  29. }
  30. luaL_checktype(L, 1, LUA_TTABLE);
  31. lv_obj_t *container = easylvgl_container_create_from_config(L, 1);
  32. if (container == NULL) {
  33. lua_pushnil(L);
  34. return 1;
  35. }
  36. easylvgl_push_component_userdata(L, container, EASYLVGL_CONTAINER_MT);
  37. return 1;
  38. }
  39. /**
  40. * Container:set_color(color)
  41. * @api container:set_color(color)
  42. * @int color 背景色(0xRRGGBB)
  43. */
  44. static int l_container_set_color(lua_State *L) {
  45. lv_obj_t *container = easylvgl_check_component(L, 1, EASYLVGL_CONTAINER_MT);
  46. uint32_t color = (uint32_t)luaL_checkinteger(L, 2);
  47. easylvgl_container_set_color(container, color);
  48. return 0;
  49. }
  50. /**
  51. * Container GC
  52. */
  53. static int l_container_gc(lua_State *L) {
  54. easylvgl_component_ud_t *ud = (easylvgl_component_ud_t *)luaL_checkudata(L, 1, EASYLVGL_CONTAINER_MT);
  55. if (ud != NULL && ud->obj != NULL) {
  56. easylvgl_component_meta_t *meta = easylvgl_component_meta_get(ud->obj);
  57. if (meta != NULL) {
  58. easylvgl_component_meta_free(meta);
  59. }
  60. lv_obj_delete(ud->obj);
  61. ud->obj = NULL;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * 注册 Container 元表
  67. */
  68. void easylvgl_register_container_meta(lua_State *L) {
  69. luaL_newmetatable(L, EASYLVGL_CONTAINER_MT);
  70. lua_pushcfunction(L, l_container_gc);
  71. lua_setfield(L, -2, "__gc");
  72. static const luaL_Reg methods[] = {
  73. {"set_color", l_container_set_color},
  74. {NULL, NULL}
  75. };
  76. luaL_newlib(L, methods);
  77. lua_setfield(L, -2, "__index");
  78. lua_pop(L, 1);
  79. }
  80. /**
  81. * Container 创建函数(供主模块注册)
  82. */
  83. int easylvgl_container_create(lua_State *L) {
  84. return l_easylvgl_container(L);
  85. }