Getting all screen names

Questions and information about using Lua scripting with Storyboard

Moderator: jamiev

Getting all screen names

Postby Benjamin » Thu Sep 13, 2018 7:12 am

Hi,

Is it possible to somehow query all screen names with Lua? So, say I have "boot_screen" and "home_screen" in Storyboard Designer, then I can execute a piece of code at runtime:

Code: Select all
screen_names = get_all_screen names()
--screen_names now contains {"boot_screen","home_screen"}


And if I later add a screen in Storyboard Designer, the Lua code would automatically handle the new screen too. That would be super useful.

Best regards,
Benjamin
Benjamin
 
Posts: 7
Joined: Mon Sep 10, 2018 7:11 am

Re: Getting all screen names

Postby MikeMarchand » Thu Sep 13, 2018 7:33 am

Hi Benjamin,

Take a look at the GREDOM module (https://www.cranksoftware.com/cranksoftware/v5.3.0/docs/apas03.html), it will allow you to walk the model at runtime.

Cheers,
Mike
MikeMarchand
 
Posts: 31
Joined: Wed Jan 21, 2015 3:33 pm

Re: Getting all screen names

Postby Benjamin » Thu Sep 13, 2018 8:52 am

Hey Mike

Thanks for your quick reply! I've looked at GREDOM and indeed it seems to expose the functionality I'd like, but I cannot get it to traverse the DOM. In a function with many screens and controls, I call:

Code: Select all
require("gredom")
local dom_obj = gredom.get_application()
local child_list = dom_obj:get_children()
print("DOM obj count: " .. #child_list)


This prints "DOM obj count: 0" which is very confusing to me. I know the idea of DOM from HTML development, where I simply take "document" and then traverse down to the children I like, but I cannot traverse this DOM somehow?

Best regards,
Benjamin

PS:
On your documentation page, it says

Code: Select all
Gets the Storyboard type of the specified DOMOBJECT. The type may be one of gredom.APP, gredom.SCREEN, gredom.LAYER, gredom.LAYER_INSTANCE, gredom.GROUP grecom.CONTROL, gredom.TABLE, gredom.TEMPLATE.

There seems to be an error with the CONTROL element, it says "grecom" instead of "gredom" and a comma is missing.
Benjamin
 
Posts: 7
Joined: Mon Sep 10, 2018 7:11 am

Re: Getting all screen names

Postby MikeMarchand » Thu Sep 13, 2018 9:26 am

Hi Benjamin,

Thanks for pointing out he typo's in the documentation. It will be fixed in the next revision of Storyboard.

I am surprised that you are seeing count: 0.

On the Thermostat sample, I added an action to the '+' button to invoke a Lua function called CBPrintScreens. Here is my code.

Code: Select all
local function GetGREDOMType(type)
  if(type == gredom.APP) then
    return "App"
  elseif(type == gredom.SCREEN) then
    return "Screen"
  elseif(type == gredom.LAYER) then
    return "Layer"
  elseif(type == gredom.GROUP) then
    return "Group"
  elseif(type == gredom.CONTROL) then
    return "Control"
  elseif(type == gredom.TABLE) then
    return "Table"
  elseif(type == gredom.TEMPLATE) then
    return "Template"
  end
end

--- @param gre#context mapargs
function CBPrintScreens(mapargs)
  require("gredom")
 
  local dom_obj = gredom.get_application()
  local child_list = dom_obj:get_children()
  print("DOM obj count: " .. #child_list)
 
  for k,v in pairs(child_list) do
    print(k, GetGREDOMType(v:get_type()), v:get_name())
  end
end


And Here is the output

Code: Select all
DOM obj count: 5
1   Screen   fan_sc
2   Screen   filter_sc
3   Screen   home_sc
4   Screen   settings_sc
5   Screen   weather_sc


Could you confirm which version of Storyboard Engine you are running and on which platform?

Thanks,
Mike
MikeMarchand
 
Posts: 31
Joined: Wed Jan 21, 2015 3:33 pm

Re: Getting all screen names

Postby Benjamin » Thu Sep 13, 2018 9:38 am

Hi Mike

I found my error. I put the call to query the DOM straight into a Lua file (outside a handler function) so it ran immediately upon starting the Storyboard Engine, before the DOM was actually built. That's why it didn't find anything. I packed your code into a handler and now it works perfectly. So I guess what I have to do is use the gre.init event to make sure the DOM is built first? Is it discouraged to execute code right inside the Lua files without being wrapped in a function?

Thanks a lot.

Cheers,
Benjamin
Benjamin
 
Posts: 7
Joined: Mon Sep 10, 2018 7:11 am

Re: Getting all screen names

Postby MikeMarchand » Thu Sep 13, 2018 9:46 am

Hi Benjamin,

That's a great question! Usually it should be avoided unless you have a specific reason for doing so. In any case, you should avoid 'gre.*' functions calls, at that time because there is no guarantee that all the required facilities have been initialized. Usually you would want to reserve initialization that requires gre.* for a callback on gre.init.

I'm glad you were able to resolve your problem. Glad to help!

Cheers,
Mike
MikeMarchand
 
Posts: 31
Joined: Wed Jan 21, 2015 3:33 pm


Return to Lua Scripting

Who is online

Users browsing this forum: Google [Bot] and 0 guests