Commit a9ead80a3f5389bbea75800d855057cae8103255

Authored by Nick Holland
1 parent f7497d52

JavaScript and JSON command line launcher

Usage:
Will load both JSON and JavaScript file from command line
scripting.example my-scene.json my-script.js

Change-Id: I36330b8bae84a532f57989b4087843d178d03e0b
com.samsung.dali-demo.xml
@@ -52,6 +52,9 @@ @@ -52,6 +52,9 @@
52 <ui-application appid="refraction-effect.example" exec="/usr/apps/com.samsung.dali-demo/bin/refraction-effect.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true"> 52 <ui-application appid="refraction-effect.example" exec="/usr/apps/com.samsung.dali-demo/bin/refraction-effect.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
53 <label>Refraction effect</label> 53 <label>Refraction effect</label>
54 </ui-application> 54 </ui-application>
  55 + <ui-application appid="scripting.example" exec="/usr/apps/com.samsung.dali-demo/bin/scripting.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
  56 + <label>Scroll View</label>
  57 + </ui-application>
55 <ui-application appid="scroll-view.example" exec="/usr/apps/com.samsung.dali-demo/bin/scroll-view.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true"> 58 <ui-application appid="scroll-view.example" exec="/usr/apps/com.samsung.dali-demo/bin/scroll-view.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
56 <label>Scroll View</label> 59 <label>Scroll View</label>
57 </ui-application> 60 </ui-application>
examples/scripting/launcher.cpp 0 → 100644
  1 +/*
  2 + * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + *
  16 + */
  17 +
  18 +// CLASS HEADER
  19 +#include "launcher.h"
  20 +
  21 +// EXTERNAL INCLUDES
  22 +#include <fstream>
  23 +#include <sys/stat.h>
  24 +#include <dali/integration-api/debug.h>
  25 +
  26 +
  27 +
  28 +using namespace Dali;
  29 +
  30 +#define TOKEN_STRING(x) #x
  31 +
  32 +namespace
  33 +{
  34 +std::string GetFileContents(const std::string& filename)
  35 +{
  36 + std::ifstream t(filename.c_str());
  37 + return std::string((std::istreambuf_iterator<char>(t)),
  38 + std::istreambuf_iterator<char>());
  39 +};
  40 +
  41 +} // unnamed namespace
  42 +
  43 +Launcher::Launcher( Dali::Application application, std::string layoutFileName, std::string scriptFileName )
  44 +: mApplication( application ),
  45 + mJSONFileName(layoutFileName ),
  46 + mJavaScriptFileName( scriptFileName )
  47 +{
  48 +
  49 + mApplication.InitSignal().Connect( this, &Launcher::Create );
  50 +}
  51 +
  52 +Launcher::~Launcher()
  53 +{
  54 +}
  55 +
  56 +void Launcher::Create( Dali::Application& application )
  57 +{
  58 + TextActor textActor = TextActor::New( "JSON & JavaScript Launcher..." );
  59 +
  60 + // Reposition the actor
  61 + textActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
  62 + textActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  63 + textActor.SetPosition( 20, 0 );
  64 +
  65 + // Get a handle to the stage
  66 + Stage stage = Stage::GetCurrent();
  67 +
  68 + // Display the actor on the stage
  69 + stage.Add( textActor );
  70 +
  71 + // change the background color to purple
  72 + Stage::GetCurrent().SetBackgroundColor( Vector4(0.2,0.2,0.4,1.0) );
  73 +
  74 + // Try loading a JSON file
  75 + if( !mJSONFileName.empty() )
  76 + {
  77 + mBuilder = Toolkit::Builder::New();
  78 +
  79 + Property::Map defaultDirs;
  80 + defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
  81 + defaultDirs[ TOKEN_STRING(DALI_MODEL_DIR) ] = DALI_MODEL_DIR;
  82 + defaultDirs[ TOKEN_STRING(DALI_SCRIPT_DIR) ] = DALI_SCRIPT_DIR;
  83 + mBuilder.AddConstants( defaultDirs );
  84 +
  85 + std::string json_data(GetFileContents( mJSONFileName ));
  86 + mBuilder.LoadFromString(json_data);
  87 + mBuilder.AddActors( stage.GetRootLayer() );
  88 + }
  89 +
  90 + // Try load a JavaScript file
  91 + if( !mJavaScriptFileName.empty() )
  92 + {
  93 + // execute the script
  94 + mScript = Toolkit::Script::New();
  95 +
  96 + mScript.ExecuteFile( mJavaScriptFileName);
  97 + }
  98 +}
  99 +
  100 +void Launcher::MainLoop()
  101 +{
  102 + mApplication.MainLoop();
  103 +}
