9c48b3f722197d110f535a289024dde8ea682afc
[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 // INTERNAL INCLUDES
27 #include "shared/view.h"
28
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 #define TOKEN_STRING(x) #x
34
35 namespace
36 {
37 std::string GetFileContents(const std::string& filename)
38 {
39   std::ifstream t(filename.c_str());
40   return std::string((std::istreambuf_iterator<char>(t)),
41                      std::istreambuf_iterator<char>());
42 };
43
44 } // unnamed namespace
45
46 Launcher::Launcher( Dali::Application application, std::string layoutFileName, std::string scriptFileName )
47 : mApplication( application ),
48   mJSONFileName(layoutFileName ),
49   mJavaScriptFileName( scriptFileName )
50 {
51
52   mApplication.InitSignal().Connect( this, &Launcher::Create );
53 }
54
55 Launcher::~Launcher()
56 {
57 }
58
59 void Launcher::Create( Dali::Application& application )
60 {
61   DemoHelper::RequestThemeChange();
62
63   TextLabel textActor = TextLabel::New( "JSON & JavaScript Launcher..." );
64
65   // Reposition the actor
66   textActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
67   textActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
68   textActor.SetPosition( 20, 0 );
69
70   // Get a handle to the stage
71   Stage stage = Stage::GetCurrent();
72
73   // Display the actor on the stage
74   stage.Add( textActor );
75
76   // change the background color to purple
77   Stage::GetCurrent().SetBackgroundColor( Vector4(0.2,0.2,0.4,1.0) );
78
79   // Try loading a JSON file
80   if( !mJSONFileName.empty() )
81   {
82     mBuilder = Toolkit::Builder::New();
83
84     Property::Map defaultDirs;
85     defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ]  = DALI_IMAGE_DIR;
86     defaultDirs[ TOKEN_STRING(DALI_MODEL_DIR) ]  = DALI_MODEL_DIR;
87     defaultDirs[ TOKEN_STRING(DALI_SCRIPT_DIR) ] = DALI_SCRIPT_DIR;
88     mBuilder.AddConstants( defaultDirs );
89
90     std::string json_data(GetFileContents( mJSONFileName ));
91     mBuilder.LoadFromString(json_data);
92     mBuilder.AddActors( stage.GetRootLayer() );
93   }
94
95   // Try load a JavaScript file
96   if( !mJavaScriptFileName.empty() )
97   {
98     // execute the script
99     mScript = Toolkit::Script::New();
100
101     mScript.ExecuteFile( mJavaScriptFileName);
102   }
103 }
104
105 void Launcher::MainLoop()
106 {
107   mApplication.MainLoop();
108 }