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