/* * Copyright (c) 2014 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ //------------------------------------------------------------------------------ // // //------------------------------------------------------------------------------ #include "dali.h" #include #include #include #include #include #include #include #include #include #include #include #include //#include #include "sys/stat.h" #include #include #include "../shared/view.h" #define TOKEN_STRING(x) #x using namespace Dali; using namespace Dali::Toolkit; namespace { const char* BACKGROUND_IMAGE( "" ); const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-change.png" ); std::string USER_DIRECTORY; std::string JSON_BROKEN(" \ { \ 'stage': \ [ \ { \ 'type':'TextView', \ 'size': [50,50,1], \ 'parent-origin': 'CENTER', \ 'text':'COULD NOT LOAD JSON FILE' \ } \ ] \ } \ "); std::string ReplaceQuotes(const std::string &single_quoted) { std::string s(single_quoted); // wrong as no embedded quote but had regex link problems std::replace(s.begin(), s.end(), '\'', '"'); return s; } std::string GetFileContents(const std::string &fn) { std::ifstream t(fn.c_str()); return std::string((std::istreambuf_iterator(t)), std::istreambuf_iterator()); }; typedef std::vector FileList; void DirectoryFileList(const std::string& directory, FileList& files) { DIR *d; struct dirent *dir; d = opendir(directory.c_str()); if (d) { while ((dir = readdir(d)) != NULL) { if (dir->d_type == DT_REG) { files.push_back( directory + std::string(dir->d_name) ); } } closedir(d); } } void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* ie "json" */, FileList& files) { typedef FileList Collection; typedef FileList::iterator Iter; Collection allFiles; DirectoryFileList(dir, allFiles); for(Iter iter = allFiles.begin(); iter != allFiles.end(); ++iter) { size_t pos = (*iter).rfind( '.' ); if( pos != std::string::npos ) { if( (*iter).substr( pos+1 ) == fileType ) { files.push_back( (*iter) ); } } } } const std::string ShortName( const std::string& name ) { size_t pos = name.rfind( '/' ); if( pos != std::string::npos ) { return name.substr( pos ); } else { return name; } } static Vector3 SetItemSize(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing) { return Vector3(layoutWidth, 50, 1); } //------------------------------------------------------------------------------ // // // //------------------------------------------------------------------------------ class FileWatcher { public: FileWatcher(void); ~FileWatcher(void); explicit FileWatcher(const std::string &fn) { SetFilename(fn) ; }; void SetFilename(const std::string &fn); std::string GetFilename() const; bool FileHasChanged(void); std::string GetFileContents(void) const { return ::GetFileContents(mstringPath) ; }; private: // compiler does // FileWatcher(const FileWatcher&); // FileWatcher &operator=(const FileWatcher &); std::time_t mLastTime; std::string mstringPath; }; FileWatcher::FileWatcher(void) : mLastTime(0) { } bool FileWatcher::FileHasChanged(void) { struct stat buf; if(0 != stat(mstringPath.c_str(), &buf)) { return false; } else { if(buf.st_mtime > mLastTime) { mLastTime = buf.st_mtime; return true; } else { mLastTime = buf.st_mtime; return false; } } return false; } FileWatcher::~FileWatcher() { } void FileWatcher::SetFilename(const std::string &fn) { mstringPath = fn; FileHasChanged(); // update last time } std::string FileWatcher::GetFilename(void) const { return mstringPath; } } // anon namespace //------------------------------------------------------------------------------ // // // //------------------------------------------------------------------------------ class ExampleApp : public ConnectionTracker, public Toolkit::ItemFactory { public: ExampleApp(Application &app) : mApp(app) { app.InitSignal().Connect(this, &ExampleApp::Create); } ~ExampleApp() {} public: void SetTitle(const std::string& title) { if(!mTitleActor) { mTitleActor = TextView::New(); // Add title to the tool bar. mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter ); } Font font = Font::New(); mTitleActor.SetText( title ); mTitleActor.SetSize( font.MeasureText( title ) ); mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle()); } bool OnToolSelectLayout( Toolkit::Button button ) { bool on = mItemView.IsVisible(); if( on ) { LeaveSelection(); } else { EnterSelection(); } return true; } void LeaveSelection() { } void EnterSelection() { Stage stage = Stage::GetCurrent(); if( mItemView ) { stage.Remove( mItemView ); } mFiles.clear(); mItemView = ItemView::New(*this); stage.Add( mItemView ); mItemView.SetParentOrigin(ParentOrigin::CENTER); mItemView.SetAnchorPoint(AnchorPoint::CENTER); mGridLayout = GridLayout::New(); mGridLayout->SetNumberOfColumns(1); mGridLayout->SetItemSizeFunction(SetItemSize); mGridLayout->SetTopMargin(DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight); mItemView.AddLayout(*mGridLayout); Vector3 size(stage.GetSize()); mItemView.ActivateLayout(0, size, 0.0f/*immediate*/); mItemView.SetKeyboardFocusable( true ); mFiles.clear(); FileList files; if( USER_DIRECTORY.size() ) { DirectoryFilesByType( USER_DIRECTORY, "json", files ); } else { DirectoryFilesByType( DALI_SCRIPT_DIR, "json", files ); } std::sort(files.begin(), files.end()); ItemId itemId = 0; for(FileList::iterator iter = files.begin(); iter != files.end(); ++iter) { JsonParser parser = JsonParser::New(); std::string data( GetFileContents( *iter ) ); parser.Parse( data ); if( parser.ParseError() ) { std::cout << "Parser Error:" << *iter << std::endl; std::cout << parser.GetErrorLineNumber() << "(" << parser.GetErrorColumn() << "):" << parser.GetErrorDescription() << std::endl; exit(1); } if( parser.GetRoot() ) { if( const TreeNode* node = parser.GetRoot()->Find("stage") ) { // only those with a stage section if( node->Size() ) { mFiles.push_back( *iter ); mItemView.InsertItem( Item(itemId, MenuItem( ShortName( *iter ) ) ), 0.5f ); itemId++; } else { std::cout << "Ignored file (stage has no nodes?):" << *iter << std::endl; } } else { std::cout << "Ignored file (no stage section):" << *iter << std::endl; } } } mTapDetector = TapGestureDetector::New(); for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i ) { mTapDetector.Attach( mItemView.GetChildAt(i) ); } mTapDetector.DetectedSignal().Connect( this, &ExampleApp::OnTap ); // Display item view on the stage stage.Add( mItemView ); mItemView.SetVisible( true ); mBuilderLayer.SetVisible( false ); SetTitle("Select"); // Itemview renderes the previous items unless its scrolled. Not sure why at the moment so we force a scroll mItemView.ScrollToItem(0, 0); } void ExitSelection() { mTapDetector.Reset(); mItemView.SetVisible( false ); mBuilderLayer.SetVisible( true ); SetTitle("View"); } void OnTap( Actor actor, TapGesture tap ) { ItemId id = mItemView.GetItemId( actor ); LoadFromFileList( id ); } Actor MenuItem(const std::string& text) { TextView t = TextView::New(); t.SetMarkupProcessingEnabled(true); t.SetText( std::string("") + ShortName( text ) + std::string("") ); t.SetTextAlignment( Alignment::HorizontalLeft ); return t; } bool OnTimer() { if( mFileWatcher.FileHasChanged() ) { LoadFromFile( mFileWatcher.GetFilename() ); } return true; } void ReloadJsonFile(const std::string& filename, Builder& builder, Layer& layer) { Stage stage = Stage::GetCurrent(); builder = Builder::New(); PropertyValueMap defaultDirs; defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR; defaultDirs[ TOKEN_STRING(DALI_MODEL_DIR) ] = DALI_MODEL_DIR; defaultDirs[ TOKEN_STRING(DALI_SCRIPT_DIR) ] = DALI_SCRIPT_DIR; builder.AddConstants( defaultDirs ); // render tasks may have been setup last load so remove them RenderTaskList taskList = stage.GetRenderTaskList(); if( taskList.GetTaskCount() > 1 ) { typedef std::vector Collection; typedef Collection::iterator ColIter; Collection tasks; for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i) { tasks.push_back( taskList.GetTask(i) ); } for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter) { taskList.RemoveTask(*iter); } RenderTask defaultTask = taskList.GetTask(0); defaultTask.SetSourceActor( stage.GetRootLayer() ); defaultTask.SetTargetFrameBuffer( FrameBufferImage() ); } unsigned int numChildren = layer.GetChildCount(); for(unsigned int i=0; i 2) { if(strcmp(argv[1], "-f") == 0) { USER_DIRECTORY = argv[2]; } } Application app = Application::New(&argc, &argv); ExampleApp dali_app(app); app.MainLoop(); return 0; }