luat_lib_lvgl_struct.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "luat_base.h"
  2. #include "luat_lvgl.h"
  3. #include "luat_zbuff.h"
  4. #include "lvgl.h"
  5. #define META_LV_ANIM "LV_ANIM*"
  6. #define META_LV_AREA "LV_AREA*"
  7. #define META_LV_CALENDAR_DATE_T "LV_CALENDAR_DATE_T*"
  8. #define META_LV_DRAW_RECT_DSC_T "LV_DRAW_RECT_DSC_T*"
  9. #define META_LV_DRAW_LABEL_DSC_T "LV_DRAW_LABEL_DSC_T*"
  10. #define META_LV_DRAW_IMG_DSC_T "LV_DRAW_IMG_DSC_T*"
  11. #define META_LV_IMG_DSC_T "LV_IMG_DSC_T*"
  12. //---------------------------------------------
  13. /*
  14. 创建一个anim
  15. @api lvgl.anim_t()
  16. @return userdata anim指针
  17. @usage
  18. local anim = lvgl.anim_t()
  19. */
  20. int luat_lv_struct_anim_t(lua_State *L) {
  21. lua_newuserdata(L, sizeof(lv_anim_t));
  22. luaL_setmetatable(L, META_LV_ANIM);
  23. return 1;
  24. }
  25. int _lvgl_struct_anim_t_newindex(lua_State *L) {
  26. lv_anim_t* anim = (lv_anim_t*)lua_touserdata(L, 1);
  27. const char* key = luaL_checkstring(L, 2);
  28. if (!strcmp("start", key)) {
  29. anim->start = luaL_optinteger(L, 3, 0);
  30. }
  31. else if (!strcmp("end", key)) {
  32. anim->end = luaL_optinteger(L, 3, 0);
  33. }
  34. else if (!strcmp("current", key)) {
  35. anim->current = luaL_optinteger(L, 3, 0);
  36. }
  37. else if (!strcmp("time", key)) {
  38. anim->time = luaL_optinteger(L, 3, 0);
  39. }
  40. else if (!strcmp("act_time", key)) {
  41. anim->act_time = luaL_optinteger(L, 3, 0);
  42. }
  43. else if (!strcmp("playback_delay", key)) {
  44. anim->playback_delay = luaL_optinteger(L, 3, 0);
  45. }
  46. else if (!strcmp("playback_time", key)) {
  47. anim->playback_time = luaL_optinteger(L, 3, 0);
  48. }
  49. else if (!strcmp("repeat_delay", key)) {
  50. anim->repeat_delay = luaL_optinteger(L, 3, 0);
  51. }
  52. else if (!strcmp("repeat_cnt", key)) {
  53. anim->repeat_cnt = luaL_optinteger(L, 3, 0);
  54. }
  55. else if (!strcmp("early_apply", key)) {
  56. anim->early_apply = luaL_optinteger(L, 3, 0);
  57. }
  58. return 0;
  59. }
  60. //---------------------------------------------
  61. /*
  62. 创建一个area
  63. @api lvgl.area_t()
  64. @return userdata area指针
  65. @usage
  66. local area = lvgl.area_t()
  67. */
  68. int luat_lv_struct_area_t(lua_State *L) {
  69. lua_newuserdata(L, sizeof(lv_area_t));
  70. luaL_setmetatable(L, META_LV_AREA);
  71. return 1;
  72. }
  73. int _lvgl_struct_area_t_newindex(lua_State *L) {
  74. lv_area_t* area = (lv_area_t*)lua_touserdata(L, 1);
  75. const char* key = luaL_checkstring(L, 2);
  76. if (!strcmp("x1", key)) {
  77. area->x1 = luaL_optinteger(L, 3, 0);
  78. }
  79. else if (!strcmp("x2", key)) {
  80. area->x2 = luaL_optinteger(L, 3, 0);
  81. }
  82. else if (!strcmp("y1", key)) {
  83. area->y1 = luaL_optinteger(L, 3, 0);
  84. }
  85. else if (!strcmp("y2", key)) {
  86. area->y2 = luaL_optinteger(L, 3, 0);
  87. }
  88. return 0;
  89. }
  90. //---------------------------------------------
  91. /*
  92. 创建一个calendar_date_t
  93. @api lvgl.calendar_date_t()
  94. @return userdata calendar_date_t指针
  95. @usage
  96. local today = lvgl.calendar_date_t()
  97. */
  98. int luat_lv_calendar_date_t(lua_State *L){
  99. lua_newuserdata(L, sizeof(lv_calendar_date_t));
  100. luaL_setmetatable(L, META_LV_CALENDAR_DATE_T);
  101. return 1;
  102. }
  103. int _lvgl_struct_calendar_date_t_newindex(lua_State *L) {
  104. lv_calendar_date_t* date_t = (lv_calendar_date_t*)lua_touserdata(L, 1);
  105. const char* key = luaL_checkstring(L, 2);
  106. if (!strcmp("year", key)) {
  107. date_t->year = luaL_optinteger(L, 3, 0);
  108. }
  109. else if (!strcmp("month", key)) {
  110. date_t->month = luaL_optinteger(L, 3, 0);
  111. }
  112. else if (!strcmp("day", key)) {
  113. date_t->day = luaL_optinteger(L, 3, 0);
  114. }
  115. return 0;
  116. }
  117. //---------------------------------------------
  118. /*
  119. 创建一个lv_draw_rect_dsc_t
  120. @api lvgl.draw_rect_dsc_t()
  121. @return userdata lv_draw_rect_dsc_t指针
  122. @usage
  123. local rect_dsc = lvgl.draw_rect_dsc_t()
  124. */
  125. int luat_lv_draw_rect_dsc_t(lua_State *L){
  126. lua_newuserdata(L, sizeof(lv_draw_rect_dsc_t));
  127. luaL_setmetatable(L, META_LV_DRAW_RECT_DSC_T);
  128. return 1;
  129. }
  130. int _lvgl_struct_draw_rect_dsc_t_newindex(lua_State *L) {
  131. lv_draw_rect_dsc_t* rect_dsc = (lv_draw_rect_dsc_t*)lua_touserdata(L, 1);
  132. const char* key = luaL_checkstring(L, 2);
  133. if (!strcmp("radius", key)) {
  134. rect_dsc->radius = luaL_optinteger(L, 3, 0);
  135. }
  136. /*Background*/
  137. else if (!strcmp("bg_color", key)) {
  138. rect_dsc->bg_color.full = luaL_optinteger(L, 3, 0);
  139. }
  140. else if (!strcmp("bg_grad_color", key)) {
  141. rect_dsc->bg_grad_color.full = luaL_optinteger(L, 3, 0);
  142. }
  143. else if (!strcmp("bg_grad_dir", key)) {
  144. rect_dsc->bg_grad_dir = luaL_optinteger(L, 3, 0);
  145. }
  146. else if (!strcmp("bg_main_color_stop", key)) {
  147. rect_dsc->bg_main_color_stop = luaL_optinteger(L, 3, 0);
  148. }
  149. else if (!strcmp("bg_grad_color_stop", key)) {
  150. rect_dsc->bg_grad_color_stop = luaL_optinteger(L, 3, 0);
  151. }
  152. else if (!strcmp("bg_opa", key)) {
  153. rect_dsc->bg_opa = luaL_optinteger(L, 3, 0);
  154. }
  155. else if (!strcmp("bg_blend_mode", key)) {
  156. rect_dsc->bg_blend_mode = luaL_optinteger(L, 3, 0);
  157. }
  158. /*Border*/
  159. else if (!strcmp("border_color", key)) {
  160. rect_dsc->border_color.full = luaL_optinteger(L, 3, 0);
  161. }
  162. else if (!strcmp("border_width", key)) {
  163. rect_dsc->border_width = luaL_optinteger(L, 3, 0);
  164. }
  165. else if (!strcmp("border_side", key)) {
  166. rect_dsc->border_side = luaL_optinteger(L, 3, 0);
  167. }
  168. else if (!strcmp("border_opa", key)) {
  169. rect_dsc->border_opa = luaL_optinteger(L, 3, 0);
  170. }
  171. else if (!strcmp("border_blend_mode", key)) {
  172. rect_dsc->border_blend_mode = luaL_optinteger(L, 3, 0);
  173. }
  174. else if (!strcmp("border_post", key)) {
  175. rect_dsc->border_post = luaL_optinteger(L, 3, 1);
  176. }
  177. /*Outline*/
  178. else if (!strcmp("outline_color", key)) {
  179. rect_dsc->outline_color.full = luaL_optinteger(L, 3, 0);
  180. }
  181. else if (!strcmp("outline_width", key)) {
  182. rect_dsc->outline_width = luaL_optinteger(L, 3, 0);
  183. }
  184. else if (!strcmp("outline_pad", key)) {
  185. rect_dsc->outline_pad = luaL_optinteger(L, 3, 0);
  186. }
  187. else if (!strcmp("outline_opa", key)) {
  188. rect_dsc->outline_opa = luaL_optinteger(L, 3, 0);
  189. }
  190. else if (!strcmp("outline_blend_mode", key)) {
  191. rect_dsc->outline_blend_mode = luaL_optinteger(L, 3, 0);
  192. }
  193. /*Shadow*/
  194. else if (!strcmp("shadow_color", key)) {
  195. rect_dsc->shadow_color.full = luaL_optinteger(L, 3, 0);
  196. }
  197. else if (!strcmp("shadow_width", key)) {
  198. rect_dsc->shadow_width = luaL_optinteger(L, 3, 0);
  199. }
  200. else if (!strcmp("shadow_ofs_x", key)) {
  201. rect_dsc->shadow_ofs_x = luaL_optinteger(L, 3, 0);
  202. }
  203. else if (!strcmp("shadow_ofs_y", key)) {
  204. rect_dsc->shadow_ofs_y = luaL_optinteger(L, 3, 0);
  205. }
  206. else if (!strcmp("shadow_spread", key)) {
  207. rect_dsc->shadow_spread = luaL_optinteger(L, 3, 0);
  208. }
  209. else if (!strcmp("shadow_opa", key)) {
  210. rect_dsc->shadow_opa = luaL_optinteger(L, 3, 0);
  211. }
  212. else if (!strcmp("shadow_blend_mode", key)) {
  213. rect_dsc->shadow_blend_mode = luaL_optinteger(L, 3, 0);
  214. }
  215. /*Pattern*/
  216. else if (!strcmp("pattern_image", key)) {
  217. rect_dsc->pattern_image = luaL_optstring(L, 3, NULL);
  218. }
  219. else if (!strcmp("pattern_font", key)) {
  220. rect_dsc->pattern_font = luaL_optstring(L, 3, NULL);
  221. }
  222. else if (!strcmp("pattern_recolor", key)) {
  223. rect_dsc->pattern_recolor.full = luaL_optinteger(L, 3, 0);
  224. }
  225. else if (!strcmp("pattern_opa", key)) {
  226. rect_dsc->pattern_opa = luaL_optinteger(L, 3, 0);
  227. }
  228. else if (!strcmp("pattern_recolor_opa", key)) {
  229. rect_dsc->pattern_recolor_opa = luaL_optinteger(L, 3, 0);
  230. }
  231. else if (!strcmp("pattern_repeat", key)) {
  232. rect_dsc->pattern_repeat = luaL_optinteger(L, 3, 1);
  233. }
  234. else if (!strcmp("pattern_blend_mode", key)) {
  235. rect_dsc->pattern_blend_mode = luaL_optinteger(L, 3, 0);
  236. }
  237. /*Value*/
  238. else if (!strcmp("value_str", key)) {
  239. rect_dsc->value_str = luaL_optstring(L, 3, NULL);
  240. }
  241. else if (!strcmp("value_font", key)) {
  242. rect_dsc->value_font = luaL_optstring(L, 3, NULL);
  243. }
  244. else if (!strcmp("value_opa", key)) {
  245. rect_dsc->value_opa = luaL_optinteger(L, 3, 0);
  246. }
  247. else if (!strcmp("value_color", key)) {
  248. rect_dsc->value_color.full = luaL_optinteger(L, 3, 0);
  249. }
  250. else if (!strcmp("value_ofs_x", key)) {
  251. rect_dsc->value_ofs_x = luaL_optinteger(L, 3, 0);
  252. }
  253. else if (!strcmp("value_ofs_y", key)) {
  254. rect_dsc->value_ofs_y = luaL_optinteger(L, 3, 0);
  255. }
  256. else if (!strcmp("value_letter_space", key)) {
  257. rect_dsc->value_letter_space = luaL_optinteger(L, 3, 0);
  258. }
  259. else if (!strcmp("value_line_space", key)) {
  260. rect_dsc->value_line_space = luaL_optinteger(L, 3, 0);
  261. }
  262. else if (!strcmp("value_align", key)) {
  263. rect_dsc->value_align = luaL_optinteger(L, 3, 0);
  264. }
  265. else if (!strcmp("value_blend_mode", key)) {
  266. rect_dsc->value_blend_mode = luaL_optinteger(L, 3, 0);
  267. }
  268. return 0;
  269. }
  270. //---------------------------------------------
  271. /*
  272. 创建一个lv_draw_label_dsc_t
  273. @api lvgl.draw_label_dsc_t()
  274. @return userdata lv_draw_label_dsc_t指针
  275. @usage
  276. local rect_dsc = lvgl.draw_label_dsc_t()
  277. */
  278. int luat_lv_draw_label_dsc_t(lua_State *L){
  279. lua_newuserdata(L, sizeof(lv_draw_label_dsc_t));
  280. luaL_setmetatable(L, META_LV_DRAW_LABEL_DSC_T);
  281. return 1;
  282. }
  283. int _lvgl_struct_draw_label_dsc_t_newindex(lua_State *L) {
  284. lv_draw_label_dsc_t* label_dsc = (lv_draw_label_dsc_t*)lua_touserdata(L, 1);
  285. const char* key = luaL_checkstring(L, 2);
  286. if (!strcmp("color", key)) {
  287. label_dsc->color.full = luaL_optinteger(L, 3, 0);
  288. }
  289. else if (!strcmp("sel_color", key)) {
  290. label_dsc->sel_color.full = luaL_optinteger(L, 3, 0);
  291. }
  292. else if (!strcmp("sel_bg_color", key)) {
  293. label_dsc->sel_bg_color.full = luaL_optinteger(L, 3, 0);
  294. }
  295. else if (!strcmp("font", key)) {
  296. label_dsc->font = luaL_optstring(L, 3, NULL);
  297. }
  298. else if (!strcmp("opa", key)) {
  299. label_dsc->opa = luaL_optinteger(L, 3, 0);
  300. }
  301. else if (!strcmp("line_space", key)) {
  302. label_dsc->line_space = luaL_optinteger(L, 3, 0);
  303. }
  304. else if (!strcmp("letter_space", key)) {
  305. label_dsc->letter_space = luaL_optinteger(L, 3, 0);
  306. }
  307. else if (!strcmp("sel_start", key)) {
  308. label_dsc->sel_start = luaL_optinteger(L, 3, 0);
  309. }
  310. else if (!strcmp("sel_end", key)) {
  311. label_dsc->sel_end = luaL_optinteger(L, 3, 0);
  312. }
  313. else if (!strcmp("ofs_x", key)) {
  314. label_dsc->ofs_x = luaL_optinteger(L, 3, 0);
  315. }
  316. else if (!strcmp("ofs_y", key)) {
  317. label_dsc->ofs_y = luaL_optinteger(L, 3, 0);
  318. }
  319. else if (!strcmp("bidi_dir", key)) {
  320. label_dsc->bidi_dir = luaL_optinteger(L, 3, 0);
  321. }
  322. else if (!strcmp("flag", key)) {
  323. label_dsc->flag = luaL_optinteger(L, 3, 0);
  324. }
  325. else if (!strcmp("decor", key)) {
  326. label_dsc->decor = luaL_optinteger(L, 3, 0);
  327. }
  328. else if (!strcmp("blend_mode", key)) {
  329. label_dsc->blend_mode = luaL_optinteger(L, 3, 0);
  330. }
  331. return 0;
  332. }
  333. //---------------------------------------------
  334. /*
  335. 创建一个lv_draw_img_dsc_t
  336. @api lvgl.draw_img_dsc_t()
  337. @return userdata lv_draw_img_dsc_t指针
  338. @usage
  339. local draw_img_dsc_t = lvgl.draw_img_dsc_t()
  340. */
  341. int luat_lv_draw_img_dsc_t(lua_State *L){
  342. lua_newuserdata(L, sizeof(lv_draw_img_dsc_t));
  343. luaL_setmetatable(L, META_LV_DRAW_IMG_DSC_T);
  344. return 1;
  345. }
  346. int _lvgl_struct_draw_img_dsc_t_newindex(lua_State *L) {
  347. lv_draw_img_dsc_t* draw_img_dsc_t = (lv_draw_img_dsc_t*)lua_touserdata(L, 1);
  348. const char* key = luaL_checkstring(L, 2);
  349. if (!strcmp("opa", key)) {
  350. draw_img_dsc_t->opa = luaL_optinteger(L, 3, 0);
  351. }
  352. else if (!strcmp("angle", key)) {
  353. draw_img_dsc_t->angle = luaL_optinteger(L, 3, 0);
  354. }
  355. // else if (!strcmp("pivot", key)) {
  356. // img_dsc_t->pivot = (lv_point_t)luaL_optinteger(L, 3, 0);
  357. // }
  358. else if (!strcmp("zoom", key)) {
  359. draw_img_dsc_t->zoom = luaL_optinteger(L, 3, 0);
  360. }
  361. else if (!strcmp("recolor", key)) {
  362. draw_img_dsc_t->recolor.full = luaL_optinteger(L, 3, 0);
  363. }
  364. else if (!strcmp("blend_mode", key)) {
  365. draw_img_dsc_t->blend_mode = luaL_optinteger(L, 3, 0);
  366. }
  367. else if (!strcmp("antialias", key)) {
  368. draw_img_dsc_t->antialias = luaL_optinteger(L, 3, 1);
  369. }
  370. return 0;
  371. }
  372. //---------------------------------------------
  373. /*
  374. 创建一个lv_img_dsc_t
  375. @api lvgl.draw_img_dsc_t()
  376. @return userdata lv_img_dsc_t指针
  377. @usage
  378. local img_dsc_t = lvgl.img_dsc_t()
  379. */
  380. int luat_lv_img_dsc_t(lua_State *L){
  381. lua_newuserdata(L, sizeof(lv_img_dsc_t));
  382. luaL_setmetatable(L, META_LV_IMG_DSC_T);
  383. return 1;
  384. }
  385. int _lvgl_struct_img_dsc_t_newindex(lua_State *L) {
  386. lv_img_dsc_t* img_dsc_t = (lv_img_dsc_t*)lua_touserdata(L, 1);
  387. const char* key = luaL_checkstring(L, 2);
  388. if (!strcmp("header", key)) {
  389. // img_dsc_t->header = luaL_optinteger(L, 3, 0);
  390. }
  391. else if (!strcmp("data_size", key)) {
  392. img_dsc_t->data_size = luaL_optinteger(L, 3, 0);
  393. }
  394. else if (!strcmp("data", key)) {
  395. luat_zbuff_t* cbuff = (luat_zbuff_t *)luaL_checkudata(L, 3, "ZBUFF*");
  396. img_dsc_t->data = cbuff->addr;
  397. }
  398. else if (!strcmp("header_cf", key)) {
  399. img_dsc_t->header.cf = luaL_optinteger(L, 3, 5);
  400. }
  401. else if (!strcmp("header_w", key)) {
  402. img_dsc_t->header.w = luaL_optinteger(L, 3, 11);
  403. }
  404. else if (!strcmp("header_h", key)) {
  405. img_dsc_t->header.h = luaL_optinteger(L, 3, 11);
  406. }
  407. return 0;
  408. }
  409. //--------------------------------------------
  410. void luat_lvgl_struct_init(lua_State *L) {
  411. // lv_anim*
  412. luaL_newmetatable(L, META_LV_ANIM);
  413. lua_pushcfunction(L, _lvgl_struct_anim_t_newindex);
  414. lua_setfield( L, -2, "__newindex" );
  415. lua_pop(L, 1);
  416. // lv_area
  417. luaL_newmetatable(L, META_LV_AREA);
  418. lua_pushcfunction(L, _lvgl_struct_area_t_newindex);
  419. lua_setfield( L, -2, "__newindex" );
  420. lua_pop(L, 1);
  421. // lv_calendar_date_t
  422. luaL_newmetatable(L, META_LV_CALENDAR_DATE_T);
  423. lua_pushcfunction(L, _lvgl_struct_calendar_date_t_newindex);
  424. lua_setfield( L, -2, "__newindex" );
  425. lua_pop(L, 1);
  426. // lv_draw_rect_dsc_t
  427. luaL_newmetatable(L, META_LV_DRAW_RECT_DSC_T);
  428. lua_pushcfunction(L, _lvgl_struct_draw_rect_dsc_t_newindex);
  429. lua_setfield( L, -2, "__newindex" );
  430. lua_pop(L, 1);
  431. // draw_label_dsc_t
  432. luaL_newmetatable(L, META_LV_DRAW_LABEL_DSC_T);
  433. lua_pushcfunction(L, _lvgl_struct_draw_label_dsc_t_newindex);
  434. lua_setfield( L, -2, "__newindex" );
  435. lua_pop(L, 1);
  436. // lv_draw_img_dsc_t
  437. luaL_newmetatable(L, META_LV_DRAW_IMG_DSC_T);
  438. lua_pushcfunction(L, _lvgl_struct_draw_img_dsc_t_newindex);
  439. lua_setfield( L, -2, "__newindex" );
  440. lua_pop(L, 1);
  441. // lv_img_dsc_t
  442. luaL_newmetatable(L, META_LV_IMG_DSC_T);
  443. lua_pushcfunction(L, _lvgl_struct_img_dsc_t_newindex);
  444. lua_setfield( L, -2, "__newindex" );
  445. lua_pop(L, 1);
  446. }