[dali_1.1.18] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / builder / examples.cpp
1 /*
2  * Copyright (c) 2014 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 //------------------------------------------------------------------------------
19 //
20 //
21 //------------------------------------------------------------------------------
22
23 #include <dali/dali.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/builder/builder.h>
26 #include <dali-toolkit/devel-api/builder/tree-node.h>
27 #include <dali-toolkit/devel-api/builder/json-parser.h>
28 #include <dali-toolkit/devel-api/controls/popup/popup.h>
29 #include <map>
30 #include <string>
31 #include <fstream>
32 #include <streambuf>
33 #include <sstream>
34 #include <dirent.h>
35 #include <stdio.h>
36 #include <iostream>
37
38 #include "sys/stat.h"
39 #include <ctime>
40 #include <cstring>
41
42 #include <dali/integration-api/debug.h>
43 #include "shared/view.h"
44
45 #define TOKEN_STRING(x) #x
46
47 using namespace Dali;
48 using namespace Dali::Toolkit;
49
50 namespace
51 {
52
53 const char* BACKGROUND_IMAGE( "" );
54 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
55 const char* EDIT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
56 const char* EDIT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
57
58 std::string USER_DIRECTORY;
59
60 std::string JSON_BROKEN("                                      \
61 {                                                              \
62   'stage':                                                     \
63   [                                                            \
64     {                                                          \
65       'type':'TextLabel',                                      \
66       'size': [50,50,1],                                       \
67       'parentOrigin': 'CENTER',                                \
68       'text':'COULD NOT LOAD JSON FILE'                        \
69     }                                                          \
70   ]                                                            \
71 }                                                              \
72 ");
73
74 std::string ReplaceQuotes(const std::string &single_quoted)
75 {
76   std::string s(single_quoted);
77
78   // wrong as no embedded quote but had regex link problems
79   std::replace(s.begin(), s.end(), '\'', '"');
80
81   return s;
82 }
83
84 std::string GetFileContents(const std::string &fn)
85 {
86   std::ifstream t(fn.c_str());
87   return std::string((std::istreambuf_iterator<char>(t)),
88                      std::istreambuf_iterator<char>());
89 };
90
91 typedef std::vector<std::string> FileList;
92
93 void DirectoryFileList(const std::string& directory, FileList& files)
94 {
95   DIR           *d;
96   struct dirent *dir;
97   d = opendir(directory.c_str());
98   if (d)
99   {
100     while ((dir = readdir(d)) != NULL)
101     {
102       if (dir->d_type == DT_REG)
103       {
104         files.push_back( directory + std::string(dir->d_name) );
105       }
106     }
107
108     closedir(d);
109   }
110 }
111
112 void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* ie "json" */, FileList& files)
113 {
114   typedef FileList Collection;
115   typedef FileList::iterator Iter;
116
117   Collection allFiles;
118   DirectoryFileList(dir, allFiles);
119
120   for(Iter iter = allFiles.begin(); iter != allFiles.end(); ++iter)
121   {
122     size_t pos = (*iter).rfind( '.' );
123     if( pos != std::string::npos )
124     {
125       if( (*iter).substr( pos+1 ) == fileType )
126       {
127         files.push_back( (*iter) );
128       }
129     }
130   }
131 }
132
133 const std::string ShortName( const std::string& name )
134 {
135   size_t pos = name.rfind( '/' );
136
137   if( pos != std::string::npos )
138   {
139     return name.substr( pos );
140   }
141   else
142   {
143     return name;
144   }
145 }
146
147 //------------------------------------------------------------------------------
148 //
149 //
150 //
151 //------------------------------------------------------------------------------
152 class FileWatcher
153 {
154 public:
155   FileWatcher(void);
156   ~FileWatcher(void);
157   explicit FileWatcher(const std::string &fn) { SetFilename(fn) ; };
158
159   void SetFilename(const std::string &fn);
160   std::string GetFilename() const;
161
162   bool FileHasChanged(void);
163   std::string GetFileContents(void) const { return ::GetFileContents(mstringPath) ; };
164
165 private:
166   // compiler does
167   // FileWatcher(const FileWatcher&);
168   // FileWatcher &operator=(const FileWatcher &);
169
170   std::time_t mLastTime;
171   std::string mstringPath;
172
173 };
174
175 FileWatcher::FileWatcher(void) : mLastTime(0)
176 {
177 }
178
179 bool FileWatcher::FileHasChanged(void)
180 {
181   struct stat buf;
182
183   if(0 != stat(mstringPath.c_str(), &buf))
184   {
185     return false;
186   }
187   else
188   {
189     if(buf.st_mtime > mLastTime)
190     {
191       mLastTime = buf.st_mtime;
192       return true;
193     }
194     else
195     {
196       mLastTime = buf.st_mtime;
197       return false;
198     }
199   }
200
201   return false;
202 }
203
204 FileWatcher::~FileWatcher()
205 {
206 }
207
208 void FileWatcher::SetFilename(const std::string &fn)
209 {
210   mstringPath = fn;
211   FileHasChanged(); // update last time
212 }
213
214 std::string FileWatcher::GetFilename(void) const
215 {
216   return mstringPath;
217 }
218
219
220 } // anon namespace
221
222
223 //------------------------------------------------------------------------------
224 //
225 //
226 //
227 //------------------------------------------------------------------------------
228 class ExampleApp : public ConnectionTracker, public Toolkit::ItemFactory
229 {
230 public:
231   ExampleApp(Application &app) : mApp(app)
232   {
233     app.InitSignal().Connect(this, &ExampleApp::Create);
234   }
235
236   ~ExampleApp() {}
237
238 public:
239
240   void SetTitle(const std::string& title)
241   {
242     if(!mTitleActor)
243     {
244       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
245       // Add title to the tool bar.
246       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
247     }
248
249     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
250   }
251
252   bool OnToolSelectLayout( Toolkit::Button button )
253   {
254     bool on = mItemView.IsVisible();
255
256     if( on )
257     {
258       LeaveSelection();
259     }
260     else
261     {
262       EnterSelection();
263     }
264
265     return true;
266   }
267
268   void LeaveSelection()
269   {
270
271   }
272
273   void EnterSelection()
274   {
275     Stage stage = Stage::GetCurrent();
276     stage.SetBackgroundColor( Color::WHITE );
277
278     mTapDetector = TapGestureDetector::New();
279     mTapDetector.DetectedSignal().Connect( this, &ExampleApp::OnTap );
280
281     if( mItemView )
282     {
283       stage.Remove( mItemView );
284     }
285
286     mFiles.clear();
287
288     mItemView = ItemView::New(*this);
289     stage.Add( mItemView );
290     mItemView.SetParentOrigin(ParentOrigin::CENTER);
291     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
292     mLayout = DefaultItemLayout::New( DefaultItemLayout::LIST );
293
294     mLayout->SetItemSize( Vector3( stage.GetSize().width, 50, 1 ) );
295
296     mItemView.AddLayout( *mLayout );
297
298     mItemView.SetKeyboardFocusable( true );
299
300     mFiles.clear();
301     FileList files;
302
303     if( USER_DIRECTORY.size() )
304     {
305       DirectoryFilesByType( USER_DIRECTORY, "json", files );
306     }
307     else
308     {
309       DirectoryFilesByType( DEMO_SCRIPT_DIR, "json", files );
310     }
311
312     std::sort(files.begin(), files.end());
313
314     ItemId itemId = 0;
315     for(FileList::iterator iter = files.begin(); iter != files.end(); ++iter)
316     {
317       JsonParser parser = JsonParser::New();
318
319       std::string data( GetFileContents( *iter ) );
320
321       parser.Parse( data );
322
323       if( parser.ParseError() )
324       {
325         std::cout << "Parser Error:" << *iter << std::endl;
326         std::cout << parser.GetErrorLineNumber() << "(" << parser.GetErrorColumn() << "):" << parser.GetErrorDescription() << std::endl;
327         exit(1);
328       }
329
330       if( parser.GetRoot() )
331       {
332         if( const TreeNode* node = parser.GetRoot()->Find("stage") )
333         {
334           // only those with a stage section
335           if( node->Size() )
336           {
337             mFiles.push_back( *iter );
338
339             mItemView.InsertItem( Item(itemId,
340                                        MenuItem( ShortName( *iter ) ) ),
341                                   0.5f );
342
343             itemId++;
344           }
345           else
346           {
347             std::cout << "Ignored file (stage has no nodes?):" << *iter << std::endl;
348           }
349         }
350         else
351         {
352           std::cout << "Ignored file (no stage section):" << *iter << std::endl;
353         }
354       }
355     }
356
357     // Display item view on the stage
358     stage.Add( mItemView );
359
360     mItemView.SetVisible( true );
361     mBuilderLayer.SetVisible( false );
362
363     SetTitle("Select");
364
365     // Activate the layout
366     Vector3 size(stage.GetSize());
367     mItemView.ActivateLayout(0, size, 0.0f/*immediate*/);
368   }
369
370   void ExitSelection()
371   {
372     mTapDetector.Reset();
373
374     mItemView.SetVisible( false );
375     mBuilderLayer.SetVisible( true );
376
377     SetTitle("View");
378   }
379
380   void OnTap( Actor actor, const TapGesture& tap )
381   {
382     ItemId id = mItemView.GetItemId( actor );
383
384     LoadFromFileList( id );
385   }
386
387   Actor MenuItem(const std::string& text)
388   {
389     TextLabel label = TextLabel::New( ShortName( text ) );
390     label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "builderlabel" );
391     label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
392
393     // Hook up tap detector
394     mTapDetector.Attach( label );
395
396     return label;
397   }
398
399   bool OnTimer()
400   {
401     if( mFileWatcher.FileHasChanged() )
402     {
403       LoadFromFile( mFileWatcher.GetFilename() );
404     }
405
406     return true;
407   }
408
409   void ReloadJsonFile(const std::string& filename, Builder& builder, Layer& layer)
410   {
411     Stage stage = Stage::GetCurrent();
412
413     builder = Builder::New();
414     builder.QuitSignal().Connect( this, &ExampleApp::OnBuilderQuit );
415
416     Property::Map defaultDirs;
417     defaultDirs[ TOKEN_STRING(DEMO_IMAGE_DIR) ]  = DEMO_IMAGE_DIR;
418     defaultDirs[ TOKEN_STRING(DEMO_MODEL_DIR) ]  = DEMO_MODEL_DIR;
419     defaultDirs[ TOKEN_STRING(DEMO_SCRIPT_DIR) ] = DEMO_SCRIPT_DIR;
420
421     builder.AddConstants( defaultDirs );
422
423     // render tasks may have been setup last load so remove them
424     RenderTaskList taskList = stage.GetRenderTaskList();
425     if( taskList.GetTaskCount() > 1 )
426     {
427       typedef std::vector<RenderTask> Collection;
428       typedef Collection::iterator ColIter;
429       Collection tasks;
430
431       for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i)
432       {
433         tasks.push_back( taskList.GetTask(i) );
434       }
435
436       for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter)
437       {
438         taskList.RemoveTask(*iter);
439       }
440
441       RenderTask defaultTask = taskList.GetTask(0);
442       defaultTask.SetSourceActor( stage.GetRootLayer() );
443       defaultTask.SetTargetFrameBuffer( FrameBufferImage() );
444     }
445
446     unsigned int numChildren = layer.GetChildCount();
447
448     for(unsigned int i=0; i<numChildren; ++i)
449     {
450       layer.Remove( layer.GetChildAt(0) );
451     }
452
453     std::string data(GetFileContents(filename));
454
455     try
456     {
457       builder.LoadFromString(data);
458     }
459     catch(...)
460     {
461       builder.LoadFromString(ReplaceQuotes(JSON_BROKEN));
462     }
463
464     builder.AddActors( layer );
465   }
466
467
468   void LoadFromFileList( size_t index )
469   {
470     if( index < mFiles.size())
471     {
472       const std::string& name = mFiles[index];
473       mFileWatcher.SetFilename( name );
474       LoadFromFile( name );
475     }
476   }
477
478   void LoadFromFile( const std::string& name )
479   {
480     ReloadJsonFile( name, mBuilder, mBuilderLayer );
481
482     // do this here as GetCurrentSize()
483     mBuilderLayer.SetParentOrigin(ParentOrigin::CENTER);
484     mBuilderLayer.SetAnchorPoint(AnchorPoint::CENTER);
485     Dali::Vector3 size = Stage::GetCurrent().GetRootLayer().GetCurrentSize();
486     size.y -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight;
487     mBuilderLayer.SetSize( size );
488
489     mBuilderLayer.LowerToBottom();
490     Stage::GetCurrent().GetRootLayer().RaiseToTop();
491
492     ExitSelection();
493   }
494
495   void Create(Application& app)
496   {
497     Stage stage = Stage::GetCurrent();
498
499     Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent);
500
501     Layer contents = DemoHelper::CreateView( app,
502                                              mView,
503                                              mToolBar,
504                                              BACKGROUND_IMAGE,
505                                              TOOLBAR_IMAGE,
506                                              "" );
507
508     SetTitle("Builder");
509
510     mBuilderLayer = Layer::New();
511     stage.GetRootLayer().Add(mBuilderLayer);
512
513
514     // Create an edit mode button. (left of toolbar)
515     Toolkit::PushButton editButton = Toolkit::PushButton::New();
516     editButton.SetUnselectedImage( EDIT_IMAGE );
517     editButton.SetSelectedImage( EDIT_IMAGE_SELECTED );
518     editButton.ClickedSignal().Connect( this, &ExampleApp::OnToolSelectLayout);
519     editButton.SetLeaveRequired( true );
520     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
521
522     EnterSelection();
523
524     mTimer = Timer::New( 500 ); // ms
525     mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer);
526     mTimer.Start();
527
528   } // Create(app)
529
530   virtual unsigned int GetNumberOfItems()
531   {
532     return mFiles.size();
533   }
534
535   virtual Actor NewItem(unsigned int itemId)
536   {
537     DALI_ASSERT_DEBUG( itemId < mFiles.size() );
538     return MenuItem( ShortName( mFiles[itemId] ) );
539   }
540
541   /**
542    * Main key event handler
543    */
544   void OnKeyEvent(const KeyEvent& event)
545   {
546     if(event.state == KeyEvent::Down)
547     {
548       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
549       {
550         if ( mItemView.IsVisible() )
551         {
552           mApp.Quit();
553         }
554         else
555         {
556           EnterSelection();
557         }
558       }
559     }
560   }
561
562   /**
563    * Event handler when Builder wants to quit (we only want to close the shown json unless we're at the top-level)
564    */
565   void OnBuilderQuit()
566   {
567     if ( mItemView.IsVisible() )
568     {
569       mApp.Quit();
570     }
571     else
572     {
573       EnterSelection();
574     }
575   }
576
577 private:
578   Application& mApp;
579
580   ItemLayoutPtr mLayout;
581   ItemView mItemView;
582
583   Toolkit::Control mView;
584   unsigned int mOrientation;
585
586   Toolkit::ToolBar mToolBar;
587   TextLabel mTitleActor;             ///< The Toolbar's Title.
588
589   Layer mBuilderLayer;
590
591   Toolkit::Popup mMenu;
592
593   TapGestureDetector mTapDetector;
594
595   // builder
596   Builder mBuilder;
597
598   FileList mFiles;
599
600   FileWatcher mFileWatcher;
601   Timer mTimer;
602
603
604 };
605
606 //------------------------------------------------------------------------------
607 //
608 //
609 //
610 //------------------------------------------------------------------------------
611 int main(int argc, char **argv)
612 {
613   if(argc > 2)
614   {
615     if(strcmp(argv[1], "-f") == 0)
616     {
617       USER_DIRECTORY = argv[2];
618     }
619   }
620
621   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
622
623   ExampleApp dali_app(app);
624
625   app.MainLoop();
626
627   return 0;
628 }