Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-demo.git] / examples / scripting / launcher.cpp
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   // Get a handle to the stage
59   Stage stage = Stage::GetCurrent();
60
61   // change the background color to purple
62   Stage::GetCurrent().SetBackgroundColor( Vector4(0.2,0.2,0.4,1.0) );
63
64   // Try loading a JSON file
65   if( !mJSONFileName.empty() )
66   {
67     mBuilder = Toolkit::Builder::New();
68
69     Property::Map defaultDirs;
70     defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ]  = DALI_IMAGE_DIR;
71     defaultDirs[ TOKEN_STRING(DALI_MODEL_DIR) ]  = DALI_MODEL_DIR;
72     defaultDirs[ TOKEN_STRING(DALI_SCRIPT_DIR) ] = DALI_SCRIPT_DIR;
73     mBuilder.AddConstants( defaultDirs );
74
75     std::string json_data(GetFileContents( mJSONFileName ));
76     mBuilder.LoadFromString(json_data);
77     mBuilder.AddActors( stage.GetRootLayer() );
78   }
79
80   // Try load a JavaScript file
81   if( !mJavaScriptFileName.empty() )
82   {
83     // execute the script
84     mScript = Toolkit::Script::New();
85
86     mScript.ExecuteFile( mJavaScriptFileName);
87   }
88 }
89
90 void Launcher::MainLoop()
91 {
92   mApplication.MainLoop();
93 }