Commit a7a4feea authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua: updated menu.get to return string by default

to get an integer: menu.get("Foo", "Bar", 0)
old syntax is still valid: menu.get("Foo", "Bar", "")
updated API tests
http://www.magiclantern.fm/forum/index.php?topic=21145

--HG--
branch : lua_fix
parent c60d970a5b08
......@@ -188,15 +188,24 @@ static int get_index_for_choices(struct menu_entry * menu_entry, const char * va
/// Get the value of some existing ML menu entry.
// @tparam string menu name of the parent menu ('Audio', 'Expo', 'Overlay', 'Shoot', 'Movie', etc)
// @tparam string entry name of the menu entry
// @tparam[opt] string as_string pass empty string "" to get the result as string (default is int)
// @tparam[opt] ?int|string ret_type desired return type (optional, default string)
//
// By default, this function returns a string (the current menu text).
//
// Pass any integer to get the result as int, i.e. the internal integer value for this menu entry.
// Usually, 0 = OFF and 1 = ON, numeric values "just work", pickbox indices are from 0,
// but each menu entry may define its own meaning - YMMV).
//
// You may also pass a string (for compatibility reasons); this will not change the default behavior.
// @treturn ?int|string|nil the current value of the requested menu entry (nil if menu entry not found)
// @function get
static int luaCB_menu_get(lua_State * L)
{
LUA_PARAM_STRING(menu, 1);
LUA_PARAM_STRING(entry, 2);
LUA_PARAM_STRING_OPTIONAL(as_string, 3, NULL);
int as_string = lua_gettop(L) < 3 || lua_type(L, 3) == LUA_TSTRING;
if (as_string)
{
struct menu_display_info info;
......
......@@ -328,16 +328,16 @@ function test_menu()
-- picture styles should be set-able as string
-- numeric works too, as it sets the internal index
assert(menu.set("Expo", "Picture Style", "Portrait"))
assert(menu.get("Expo", "Picture Style", "") == "Portrait")
assert(menu.get("Expo", "Picture Style") == "Portrait")
msleep(1000)
assert(menu.set("Expo", "Picture Style", 5)) -- OK, selects Neutral
-- skip string test, as other camera models may use different indices
msleep(1000)
assert(menu.set("Expo", "Picture Style", "Landscape"))
assert(menu.get("Expo", "Picture Style", "") == "Landscape")
assert(menu.get("Expo", "Picture Style") == "Landscape")
msleep(1000)
assert(menu.set("Expo", "Picture Style", 1234) == false) -- should fail, out of range
assert(menu.get("Expo", "Picture Style", "") == "Landscape") -- old selection should not change
assert(menu.get("Expo", "Picture Style") == "Landscape") -- old selection should not change
msleep(1000)
assert(menu.select("Overlay"))
......@@ -348,24 +348,24 @@ function test_menu()
-- boolean items should be set-able as int (0 or 1)
assert(menu.set("Shoot", "Advanced Bracket", 1))
assert(menu.get("Shoot", "Advanced Bracket") == 1)
assert(menu.get("Shoot", "Advanced Bracket", 0) == 1)
msleep(1000)
-- or as string (if the user interface displays "ON" or "OFF")
-- here, actual string will be "ON, 10s" or similar (also accepted)
assert(menu.set("Shoot", "Intervalometer", "ON"))
assert(menu.get("Shoot", "Intervalometer") == 1)
assert(menu.get("Shoot", "Intervalometer", 0) == 1)
msleep(1000)
-- turning off should be straightforward
assert(menu.set("Shoot", "Advanced Bracket", "OFF"))
assert(menu.get("Shoot", "Advanced Bracket") == 0)
assert(menu.get("Shoot", "Advanced Bracket", "") == "OFF")
assert(menu.get("Shoot", "Advanced Bracket", 0) == 0)
assert(menu.get("Shoot", "Advanced Bracket") == "OFF")
msleep(1000)
assert(menu.set("Shoot", "Intervalometer", 0))
assert(menu.get("Shoot", "Intervalometer") == 0)
assert(menu.get("Shoot", "Intervalometer", "") == "OFF")
assert(menu.get("Shoot", "Intervalometer", 0) == 0)
assert(menu.get("Shoot", "Intervalometer") == "OFF")
msleep(1000)
-- move to Intervalometer menu
......@@ -382,26 +382,26 @@ function test_menu()
-- for speed reasons (so entering 1m10s will fail)
-- smaller ranges are OK for trying every single value
assert(menu.set("Intervalometer", "Take a pic every", "1m30s"))
assert(menu.get("Intervalometer", "Take a pic every") == 90)
assert(menu.get("Intervalometer", "Take a pic every", "") == "1m30s")
assert(menu.get("Intervalometer", "Take a pic every", 0) == 90)
assert(menu.get("Intervalometer", "Take a pic every") == "1m30s")
msleep(1000)
-- actual string will be 10s
assert(menu.set("Intervalometer", "Take a pic every", "10"))
assert(menu.get("Intervalometer", "Take a pic every") == 10)
assert(menu.get("Intervalometer", "Take a pic every", "") == "10s")
assert(menu.get("Intervalometer", "Take a pic every", 0) == 10)
assert(menu.get("Intervalometer", "Take a pic every") == "10s")
msleep(1000)
-- integer should work as well - e.g. 1m10s should work now
assert(menu.set("Intervalometer", "Take a pic every", 70))
assert(menu.get("Intervalometer", "Take a pic every") == 70)
assert(menu.get("Intervalometer", "Take a pic every", "") == "1m10s")
assert(menu.get("Intervalometer", "Take a pic every", 0) == 70)
assert(menu.get("Intervalometer", "Take a pic every") == "1m10s")
msleep(1000)
-- out of range, should fail
assert(menu.set("Intervalometer", "Take a pic every", 7000000) == false)
assert(menu.get("Intervalometer", "Take a pic every") == 70)
assert(menu.get("Intervalometer", "Take a pic every", "") == "1m10s")
assert(menu.get("Intervalometer", "Take a pic every", 0) == 70)
assert(menu.get("Intervalometer", "Take a pic every") == "1m10s")
msleep(1000)
-- exit submenu
......@@ -452,8 +452,8 @@ function test_menu()
assert(menu.select("Shoot", "Crocodile") == false)
-- menu.get/set return nil if the menu was not found
assert(menu.get("Shoot", "Introvolometer") == nil)
assert(menu.get("Shoot", "Brack", "") == nil)
assert(menu.get("Shoot", "Introvolometer", 0) == nil)
assert(menu.get("Shoot", "Brack") == nil)
assert(menu.set("Shoot", "Introvolometer", 1) == nil)
assert(menu.set("Shoot", "Introvolometer", "OFF") == nil)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment