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>
40 #include <dali/integration-api/debug.h>
41 #include "shared/view.h"
43 #define TOKEN_STRING(x) #x
46 using namespace Dali::Toolkit;
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" );
55 std::string USER_DIRECTORY;
57 std::string JSON_BROKEN(" \
64 'parent-origin': 'CENTER', \
65 'text':'COULD NOT LOAD JSON FILE' \
71 std::string ReplaceQuotes(const std::string &single_quoted)
73 std::string s(single_quoted);
75 // wrong as no embedded quote but had regex link problems
76 std::replace(s.begin(), s.end(), '\'', '"');
81 std::string GetFileContents(const std::string &fn)
83 std::ifstream t(fn.c_str());
84 return std::string((std::istreambuf_iterator<char>(t)),
85 std::istreambuf_iterator<char>());
88 typedef std::vector<std::string> FileList;
90 void DirectoryFileList(const std::string& directory, FileList& files)
94 d = opendir(directory.c_str());
97 while ((dir = readdir(d)) != NULL)
99 if (dir->d_type == DT_REG)
101 files.push_back( directory + std::string(dir->d_name) );
109 void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* ie "json" */, FileList& files)
111 typedef FileList Collection;
112 typedef FileList::iterator Iter;
115 DirectoryFileList(dir, allFiles);
117 for(Iter iter = allFiles.begin(); iter != allFiles.end(); ++iter)
119 size_t pos = (*iter).rfind( '.' );
120 if( pos != std::string::npos )
122 if( (*iter).substr( pos+1 ) == fileType )
124 files.push_back( (*iter) );
130 const std::string ShortName( const std::string& name )
132 size_t pos = name.rfind( '/' );
134 if( pos != std::string::npos )
136 return name.substr( pos );
144 static Vector3 SetItemSize(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
146 return Vector3(layoutWidth, 50, 1);
149 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
159 explicit FileWatcher(const std::string &fn) { SetFilename(fn) ; };
161 void SetFilename(const std::string &fn);
162 std::string GetFilename() const;
164 bool FileHasChanged(void);
165 std::string GetFileContents(void) const { return ::GetFileContents(mstringPath) ; };
169 // FileWatcher(const FileWatcher&);
170 // FileWatcher &operator=(const FileWatcher &);
172 std::time_t mLastTime;
173 std::string mstringPath;
177 FileWatcher::FileWatcher(void) : mLastTime(0)
181 bool FileWatcher::FileHasChanged(void)
185 if(0 != stat(mstringPath.c_str(), &buf))
191 if(buf.st_mtime > mLastTime)
193 mLastTime = buf.st_mtime;
198 mLastTime = buf.st_mtime;
206 FileWatcher::~FileWatcher()
210 void FileWatcher::SetFilename(const std::string &fn)
213 FileHasChanged(); // update last time
216 std::string FileWatcher::GetFilename(void) const
225 //------------------------------------------------------------------------------
229 //------------------------------------------------------------------------------
230 class ExampleApp : public ConnectionTracker, public Toolkit::ItemFactory
233 ExampleApp(Application &app) : mApp(app)
235 app.InitSignal().Connect(this, &ExampleApp::Create);
242 void SetTitle(const std::string& title)
246 mTitleActor = TextView::New();
247 // Add title to the tool bar.
248 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
251 Font font = Font::New();
252 mTitleActor.SetText( title );
253 mTitleActor.SetSize( font.MeasureText( title ) );
254 mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
257 bool OnToolSelectLayout( Toolkit::Button button )
259 bool on = mItemView.IsVisible();
273 void LeaveSelection()
278 void EnterSelection()
280 Stage stage = Stage::GetCurrent();
282 mTapDetector = TapGestureDetector::New();
283 mTapDetector.DetectedSignal().Connect( this, &ExampleApp::OnTap );
287 stage.Remove( mItemView );
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);
300 mGridLayout->SetItemSizeFunction(SetItemSize);
302 mGridLayout->SetTopMargin(DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight);
304 mItemView.AddLayout(*mGridLayout);
306 Vector3 size(stage.GetSize());
307 mItemView.ActivateLayout(0, size, 0.0f/*immediate*/);
308 mItemView.SetKeyboardFocusable( true );
313 if( USER_DIRECTORY.size() )
315 DirectoryFilesByType( USER_DIRECTORY, "json", files );
319 DirectoryFilesByType( DALI_SCRIPT_DIR, "json", files );
322 std::sort(files.begin(), files.end());
325 for(FileList::iterator iter = files.begin(); iter != files.end(); ++iter)
327 JsonParser parser = JsonParser::New();
329 std::string data( GetFileContents( *iter ) );
331 parser.Parse( data );
333 if( parser.ParseError() )
335 std::cout << "Parser Error:" << *iter << std::endl;
336 std::cout << parser.GetErrorLineNumber() << "(" << parser.GetErrorColumn() << "):" << parser.GetErrorDescription() << std::endl;
340 if( parser.GetRoot() )
342 if( const TreeNode* node = parser.GetRoot()->Find("stage") )
344 // only those with a stage section
347 mFiles.push_back( *iter );
349 mItemView.InsertItem( Item(itemId,
350 MenuItem( ShortName( *iter ) ) ),
357 std::cout << "Ignored file (stage has no nodes?):" << *iter << std::endl;
362 std::cout << "Ignored file (no stage section):" << *iter << std::endl;
367 // Display item view on the stage
368 stage.Add( mItemView );
370 mItemView.SetVisible( true );
371 mBuilderLayer.SetVisible( false );
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);
382 mTapDetector.Reset();
384 mItemView.SetVisible( false );
385 mBuilderLayer.SetVisible( true );
390 void OnTap( Actor actor, const TapGesture& tap )
392 ItemId id = mItemView.GetItemId( actor );
394 LoadFromFileList( id );
397 Actor MenuItem(const std::string& text)
399 TextView t = TextView::New();
400 t.SetResizePolicy( FILL_TO_PARENT, WIDTH );
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 );
485 // Force relayout on layer
486 layer.RelayoutRequestTree();
490 void LoadFromFileList( size_t index )
492 if( index < mFiles.size())
494 const std::string& name = mFiles[index];
495 mFileWatcher.SetFilename( name );
496 LoadFromFile( name );
500 void LoadFromFile( const std::string& name )
502 ReloadJsonFile( name, mBuilder, mBuilderLayer );
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 );
511 mBuilderLayer.LowerToBottom();
512 Stage::GetCurrent().GetRootLayer().RaiseToTop();
517 void Create(Application& app)
519 Stage stage = Stage::GetCurrent();
521 Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent);
523 Layer contents = DemoHelper::CreateView( app,
532 mBuilderLayer = Layer::New();
533 stage.GetRootLayer().Add(mBuilderLayer);
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 );
545 mTimer = Timer::New( 500 ); // ms
546 mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer);
551 virtual unsigned int GetNumberOfItems()
553 return mFiles.size();
556 virtual Actor NewItem(unsigned int itemId)
558 DALI_ASSERT_DEBUG( itemId < mFiles.size() );
559 return MenuItem( ShortName( mFiles[itemId] ) );
563 * Main key event handler
565 void OnKeyEvent(const KeyEvent& event)
567 if(event.state == KeyEvent::Down)
569 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
571 if ( mItemView.IsVisible() )
584 * Event handler when Builder wants to quit (we only want to close the shown json unless we're at the top-level)
588 if ( mItemView.IsVisible() )
601 GridLayoutPtr mGridLayout;
605 unsigned int mOrientation;
607 Toolkit::ToolBar mToolBar;
608 TextView mTitleActor; ///< The Toolbar's Title.
612 Toolkit::Popup mMenu;
614 TapGestureDetector mTapDetector;
621 FileWatcher mFileWatcher;
627 //------------------------------------------------------------------------------
631 //------------------------------------------------------------------------------
632 int main(int argc, char **argv)
636 if(strcmp(argv[1], "-f") == 0)
638 USER_DIRECTORY = argv[2];
642 Application app = Application::New(&argc, &argv);
644 ExampleApp dali_app(app);