/*
- * 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.
#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
-#include <dali-toolkit/devel-api/image-atlas/image-atlas.h>
-
-#include <assert.h>
-#include <cstdlib>
-#include <string.h>
-#include <iostream>
-
-#include "shared/view.h"
+#include <dali-toolkit/devel-api/controls/page-turn-view/page-factory.h>
+#include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-landscape-view.h>
+#include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-portrait-view.h>
+#include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-view.h>
using namespace Dali;
using namespace Dali::Toolkit;
-// LOCAL STUFF
namespace
{
-const char* const CHANGE_IMAGE_ICON(DEMO_IMAGE_DIR "icon-change.png");
-const char* const CHANGE_IMAGE_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
-
-// The content amount of one page between portrait and landscape view are different
-// set a ratio to modify the current page number when the rotation is changed
-const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
const char* BOOK_COVER_PORTRAIT = ( DEMO_IMAGE_DIR "book-portrait-cover.jpg" );
const char* BOOK_COVER_LANDSCAPE = ( DEMO_IMAGE_DIR "book-landscape-cover.jpg" );
DEMO_IMAGE_DIR "book-portrait-p2.jpg",
DEMO_IMAGE_DIR "book-portrait-p3.jpg",
DEMO_IMAGE_DIR "book-portrait-p4.jpg",
- DEMO_IMAGE_DIR "book-portrait-p5.jpg"
+ DEMO_IMAGE_DIR "book-portrait-p5.jpg",
+ DEMO_IMAGE_DIR "book-portrait-p6.jpg",
+ DEMO_IMAGE_DIR "book-portrait-p7.jpg",
+ DEMO_IMAGE_DIR "book-portrait-p8.jpg"
};
const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
};
const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
-}// end LOCAL STUFF
+enum DemoOrientation
+{
+ PORTRAIT,
+ LANDSCAPE,
+ UNKNOWN
+};
+
+}
class PortraitPageFactory : public PageFactory
{
*/
virtual unsigned int GetNumberOfPages()
{
- return 10*NUMBER_OF_PORTRAIT_IMAGE + 1;
+ return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
}
+
/**
- * Create an image to represent a page.
+ * Create texture to represent a page.
* @param[in] pageId The ID of the page to create.
- * @return An image, or an uninitialized pointer if the ID is out of range.
+ * @return The texture.
*/
- virtual Image NewPage( unsigned int pageId )
+ virtual Texture NewPage( unsigned int pageId )
{
- Image page;
+ Texture texture;
+ PixelData pixels;
if( pageId == 0 )
{
- page = ResourceImage::New( BOOK_COVER_PORTRAIT );
+ pixels = SyncImageLoader::Load( BOOK_COVER_PORTRAIT );
}
else
{
- page = ResourceImage::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] );
+ pixels = SyncImageLoader::Load( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] );
+ }
+
+ if( pixels )
+ {
+ texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
+ texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
}
- return page;
+ return texture;
}
};
class LandscapePageFactory : public PageFactory
{
-
/**
* Query the number of pages available from the factory.
* The maximum available page has an ID of GetNumberOfPages()-1.
*/
virtual unsigned int GetNumberOfPages()
{
- return 10*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
+ return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
}
+
/**
- * Create an image to represent a page.
+ * Create texture to represent a page.
* @param[in] pageId The ID of the page to create.
- * @return An image, or an uninitialized pointer if the ID is out of range.
+ * @return The texture.
*/
- virtual Image NewPage( unsigned int pageId )
+ virtual Texture NewPage( unsigned int pageId )
{
- if( mImageSize.GetWidth() == 0u || mImageSize.GetHeight() == 0u )
- {
- mImageSize = ResourceImage::GetImageSize(BOOK_COVER_LANDSCAPE);
- }
+ Texture texture;
+
+ PixelData pixelsFront;
+ PixelData pixelsBack;
- ImageAtlas atlas = ImageAtlas::New( mImageSize.GetWidth()*2u, mImageSize.GetHeight(), Pixel::RGB888 );
- Vector4 textureRect;
if( pageId == 0 )
{
- atlas.Upload( textureRect, BOOK_COVER_LANDSCAPE, mImageSize );
- atlas.Upload( textureRect, BOOK_COVER_BACK_LANDSCAPE, mImageSize );
+ pixelsFront = SyncImageLoader::Load( BOOK_COVER_LANDSCAPE );
+ pixelsBack = SyncImageLoader::Load( BOOK_COVER_BACK_LANDSCAPE );
}
else
{
unsigned int imageId = (pageId-1)*2;
- atlas.Upload( textureRect, PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ], mImageSize );
- atlas.Upload( textureRect, PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ], mImageSize );
+ pixelsFront = SyncImageLoader::Load( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] );
+ pixelsBack = SyncImageLoader::Load( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] );
}
- return atlas.GetAtlas();
- }
+ if( pixelsFront && pixelsBack )
+ {
+ texture = Texture::New( TextureType::TEXTURE_2D, pixelsFront.GetPixelFormat(), pixelsFront.GetWidth()*2, pixelsFront.GetHeight() );
+ texture.Upload( pixelsFront, 0, 0, 0, 0, pixelsFront.GetWidth(), pixelsFront.GetHeight() );
+ texture.Upload( pixelsBack, 0, 0, pixelsFront.GetWidth(), 0, pixelsBack.GetWidth(), pixelsBack.GetHeight() );
+ }
- ImageDimensions mImageSize;
+ return texture;
+ }
};
/**
- * This example shows how to use the page turn UI control to implement the page-turn demo
- * The effect follows the pan gesture to animate the page
- * Pan the image inwards, the page will bent,
- * Depends on the distance of the panning, the image might turn over or slide back
- * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
- * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
-*/
-class PageTurnController : public ConnectionTracker
+ * This example shows how to use the PageTurnView UI control
+ */
+class PageTurnExample : public ConnectionTracker
{
public:
- PageTurnController( Application &app );
- ~PageTurnController();
- //This method gets called once the main loop of application is up and running
- void OnInit( Application& app );
+ PageTurnExample( Application &app );
-private:
+ ~PageTurnExample();
- /**
- * This method gets called when the button is clicked, switch between portrait and landscape views
- */
- bool OnButtonClicked(Toolkit::Button button);
+ void OnInit( Application& app );
- /**
- * Main key event handler
- */
- void OnKeyEvent(const KeyEvent& event);
+private:
- /**
- * Callback function of page turned signal
- * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
- * @param[in] pageIndex The index of the page turned over
- * @param[in] isTurningForward The turning direction, forwards or backwards
- */
- void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
- /**
- * Callback function of page turned signal
- * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
- * @param[in] pageIndex The index of the page turned over
- * @param[in] isTurningForward The turning direction, forwards or backwards
- */
- void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
+ void OnWindowResized( Window window, Window::WindowSize size );
- /**
- * Callback function of page started pan signal
- *
- * @param[in] pageTurnView The calling page turn view
- */
- void OnPageStartedPan( PageTurnView pageTurnView );
+ void Rotate( DemoOrientation orientation );
- /**
- * Callback function of page finished pan signal
- *
- * @param[in] pageTurnView The calling page turn view
- */
- void OnPageFinishedPan( PageTurnView pageTurnView );
+ void OnKeyEvent(const KeyEvent& event);
private:
Application& mApplication;
- Layer mButtonLayer;
PageTurnView mPageTurnPortraitView;
PageTurnView mPageTurnLandscapeView;
PortraitPageFactory mPortraitPageFactory;
LandscapePageFactory mLandscapePageFactory;
- bool mIsPortrait;
+ DemoOrientation mOrientation;
};
-PageTurnController::PageTurnController( Application &app )
-:mApplication( app ),
- mIsPortrait( true )
+PageTurnExample::PageTurnExample( Application &app )
+: mApplication( app ),
+ mOrientation( UNKNOWN )
{
- // Connect to the Application's Init signal
- app.InitSignal().Connect( this, &PageTurnController::OnInit );
+ app.InitSignal().Connect( this, &PageTurnExample::OnInit );
}
-PageTurnController::~PageTurnController()
+PageTurnExample::~PageTurnExample()
{
}
-
-void PageTurnController::OnInit( Application& app )
+/**
+ * The Init signal is received once (only) during the Application lifetime
+ */
+void PageTurnExample::OnInit( Application& app )
{
- // The Init signal is received once ( only ) during the Application lifetime
+ Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnExample::OnKeyEvent);
- Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
+ Window window = app.GetWindow();
+ window.AddAvailableOrientation( Window::PORTRAIT );
+ window.AddAvailableOrientation( Window::LANDSCAPE );
+ window.AddAvailableOrientation( Window::PORTRAIT_INVERSE );
+ window.AddAvailableOrientation( Window::LANDSCAPE_INVERSE );
+ window.ResizeSignal().Connect( this, &PageTurnExample::OnWindowResized );
- Stage stage = Stage::GetCurrent();
- Vector2 stageSize = stage.GetSize();
-
- mButtonLayer = Layer::New();
- mButtonLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
- mButtonLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
- mButtonLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
- Toolkit::PushButton button = Toolkit::PushButton::New();
- button.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
- button.SetParentOrigin( ParentOrigin::TOP_RIGHT );
- button.SetUnselectedImage( CHANGE_IMAGE_ICON );
- button.SetSelectedImage( CHANGE_IMAGE_ICON_SELECTED );
- button.SetLeaveRequired( true );
- button.SetScale(1.5f);
- button.PressedSignal().Connect( this, &PageTurnController::OnButtonClicked );
- stage.Add( mButtonLayer );
- mButtonLayer.Add(button);
-
- Vector2 bookSize( stageSize.x > stageSize.y ? stageSize.y : stageSize.x,
- stageSize.x > stageSize.y ? stageSize.x : stageSize.y );
-
- mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, bookSize );
- mPageTurnPortraitView.SetParentOrigin( ParentOrigin::CENTER );
- mPageTurnPortraitView.SetAnchorPoint( AnchorPoint::CENTER );
- mPageTurnPortraitView.SetProperty( PageTurnView::Property::SPINE_SHADOW, Vector2(70.f, 30.f) );
- mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
- mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
- mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
- mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
-
- mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(bookSize.y*0.5f, bookSize.x) );
- mPageTurnLandscapeView.SetParentOrigin( ParentOrigin::CENTER );
- mPageTurnLandscapeView.SetAnchorPoint( AnchorPoint::CENTER );
- mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
- mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
- mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
- mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
-
- if( stageSize.x > stageSize.y )
- {
- stage.Add(mPageTurnLandscapeView);
- mPageTurnPortraitView.SetOrientation(Degree(90.f), Vector3::ZAXIS);
- mIsPortrait = false;
- }
- else
- {
- stage.Add(mPageTurnPortraitView);
- mPageTurnLandscapeView.SetOrientation(Degree(90.f), Vector3::ZAXIS);
- mIsPortrait = true;
- }
+ Window::WindowSize size = window.GetSize();
+ Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
+}
- mButtonLayer.RaiseToTop();
+void PageTurnExample::OnWindowResized( Window window, Window::WindowSize size )
+{
+ Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
}
-bool PageTurnController::OnButtonClicked(Toolkit::Button button)
+void PageTurnExample::Rotate( DemoOrientation orientation )
{
- if( mIsPortrait )
- {
- mPageTurnPortraitView.Unparent();
- Stage::GetCurrent().Add( mPageTurnLandscapeView );
- int pageId = mPageTurnPortraitView.GetProperty( PageTurnView::Property::CURRENT_PAGE_ID ).Get<int>();
- int currentPage = ceil( static_cast<float>(pageId) / PAGE_NUMBER_CORRESPONDING_RATIO );
- mPageTurnLandscapeView.SetProperty(PageTurnView::Property::CURRENT_PAGE_ID, currentPage );
- }
- else
+ Stage stage = Stage::GetCurrent();
+ Vector2 stageSize = stage.GetSize();
+
+ if( mOrientation != orientation )
{
- mPageTurnLandscapeView.Unparent();
- Stage::GetCurrent().Add( mPageTurnPortraitView );
- int pageId = mPageTurnLandscapeView.GetProperty( PageTurnView::Property::CURRENT_PAGE_ID ).Get<int>();
- int currentPage = floor(pageId * PAGE_NUMBER_CORRESPONDING_RATIO );
- mPageTurnPortraitView.SetProperty(PageTurnView::Property::CURRENT_PAGE_ID, currentPage );
- }
+ mOrientation = orientation;
- mIsPortrait = !mIsPortrait;
- mButtonLayer.RaiseToTop();
- return true;
+ if( PORTRAIT == orientation )
+ {
+ if( !mPageTurnPortraitView )
+ {
+ mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
+ mPageTurnPortraitView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+ }
+
+ if( mPageTurnLandscapeView )
+ {
+ stage.Remove( mPageTurnLandscapeView );
+ }
+ stage.Add( mPageTurnPortraitView );
+ }
+ else if( LANDSCAPE == orientation )
+ {
+ if( !mPageTurnLandscapeView )
+ {
+ mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.x*0.5f, stageSize.y) );
+ mPageTurnLandscapeView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+ }
+
+ if( mPageTurnPortraitView )
+ {
+ stage.Remove( mPageTurnPortraitView );
+ }
+
+ stage.Add( mPageTurnLandscapeView );
+ }
+ }
}
/**
* Main key event handler
*/
-void PageTurnController::OnKeyEvent(const KeyEvent& event)
+void PageTurnExample::OnKeyEvent(const KeyEvent& event)
{
if(event.state == KeyEvent::Down)
{
}
}
-void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
-{
- std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
- << " page " << pageIndex
- << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
- << std::endl;
-}
-
-void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
-{
- std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
- << " page " << pageIndex
- << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
- << std::endl;
-}
-
-void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
-{
- std::cout<< "Starting to pan" << std::endl;
-}
-
-void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
-{
- std::cout<< "Finished panning" << std::endl;
-}
-
// Entry point for applications
-int main( int argc, char **argv )
+int DALI_EXPORT_API main( int argc, char **argv )
{
- Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
- PageTurnController test ( app );
+ Application app = Application::New(&argc, &argv);
+ PageTurnExample test ( app );
app.MainLoop();
return 0;
}
+