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