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