diff options
author | William Wilgus <wilgus.william@gmail.com> | 2021-04-29 01:56:49 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2021-04-30 13:38:54 +0000 |
commit | 20cd89908d3b7276c50295ccd6c4f09f7324f6e2 (patch) | |
tree | 794d3da185c17429a64881eb8702dae9659f1b5a /apps/plugins/lua_scripts/submenu_demo.lua | |
parent | 48b77898dce686f77b86adb977ba49400f5a3f7d (diff) | |
download | rockbox-20cd89908d3b7276c50295ccd6c4f09f7324f6e2.tar.gz rockbox-20cd89908d3b7276c50295ccd6c4f09f7324f6e2.zip |
lua submenus add a way to dynamically add items
updated example script and renamed some functions as well
fixed bug in printtable cursor position if greater than maxlines for the
screen would reset to item 1
now we move the list start and select it
(and try to center it on the screen)
fixed a few bugs in the add_menu code
Change-Id: I01dead0481ef2e925af8b4cc6c14e36c2859dbba
Diffstat (limited to 'apps/plugins/lua_scripts/submenu_demo.lua')
-rw-r--r-- | apps/plugins/lua_scripts/submenu_demo.lua | 44 |
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 | |||
13 | local function ITEM_MENU() | 13 | local 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() | |||
32 | end | 32 | end |
33 | 33 | ||
34 | local function USERITEMS() | 34 | local 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 | ||
39 | end | 67 | end |
40 | 68 | ||
41 | local function MAIN_MENU() | 69 | local 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 |