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.
18 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
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>
33 #include <boost/scoped_ptr.hpp>
38 //#include <boost/regex.hpp>
42 #include <dali/integration-api/debug.h>
43 #include "shared/view.h"
45 #define TOKEN_STRING(x) #x
48 using namespace Dali::Toolkit;
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" );
57 std::string USER_DIRECTORY;
59 std::string JSON_BROKEN(" \
66 'parent-origin': 'CENTER', \
67 'text':'COULD NOT LOAD JSON FILE' \
73 std::string ReplaceQuotes(const std::string &single_quoted)
75 std::string s(single_quoted);
77 // wrong as no embedded quote but had regex link problems
78 std::replace(s.begin(), s.end(), '\'', '"');
83 std::string GetFileContents(const std::string &fn)
85 std::ifstream t(fn.c_str());
86 return std::string((std::istreambuf_iterator<char>(t)),
87 std::istreambuf_iterator<char>());
90 typedef std::vector<std::string> FileList;
92 void DirectoryFileList(const std::string& directory, FileList& files)
96 d = opendir(directory.c_str());
99 while ((dir = readdir(d)) != NULL)
101 if (dir->d_type == DT_REG)
103 files.push_back( directory + std::string(dir->d_name) );
111 void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* ie "json" */, FileList& files)
113 typedef FileList Collection;
114 typedef FileList::iterator Iter;
117 DirectoryFileList(dir, allFiles);
119 for(Iter iter = allFiles.begin(); iter != allFiles.end(); ++iter)
121 size_t pos = (*iter).rfind( '.' );
122 if( pos != std::string::npos )
124 if( (*iter).substr( pos+1 ) == fileType )
126 files.push_back( (*iter) );
132 const std::string ShortName( const std::string& name )
134 size_t pos = name.rfind( '/' );
136 if( pos != std::string::npos )
138 return name.substr( pos );
146 static Vector3 SetItemSize(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
148 return Vector3(layoutWidth, 50, 1);
151 //------------------------------------------------------------------------------
155 //------------------------------------------------------------------------------
161 explicit FileWatcher(const std::string &fn) { SetFilename(fn) ; };
163 void SetFilename(const std::string &fn);
164 std::string GetFilename() const;
166 bool FileHasChanged(void);
167 std::string GetFileContents(void) const { return ::GetFileContents(mstringPath) ; };
171 // FileWatcher(const FileWatcher&);
172 // FileWatcher &operator=(const FileWatcher &);
174 std::time_t mLastTime;
175 std::string mstringPath;
179 FileWatcher::FileWatcher(void) : mLastTime(0)
183 bool FileWatcher::FileHasChanged(void)
187 if(0 != stat(mstringPath.c_str(), &buf))
193 if(buf.st_mtime > mLastTime)
195 mLastTime = buf.st_mtime;
200 mLastTime = buf.st_mtime;
208 FileWatcher::~FileWatcher()
212 void FileWatcher::SetFilename(const std::string &fn)
215 FileHasChanged(); // update last time
218 std::string FileWatcher::GetFilename(void) const
227 //------------------------------------------------------------------------------
231 //------------------------------------------------------------------------------
232 class ExampleApp : public ConnectionTracker, public Toolkit::ItemFactory
235 ExampleApp(Application &app) : mApp(app)
237 app.InitSignal().Connect(this, &ExampleApp::Create);
244 void SetTitle(const std::string& title)
248 mTitleActor = TextView::New();
249 // Add title to the tool bar.
250 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
253 Font font = Font::New();
254 mTitleActor.SetText( title );
255 mTitleActor.SetSize( font.MeasureText( title ) );
256 mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
259 bool OnToolSelectLayout( Toolkit::Button button )
261 bool on = mItemView.IsVisible();
275 void LeaveSelection()
280 void EnterSelection()
282 Stage stage = Stage::GetCurrent();
284 mTapDetector = TapGestureDetector::New();
285 mTapDetector.DetectedSignal().Connect( this, &ExampleApp::OnTap );
289 stage.Remove( mItemView );
294 mItemView = ItemView::New(*this);
295 stage.Add( mItemView );
296 mItemView.SetParentOrigin(ParentOrigin::CENTER);
297 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
298 mGridLayout = GridLayout::New();
299 mGridLayout->SetNumberOfColumns(1);
301 mGridLayout->SetItemSizeFunction(SetItemSize);
303 mGridLayout->SetTopMargin(DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight);
305 mItemView.AddLayout(*mGridLayout);
307 Vector3 size(stage.GetSize());
308 mItemView.ActivateLayout(0, size, 0.0f/*immediate*/);
309 mItemView.SetKeyboardFocusable( true );
314 if( USER_DIRECTORY.size() )
316 DirectoryFilesByType( USER_DIRECTORY, "json", files );
320 DirectoryFilesByType( DALI_SCRIPT_DIR, "json", files );
323 std::sort(files.begin(), files.end());
326 for(FileList::iterator iter = files.begin(); iter != files.end(); ++iter)
328 JsonParser parser = JsonParser::New();
330 std::string data( GetFileContents( *iter ) );
332 parser.Parse( data );
334 if( parser.ParseError() )
336 std::cout << "Parser Error:" << *iter << std::endl;
337 std::cout << parser.GetErrorLineNumber() << "(" << parser.GetErrorColumn() << "):" << parser.GetErrorDescription() << std::endl;
341 if( parser.GetRoot() )
343 if( const TreeNode* node = parser.GetRoot()->Find("stage") )
345 // only those with a stage section
348 mFiles.push_back( *iter );
350 mItemView.InsertItem( Item(itemId,
351 MenuItem( ShortName( *iter ) ) ),
358 std::cout << "Ignored file (stage has no nodes?):" << *iter << std::endl;
363 std::cout << "Ignored file (no stage section):" << *iter << std::endl;
368 // Display item view on the stage
369 stage.Add( mItemView );
371 mItemView.SetVisible( true );
372 mBuilderLayer.SetVisible( false );
376 // Itemview renderes the previous items unless its scrolled. Not sure why at the moment so we force a scroll
377 mItemView.ScrollToItem(0, 0);
383 mTapDetector.Reset();
385 mItemView.SetVisible( false );
386 mBuilderLayer.SetVisible( true );
391 void OnTap( Actor actor, const TapGesture& tap )
393 ItemId id = mItemView.GetItemId( actor );
395 LoadFromFileList( id );
398 Actor MenuItem(const std::string& text)
400 TextView t = TextView::New();
401 t.SetMarkupProcessingEnabled(true);
403 int size = static_cast<int>(DemoHelper::ScalePointSize(6));
405 std::ostringstream fontString;
406 fontString << "<font size="<< size <<">"<< ShortName( text ) << "</font>";
408 t.SetText( fontString.str() );
410 t.SetTextAlignment( Alignment::HorizontalLeft );
412 // Hook up tap detector
413 mTapDetector.Attach( t );
420 if( mFileWatcher.FileHasChanged() )
422 LoadFromFile( mFileWatcher.GetFilename() );
428 void ReloadJsonFile(const std::string& filename, Builder& builder, Layer& layer)
430 Stage stage = Stage::GetCurrent();
432 builder = Builder::New();
433 builder.QuitSignal().Connect( this, &ExampleApp::OnBuilderQuit );
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;
440 builder.AddConstants( defaultDirs );
442 // render tasks may have been setup last load so remove them
443 RenderTaskList taskList = stage.GetRenderTaskList();
444 if( taskList.GetTaskCount() > 1 )
446 typedef std::vector<RenderTask> Collection;
447 typedef Collection::iterator ColIter;
450 for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i)
452 tasks.push_back( taskList.GetTask(i) );
455 for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter)
457 taskList.RemoveTask(*iter);
460 RenderTask defaultTask = taskList.GetTask(0);
461 defaultTask.SetSourceActor( stage.GetRootLayer() );
462 defaultTask.SetTargetFrameBuffer( FrameBufferImage() );
465 unsigned int numChildren = layer.GetChildCount();
467 for(unsigned int i=0; i<numChildren; ++i)
469 layer.Remove( layer.GetChildAt(0) );
472 std::string data(GetFileContents(filename));
476 builder.LoadFromString(data);
480 builder.LoadFromString(ReplaceQuotes(JSON_BROKEN));
483 builder.AddActors( layer );
488 void LoadFromFileList( size_t index )
490 if( index < mFiles.size())
492 const std::string& name = mFiles[index];
493 mFileWatcher.SetFilename( name );
494 LoadFromFile( name );
498 void LoadFromFile( const std::string& name )
500 ReloadJsonFile( name, mBuilder, mBuilderLayer );
502 // do this here as GetCurrentSize()
503 mBuilderLayer.SetParentOrigin(ParentOrigin::CENTER);
504 mBuilderLayer.SetAnchorPoint(AnchorPoint::CENTER);
505 Dali::Vector3 size = Stage::GetCurrent().GetRootLayer().GetCurrentSize();
506 size.y -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight;
507 mBuilderLayer.SetSize( size );
509 mBuilderLayer.LowerToBottom();
510 Stage::GetCurrent().GetRootLayer().RaiseToTop();
515 void Create(Application& app)
517 Stage stage = Stage::GetCurrent();
519 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent);
521 Layer contents = DemoHelper::CreateView( app,
530 mBuilderLayer = Layer::New();
531 stage.GetRootLayer().Add(mBuilderLayer);
534 // Create an edit mode button. (left of toolbar)
535 Toolkit::PushButton editButton = Toolkit::PushButton::New();
536 editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) );
537 editButton.ClickedSignal().Connect( this, &ExampleApp::OnToolSelectLayout);
538 editButton.SetLeaveRequired( true );
539 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
543 mTimer = Timer::New( 500 ); // ms
544 mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer);
549 virtual unsigned int GetNumberOfItems()
551 return mFiles.size();
554 virtual Actor NewItem(unsigned int itemId)
556 DALI_ASSERT_DEBUG( itemId < mFiles.size() );
557 return MenuItem( ShortName( mFiles[itemId] ) );
561 * Main key event handler
563 void OnKeyEvent(const KeyEvent& event)
565 if(event.state == KeyEvent::Down)
567 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
569 if ( mItemView.IsVisible() )
582 * Event handler when Builder wants to quit (we only want to close the shown json unless we're at the top-level)
586 if ( mItemView.IsVisible() )
599 GridLayoutPtr mGridLayout;
603 unsigned int mOrientation;
605 Toolkit::ToolBar mToolBar;
606 TextView mTitleActor; ///< The Toolbar's Title.
610 Toolkit::Popup mMenu;
612 TapGestureDetector mTapDetector;
619 FileWatcher mFileWatcher;
625 //------------------------------------------------------------------------------
629 //------------------------------------------------------------------------------
630 int main(int argc, char **argv)
634 if(strcmp(argv[1], "-f") == 0)
636 USER_DIRECTORY = argv[2];
640 Application app = Application::New(&argc, &argv);
642 ExampleApp dali_app(app);