examples/scripting/launcher.h 0 → 100644
  1 +#ifndef DALI_APP_H
  2 +#define DALI_APP_H
  3 +
  4 +/*
  5 + * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  6 + *
  7 + * Licensed under the Apache License, Version 2.0 (the "License");
  8 + * you may not use this file except in compliance with the License.
  9 + * You may obtain a copy of the License at
  10 + *
  11 + * http://www.apache.org/licenses/LICENSE-2.0
  12 + *
  13 + * Unless required by applicable law or agreed to in writing, software
  14 + * distributed under the License is distributed on an "AS IS" BASIS,
  15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16 + * See the License for the specific language governing permissions and
  17 + * limitations under the License.
  18 + *
  19 + */
  20 +
  21 +// EXTERNAL INCLUDES
  22 +#include <dali/dali.h>
  23 +#include <dali-toolkit/dali-toolkit.h>
  24 +
  25 +/**
  26 + * Example app that can load both JSON and JavaScript files from command line
  27 + * E.g. scripting.example my-first.js my-first.json
  28 + * See dali-demo/resources/scripts for example JSON and JavaScript files
  29 + */
  30 +class Launcher: public Dali::ConnectionTracker
  31 +{
  32 +public:
  33 +
  34 + /**
  35 + * @brief Construcctor
  36 + * @param application application
  37 + * @param layoutFileName JSON file to run
  38 + * @param scriptFileName JavaScript file to run
  39 + */
  40 + Launcher( Dali::Application application, std::string layoutFileName, std::string scriptFileName );
  41 +
  42 + /**
  43 + * @brief destructor
  44 + */
  45 + ~Launcher();
  46 +
  47 + /**
  48 + * @brief create app
  49 + */
  50 + void Create( Dali::Application& application );
  51 +
  52 + /**
  53 + * @brief run application MainLoop
  54 + */
  55 + void MainLoop();
  56 +
  57 +private:
  58 +
  59 + Dali::Toolkit::Script mScript; ///< Used to load and execute JavaScript
  60 + Dali::Toolkit::Builder mBuilder; ///< Used to parse JSON
  61 + Dali::Application mApplication; ///< application
  62 + std::string mJSONFileName; ///< JSON filename
  63 + std::string mJavaScriptFileName; ///< JavaScript filename
  64 +};
  65 +
  66 +#endif // header
examples/scripting/scripting-example.cpp 0 → 100644
  1 +/*
  2 + * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + *
  16 + */
  17 +
  18 +// EXTERNAL INCLUDES
  19 +#include <string>
  20 +#include <stdio.h>
  21 +#include <sys/stat.h>
  22 +#include <dali/integration-api/debug.h>
  23 +
  24 +// INTERNAL INCLUDES
  25 +#include "launcher.h"
  26 +
  27 +namespace
  28 +{
  29 +bool CheckIfFileExists( const std::string& filename )
  30 +{
  31 + struct stat buf;
  32 + // fstat returns -1 on error
  33 + if (stat( filename.c_str(), &buf) != -1)
  34 + {
  35 + return true;
  36 + }
  37 + return false;
  38 +}
  39 +
  40 +}
  41 +int main( int argc, char* argv[] )
  42 +{
  43 + // pull out the JSON file and JavaScript file from the command line arguments
  44 + std::string javaScriptFileName;
  45 + std::string jSONFileName;
  46 +
  47 + for( int i = 1 ; i < argc ; ++i )
  48 + {
  49 + std::string arg( argv[i] );
  50 +
  51 + size_t idx = std::string::npos;
  52 +
  53 + idx = arg.find( ".json" );
  54 + if( idx != std::string::npos )
  55 + {
  56 + jSONFileName = arg;
  57 + }
  58 + else
  59 + {
  60 + idx = arg.find( ".js" );
  61 + if( idx != std::string::npos )
  62 + {
  63 + javaScriptFileName = arg;
  64 + }
  65 + }
  66 + }
  67 +
  68 + if( !jSONFileName.empty() )
  69 + {
  70 + bool exists = CheckIfFileExists( jSONFileName );
  71 + if( !exists )
  72 + {
  73 + DALI_ASSERT_ALWAYS( 0 && "JSON file not found ")
  74 + }
  75 + }
  76 + if( !javaScriptFileName.empty() )
  77 + {
  78 + bool exists = CheckIfFileExists( javaScriptFileName );
  79 + if( !exists )
  80 + {
  81 + DALI_ASSERT_ALWAYS( 0 && "JavaScript file not found ")
  82 + }
  83 + }
  84 + if( jSONFileName.empty() && javaScriptFileName.empty() )
  85 + {
  86 + printf("Please specify a JSON and/or JavaScript file to load, e.g. scripting.example mylayout.json my-test.js\n");
  87 + return -1;
  88 + }
  89 +
  90 +
  91 + Launcher daliApplication( Dali::Application::New( &argc, &argv ), jSONFileName, javaScriptFileName );
  92 +
  93 + daliApplication.MainLoop();
  94 +
  95 + return 0;
  96 +}
