2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //------------------------------------------------------------------------------
19 // Run a json script layout file
21 // - watches an named file and reloads actor tree if the file changes
23 // builder-run layout.json
25 // and edit layout.json in a text editor saving to trigger the reload
27 //------------------------------------------------------------------------------
29 #include <dali/dali.h>
30 #include <dali-toolkit/dali-toolkit.h>
31 #include <dali-toolkit/public-api/builder/builder.h>
32 #include <dali-toolkit/public-api/builder/tree-node.h>
38 #include <boost/scoped_ptr.hpp>
40 //#include <boost/regex.hpp>
44 #include <dali/integration-api/debug.h>
46 #define TOKEN_STRING(x) #x
49 using namespace Dali::Toolkit;
54 std::string JSON_BROKEN(" \
61 'parent-origin': 'CENTER', \
62 'text':'COULD NOT LOAD JSON FILE' \
68 std::string ReplaceQuotes(const std::string &single_quoted)
70 std::string s(single_quoted);
72 // wrong as no embedded quote but had regex link problems
73 std::replace(s.begin(), s.end(), '\'', '"');
81 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
91 explicit FileWatcher(const std::string &fn): mLastTime(0) { SetFilename(fn) ; };
93 void SetFilename(const std::string &fn);
94 std::string GetFilename();
96 bool FileHasChanged(void);
97 std::string GetFileContents(void) { return GetFileContents(mstringPath) ; };
101 // FileWatcher(const FileWatcher&);
102 // FileWatcher &operator=(const FileWatcher &);
104 std::time_t mLastTime;
105 std::string mstringPath;
107 std::string GetFileContents(const std::string &fn)
109 std::ifstream t(fn.c_str());
110 return std::string((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
114 FileWatcher::FileWatcher(void) : mLastTime(0)
118 bool FileWatcher::FileHasChanged(void)
122 if(0 != stat(mstringPath.c_str(), &buf))
124 DALI_LOG_WARNING("File does not exist '%s'\n", mstringPath.c_str());
129 if(buf.st_mtime > mLastTime)
131 mLastTime = buf.st_mtime;
136 mLastTime = buf.st_mtime;
144 FileWatcher::~FileWatcher()
148 void FileWatcher::SetFilename(const std::string &fn)
153 std::string FileWatcher::GetFilename(void)
159 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 class ExampleApp : public ConnectionTracker
167 ExampleApp(Application &app) : mApp(app)
169 app.InitSignal().Connect(this, &ExampleApp::Create);
176 void SetJSONFilename(std::string const &fn) { fw.SetFilename(fn) ; };
178 void Create(Application& app)
180 mTimer = Timer::New( 500 ); // ms
181 mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer);
184 // Connect to key events in order to exit
185 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent);
195 void ReloadJsonFile(Builder& builder, Layer& layer)
197 Stage stage = Stage::GetCurrent();
199 builder = Builder::New();
200 builder.QuitSignal().Connect( this, &ExampleApp::OnBuilderQuit );
202 Property::Map defaultDirs;
203 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
204 defaultDirs[ TOKEN_STRING(DALI_MODEL_DIR) ] = DALI_MODEL_DIR;
205 defaultDirs[ TOKEN_STRING(DALI_SCRIPT_DIR) ] = DALI_SCRIPT_DIR;
207 builder.AddConstants( defaultDirs );
211 layer = Layer::New();
212 layer.SetParentOrigin(ParentOrigin::CENTER);
213 layer.SetAnchorPoint(AnchorPoint::CENTER);
214 layer.SetSize( stage.GetRootLayer().GetCurrentSize() );
215 stage.GetRootLayer().Add(layer);
217 // render tasks may have been setup last load so remove them
218 RenderTaskList taskList = stage.GetRenderTaskList();
219 if( taskList.GetTaskCount() > 1 )
221 typedef std::vector<RenderTask> Collection;
222 typedef Collection::iterator ColIter;
225 for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i)
227 tasks.push_back( taskList.GetTask(i) );
230 for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter)
232 taskList.RemoveTask(*iter);
235 RenderTask defaultTask = taskList.GetTask(0);
236 defaultTask.SetSourceActor( stage.GetRootLayer() );
237 defaultTask.SetTargetFrameBuffer( FrameBufferImage() );
241 unsigned int numChildren = layer.GetChildCount();
243 for(unsigned int i=0; i<numChildren; ++i)
245 layer.Remove( layer.GetChildAt(0) );
248 std::string data(fw.GetFileContents());
252 builder.LoadFromString(data);
256 builder.LoadFromString(ReplaceQuotes(JSON_BROKEN));
259 builder.AddActors( layer );
266 if(fw.FileHasChanged())
268 ReloadJsonFile( mBuilder, mRootLayer );
274 // Process Key events to Quit on back-key
275 void OnKeyEvent( const KeyEvent& event )
277 if( event.state == KeyEvent::Down )
279 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
294 //------------------------------------------------------------------------------
298 //------------------------------------------------------------------------------
299 int main(int argc, char **argv)
301 Application dali_app = Application::New(&argc, &argv);
303 ExampleApp app(dali_app);
308 std::cout << "Loading file:" << argc << " " << argv[1] << std::endl;
309 app.SetJSONFilename(argv[1]);
313 DALI_ASSERT_ALWAYS(!"Specify JSON file on command line\n");