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/devel-api/builder/builder.h>
32 #include <dali-toolkit/devel-api/builder/tree-node.h>
42 #include <dali/integration-api/debug.h>
44 #define TOKEN_STRING(x) #x
47 using namespace Dali::Toolkit;
52 std::string JSON_BROKEN(" \
59 'parentOrigin': 'CENTER', \
60 'text':'COULD NOT LOAD JSON FILE' \
66 std::string ReplaceQuotes(const std::string &single_quoted)
68 std::string s(single_quoted);
70 // wrong as no embedded quote but had regex link problems
71 std::replace(s.begin(), s.end(), '\'', '"');
79 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
89 explicit FileWatcher(const std::string &fn): mLastTime(0) { SetFilename(fn) ; };
91 void SetFilename(const std::string &fn);
92 std::string GetFilename();
94 bool FileHasChanged(void);
95 std::string GetFileContents(void) { return GetFileContents(mstringPath) ; };
99 // FileWatcher(const FileWatcher&);
100 // FileWatcher &operator=(const FileWatcher &);
102 std::time_t mLastTime;
103 std::string mstringPath;
105 std::string GetFileContents(const std::string &fn)
107 std::ifstream t(fn.c_str());
108 return std::string((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
112 FileWatcher::FileWatcher(void) : mLastTime(0)
116 bool FileWatcher::FileHasChanged(void)
120 if(0 != stat(mstringPath.c_str(), &buf))
122 DALI_LOG_WARNING("File does not exist '%s'\n", mstringPath.c_str());
127 if(buf.st_mtime > mLastTime)
129 mLastTime = buf.st_mtime;
134 mLastTime = buf.st_mtime;
142 FileWatcher::~FileWatcher()
146 void FileWatcher::SetFilename(const std::string &fn)
151 std::string FileWatcher::GetFilename(void)
157 //------------------------------------------------------------------------------
161 //------------------------------------------------------------------------------
162 class ExampleApp : public ConnectionTracker
165 ExampleApp(Application &app) : mApp(app)
167 app.InitSignal().Connect(this, &ExampleApp::Create);
174 void SetJSONFilename(std::string const &fn) { fw.SetFilename(fn) ; };
176 void Create(Application& app)
178 mTimer = Timer::New( 500 ); // ms
179 mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer);
182 // Connect to key events in order to exit
183 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent);
193 void ReloadJsonFile(Builder& builder, Layer& layer)
195 Stage stage = Stage::GetCurrent();
196 stage.SetBackgroundColor( Color::WHITE );
198 builder = Builder::New();
199 builder.QuitSignal().Connect( this, &ExampleApp::OnBuilderQuit );
201 Property::Map defaultDirs;
202 defaultDirs[ TOKEN_STRING(DEMO_IMAGE_DIR) ] = DEMO_IMAGE_DIR;
203 defaultDirs[ TOKEN_STRING(DEMO_MODEL_DIR) ] = DEMO_MODEL_DIR;
204 defaultDirs[ TOKEN_STRING(DEMO_SCRIPT_DIR) ] = DEMO_SCRIPT_DIR;
206 builder.AddConstants( defaultDirs );
210 layer = Layer::New();
211 layer.SetParentOrigin(ParentOrigin::CENTER);
212 layer.SetAnchorPoint(AnchorPoint::CENTER);
213 layer.SetSize( stage.GetRootLayer().GetCurrentSize() );
214 stage.GetRootLayer().Add(layer);
216 // render tasks may have been setup last load so remove them
217 RenderTaskList taskList = stage.GetRenderTaskList();
218 if( taskList.GetTaskCount() > 1 )
220 typedef std::vector<RenderTask> Collection;
221 typedef Collection::iterator ColIter;
224 for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i)
226 tasks.push_back( taskList.GetTask(i) );
229 for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter)
231 taskList.RemoveTask(*iter);
234 RenderTask defaultTask = taskList.GetTask(0);
235 defaultTask.SetSourceActor( stage.GetRootLayer() );
236 defaultTask.SetTargetFrameBuffer( FrameBufferImage() );
240 unsigned int numChildren = layer.GetChildCount();
242 for(unsigned int i=0; i<numChildren; ++i)
244 layer.Remove( layer.GetChildAt(0) );
247 std::string data(fw.GetFileContents());
251 builder.LoadFromString(data);
255 builder.LoadFromString(ReplaceQuotes(JSON_BROKEN));
258 builder.AddActors( layer );
265 if(fw.FileHasChanged())
267 ReloadJsonFile( mBuilder, mRootLayer );
273 // Process Key events to Quit on back-key
274 void OnKeyEvent( const KeyEvent& event )
276 if( event.state == KeyEvent::Down )
278 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
293 //------------------------------------------------------------------------------
297 //------------------------------------------------------------------------------
298 int main(int argc, char **argv)
301 Application dali_app = Application::New(&argc, &argv, DEMO_THEME_PATH);
302 ExampleApp app(dali_app);
306 std::cout << "Loading file:" << argc << " " << argv[1] << std::endl;
307 app.SetJSONFilename(argv[1]);
311 DALI_ASSERT_ALWAYS(!"Specify JSON file on command line\n");