X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fbuilder%2Fexamples.cpp;h=483559953f752137e131de9aa783a53e7b21903b;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=569aa830c0624e085f950eac4022e23173ed8f3f;hpb=547dbafef42c0a7d75e2857d679619a3461da5bc;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/builder/examples.cpp b/examples/builder/examples.cpp index 569aa83..4835599 100644 --- a/examples/builder/examples.cpp +++ b/examples/builder/examples.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 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. @@ -20,25 +20,28 @@ // //------------------------------------------------------------------------------ -#include "dali.h" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include + #include #include +#include #include +#include +#include +#include +#include -//#include -#include "sys/stat.h" +#include #include +#include "sys/stat.h" +#include #include #include "shared/view.h" @@ -49,28 +52,29 @@ 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" ); +const char* BACKGROUND_IMAGE(""); +const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png"); +const char* EDIT_IMAGE(DEMO_IMAGE_DIR "icon-change.png"); +const char* EDIT_IMAGE_SELECTED(DEMO_IMAGE_DIR "icon-change-selected.png"); std::string USER_DIRECTORY; -std::string JSON_BROKEN(" \ +std::string JSON_BROKEN( + " \ { \ 'stage': \ [ \ { \ - 'type':'TextView', \ + 'type':'TextLabel', \ 'size': [50,50,1], \ - 'parent-origin': 'CENTER', \ + 'parentOrigin': 'CENTER', \ 'text':'COULD NOT LOAD JSON FILE' \ } \ ] \ } \ "); -std::string ReplaceQuotes(const std::string &single_quoted) +std::string ReplaceQuotes(const std::string& single_quoted) { std::string s(single_quoted); @@ -80,27 +84,32 @@ std::string ReplaceQuotes(const std::string &single_quoted) return s; } -std::string GetFileContents(const std::string &fn) +std::string GetFileContents(const std::string& fn) { - std::ifstream t(fn.c_str()); - return std::string((std::istreambuf_iterator(t)), - std::istreambuf_iterator()); + std::streampos bufferSize = 0; + Dali::Vector fileBuffer; + if(!Dali::FileLoader::ReadFile(fn, bufferSize, fileBuffer, FileLoader::FileType::BINARY)) + { + return std::string(); + } + + return std::string(&fileBuffer[0], bufferSize); }; typedef std::vector FileList; void DirectoryFileList(const std::string& directory, FileList& files) { - DIR *d; - struct dirent *dir; + DIR* d; + struct dirent* dir; d = opendir(directory.c_str()); - if (d) + if(d) { - while ((dir = readdir(d)) != NULL) + while((dir = readdir(d)) != NULL) { - if (dir->d_type == DT_REG) + if(dir->d_type == DT_REG) { - files.push_back( directory + std::string(dir->d_name) ); + files.push_back(directory + std::string(dir->d_name)); } } @@ -110,7 +119,7 @@ void DirectoryFileList(const std::string& directory, FileList& files) void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* ie "json" */, FileList& files) { - typedef FileList Collection; + typedef FileList Collection; typedef FileList::iterator Iter; Collection allFiles; @@ -118,24 +127,25 @@ void DirectoryFilesByType(const std::string& dir, const std::string& fileType /* for(Iter iter = allFiles.begin(); iter != allFiles.end(); ++iter) { - size_t pos = (*iter).rfind( '.' ); - if( pos != std::string::npos ) + size_t pos = (*iter).rfind('.'); + if(pos != std::string::npos) { - if( (*iter).substr( pos+1 ) == fileType ) + if((*iter).substr(pos + 1) == fileType) { - files.push_back( (*iter) ); + files.push_back((*iter)); } } } } -const std::string ShortName( const std::string& name ) +const std::string ShortName(const std::string& name) { - size_t pos = name.rfind( '/' ); + size_t pos = name.rfind('/'); - if( pos != std::string::npos ) + if(pos != std::string::npos) { - return name.substr( pos ); + pos++; + return name.substr(pos); } else { @@ -143,11 +153,6 @@ const std::string ShortName( const std::string& name ) } } -static Vector3 SetItemSize(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing) -{ - return Vector3(layoutWidth, 50, 1); -} - //------------------------------------------------------------------------------ // // @@ -158,13 +163,19 @@ class FileWatcher public: FileWatcher(void); ~FileWatcher(void); - explicit FileWatcher(const std::string &fn) { SetFilename(fn) ; }; + explicit FileWatcher(const std::string& fn) + { + SetFilename(fn); + }; - void SetFilename(const std::string &fn); + void SetFilename(const std::string& fn); std::string GetFilename() const; - bool FileHasChanged(void); - std::string GetFileContents(void) const { return ::GetFileContents(mstringPath) ; }; + bool FileHasChanged(void); + std::string GetFileContents(void) const + { + return ::GetFileContents(mstringPath); + }; private: // compiler does @@ -173,10 +184,10 @@ private: std::time_t mLastTime; std::string mstringPath; - }; -FileWatcher::FileWatcher(void) : mLastTime(0) +FileWatcher::FileWatcher(void) +: mLastTime(0) { } @@ -209,7 +220,7 @@ FileWatcher::~FileWatcher() { } -void FileWatcher::SetFilename(const std::string &fn) +void FileWatcher::SetFilename(const std::string& fn) { mstringPath = fn; FileHasChanged(); // update last time @@ -220,9 +231,7 @@ std::string FileWatcher::GetFilename(void) const return mstringPath; } - -} // anon namespace - +} // namespace //------------------------------------------------------------------------------ // @@ -232,92 +241,66 @@ std::string FileWatcher::GetFilename(void) const class ExampleApp : public ConnectionTracker, public Toolkit::ItemFactory { public: - ExampleApp(Application &app) : mApp(app) + ExampleApp(Application& app) + : mApp(app) { app.InitSignal().Connect(this, &ExampleApp::Create); } - ~ExampleApp() {} + ~ExampleApp() + { + } public: - void SetTitle(const std::string& title) { if(!mTitleActor) { - mTitleActor = TextView::New(); + mTitleActor = DemoHelper::CreateToolBarLabel(""); // Add title to the tool bar. - mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter ); + mToolBar.AddControl(mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HORIZONTAL_CENTER); } - Font font = Font::New(); - mTitleActor.SetText( title ); - mTitleActor.SetSize( font.MeasureText( title ) ); - mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle()); + mTitleActor.SetProperty(TextLabel::Property::TEXT, title); } - bool OnToolSelectLayout( Toolkit::Button button ) + bool OnBackButtonPressed(Toolkit::Button button) { - bool on = mItemView.IsVisible(); - - if( on ) - { - LeaveSelection(); - } - else - { - EnterSelection(); - } - + OnQuitOrBack(); return true; } - void LeaveSelection() + void SetUpItemView() { - - } - - void EnterSelection() - { - Stage stage = Stage::GetCurrent(); + Window window = mApp.GetWindow(); mTapDetector = TapGestureDetector::New(); - mTapDetector.DetectedSignal().Connect( this, &ExampleApp::OnTap ); - - if( mItemView ) - { - stage.Remove( mItemView ); - } + mTapDetector.DetectedSignal().Connect(this, &ExampleApp::OnTap); 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); + mItemView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mItemView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mLayout = DefaultItemLayout::New(DefaultItemLayout::LIST); - mGridLayout->SetTopMargin(DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight); + mLayout->SetItemSize(Vector3(window.GetSize().GetWidth(), 50, 1)); - mItemView.AddLayout(*mGridLayout); + mItemView.AddLayout(*mLayout); - Vector3 size(stage.GetSize()); - mItemView.ActivateLayout(0, size, 0.0f/*immediate*/); - mItemView.SetKeyboardFocusable( true ); + mItemView.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true); mFiles.clear(); FileList files; - if( USER_DIRECTORY.size() ) + if(USER_DIRECTORY.size()) { - DirectoryFilesByType( USER_DIRECTORY, "json", files ); + DirectoryFilesByType(USER_DIRECTORY, "json", files); } else { - DirectoryFilesByType( DALI_SCRIPT_DIR, "json", files ); + DirectoryFilesByType(DEMO_SCRIPT_DIR, "json", files); } std::sort(files.begin(), files.end()); @@ -327,29 +310,29 @@ public: { JsonParser parser = JsonParser::New(); - std::string data( GetFileContents( *iter ) ); + std::string data(GetFileContents(*iter)); - parser.Parse( data ); + parser.Parse(data); - if( parser.ParseError() ) + 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(parser.GetRoot()) { - if( const TreeNode* node = parser.GetRoot()->Find("stage") ) + if(const TreeNode* node = parser.GetRoot()->Find("stage")) { // only those with a stage section - if( node->Size() ) + if(node->Size()) { - mFiles.push_back( *iter ); + mFiles.push_back(*iter); - mItemView.InsertItem( Item(itemId, - MenuItem( ShortName( *iter ) ) ), - 0.5f ); + mItemView.InsertItem(Item(itemId, + MenuItem(ShortName(*iter))), + 0.5f); itemId++; } @@ -365,61 +348,35 @@ public: } } - // 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"); + // Activate the layout + Vector3 size(window.GetSize()); + mItemView.ActivateLayout(0, size, 0.0f /*immediate*/); } - void OnTap( Actor actor, const TapGesture& tap ) + void OnTap(Actor actor, const TapGesture& tap) { - ItemId id = mItemView.GetItemId( actor ); + ItemId id = mItemView.GetItemId(actor); - LoadFromFileList( id ); + LoadFromFileList(id); } Actor MenuItem(const std::string& text) { - TextView t = TextView::New(); - t.SetMarkupProcessingEnabled(true); - - int size = static_cast(DemoHelper::ScalePointSize(6)); - - std::ostringstream fontString; - fontString << ""<< ShortName( text ) << ""; - - t.SetText( fontString.str() ); - - t.SetTextAlignment( Alignment::HorizontalLeft ); + TextLabel label = TextLabel::New(ShortName(text)); + label.SetStyleName("BuilderLabel"); + label.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH); // Hook up tap detector - mTapDetector.Attach( t ); + mTapDetector.Attach(label); - return t; + return label; } bool OnTimer() { - if( mFileWatcher.FileHasChanged() ) + if(mFileWatcher.FileHasChanged()) { - LoadFromFile( mFileWatcher.GetFilename() ); + LoadFromFile(mFileWatcher.GetFilename()); } return true; @@ -427,29 +384,29 @@ public: void ReloadJsonFile(const std::string& filename, Builder& builder, Layer& layer) { - Stage stage = Stage::GetCurrent(); + Window window = mApp.GetWindow(); builder = Builder::New(); - builder.QuitSignal().Connect( this, &ExampleApp::OnBuilderQuit ); + builder.QuitSignal().Connect(this, &ExampleApp::OnQuitOrBack); Property::Map 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; + defaultDirs[TOKEN_STRING(DEMO_IMAGE_DIR)] = DEMO_IMAGE_DIR; + defaultDirs[TOKEN_STRING(DEMO_MODEL_DIR)] = DEMO_MODEL_DIR; + defaultDirs[TOKEN_STRING(DEMO_SCRIPT_DIR)] = DEMO_SCRIPT_DIR; - builder.AddConstants( defaultDirs ); + builder.AddConstants(defaultDirs); // render tasks may have been setup last load so remove them - RenderTaskList taskList = stage.GetRenderTaskList(); - if( taskList.GetTaskCount() > 1 ) + RenderTaskList taskList = window.GetRenderTaskList(); + if(taskList.GetTaskCount() > 1) { typedef std::vector Collection; - typedef Collection::iterator ColIter; - Collection tasks; + typedef Collection::iterator ColIter; + Collection tasks; for(unsigned int i = 1; i < taskList.GetTaskCount(); ++i) { - tasks.push_back( taskList.GetTask(i) ); + tasks.push_back(taskList.GetTask(i)); } for(ColIter iter = tasks.begin(); iter != tasks.end(); ++iter) @@ -458,15 +415,15 @@ public: } RenderTask defaultTask = taskList.GetTask(0); - defaultTask.SetSourceActor( stage.GetRootLayer() ); - defaultTask.SetTargetFrameBuffer( FrameBufferImage() ); + defaultTask.SetSourceActor(window.GetRootLayer()); + defaultTask.SetFrameBuffer(FrameBuffer()); } unsigned int numChildren = layer.GetChildCount(); - for(unsigned int i=0; i(Actor::Property::SIZE); size.y -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight; - mBuilderLayer.SetSize( size ); + mBuilderLayer.SetProperty(Actor::Property::SIZE, size); - mBuilderLayer.LowerToBottom(); - Stage::GetCurrent().GetRootLayer().RaiseToTop(); - - ExitSelection(); + mNavigationView.Push(mBuilderLayer); } void Create(Application& app) { - Stage stage = Stage::GetCurrent(); + Window window = app.GetWindow(); - Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent); + window.KeyEventSignal().Connect(this, &ExampleApp::OnKeyEvent); - Layer contents = DemoHelper::CreateView( app, - mView, - mToolBar, - BACKGROUND_IMAGE, - TOOLBAR_IMAGE, - "" ); + Layer contents = DemoHelper::CreateView(app, + mView, + mToolBar, + BACKGROUND_IMAGE, + TOOLBAR_IMAGE, + ""); - SetTitle("Builder"); + SetTitle("Select Example"); mBuilderLayer = Layer::New(); - stage.GetRootLayer().Add(mBuilderLayer); - // Create an edit mode button. (left of toolbar) - Toolkit::PushButton editButton = Toolkit::PushButton::New(); - editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) ); - editButton.ClickedSignal().Connect( this, &ExampleApp::OnToolSelectLayout); - editButton.SetLeaveRequired( true ); - mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING ); - - EnterSelection(); - - mTimer = Timer::New( 500 ); // ms - mTimer.TickSignal().Connect( this, &ExampleApp::OnTimer); + Toolkit::PushButton backButton = Toolkit::PushButton::New(); + backButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EDIT_IMAGE); + backButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EDIT_IMAGE_SELECTED); + backButton.ClickedSignal().Connect(this, &ExampleApp::OnBackButtonPressed); + backButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true); + mToolBar.AddControl(backButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_LEFT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING); + + mNavigationView = Toolkit::NavigationView::New(); + mNavigationView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS); + mNavigationView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT); + + window.Add(mNavigationView); + + // Set up the background gradient. + Property::Array stopOffsets; + stopOffsets.PushBack(0.0f); + stopOffsets.PushBack(1.0f); + Property::Array stopColors; + stopColors.PushBack(Color::WHITE); + stopColors.PushBack(Vector4(0.45f, 0.70f, 0.80f, 1.0f)); // Medium bright, pastel blue + const float percentageWindowHeight = window.GetSize().GetHeight() * 0.6f; + + mNavigationView.SetProperty(Toolkit::Control::Property::BACKGROUND, Dali::Property::Map().Add(Toolkit::Visual::Property::TYPE, Dali::Toolkit::Visual::GRADIENT).Add(Toolkit::GradientVisual::Property::STOP_OFFSET, stopOffsets).Add(Toolkit::GradientVisual::Property::STOP_COLOR, stopColors).Add(Toolkit::GradientVisual::Property::START_POSITION, Vector2(0.0f, -percentageWindowHeight)).Add(Toolkit::GradientVisual::Property::END_POSITION, Vector2(0.0f, percentageWindowHeight)).Add(Toolkit::GradientVisual::Property::UNITS, Toolkit::GradientVisual::Units::USER_SPACE)); + + SetUpItemView(); + mNavigationView.Push(mItemView); + + mTimer = Timer::New(500); // ms + mTimer.TickSignal().Connect(this, &ExampleApp::OnTimer); mTimer.Start(); } // Create(app) @@ -553,8 +521,8 @@ public: virtual Actor NewItem(unsigned int itemId) { - DALI_ASSERT_DEBUG( itemId < mFiles.size() ); - return MenuItem( ShortName( mFiles[itemId] ) ); + DALI_ASSERT_DEBUG(itemId < mFiles.size()); + return MenuItem(ShortName(mFiles[itemId])); } /** @@ -562,18 +530,11 @@ public: */ void OnKeyEvent(const KeyEvent& event) { - if(event.state == KeyEvent::Down) + if(event.GetState() == KeyEvent::DOWN) { - if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) ) + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) { - if ( mItemView.IsVisible() ) - { - mApp.Quit(); - } - else - { - EnterSelection(); - } + OnQuitOrBack(); } } } @@ -581,34 +542,32 @@ public: /** * Event handler when Builder wants to quit (we only want to close the shown json unless we're at the top-level) */ - void OnBuilderQuit() + void OnQuitOrBack() { - if ( mItemView.IsVisible() ) + if(mItemView.GetProperty(Actor::Property::CONNECTED_TO_SCENE)) { mApp.Quit(); } else { - EnterSelection(); + mNavigationView.Pop(); } } private: Application& mApp; - GridLayoutPtr mGridLayout; - ItemView mItemView; + ItemLayoutPtr mLayout; + ItemView mItemView; + Toolkit::NavigationView mNavigationView; - Toolkit::View mView; - unsigned int mOrientation; + Toolkit::Control mView; Toolkit::ToolBar mToolBar; - TextView mTitleActor; ///< The Toolbar's Title. + TextLabel mTitleActor; Layer mBuilderLayer; - Toolkit::Popup mMenu; - TapGestureDetector mTapDetector; // builder @@ -617,9 +576,7 @@ private: FileList mFiles; FileWatcher mFileWatcher; - Timer mTimer; - - + Timer mTimer; }; //------------------------------------------------------------------------------ @@ -627,7 +584,7 @@ private: // // //------------------------------------------------------------------------------ -int main(int argc, char **argv) +int DALI_EXPORT_API main(int argc, char** argv) { if(argc > 2) { @@ -637,7 +594,7 @@ int main(int argc, char **argv) } } - Application app = Application::New(&argc, &argv); + Application app = Application::New(&argc, &argv, DEMO_THEME_PATH); ExampleApp dali_app(app);