summaryrefslogtreecommitdiff
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
13local function ITEM_MENU() 13local function ITEM_MENU()
14 14
15 local function flung(i, menu_t, func_t) 15 local function flung(i, menu_t, func_t)
16 local parent = get_parent() or 0 16 local parent = submenu_get_parent() or 0
17 rb.splash(100, "flung " .. (menu_t[parent] or "?")) 17 rb.splash(100, "flung " .. (menu_t[parent] or "?"))
18 end 18 end
19 19
20 local function foo(i, menu_t, func_t) 20 local function foo(i, menu_t, func_t)
21 local parent = get_parent() or 0 21 local parent = submenu_get_parent() or 0
22 rb.splash(100, "FOO " .. menu_t[parent]) 22 rb.splash(100, "FOO " .. menu_t[parent])
23 end 23 end
24 24
25 local function far(i, menu_t, func_t) 25 local function far(i, menu_t, func_t)
26 local parent = get_parent() or 0 26 local parent = submenu_get_parent() or 0
27 rb.splash(100, "far" .. menu_t[parent]) 27 rb.splash(100, "far" .. menu_t[parent])
28 end 28 end
29 29
@@ -32,16 +32,44 @@ local function ITEM_MENU()
32end 32end
33 33
34local function USERITEMS() 34local function USERITEMS()
35 local lv = 2
36 local mt = {"Item_1", "Item_2", "Item_3"}
37 local ft = {}
35 38
36 return {"Item_1", "Item_2", "Item_3"}, 39 local function insert_item(i, name, func) --closure
37 {create_sub_menu(2, ITEM_MENU()), create_sub_menu(2, ITEM_MENU()), 40 submenu_insert(mt, i, name)
38 create_sub_menu(2, ITEM_MENU()), function() end} 41 submenu_insert(ft, i, func)
42 end
43
44 for i = 1, #mt, 1 do
45 ft[i] = submenu_create(lv, ITEM_MENU())
46 end
47
48 local function add_new(i, menu_t, func_t)
49 local parent, lv = submenu_get_parent(lv - 1)
50 local last = #mt
51 local name = "Item_" .. tostring(last)
52 local func = submenu_create(lv + 1, ITEM_MENU())
53
54 local lv_out, item_out, removed = submenu_collapse(parent, lv + 1)-- collapse others
55 submenu_collapse(parent, lv) -- collapse the parent
56
57 insert_item(last, name, func)
58
59 func_t[parent](parent, menu_t, func_t) -- reopen parent
60 menu_ctx.start = i - removed
61 return true
62 end
63
64 local next = #mt + 1
65 insert_item(next, "Add New", add_new)
66 return mt, ft
39end 67end
40 68
41local function MAIN_MENU() 69local function MAIN_MENU()
42 70
43 local function go_back(i, m, f) 71 local function go_back(i, m, f)
44 local parent = get_parent() or 0 72 local parent = submenu_get_parent() or 0
45 if parent > 0 then 73 if parent > 0 then
46 f[parent](parent, m, f) 74 f[parent](parent, m, f)
47 else 75 else
@@ -60,7 +88,7 @@ local function MAIN_MENU()
60 local ft = { 88 local ft = {
61 [0] = go_back, --if user cancels do this function 89 [0] = go_back, --if user cancels do this function
62 [1] = false, -- shouldn't happen title occupies this slot 90 [1] = false, -- shouldn't happen title occupies this slot
63 [2] = create_sub_menu(1, USERITEMS()), 91 [2] = submenu_create(1, USERITEMS()),
64 [3] = go_back, 92 [3] = go_back,
65 } 93 }
66 return mt, ft, get_ctx_menu 94 return mt, ft, get_ctx_menu