From 55a760672223a26e6ba3be5523246fbac795bb11 Mon Sep 17 00:00:00 2001 From: Neo Xu Date: Mon, 5 Aug 2024 18:00:11 +0800 Subject: [PATCH] feat(example): add userdata example Similar to lvgl user_data, the lua lvgl obj also have user_data for user. It uses userdata's uservalue for this purpose. Signed-off-by: Neo Xu --- examples/examples.lua | 1 + examples/userdata.lua | 29 +++++++++++++++++++++++++++++ examples/uservalue.lua | 34 ---------------------------------- 3 files changed, 30 insertions(+), 34 deletions(-) create mode 100644 examples/userdata.lua delete mode 100644 examples/uservalue.lua diff --git a/examples/examples.lua b/examples/examples.lua index bfbc4e6..e64c15c 100644 --- a/examples/examples.lua +++ b/examples/examples.lua @@ -43,5 +43,6 @@ createBtn(container, "keyboard") createBtn(container, "animation") createBtn(container, "pointer") createBtn(container, "analogTime") +createBtn(container, "userdata") createBtn(container, "flappyBird/flappyBird") createBtn(container, "tests") diff --git a/examples/userdata.lua b/examples/userdata.lua new file mode 100644 index 0000000..755b4c8 --- /dev/null +++ b/examples/userdata.lua @@ -0,0 +1,29 @@ +-- user_data can store any type of lua value +local obj1 = lvgl.Label { + text_color = "#333", + border_color = "#f00", + border_width = 1, + align = lvgl.ALIGN.TOP_LEFT +} + +obj1.user_data = 123.456 +obj1.text = "user_data:\n" .. obj1.user_data + +-- use user_data to store table +local obj2 = lvgl.Label { + text_color = "#333", + border_color = "#f00", + border_width = 1, + align = lvgl.ALIGN.TOP_RIGHT +} + +obj2.user_data = { + abc = "hello", + def = "world", + [1] = 123, +} +obj2.text = "user_data:" +for k, v in pairs(obj2.user_data) do + print(k, v) + obj2.text = obj2.text .. string.format("%s: %s\n", k, v) +end diff --git a/examples/uservalue.lua b/examples/uservalue.lua deleted file mode 100644 index 414d8f0..0000000 --- a/examples/uservalue.lua +++ /dev/null @@ -1,34 +0,0 @@ -collectgarbage("collect") -print("initial: ", collectgarbage("count")) - -local str_t = {} -for i = 1, 1024 * 1024 do - str_t[#str_t + 1] = 'a' -end - -local str = table.concat(str_t) -str_t = nil - -collectgarbage("collect") -collectgarbage("collect") -print("after string collect: ", collectgarbage("count")) - -for i = 1, 10 do - local label = lvgl.Label(nil) - label:set_text_static(str) - label:delete() -end -str = nil - -print("after set text: ", collectgarbage("count")) -collectgarbage("collect") -print("after collect: ", collectgarbage("count")) -print("again: ", collectgarbage("count")) -collectgarbage("collect") -print("again2: ", collectgarbage("count")) -collectgarbage("collect") -print("again3: ", collectgarbage("count")) - -local label = lvgl.Label(nil) -label:set({ text = "Test Done" }) -label:center()