summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/widgets
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-24 15:13:10 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-24 15:13:10 +1100
commit7c6eb2997cbba350e7384151e13659271508e08f (patch)
treeb6f95a4843521e69b24cbf4c126d84442d19fc23 /lib/luavgl/src/widgets
parent230721cd6271f3239b42e1d2471f8db15bebd712 (diff)
downloadtangara-fw-7c6eb2997cbba350e7384151e13659271508e08f.tar.gz
Migrate 'now playing' screen to lua
Diffstat (limited to 'lib/luavgl/src/widgets')
-rw-r--r--lib/luavgl/src/widgets/bar.c87
-rw-r--r--lib/luavgl/src/widgets/widgets.c12
2 files changed, 99 insertions, 0 deletions
diff --git a/lib/luavgl/src/widgets/bar.c b/lib/luavgl/src/widgets/bar.c
new file mode 100644
index 00000000..bab38aae
--- /dev/null
+++ b/lib/luavgl/src/widgets/bar.c
@@ -0,0 +1,87 @@
+#include "luavgl.h"
+#include "private.h"
+#include <src/misc/lv_anim.h>
+#include <src/widgets/lv_bar.h>
+
+static int luavgl_bar_create(lua_State *L)
+{
+ return luavgl_obj_create_helper(L, lv_bar_create);
+}
+
+static void _lv_bar_set_range(void *obj, lua_State *L)
+{
+ int min=0, max=100;
+
+ int type = lua_type(L, -1);
+ if (type == LUA_TTABLE) {
+ lua_getfield(L, -1, "min");
+ min = lua_tointeger(L, -1);
+ lua_pop(L, 1);
+ lua_getfield(L, -1, "max");
+ max = luavgl_tointeger(L, -1);
+ lua_pop(L, 1);
+ }
+
+ lv_bar_set_range(obj, min, max);
+}
+
+static void _lv_bar_set_value(void *obj, int value)
+{
+ lv_bar_set_value(obj, value, LV_ANIM_OFF);
+}
+
+static const luavgl_value_setter_t bar_property_table[] = {
+ {"range", SETTER_TYPE_STACK, {.setter_stack = _lv_bar_set_range}},
+ {"value", SETTER_TYPE_INT, {.setter = (setter_int_t)_lv_bar_set_value}},
+};
+
+LUALIB_API int luavgl_bar_set_property_kv(lua_State *L, void *data)
+{
+ lv_obj_t *obj = data;
+ int ret = luavgl_set_property(L, obj, bar_property_table);
+
+ if (ret == 0) {
+ return 0;
+ }
+ /* a base obj property? */
+ ret = luavgl_obj_set_property_kv(L, obj);
+ if (ret != 0) {
+ debug("unkown property for bar.\n");
+ }
+
+ return ret;
+}
+
+static int luavgl_bar_set(lua_State *L)
+{
+ lv_obj_t *obj = luavgl_to_obj(L, 1);
+
+ if (!lua_istable(L, -1)) {
+ luaL_error(L, "expect a table on 2nd para.");
+ return 0;
+ }
+
+ luavgl_iterate(L, -1, luavgl_bar_set_property_kv, obj);
+
+ return 0;
+}
+
+static int luavgl_bar_tostring(lua_State *L)
+{
+ lv_obj_t *obj = luavgl_to_obj(L, 1);
+ lua_pushfstring(L, "lv_bar:%p", obj);
+ return 1;
+}
+
+static const luaL_Reg luavgl_bar_methods[] = {
+ {"set", luavgl_bar_set },
+ {NULL, NULL },
+};
+
+static void luavgl_bar_init(lua_State *L)
+{
+ luavgl_obj_newmetatable(L, &lv_bar_class, "lv_bar", luavgl_bar_methods);
+ lua_pushcfunction(L, luavgl_bar_tostring);
+ lua_setfield(L, -2, "__tostring");
+ lua_pop(L, 1);
+}
diff --git a/lib/luavgl/src/widgets/widgets.c b/lib/luavgl/src/widgets/widgets.c
index 19b789a7..e5f64f23 100644
--- a/lib/luavgl/src/widgets/widgets.c
+++ b/lib/luavgl/src/widgets/widgets.c
@@ -1,6 +1,10 @@
#include "luavgl.h"
#include "private.h"
+#if LV_USE_BAR
+#include "bar.c"
+#endif
+
#if LV_USE_BTN
#include "btn.c"
#endif
@@ -50,6 +54,10 @@ static int luavgl_obj_create(lua_State *L);
static const luaL_Reg widget_create_methods[] = {
{"Object", luavgl_obj_create },
+#if LV_USE_BAR
+ {"Bar", luavgl_bar_create},
+#endif
+
#if LV_USE_BTN
{"Button", luavgl_btn_create},
#endif
@@ -142,4 +150,8 @@ static void luavgl_widgets_init(lua_State *L)
luavgl_btn_init(L);
#endif
+#if LV_USE_BAR
+ luavgl_bar_init(L);
+#endif
+
}