X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fimage-scaling-irregular-grid%2Fimage-scaling-irregular-grid-example.cpp;h=cb41669c85cefcda1205f3df08a74f6115b38153;hb=1a473d5189ca7e7d55aca3a64a8a4ff2dc3b6c67;hp=39fe97ce278c78d627efc1b5626bad52beeec79d;hpb=46e6a0e92784c860b2b5425e34941c6574dd8062;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/image-scaling-irregular-grid/image-scaling-irregular-grid-example.cpp b/examples/image-scaling-irregular-grid/image-scaling-irregular-grid-example.cpp index 39fe97c..cb41669 100644 --- a/examples/image-scaling-irregular-grid/image-scaling-irregular-grid-example.cpp +++ b/examples/image-scaling-irregular-grid/image-scaling-irregular-grid-example.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. @@ -46,6 +46,10 @@ #include #include #include +#include +#include +#include // std::default_random_engine +#include // std::chrono::system_clock // INTERNAL INCLUDES #include "grid-flags.h" @@ -165,43 +169,34 @@ const char* IMAGE_PATHS[] = { NULL }; const unsigned NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u; +const unsigned int INITIAL_IMAGES_TO_LOAD = 10; /** - * Creates an Image + * Creates an ImageView * * @param[in] filename The path of the image. * @param[in] width The width of the image in pixels. * @param[in] height The height of the image in pixels. * @param[in] fittingMode The mode to use when scaling the image to fit the desired dimensions. */ -Image CreateImage(const std::string& filename, unsigned int width, unsigned int height, Dali::FittingMode::Type fittingMode ) +ImageView CreateImageView(const std::string& filename, int width, int height, Dali::FittingMode::Type fittingMode ) { -#ifdef DEBUG_PRINT_DIAGNOSTICS - fprintf( stderr, "CreateImage(%s, %u, %u, fittingMode=%u)\n", filename.c_str(), width, height, unsigned( fittingMode ) ); -#endif - Image image = ResourceImage::New( filename, ImageDimensions( width, height ), fittingMode, Dali::SamplingMode::BOX_THEN_LINEAR ); - return image; -} + ImageView imageView = ImageView::New(); -/** - * Creates an ImageView - * - * @param[in] filename The path of the image. - * @param[in] width The width of the image in pixels. - * @param[in] height The height of the image in pixels. - * @param[in] fittingMode The mode to use when scaling the image to fit the desired dimensions. - */ -ImageView CreateImageView(const std::string& filename, unsigned int width, unsigned int height, Dali::FittingMode::Type fittingMode ) -{ - Image img = CreateImage( filename, width, height, fittingMode ); - ImageView actor = ImageView::New( img ); - actor.SetName( filename ); - actor.SetParentOrigin(ParentOrigin::CENTER); - actor.SetAnchorPoint(AnchorPoint::CENTER); + Property::Map map; + map[Toolkit::ImageVisual::Property::URL] = filename; + map[Toolkit::ImageVisual::Property::DESIRED_WIDTH] = width; + map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = height; + map[Toolkit::ImageVisual::Property::FITTING_MODE] = fittingMode; + imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map ); + + imageView.SetProperty( Dali::Actor::Property::NAME, filename ); + imageView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER); + imageView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER); - return actor; + return imageView; } /** Cycle the scaling mode options. */ @@ -268,7 +263,8 @@ public: ImageScalingIrregularGridController( Application& application ) : mApplication( application ), - mScrolling( false ) + mScrolling( false ), + mImagesLoaded( 0 ) { std::cout << "ImageScalingIrregularGridController::ImageScalingIrregularGridController" << std::endl; @@ -282,20 +278,32 @@ public: } /** + * Called everytime an ImageView has loaded it's image + */ + void ResourceReadySignal( Toolkit::Control control ) + { + mImagesLoaded++; + // To allow fast startup, we only place a small number of ImageViews on window first + if ( mImagesLoaded == INITIAL_IMAGES_TO_LOAD ) + { + // Adding the ImageViews to the window will trigger loading of the Images + mGridActor.Add( mOffWindowImageViews ); + } + } + + + /** * One-time setup in response to Application InitSignal. */ void Create( Application& application ) { std::cout << "ImageScalingIrregularGridController::Create" << std::endl; - // Get a handle to the stage: - Stage stage = Stage::GetCurrent(); + // Get a handle to the window: + Window window = application.GetWindow(); // Connect to input event signals: - stage.KeyEventSignal().Connect(this, &ImageScalingIrregularGridController::OnKeyEvent); - - // Hide the indicator bar - mApplication.GetWindow().ShowIndicator(Dali::Window::INVISIBLE); + window.KeyEventSignal().Connect(this, &ImageScalingIrregularGridController::OnKeyEvent); // Create a default view with a default tool bar: mContentLayer = DemoHelper::CreateView( mApplication, @@ -307,13 +315,18 @@ public: // Create an image scaling toggle button. (right of toolbar) Toolkit::PushButton toggleScalingButton = Toolkit::PushButton::New(); - toggleScalingButton.SetUnselectedImage( TOGGLE_SCALING_IMAGE ); - toggleScalingButton.SetSelectedImage( TOGGLE_SCALING_IMAGE_SELECTED ); + toggleScalingButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE ); + toggleScalingButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE_SELECTED ); toggleScalingButton.ClickedSignal().Connect( this, &ImageScalingIrregularGridController::OnToggleScalingTouched ); - mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING ); + mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING ); SetTitle( APPLICATION_TITLE ); + mOffWindowImageViews = Actor::New(); + mOffWindowImageViews.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER ); + mOffWindowImageViews.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER); + mOffWindowImageViews.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); + // Build the main content of the widow: PopulateContentLayer( DEFAULT_SCALING_MODE ); } @@ -323,19 +336,19 @@ public: */ void PopulateContentLayer( const Dali::FittingMode::Type fittingMode ) { - Stage stage = Stage::GetCurrent(); - Vector2 stageSize = stage.GetSize(); + Window window = mApplication.GetWindow(); + Vector2 windowSize = window.GetSize(); float fieldHeight; - Actor imageField = BuildImageField( stageSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight ); + Actor imageField = BuildImageField( windowSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight ); mScrollView = ScrollView::New(); mScrollView.ScrollStartedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollStarted ); mScrollView.ScrollCompletedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollCompleted ); - mScrollView.SetAnchorPoint(AnchorPoint::CENTER); - mScrollView.SetParentOrigin(ParentOrigin::CENTER); + mScrollView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER); + mScrollView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER); mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); @@ -344,12 +357,12 @@ public: // Restrict scrolling to mostly vertical only, but with some horizontal wiggle-room: - RulerPtr rulerX = new FixedRuler( stageSize.width ); //< Pull the view back to the grid's centre-line when touch is release using a snapping ruler. - rulerX->SetDomain( RulerDomain( stageSize.width * -0.125f, stageSize.width * 1.125f ) ); //< Scroll slightly left/right of image field. + RulerPtr rulerX = new FixedRuler( windowSize.width ); //< Pull the view back to the grid's centre-line when touch is release using a snapping ruler. + rulerX->SetDomain( RulerDomain( windowSize.width * -0.125f, windowSize.width * 1.125f ) ); //< Scroll slightly left/right of image field. mScrollView.SetRulerX ( rulerX ); - RulerPtr rulerY = new DefaultRuler(); //< Snap in multiples of a screen / stage height - rulerY->SetDomain( RulerDomain( - fieldHeight * 0.5f + stageSize.height * 0.5f - GRID_CELL_PADDING, fieldHeight * 0.5f + stageSize.height * 0.5f + GRID_CELL_PADDING ) ); + RulerPtr rulerY = new DefaultRuler(); //< Snap in multiples of a screen / window height + rulerY->SetDomain( RulerDomain( - fieldHeight * 0.5f + windowSize.height * 0.5f - GRID_CELL_PADDING, fieldHeight * 0.5f + windowSize.height * 0.5f + GRID_CELL_PADDING ) ); mScrollView.SetRulerY ( rulerY ); mContentLayer.Add( mScrollView ); @@ -357,18 +370,18 @@ public: mGridActor = imageField; // Create the scroll bar - mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::Vertical); - mScrollBarVertical.SetParentOrigin(ParentOrigin::TOP_RIGHT); - mScrollBarVertical.SetAnchorPoint(AnchorPoint::TOP_RIGHT); + mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::VERTICAL); + mScrollBarVertical.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_RIGHT); + mScrollBarVertical.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_RIGHT); mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT); mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH); mScrollView.Add(mScrollBarVertical); - mScrollBarHorizontal = ScrollBar::New(Toolkit::ScrollBar::Horizontal); - mScrollBarHorizontal.SetParentOrigin(ParentOrigin::BOTTOM_LEFT); - mScrollBarHorizontal.SetAnchorPoint(AnchorPoint::TOP_LEFT); + mScrollBarHorizontal = ScrollBar::New(Toolkit::ScrollBar::HORIZONTAL); + mScrollBarHorizontal.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_LEFT); + mScrollBarHorizontal.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT); mScrollBarHorizontal.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH); - mScrollBarHorizontal.SetOrientation(Quaternion(Radian( 1.5f * Math::PI ), Vector3::ZAXIS)); + mScrollBarHorizontal.SetProperty( Actor::Property::ORIENTATION, Quaternion( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS) ) ); mScrollView.Add(mScrollBarHorizontal); mScrollView.OnRelayoutSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollViewRelayout ); @@ -380,7 +393,7 @@ public: void OnScrollViewRelayout(Actor actor) { // Make the height of the horizontal scroll bar to be the same as the width of scroll view. - mScrollBarHorizontal.SetSize(Vector2(0.0f, mScrollView.GetRelayoutSize( Dimension::WIDTH) )); + mScrollBarHorizontal.SetProperty( Actor::Property::SIZE, Vector2(0.0f, mScrollView.GetRelayoutSize( Dimension::WIDTH) )); } /** @@ -406,8 +419,10 @@ public: } } // Stir-up the list to get some nice irregularity in the generated field: - std::random_shuffle( configurations.begin(), configurations.end() ); - std::random_shuffle( configurations.begin(), configurations.end() ); + unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count(); + std::shuffle( configurations.begin(), configurations.end(), std::default_random_engine(seed) ); + seed = std::chrono::system_clock::now().time_since_epoch().count(); + std::shuffle( configurations.begin(), configurations.end(), std::default_random_engine(seed) ); // Place the images in the grid: @@ -440,8 +455,8 @@ public: Actor gridActor = Actor::New(); gridActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); - gridActor.SetParentOrigin( ParentOrigin::CENTER ); - gridActor.SetAnchorPoint( AnchorPoint::CENTER ); + gridActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + gridActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER ); // Work out the constants of the grid and cell dimensions and positions: const float cellWidth = fieldWidth / gridWidth; @@ -450,8 +465,9 @@ public: outFieldHeight = actualGridHeight * cellHeight; const Vector2 gridOrigin = Vector2( -fieldWidth * 0.5f, -outFieldHeight * 0.5 ); + unsigned int count = 0; // Build the image actors in their right locations in their parent's frame: - for( std::vector::const_iterator i = placedImages.begin(), end = placedImages.end(); i != end; ++i ) + for( std::vector::const_iterator i = placedImages.begin(), end = placedImages.end(); i != end; ++i, ++count ) { const PositionedImage& imageSource = *i; const Vector2 imageSize = imageSource.imageGridDims * cellSize - Vector2( GRID_CELL_PADDING * 2, GRID_CELL_PADDING * 2 ); @@ -459,14 +475,23 @@ public: const Vector2 imagePosition = imageRegionCorner + Vector2( GRID_CELL_PADDING , GRID_CELL_PADDING ) + imageSize * 0.5f; ImageView image = CreateImageView( imageSource.configuration.path, imageSize.x, imageSize.y, fittingMode ); - image.SetPosition( Vector3( imagePosition.x, imagePosition.y, 0 ) ); - image.SetSize( imageSize ); + image.SetProperty( Actor::Property::POSITION, Vector3( imagePosition.x, imagePosition.y, 0 ) ); + image.SetProperty( Actor::Property::SIZE, imageSize ); image.TouchedSignal().Connect( this, &ImageScalingIrregularGridController::OnTouchImage ); - mFittingModes[image.GetId()] = fittingMode; - mResourceUrls[image.GetId()] = imageSource.configuration.path; - mSizes[image.GetId()] = imageSize; - - gridActor.Add( image ); + image.ResourceReadySignal().Connect( this, &ImageScalingIrregularGridController::ResourceReadySignal ); + mFittingModes[image.GetProperty< int >( Actor::Property::ID )] = fittingMode; + mResourceUrls[image.GetProperty< int >( Actor::Property::ID )] = imageSource.configuration.path; + mSizes[image.GetProperty< int >( Actor::Property::ID )] = imageSize; + if ( count < INITIAL_IMAGES_TO_LOAD ) + { + gridActor.Add( image ); + } + else + { + // Store the ImageView in an offwindow actor until the inital batch of ImageViews have finished loading their images + // Required + mOffWindowImageViews.Add( image ); + } } return gridActor; @@ -475,14 +500,13 @@ public: /** * Upon Touching an image (Release), change its scaling mode and make it spin, provided we're not scrolling. * @param[in] actor The actor touched - * @param[in] event The TouchEvent. + * @param[in] event The Touch information. */ bool OnTouchImage( Actor actor, const TouchEvent& event ) { - if( (event.points.size() > 0) && (!mScrolling) ) + if( ( event.GetPointCount() > 0 ) && ( !mScrolling ) ) { - TouchPoint point = event.points[0]; - if(point.state == TouchPoint::Up) + if( event.GetState( 0 ) == PointState::UP ) { // Spin the image a few times: Animation animation = Animation::New(SPIN_DURATION); @@ -490,17 +514,22 @@ public: animation.Play(); // Change the scaling mode: - const unsigned id = actor.GetId(); + const unsigned id = actor.GetProperty< int >( Actor::Property::ID ); Dali::FittingMode::Type newMode = NextMode( mFittingModes[id] ); - const Vector2 imageSize = mSizes[actor.GetId()]; + const Vector2 imageSize = mSizes[actor.GetProperty< int >( Actor::Property::ID )]; - const std::string& url = mResourceUrls[id]; - Image newImage = CreateImage( url, imageSize.width + 0.5f, imageSize.height + 0.5f, newMode ); ImageView imageView = ImageView::DownCast( actor ); - if(imageView) + if( imageView) { - imageView.SetImage( newImage ); + Property::Map map; + map[Visual::Property::TYPE] = Visual::IMAGE; + map[ImageVisual::Property::URL] = mResourceUrls[id]; + map[ImageVisual::Property::DESIRED_WIDTH] = imageSize.width + 0.5f; + map[ImageVisual::Property::DESIRED_HEIGHT] = imageSize.height + 0.5f; + map[ImageVisual::Property::FITTING_MODE] = newMode; + imageView.SetProperty( ImageView::Property::IMAGE, map ); } + mFittingModes[id] = newMode; } } @@ -513,7 +542,7 @@ 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 ) ) @@ -538,12 +567,20 @@ public: if( gridImageView ) { // Cycle the scaling mode options: - unsigned int id = gridImageView.GetId(); + unsigned int id = gridImageView.GetProperty< int >( Actor::Property::ID ); const Vector2 imageSize = mSizes[ id ]; Dali::FittingMode::Type newMode = NextMode( mFittingModes[ id ] ); - Image newImage = CreateImage( mResourceUrls[ id ], imageSize.width, imageSize.height, newMode ); - gridImageView.SetImage( newImage ); + + Property::Map map; + map[Visual::Property::TYPE] = Visual::IMAGE; + map[ImageVisual::Property::URL] = mResourceUrls[id]; + map[ImageVisual::Property::DESIRED_WIDTH] = imageSize.width; + map[ImageVisual::Property::DESIRED_HEIGHT] = imageSize.height; + map[ImageVisual::Property::FITTING_MODE] = newMode; + gridImageView.SetProperty( ImageView::Property::IMAGE, map ); + + mFittingModes[ id ] = newMode; @@ -563,7 +600,7 @@ public: { 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 ); } mTitleActor.SetProperty( TextLabel::Property::TEXT, title ); @@ -597,6 +634,7 @@ private: Toolkit::ToolBar mToolBar; ///< The View's Toolbar. TextLabel mTitleActor; ///< The Toolbar's Title. Actor mGridActor; ///< The container for the grid of images + Actor mOffWindowImageViews; ///< ImageViews held off window until the inital batch have loaded their images ScrollView mScrollView; ///< ScrollView UI Component ScrollBar mScrollBarVertical; ScrollBar mScrollBarHorizontal; @@ -604,21 +642,13 @@ private: std::map mFittingModes; ///< Stores the current scaling mode of each image, keyed by image actor id. std::map mResourceUrls; ///< Stores the url of each image, keyed by image actor id. std::map mSizes; ///< Stores the current size of each image, keyed by image actor id. + unsigned int mImagesLoaded; ///< How many images have been loaded }; -void RunTest( Application& application ) +int DALI_EXPORT_API main( int argc, char **argv ) { + Application application = Application::New( &argc, &argv, DEMO_THEME_PATH ); ImageScalingIrregularGridController test( application ); - application.MainLoop(); -} - -/** Entry point for Linux & Tizen applications */ -int main( int argc, char **argv ) -{ - Application application = Application::New( &argc, &argv, DEMO_THEME_PATH ); - - RunTest( application ); - return 0; }