summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua_scripts/submenu_demo.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua_scripts/submenu_demo.lua')
-rw-r--r--apps/plugins/lua_scripts/submenu_demo.lua44
1 files changed, 36 insertions, 8 deletions
diff --git a/apps/plugins/lua_scripts/submenu_demo.lua b/apps/plugins/lua_scripts/submenu_demo.lua
index 75fec11979..8eac825474 100644
--- a/apps/plugins/lua_scripts/submenu_demo.lua
+++ b/apps/plugins/lua_scripts/submenu_demo.lua
@@ -13,17 +13,17 @@ end
local function ITEM_MENU()
local function flung(i, menu_t, func_t)
- local parent = get_parent() or 0
+ local parent = submenu_get_parent() or 0
rb.splash(100, "flung " .. (menu_t[parent] or "?"))
end
local function foo(i, menu_t, func_t)
- local parent = get_parent() or 0
+ local parent = submenu_get_parent() or 0
rb.splash(100, "FOO " .. menu_t[parent])
end
local function far(i, menu_t, func_t)
- local parent = get_parent() or 0
+ local parent = submenu_get_parent() or 0
rb.splash(100, "far" .. menu_t[parent])
end
@@ -32,16 +32,44 @@ local function ITEM_MENU()
end
local function USERITEMS()
+ local lv = 2
+ local mt = {"Item_1", "Item_2", "Item_3"}
+ local ft = {}
- return {"Item_1", "Item_2", "Item_3"},
- {create_sub_menu(2, ITEM_MENU()), create_sub_menu(2, ITEM_MENU()),
- create_sub_menu(2, ITEM_MENU()), function() end}
+ local function insert_item(i, name, func) --closure
+ submenu_insert(mt, i, name)
+ submenu_insert(ft, i, func)
+ end
+
+ for i = 1, #mt, 1 do
+ ft[i] = submenu_create(lv, ITEM_MENU())
+ end
+
+ local function add_new(i, menu_t, func_t)
+ local parent, lv = submenu_get_parent(lv - 1)
+ local last = #mt
+ local name = "Item_" .. tostring(last)
+ local func = submenu_create(lv + 1, ITEM_MENU())
+
+ local lv_out, item_out, removed = submenu_collapse(parent, lv + 1)-- collapse others
+ submenu_collapse(parent, lv) -- collapse the parent
+
+ insert_item(last, name, func)
+
+ func_t[parent](parent, menu_t, func_t) -- reopen parent
+ menu_ctx.start = i - removed
+ return true
+ end
+
+ local next = #mt + 1
+ insert_item(next, "Add New", add_new)
+ return mt, ft
end
local function MAIN_MENU()
local function go_back(i, m, f)
- local parent = get_parent() or 0
+ local parent = submenu_get_parent() or 0
if parent > 0 then
f[parent](parent, m, f)
else
@@ -60,7 +88,7 @@ local function MAIN_MENU()
local ft = {
[0] = go_back, --if user cancels do this function
[1] = false, -- shouldn't happen title occupies this slot
- [2] = create_sub_menu(1, USERITEMS()),
+ [2] = submenu_create(1, USERITEMS()),
[3] = go_back,
}
return mt, ft, get_ctx_menu