I’m probably not going to explain this well, but thought I’d give it a go…
Looking for input on a better way to handle the loading of ’scenes’ based upon JSON input. It DOES work, but it seems complicated and I wondered if the gurus had any wise words before I go too far down the garden path
I saw a post, or some code once that loaded a map or web view (I think) based upon a JSON file. For the life of me I can’t find it again.
The JSON being loaded (at the bottom of the post) below controls what is shown in the scene. It does work with my current design but I don’t think its’ optimal. I don’t mind removing items like “middle” for positioning, as they aren’t valid values for the controls being used, I use them to centre items in code based on device.
The design is to be able to build a scene dynamically based on a JSON file.
Currently the scene is loaded based upon the type. Eg: if it’s a map type, it loads the map scene, if its a web tip it loads the web scene. Here is some of the code to either confuse or enlighten you. Its not complete as I pared it down for informational use!
-- This is the parameters module \_pParm function \_mv.pparm(params) -- \*\* Add Here \*\* These are parsed when we run the create in WebView. -- They are the parameters passed INTO to the scene. \_mv.itemID = params.itemID \_mv.itemType=params.itemType \_mv.backgroundColour = params.backgroundColour \_mv.buttonimageremote=params.buttonimageremote \_mv.backgroundImageLocal=params.backgroundImageLocal \_mv.pX = params.xpos \_mv.pY = params.ypos \_mv.pW = params.vwidth \_mv.pH = params.vheight if \_mv.pX == 0 then \_mv.pX = display.contentCenterX end if \_mv.pY == 0 then \_mv.pY = display.contentCenterY end if \_mv.pW == 0 then \_mv.pW = display.actualContentWidth end if \_mv.pH == 0 then \_mv.pH = display.actualContentHeight end end — ========================================================= -- These are the parameters passed INTO the WebView scene. function \_mv.processJSON(tbl) -- \*\*Add Here \*\* This is the JSON processing once we load the JSON table. -- This is where we also translate values for properties. for k, v in pairs( tbl ) do -- print(k,v) if (k=="backgroundColour") then if v=="" then -- default colour else \_mv.backgroundColour = v end end if (k=="WebURL") then if v=="" then print("Error: No WebURL provided") else \_mv.WebURL = v common:parmprint("WebURL provided:"..\_mv.WebURL) end end if (k=="buttonimageremote") then if v=="" then else \_mv.buttonimageremote = v common:parmprint("buttonRemote provided:"..\_mv.buttonimageremote) end end end — ========================================================= -- EG: Values being passed OUT of the scene. local sceneParms = { time = 500,effect="fade", params = { backgroundColour = \_mv.backgroundColour, WebURL = \_mv.WebURL, buttonimageremote=\_mv.buttonimageremote, xpos=0, ypos=wyp-50, vwidth=0, vheight=mvh } } return sceneParms
— =========================== — Finally, this is the map view code -- ============================================ -- THis function will load all the properties we should need. mv.pparm(params) -- Load the parameters from the module. button = widget.newButton({ defaultFile = mv.buttonimagelocal, width=250, height=75, onEvent=exitPressed }) button.x =display.contentCenterX button.y = display.screenOriginY + 60 sceneGroup:insert(button) button.label = display.newText( mv.buttonText, button.x, button.y, mv.buttonFont, mv.buttonFontSize ) sceneGroup:insert(button.label) mapView = native.newMapView( mv.pX, mv.pY, mv.pW , mv.pH ) mapView.anchorY = 0 sceneGroup:insert(mapView) end
Finally, here’s the JSON excerpt.
"screens": { "12345678": { "itemID": 12345678, "itemType": "\_WEBView", "loadScreenID": "99345681", "buttonimagelocal": "defaultAssets/icnMenu.png", "buttonimageremote": "", "buttonX": "middle", "buttonY": "top", "buttonText": "Bing Click", "buttonFont": "Slabo27px-Regular.ttf", "buttonFontSize": 30, "backgroundImageLocal": "", "backgroundImageRemote": "", "backgroundColour": "red", "WebURL": "http://bing.com", "webWidth": "", "webHeight": "" }, "12345679": { "itemID": 12345679, "itemType": "\_mapView", "loadScreenID": "99345681", "buttonimagelocal": "defaultAssets/dftButton.png", "buttonimageremote": "", "buttonX": "middle", "buttonY": "top", "buttonText": "Map Click", "buttonFont": "Slabo27px-Regular.ttf", "buttonFontSize": 30, "backgroundImageLocal": "", "backgroundImageRemote": "", "backgroundColour": "red" },
Thoughts, comments?