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