X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fimage-view%2Fimage-view-example.cpp;h=f6c74b8217294023df677c40fed8c99c7129cf7c;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=e2779c257d4fe5f524ed861ab06de3e87679860e;hpb=9a38745c71d57fe4d8926c3d776a835f946d025e;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/image-view/image-view-example.cpp b/examples/image-view/image-view-example.cpp index e2779c2..f6c74b8 100644 --- a/examples/image-view/image-view-example.cpp +++ b/examples/image-view/image-view-example.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 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. @@ -15,56 +15,74 @@ * */ -#include "shared/view.h" -#include #include -#include +#include +#include +#include +#include +#include +#include +#include "shared/view.h" using namespace Dali; namespace { - -const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" ); -const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); -const char* APPLICATION_TITLE( "Image view" ); +const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg"); +const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png"); +const char* APPLICATION_TITLE("Image view"); const char* IMAGE_PATH[] = { - DALI_IMAGE_DIR "blocks-ball.png", - DALI_IMAGE_DIR "gallery-small-23.jpg", - DALI_IMAGE_DIR "selection-popup-bg.2.9.png", - DALI_IMAGE_DIR "heartsframe.9.png", -}; + DEMO_IMAGE_DIR "gallery-small-23.jpg", + DEMO_IMAGE_DIR "woodEffect.jpg", + DEMO_IMAGE_DIR "wood.png", // 32bits image + DEMO_IMAGE_DIR "heartsframe.9.png", + DEMO_IMAGE_DIR "World.svg"}; -const char* RESOURCE_IMAGE_PATH[] = { - DALI_IMAGE_DIR "contacts-image.png", - DALI_IMAGE_DIR "gallery-small-27.jpg", - DALI_IMAGE_DIR "selection-popup-bg.8.9.png", - DALI_IMAGE_DIR "heartsframe.9.png", +const unsigned int NUMBER_OF_IMAGES = 3; + +enum CellPlacement +{ + TOP_BUTTON, + MID_BUTTON, + LOWER_BUTTON, + IMAGE, + NUMBER_OF_ROWS }; -const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*); -const unsigned int NUM_RESOURCE_IMAGES = sizeof(RESOURCE_IMAGE_PATH) / sizeof(char*); +unsigned int GetButtonIndex(Toolkit::Button button) +{ + std::string buttonName = button.GetProperty(Dali::Actor::Property::NAME); + unsigned int index = 0; + + if(buttonName != "") + { + index = std::stoul(buttonName); + } + + return index; +} + +const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*); -const unsigned int COLUMNS = 3; -const unsigned int ROWS = 4; +std::string EXAMPLE_INSTRUCTIONS = + "Instructions: Change button cycles through different image visuals, " + "on/off takes the ImageView and it's current visual on or off window."; -} // namespace +const float CORNER_RADIUS_VALUE(20.0f); -class ImageViewController: public ConnectionTracker +} // namespace + +class ImageViewController : public ConnectionTracker { - public: - - ImageViewController( Application& application ) - : mApplication( application ), - mCurrentPositionToggle( 0, 0 ), - mCurrentPositionImage( 0, 0 ), - mToggleOff( true ), - mUseResource( false ), - mImageIdx( 1 ) +public: + ImageViewController(Application& application) + : mApplication(application), + mCurrentPositionToggle(0, 0), + mCurrentPositionImage(0, 0) { // Connect to the Application's Init signal - mApplication.InitSignal().Connect( this, &ImageViewController::Create ); + mApplication.InitSignal().Connect(this, &ImageViewController::Create); } ~ImageViewController() @@ -72,186 +90,198 @@ class ImageViewController: public ConnectionTracker // Nothing to do here } - void Create( Application& application ) + void Create(Application& application) { // The Init signal is received once (only) during the Application lifetime // Creates a default view with a default tool bar. - // The view is added to the stage. - mContentLayer = DemoHelper::CreateView( application, - mView, - mToolBar, - BACKGROUND_IMAGE, - TOOLBAR_IMAGE, - APPLICATION_TITLE ); - - - mTable = Toolkit::TableView::New( ROWS, COLUMNS ); - mTable.SetAnchorPoint( AnchorPoint::CENTER ); - mTable.SetParentOrigin( ParentOrigin::CENTER ); - mTable.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS ); - Vector3 offset( -50.0f, -350.0f, 0.0f ); - mTable.SetSizeModeFactor( offset ); - - mContentLayer.Add( mTable ); - - for( unsigned int y = 0; y < ROWS; ++y ) + // The view is added to the window. + mContentLayer = DemoHelper::CreateView(application, + mView, + mToolBar, + BACKGROUND_IMAGE, + TOOLBAR_IMAGE, + APPLICATION_TITLE); + + // Create a table view to show a pair of buttons above each image. + mTable = Toolkit::TableView::New(CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES); + mTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mTable.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS); + Vector3 offset(0.9f, 0.70f, 0.0f); + mTable.SetProperty(Actor::Property::SIZE_MODE_FACTOR, offset); + mTable.SetFitHeight(CellPlacement::TOP_BUTTON); + mTable.SetFitHeight(CellPlacement::MID_BUTTON); + mTable.SetFitHeight(CellPlacement::LOWER_BUTTON); + mContentLayer.Add(mTable); + + Toolkit::TextLabel instructions = Toolkit::TextLabel::New(EXAMPLE_INSTRUCTIONS); + instructions.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH); + instructions.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER); + instructions.SetProperty(Actor::Property::POSITION_Y, -50.0f); + instructions.SetProperty(Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL, true); + instructions.SetProperty(Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT, 10); + mContentLayer.Add(instructions); + + for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++) { - for( unsigned int x = 0; x < COLUMNS; ++x ) - { - mImageViews[x][y] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] ); - mImageViews[x][y].SetParentOrigin( ParentOrigin::CENTER ); - mImageViews[x][y].SetAnchorPoint( AnchorPoint::CENTER ); - mImageViews[x][y].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); - - mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) ); - } + Toolkit::PushButton button = Toolkit::PushButton::New(); + button.SetProperty(Toolkit::Button::Property::LABEL, "on/off"); + button.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER); + button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER); + button.ClickedSignal().Connect(this, &ImageViewController::ToggleImageOnWindow); + button.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH); + button.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT); + std::string s = std::to_string(x); + button.SetProperty(Dali::Actor::Property::NAME, s); + mTable.AddChild(button, Toolkit::TableView::CellPosition(CellPlacement::TOP_BUTTON, x)); + + Toolkit::PushButton button2 = Toolkit::PushButton::New(); + button2.SetProperty(Toolkit::Button::Property::LABEL, "Change"); + button2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER); + button2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER); + button2.ClickedSignal().Connect(this, &ImageViewController::ChangeImageClicked); + button2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH); + button2.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT); + button2.SetProperty(Dali::Actor::Property::NAME, s); + mTable.AddChild(button2, Toolkit::TableView::CellPosition(CellPlacement::MID_BUTTON, x)); + + Toolkit::PushButton button3 = Toolkit::PushButton::New(); + button3.SetProperty(Toolkit::Button::Property::LABEL, "Round"); + button3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER); + button3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER); + button3.ClickedSignal().Connect(this, &ImageViewController::RoundedCornerClicked); + button3.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH); + button3.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT); + button3.SetProperty(Dali::Actor::Property::NAME, s); + mTable.AddChild(button3, Toolkit::TableView::CellPosition(CellPlacement::LOWER_BUTTON, x)); + + mImageViews[x] = Toolkit::ImageView::New(); + Property::Map imagePropertyMap; + imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); + imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[0]); + mImageViews[x].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap); + + mImageViews[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mImageViews[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mImageViews[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS); + mTable.AddChild(mImageViews[x], Toolkit::TableView::CellPosition(CellPlacement::IMAGE, x)); + + // Set changeable counter and toggle for each ImageView + mImageViewImageIndexStatus[x] = 0; + mImageViewToggleStatus[x] = true; + mImageViewRoundedCornerStatus[x] = false; } - Toolkit::TableView buttonsTable = Toolkit::TableView::New( 3, 1 ); - buttonsTable.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER ); - buttonsTable.SetParentOrigin( ParentOrigin::BOTTOM_CENTER ); - buttonsTable.SetFitHeight( 0 ); - buttonsTable.SetFitHeight( 1 ); - buttonsTable.SetFitHeight( 2 ); - buttonsTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); - - Toolkit::PushButton button = Toolkit::PushButton::New(); - button.SetLabelText( "Toggle on/off stage" ); - button.SetParentOrigin( ParentOrigin::CENTER ); - button.SetAnchorPoint( AnchorPoint::CENTER ); - button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage ); - button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); - buttonsTable.AddChild( button, Toolkit::TableView::CellPosition( 0, 0 ) ); - - Toolkit::PushButton button2 = Toolkit::PushButton::New(); - button2.SetLabelText( "Change Image" ); - button2.SetParentOrigin( ParentOrigin::CENTER ); - button2.SetAnchorPoint( AnchorPoint::CENTER ); - button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked ); - button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); - buttonsTable.AddChild( button2, Toolkit::TableView::CellPosition( 1, 0 ) ); - - Toolkit::CheckBoxButton button3 = Toolkit::CheckBoxButton::New(); - button3.SetLabelText( "Use Resource Images" ); - button3.SetParentOrigin( ParentOrigin::CENTER ); - button3.SetAnchorPoint( AnchorPoint::CENTER ); - button3.ClickedSignal().Connect( this, &ImageViewController::UseResourceImagesClicked ); - button3.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); - buttonsTable.AddChild( button3, Toolkit::TableView::CellPosition( 2, 0 ) ); - - mContentLayer.Add(buttonsTable); + application.GetWindow().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent); } private: - bool ToggleImageOnStage( Toolkit::Button button ) + void ImmediateLoadImage(const char* urlToLoad) { - Toolkit::ImageView imageView = mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ]; + Property::Map imagePropertyMap; + imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); + imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, urlToLoad); + Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap); + visual.Reset(); + } + + bool ToggleImageOnWindow(Toolkit::Button button) + { + unsigned int buttonIndex = GetButtonIndex(button); + + Toolkit::ImageView imageView = mImageViews[buttonIndex]; - if( mToggleOff ) + if(mImageViewToggleStatus[buttonIndex]) { imageView.Unparent(); } else { - mTable.AddChild( imageView, mCurrentPositionToggle ); + mTable.AddChild(imageView, Toolkit::TableView::CellPosition(CellPlacement::IMAGE, GetButtonIndex(button))); } - ++mCurrentPositionToggle.columnIndex; - if( mCurrentPositionToggle.columnIndex == COLUMNS ) - { - mCurrentPositionToggle.columnIndex = 0; - ++mCurrentPositionToggle.rowIndex; - } - if( mCurrentPositionToggle.rowIndex == ROWS ) - { - mCurrentPositionToggle.rowIndex = 0; - mToggleOff = !mToggleOff; - } + mImageViewToggleStatus[buttonIndex] = !mImageViewToggleStatus[buttonIndex]; return true; } - bool ChangeImageClicked( Toolkit::Button button ) + bool ChangeImageClicked(Toolkit::Button button) { - Toolkit::ImageView imageView = mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ]; + unsigned int buttonIndex = GetButtonIndex(button); - if( mUseResource ) - { - ResourceImage image = ResourceImage::New( RESOURCE_IMAGE_PATH[ mImageIdx ] ); - imageView.SetImage( image ); - } - else + if(mImageViews[buttonIndex].GetProperty(Actor::Property::CONNECTED_TO_SCENE)) { - imageView.SetImage( IMAGE_PATH[ mImageIdx ] ); - } + ++mImageViewImageIndexStatus[buttonIndex]; - ++mCurrentPositionImage.columnIndex; - if( mCurrentPositionImage.columnIndex == COLUMNS ) - { - mCurrentPositionImage.columnIndex = 0; - ++mCurrentPositionImage.rowIndex; - } - if( mCurrentPositionImage.rowIndex == ROWS ) - { - mCurrentPositionImage.rowIndex = 0; - ++mImageIdx; - - int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES; - if( mImageIdx == numImages ) + if(mImageViewImageIndexStatus[buttonIndex] == NUMBER_OF_RESOURCES) { - mImageIdx = 0; + mImageViewImageIndexStatus[buttonIndex] = 0; } - } + // Reset corner radius state value + mImageViewRoundedCornerStatus[buttonIndex] = false; + + Property::Map imagePropertyMap; + imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); + imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[mImageViewImageIndexStatus[buttonIndex]]); + mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap); + } return true; } - bool UseResourceImagesClicked( Toolkit::Button button ) + bool RoundedCornerClicked(Toolkit::Button button) { - mUseResource = !mUseResource; + unsigned int buttonIndex = GetButtonIndex(button); - int numImages = mUseResource ? NUM_RESOURCE_IMAGES : NUM_IMAGES; - if( mImageIdx >= numImages ) + if(mImageViews[buttonIndex].GetProperty(Actor::Property::CONNECTED_TO_SCENE)) { - mImageIdx = 0; - } + mImageViewRoundedCornerStatus[buttonIndex] = !mImageViewRoundedCornerStatus[buttonIndex]; + + Property::Map imagePropertyMap; + imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE); + imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, IMAGE_PATH[mImageViewImageIndexStatus[buttonIndex]]); + imagePropertyMap.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImageViewRoundedCornerStatus[buttonIndex] ? CORNER_RADIUS_VALUE : 0.0f); + mImageViews[buttonIndex].SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap); + } return true; } + /** + * Main key event handler + */ + void OnKeyEvent(const KeyEvent& event) + { + if(event.GetState() == KeyEvent::DOWN) + { + if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK)) + { + mApplication.Quit(); + } + } + } + private: - Application& mApplication; + Application& mApplication; - Toolkit::Control mView; ///< The View instance. - Toolkit::ToolBar mToolBar; ///< The View's Toolbar. - Layer mContentLayer; ///< Content layer - Toolkit::TableView mTable; - Toolkit::ImageView mImageViews[ COLUMNS ][ ROWS ]; + Toolkit::Control mView; ///< The View instance. + Toolkit::ToolBar mToolBar; ///< The View's Toolbar. + Layer mContentLayer; ///< Content layer + Toolkit::TableView mTable; + Toolkit::ImageView mImageViews[NUMBER_OF_IMAGES]; + bool mImageViewToggleStatus[NUMBER_OF_IMAGES]; + bool mImageViewRoundedCornerStatus[NUMBER_OF_IMAGES]; + unsigned int mImageViewImageIndexStatus[NUMBER_OF_IMAGES]; Toolkit::TableView::CellPosition mCurrentPositionToggle; Toolkit::TableView::CellPosition mCurrentPositionImage; - - bool mToggleOff; - bool mUseResource; - int mImageIdx; - }; -void RunTest( Application& application ) +int DALI_EXPORT_API main(int argc, char** argv) { - ImageViewController test( application ); - + Application application = Application::New(&argc, &argv, DEMO_THEME_PATH); + ImageViewController test(application); application.MainLoop(); -} - -// Entry point for Linux & Tizen applications -// -int main( int argc, char **argv ) -{ - Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH ); - - RunTest( application ); - return 0; }