resources/scripts/simple-image-wall.js 0 → 100644
  1 +// Image Wall example
  2 +//
  3 +// Example usage of Dali API
  4 +//
  5 +//
  6 +//
  7 +// get the dali-demo image directory path
  8 +// hard code for the device to /usr/apps/com.samsung.dali-demo/images/
  9 +var imageDir = dali.DALI_DATA_DIRECTORY;
  10 +
  11 +if (imageDir != "/usr/share/dali//") {
  12 + imageDir = imageDir.substring(0, imageDir.lastIndexOf("dali/"));
  13 + imageDir += "com.samsung.dali-demo/images/";
  14 +} else // on device
  15 +{
  16 + imageDir = "/usr/apps/com.samsung.dali-demo/images/";
  17 +}
  18 +
  19 +
  20 +var NUMBER_OF_IMAGES = 40; // for now use 16 ( demo files go up to 30)
  21 +var DEMO_IMAGES = []; // array to store Dali Images
  22 +var VIDEO_WALL_ACTORS = []; // array to store Image actors
  23 +var VIDEO_WALL_ROWS = 7; // use 3 rows for the video wall
  24 +var VIDEO_WALL_COLUMNS = 12; // use 12 columns for the video wall
  25 +var VIDEO_WALL_TOTAL_ITEMS = VIDEO_WALL_COLUMNS * VIDEO_WALL_ROWS; // total items
  26 +var VIDEO_WALL_ITEM_SIZE = 128; // width / height of a item in the video wall
  27 +var BORDER_SIZE = 5;
  28 +var VIDEO_WALL_ITEM_SIZE_NO_BORDER = VIDEO_WALL_ITEM_SIZE - BORDER_SIZE;
  29 +var VIDEO_WALL_WIDTH = VIDEO_WALL_COLUMNS * VIDEO_WALL_ITEM_SIZE;
  30 +var VIDEO_WALL_HEIGHT = VIDEO_WALL_ROWS * VIDEO_WALL_ITEM_SIZE;
  31 +
  32 +var daliApp = {};
  33 +
  34 +var wallRootActor; // the root actor of the video wall
  35 +
  36 +// we want demo images of format gallery-small-1.jpg
  37 +daliApp.getFileName = function(index) {
  38 + fileName = "gallery-small-" + (index+1) + ".jpg";
  39 + return fileName;
  40 +}
  41 +
  42 +// load the images
  43 +daliApp.loadImages = function() {
  44 + for (index = 0; index < NUMBER_OF_IMAGES; ++index) {
  45 + fileName = imageDir + daliApp.getFileName(index);
  46 + DEMO_IMAGES[index] = new dali.ResourceImage( { url:fileName } );
  47 + }
  48 +}
  49 +
  50 +daliApp.createRootActor = function() {
  51 + wallRootActor = new dali.Actor();
  52 + wallRootActor.parentOrigin = dali.CENTER;
  53 + wallRootActor.anchorPoint = dali.CENTER;
  54 + dali.stage.add(wallRootActor);
  55 +}
  56 +
  57 +
  58 +
  59 +daliApp.getWallActorIndex = function(x, y) {
  60 + return x + y * VIDEO_WALL_COLUMNS;
  61 +}
  62 +
  63 +daliApp.createActors = function() {
  64 + daliApp.createRootActor();
  65 +
  66 + for (y = 0; y < VIDEO_WALL_ROWS; ++y) {
  67 + for (x = 0; x < VIDEO_WALL_COLUMNS; ++x) {
  68 +
  69 + var actorIndex = daliApp.getWallActorIndex(x, y);
  70 + var imageActor = new dali.ImageActor();
  71 +
  72 + // wrap image index between 0 and NUMBER_OF_IMAGES
  73 + var imageIndex = actorIndex % NUMBER_OF_IMAGES;
  74 +
  75 + imageActor.setImage(DEMO_IMAGES[imageIndex]);
  76 +
  77 + imageActor.parentOrigin = dali.CENTER;
  78 + imageActor.anchorPoint = dali.CENTER;
  79 + imageActor.size = [VIDEO_WALL_ITEM_SIZE_NO_BORDER, VIDEO_WALL_ITEM_SIZE_NO_BORDER, 1.0]; // start with zero size so it zooms up
  80 +
  81 + var xPosition = x * VIDEO_WALL_ITEM_SIZE;
  82 + // as the middle the wall is at zero (relative to wallRootActor), we need to subtract half the wall width.
  83 + // + add half item size because the item anchor point is the center of the wallRootActor.
  84 + xPosition = xPosition - (VIDEO_WALL_WIDTH / 2) + (VIDEO_WALL_ITEM_SIZE / 2);
  85 +
  86 + var yPosition = y * VIDEO_WALL_ITEM_SIZE;
  87 + yPosition = yPosition - (VIDEO_WALL_HEIGHT / 2) + (VIDEO_WALL_ITEM_SIZE / 2);
  88 +
  89 + imageActor.position = [xPosition, yPosition, 0.0];
  90 + // store the actor
  91 + VIDEO_WALL_ACTORS[actorIndex] = imageActor;
  92 +
  93 + // Add to the video wall root actor.
  94 + wallRootActor.add(imageActor);
  95 + }
  96 + }
  97 +}
  98 +
  99 +function Initialise() {
  100 +
  101 + daliApp.loadImages();
  102 +
  103 + daliApp.createActors();
  104 +
  105 +
  106 +}
  107 +
  108 +Initialise();