luat_easylvgl_component.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * @file luat_easylvgl_component.h
  3. * @summary EasyLVGL 组件基类接口
  4. * @responsible 组件元数据、事件绑定、配置表解析
  5. */
  6. #ifndef EASYLVGL_COMPONENT_H
  7. #define EASYLVGL_COMPONENT_H
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include "luat_easylvgl.h"
  12. /*********************
  13. * DEFINES
  14. *********************/
  15. /** 组件类型 */
  16. typedef enum {
  17. EASYLVGL_COMPONENT_BUTTON = 1,
  18. EASYLVGL_COMPONENT_LABEL,
  19. EASYLVGL_COMPONENT_IMAGE,
  20. EASYLVGL_COMPONENT_WIN,
  21. EASYLVGL_COMPONENT_DROPDOWN,
  22. EASYLVGL_COMPONENT_SWITCH,
  23. EASYLVGL_COMPONENT_MSGBOX,
  24. EASYLVGL_COMPONENT_CONTAINER,
  25. EASYLVGL_COMPONENT_TABLE,
  26. EASYLVGL_COMPONENT_TABVIEW,
  27. EASYLVGL_COMPONENT_TEXTAREA,
  28. EASYLVGL_COMPONENT_KEYBOARD
  29. } easylvgl_component_type_t;
  30. /** TabView 对齐常量 */
  31. typedef enum {
  32. EASYLVGL_TABVIEW_PAD_ALL = 0,
  33. EASYLVGL_TABVIEW_PAD_HOR,
  34. EASYLVGL_TABVIEW_PAD_VER,
  35. EASYLVGL_TABVIEW_PAD_TOP,
  36. EASYLVGL_TABVIEW_PAD_BOTTOM,
  37. EASYLVGL_TABVIEW_PAD_LEFT,
  38. EASYLVGL_TABVIEW_PAD_RIGHT,
  39. EASYLVGL_TABVIEW_PAD_MAX
  40. } easylvgl_tabview_pad_method_t;
  41. /** 事件类型 */
  42. typedef enum {
  43. EASYLVGL_EVENT_CLICKED = 0,
  44. EASYLVGL_EVENT_PRESSED,
  45. EASYLVGL_EVENT_RELEASED,
  46. EASYLVGL_EVENT_VALUE_CHANGED,
  47. EASYLVGL_EVENT_ACTION,
  48. EASYLVGL_EVENT_READY,
  49. EASYLVGL_EVENT_CLOSE,
  50. EASYLVGL_EVENT_MAX
  51. } easylvgl_event_type_t;
  52. /**********************
  53. * TYPEDEFS
  54. *********************/
  55. /**
  56. * 组件元数据
  57. */
  58. struct easylvgl_component_meta {
  59. lv_obj_t *obj; /**< LVGL 对象指针 */
  60. easylvgl_ctx_t *ctx; /**< 上下文引用 */
  61. // 回调引用(Lua registry)
  62. int callback_refs[EASYLVGL_CALLBACK_MAX]; /**< 事件回调引用数组 */
  63. // 组件类型
  64. uint8_t component_type;
  65. // 私有数据
  66. void *user_data;
  67. };
  68. /**
  69. * Msgbox 私有数据
  70. */
  71. typedef struct {
  72. lv_timer_t *timeout_timer;
  73. } easylvgl_msgbox_data_t;
  74. /**
  75. * Textarea 私有数据
  76. */
  77. typedef struct {
  78. lv_obj_t *keyboard;
  79. } easylvgl_textarea_data_t;
  80. /**
  81. * Keyboard 私有数据
  82. */
  83. typedef struct {
  84. lv_obj_t *target;
  85. } easylvgl_keyboard_data_t;
  86. /**********************
  87. * GLOBAL PROTOTYPES
  88. **********************/
  89. /**
  90. * 分配组件元数据
  91. * @param ctx 上下文指针
  92. * @param obj LVGL 对象指针
  93. * @param component_type 组件类型
  94. * @return 元数据指针,失败返回 NULL
  95. */
  96. easylvgl_component_meta_t *easylvgl_component_meta_alloc(
  97. easylvgl_ctx_t *ctx,
  98. lv_obj_t *obj,
  99. easylvgl_component_type_t component_type);
  100. /**
  101. * 释放组件元数据
  102. * @param meta 元数据指针
  103. * @pre-condition meta 必须非空
  104. * @post-condition 元数据及关联资源已释放
  105. */
  106. void easylvgl_component_meta_free(easylvgl_component_meta_t *meta);
  107. /**
  108. * 捕获配置表中的回调函数
  109. * @param L Lua 状态
  110. * @param idx 配置表在栈中的索引
  111. * @param key 回调函数键名(如 "on_click")
  112. * @return Lua registry 引用,未找到返回 LUA_NOREF
  113. */
  114. int easylvgl_component_capture_callback(void *L, int idx, const char *key);
  115. /**
  116. * 调用 Lua 回调函数
  117. * @param meta 组件元数据
  118. * @param event_type 事件类型
  119. * @param L Lua 状态
  120. * @pre-condition meta 必须非空
  121. */
  122. void easylvgl_component_call_callback(
  123. easylvgl_component_meta_t *meta,
  124. easylvgl_event_type_t event_type,
  125. void *L);
  126. /**
  127. * 调用 Msgbox Action 回调(额外传递按钮文本)
  128. */
  129. void easylvgl_component_call_action_callback(
  130. easylvgl_component_meta_t *meta,
  131. const char *action_text);
  132. /**
  133. * 绑定组件事件
  134. * @param meta 组件元数据
  135. * @param event_type 事件类型
  136. * @param callback_ref Lua 回调引用
  137. * @return 0 成功,<0 失败
  138. */
  139. int easylvgl_component_bind_event(
  140. easylvgl_component_meta_t *meta,
  141. easylvgl_event_type_t event_type,
  142. int callback_ref);
  143. /**
  144. * 释放组件所有回调引用
  145. * @param meta 组件元数据
  146. * @param L Lua 状态
  147. */
  148. void easylvgl_component_release_callbacks(easylvgl_component_meta_t *meta, void *L);
  149. /**
  150. * 从配置表读取整数字段
  151. * @param L Lua 状态
  152. * @param idx 配置表索引
  153. * @param key 字段名
  154. * @param default_value 默认值
  155. * @return 整数值
  156. */
  157. int easylvgl_marshal_integer(void *L, int idx, const char *key, int default_value);
  158. /**
  159. * 获取表字段的长度(仅支持数组)
  160. */
  161. int easylvgl_marshal_table_length(void *L, int idx, const char *key);
  162. /**
  163. * 在表字段中获取指定位置的字符串
  164. */
  165. const char *easylvgl_marshal_table_string_at(void *L, int idx, const char *key, int position);
  166. /**
  167. * 从配置表读取布尔字段
  168. * @param L Lua 状态
  169. * @param idx 配置表索引
  170. * @param key 字段名
  171. * @param default_value 默认值
  172. * @return 布尔值
  173. */
  174. bool easylvgl_marshal_bool(void *L, int idx, const char *key, bool default_value);
  175. /**
  176. * 从配置表读取字符串字段
  177. * @param L Lua 状态
  178. * @param idx 配置表索引
  179. * @param key 字段名
  180. * @param default_value 默认值
  181. * @return 字符串指针(内部字符串,不需要释放),未找到返回 default_value
  182. */
  183. const char *easylvgl_marshal_string(void *L, int idx, const char *key, const char *default_value);
  184. /**
  185. * 从配置表读取父对象
  186. * @param L Lua 状态
  187. * @param idx 配置表索引
  188. * @return LVGL 父对象指针,未指定返回 NULL
  189. */
  190. lv_obj_t *easylvgl_marshal_parent(void *L, int idx);
  191. /**
  192. * 从配置表读取点坐标(用于 pivot 等)
  193. * @param L Lua 状态
  194. * @param idx 配置表索引
  195. * @param key 字段名
  196. * @param out 输出点坐标
  197. * @return true 成功读取,false 未找到或格式错误
  198. */
  199. bool easylvgl_marshal_point(void *L, int idx, const char *key, lv_point_t *out);
  200. /**
  201. * 从 LVGL 对象获取元数据
  202. * @param obj LVGL 对象指针
  203. * @return 元数据指针,未找到返回 NULL
  204. */
  205. easylvgl_component_meta_t *easylvgl_component_meta_get(lv_obj_t *obj);
  206. /**
  207. * Button 组件:从配置表创建
  208. */
  209. lv_obj_t *easylvgl_button_create_from_config(void *L, int idx);
  210. int easylvgl_button_set_text(lv_obj_t *btn, const char *text); //设置按钮文本
  211. int easylvgl_button_set_on_click(lv_obj_t *btn, int callback_ref); //设置点击回调
  212. /**
  213. * Label 组件:从配置表创建
  214. */
  215. lv_obj_t *easylvgl_label_create_from_config(void *L, int idx);
  216. int easylvgl_label_set_text(lv_obj_t *label, const char *text); //设置标签文本
  217. const char *easylvgl_label_get_text(lv_obj_t *label); //获取标签文本
  218. /**
  219. * Dropdown 组件创建
  220. */
  221. lv_obj_t *easylvgl_dropdown_create_from_config(void *L, int idx);
  222. int easylvgl_dropdown_set_selected(lv_obj_t *dropdown, int index); //设置下拉框选中项
  223. int easylvgl_dropdown_get_selected(lv_obj_t *dropdown); //获取下拉框选中项
  224. int easylvgl_dropdown_set_on_change(lv_obj_t *dropdown, int callback_ref); //设置改变回调
  225. /**
  226. * Switch 组件创建
  227. */
  228. lv_obj_t *easylvgl_switch_create_from_config(void *L, int idx);
  229. int easylvgl_switch_set_state(lv_obj_t *sw, bool checked); //设置开关状态
  230. bool easylvgl_switch_get_state(lv_obj_t *sw); //获取开关状态
  231. int easylvgl_switch_set_on_change(lv_obj_t *sw, int callback_ref); //设置改变回调
  232. /**
  233. * Container 组件创建
  234. */
  235. lv_obj_t *easylvgl_container_create_from_config(void *L, int idx);
  236. int easylvgl_container_set_color(lv_obj_t *container, uint32_t color); //设置背景颜色
  237. /**
  238. * Table 组件创建
  239. */
  240. lv_obj_t *easylvgl_table_create_from_config(void *L, int idx);
  241. int easylvgl_table_set_cell_text(lv_obj_t *table, uint16_t row, uint16_t col, const char *text); //设置单元格文本
  242. int easylvgl_table_set_col_width(lv_obj_t *table, uint16_t col, lv_coord_t width); //调整列宽
  243. /**
  244. * TabView 组件创建
  245. */
  246. lv_obj_t *easylvgl_tabview_create_from_config(void *L, int idx);
  247. int easylvgl_tabview_set_active(lv_obj_t *tabview, uint32_t idx); //激活某页
  248. lv_obj_t *easylvgl_tabview_get_content(lv_obj_t *tabview, int idx); //获取某页容器
  249. void easylvgl_tabview_release_data(easylvgl_component_meta_t *meta); //释放内部页容器数据
  250. /**
  251. * Msgbox 组件
  252. */
  253. lv_obj_t *easylvgl_msgbox_create_from_config(void *L, int idx);
  254. int easylvgl_msgbox_set_on_action(lv_obj_t *msgbox, int callback_ref); //设置消息框按钮回调
  255. int easylvgl_msgbox_show(lv_obj_t *msgbox); //显示消息框
  256. int easylvgl_msgbox_hide(lv_obj_t *msgbox); //隐藏消息框
  257. lv_timer_t *easylvgl_msgbox_release_user_data(easylvgl_component_meta_t *meta); //释放消息框用户数据(定时器等)
  258. /**
  259. * Image 组件
  260. */
  261. lv_obj_t *easylvgl_image_create_from_config(void *L, int idx);
  262. int easylvgl_image_set_src(lv_obj_t *img, const char *src); //设置图片源
  263. int easylvgl_image_set_zoom(lv_obj_t *img, int zoom); //设置缩放比例
  264. int easylvgl_image_set_opacity(lv_obj_t *img, int opacity); //设置透明度
  265. /**
  266. * Win 组件
  267. */
  268. lv_obj_t *easylvgl_win_create_from_config(void *L, int idx);
  269. int easylvgl_win_set_title(lv_obj_t *win, const char *title); //设置窗口标题
  270. int easylvgl_win_add_content(lv_obj_t *win, lv_obj_t *child); //添加子组件到内容容器
  271. /**
  272. * Textarea组件
  273. */
  274. lv_obj_t *easylvgl_textarea_create_from_config(void *L, int idx);
  275. int easylvgl_textarea_set_text(lv_obj_t *textarea, const char *text); //设置文本内容
  276. const char *easylvgl_textarea_get_text(lv_obj_t *textarea); //获取文本内容
  277. int easylvgl_textarea_set_cursor(lv_obj_t *textarea, uint32_t pos); //设置光标位置
  278. int easylvgl_textarea_set_on_text_change(lv_obj_t *textarea, int callback_ref); //设置文本改变回调
  279. int easylvgl_textarea_attach_keyboard(lv_obj_t *textarea, lv_obj_t *keyboard); //关联键盘
  280. lv_obj_t *easylvgl_textarea_get_keyboard(lv_obj_t *textarea); //获取关联键盘
  281. /**
  282. * Keyboard组件
  283. */
  284. lv_obj_t *easylvgl_keyboard_create_from_config(void *L, int idx);
  285. int easylvgl_keyboard_set_target(lv_obj_t *keyboard, lv_obj_t *textarea); //设置目标组件
  286. int easylvgl_keyboard_show(lv_obj_t *keyboard); //显示键盘
  287. int easylvgl_keyboard_hide(lv_obj_t *keyboard); //隐藏键盘
  288. int easylvgl_keyboard_set_on_commit(lv_obj_t *keyboard, int callback_ref); //设置提交回调
  289. int easylvgl_keyboard_set_layout(lv_obj_t *keyboard, const char *layout); //设置键盘布局
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif /* EASYLVGL_COMPONENT_H */