[dali_1.0.40] Merge branch 'tizen'
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
index 9697cc7..6a0a6e2 100644 (file)
@@ -21,7 +21,7 @@
 #include <string>
 #include <algorithm>
 #include <cstdlib> // rand
-#include "../shared/view.h"
+#include "shared/view.h"
 
 #include <dali/dali.h>
 #include <dali-toolkit/dali-toolkit.h>
@@ -99,9 +99,13 @@ const char* IMAGE_PATHS[] = {
 
 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
 
-AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunctions::Linear,
-                                    AlphaFunctions::EaseIn,
-                                    AlphaFunctions::EaseOut };
+const unsigned int IMAGE_WIDTH = 256;
+const unsigned int IMAGE_HEIGHT = 256;
+const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
+
+AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunction(AlphaFunction::LINEAR),
+                                    AlphaFunction(AlphaFunction::EASE_IN),
+                                    AlphaFunction(AlphaFunction::EASE_OUT) };
 
 const unsigned int NUM_ALPHA_FUNCTIONS = sizeof(ALPHA_FUNCTIONS) / sizeof(AlphaFunction);
 
@@ -156,23 +160,8 @@ const float BUTTON_BORDER = -10.0f;
 const float MENU_OPTION_HEIGHT(140.0f);
 const float LABEL_TEXT_SIZE_Y = 20.0f;
 
-const char*             DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
-const char*             DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
-const PointSize         DEFAULT_TEXT_STYLE_POINT_SIZE( 5.0f );
-const TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::MEDIUM);
-const Vector4           DEFAULT_TEXT_STYLE_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
-
 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
 
-struct BorderSizeConstraintFunction
-{
-  Vector3 operator()(const Vector3&       current,
-                     const PropertyInput& parentSize)
-  {
-    return parentSize.GetVector3() + ITEM_BORDER_MARGIN_SIZE;
-  }
-};
-
 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
 {
   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
@@ -245,13 +234,15 @@ public:
    */
   void OnInit(Application& app)
   {
+    DemoHelper::RequestThemeChange();
+
     Stage stage = Dali::Stage::GetCurrent();
     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
 
     Vector2 stageSize = Stage::GetCurrent().GetSize();
 
     // Create a border image shared by all the item actors
-    mBorderImage = Image::New(ITEM_BORDER_IMAGE_PATH);
+    mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
 
     // Creates a default view with a default tool bar.
     // The view is added to the stage.
@@ -262,18 +253,18 @@ public:
                                              TOOLBAR_IMAGE,
                                              "" );
 
-    mView.OrientationAnimationStartedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
+    app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
 
     // Create an edit mode button. (left of toolbar)
     Toolkit::PushButton editButton = Toolkit::PushButton::New();
-    editButton.SetBackgroundImage( Image::New( EDIT_IMAGE ) );
+    editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) );
     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
     editButton.SetLeaveRequired( true );
     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
 
     // Create a layout toggle button. (right of toolbar)
     mLayoutButton = Toolkit::PushButton::New();
-    mLayoutButton.SetBackgroundImage( Image::New( SPIRAL_LAYOUT_IMAGE ) );
+    mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
     mLayoutButton.SetLeaveRequired( true );
     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
@@ -284,9 +275,9 @@ public:
     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
-    mDeleteButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
-    mDeleteButton.SetButtonImage( Image::New( DELETE_IMAGE ) );
-    mDeleteButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
+    mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
+    mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
+    mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
     mDeleteButton.SetLeaveRequired( true );
     mDeleteButton.SetVisible( false );
@@ -298,8 +289,8 @@ public:
     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mInsertButton.SetDrawMode( DrawMode::OVERLAY );
-    mInsertButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
-    mInsertButton.SetButtonImage( Image::New( INSERT_IMAGE ) );
+    mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
+    mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
     mInsertButton.SetLeaveRequired( true );
@@ -312,8 +303,8 @@ public:
     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
-    mReplaceButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
-    mReplaceButton.SetButtonImage( Image::New( REPLACE_IMAGE ) );
+    mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
+    mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
     mReplaceButton.SetLeaveRequired( true );
@@ -321,6 +312,7 @@ public:
     stage.Add( mReplaceButton );
 
     // Create the item view actor
+    mImageAtlas = CreateImageAtlas();
     mItemView = ItemView::New(*this);
     mItemView.SetParentOrigin(ParentOrigin::CENTER);
     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
@@ -349,6 +341,9 @@ public:
     // Set the title and icon to the current layout
     SetLayoutTitle();
     SetLayoutImage();
+
+    // Store one 1x1 white image for multiple items to share for backgrounds:
+    mWhiteImage = BufferImage::WHITE();
   }
 
   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocusNavigationDirection direction )
@@ -419,7 +414,7 @@ public:
    * Orientation changed signal callback
    * @param orientation
    */
-  void OnOrientationChanged( View view, Animation& animation, const Orientation& orientation )
+  void OnOrientationChanged( Orientation orientation )
   {
     const unsigned int angle = orientation.GetDegrees();
 
@@ -841,19 +836,19 @@ public:
       {
         case SPIRAL_LAYOUT:
         {
-          mLayoutButton.SetBackgroundImage( Image::New( SPIRAL_LAYOUT_IMAGE ) );
+          mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
           break;
         }
 
         case GRID_LAYOUT:
         {
-          mLayoutButton.SetBackgroundImage( Image::New( GRID_LAYOUT_IMAGE ) );
+          mLayoutButton.SetBackgroundImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
           break;
         }
 
         case DEPTH_LAYOUT:
         {
-          mLayoutButton.SetBackgroundImage( Image::New( DEPTH_LAYOUT_IMAGE ) );
+          mLayoutButton.SetBackgroundImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
           break;
         }
 
@@ -882,8 +877,12 @@ public: // From ItemFactory
   virtual Actor NewItem(unsigned int itemId)
   {
     // Create an image actor for this item
-    Image image = Image::New( IMAGE_PATHS[itemId % NUM_IMAGES] );
-    Actor actor = ImageActor::New(image);
+    unsigned int imageId = itemId % NUM_IMAGES;
+    ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
+                                     (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
+                                      IMAGE_WIDTH,
+                                      IMAGE_HEIGHT );
+    Actor actor = ImageActor::New(mImageAtlas, pixelArea);
     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
 
     // Add a border image child actor
@@ -894,9 +893,8 @@ public: // From ItemFactory
     borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
     borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
     borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
-
-    Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), BorderSizeConstraintFunction() );
-    borderActor.ApplyConstraint(constraint);
+    borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
+    borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
     actor.Add(borderActor);
     actor.SetKeyboardFocusable( true );
 
@@ -905,7 +903,7 @@ public: // From ItemFactory
 
     // Add a checkbox child actor; invisible until edit-mode is enabled
 
-    ImageActor checkbox = ImageActor::New( BitmapImage::WHITE() );
+    ImageActor checkbox = ImageActor::New( mWhiteImage );
     checkbox.SetName( "CheckBox" );
     checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
@@ -921,7 +919,7 @@ public: // From ItemFactory
     }
     actor.Add( checkbox );
 
-    ImageActor tick = ImageActor::New( Image::New(SELECTED_IMAGE) );
+    ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
     tick.SetColorMode( USE_OWN_COLOR );
     tick.SetName( "Tick" );
     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
@@ -943,6 +941,23 @@ public: // From ItemFactory
 private:
 
   /**
+   * Create an Atlas to tile the images inside.
+   */
+  Atlas CreateImageAtlas()
+  {
+    const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
+    const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
+    Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
+
+    for( unsigned int i = 0; i < NUM_IMAGES; i++ )
+    {
+      atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
+    }
+
+    return atlas;
+  }
+
+  /**
    * Sets/Updates the title of the View
    * @param[in] title The new title for the view.
    */
@@ -950,15 +965,12 @@ private:
   {
     if(!mTitleActor)
     {
-      mTitleActor = TextView::New();
+      mTitleActor = DemoHelper::CreateToolBarLabel( "" );
       // 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());
+    mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
   }
 
   void ShowMenu()
@@ -969,61 +981,48 @@ private:
     mMenu = Toolkit::Popup::New();
     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
+    mMenu.SetSize( popupWidth, MENU_OPTION_HEIGHT * 2 );
     mMenu.OutsideTouchedSignal().Connect( this, &ItemViewExample::HideMenu );
-    stage.Add( mMenu );
 
     TableView tableView = TableView::New( 0, 0 );
-    Vector2 tableSize = Vector2( popupWidth, MENU_OPTION_HEIGHT * 2 );
-    tableView.SetSize( tableSize );
+    tableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
     mMenu.Add( tableView );
 
     Slider slider = Slider::New();
-    slider.SetProperty( Slider::LOWER_BOUND_PROPERTY, 0.0f );
-    slider.SetProperty( Slider::UPPER_BOUND_PROPERTY, 3.0f );
-    slider.SetProperty( Slider::VALUE_PROPERTY, mDurationSeconds );
-    slider.SetProperty( Slider::VALUE_PRECISION_PROPERTY, 2 );
-    slider.SetProperty( Slider::SHOW_POPUP_PROPERTY, true );
+    slider.SetProperty( Slider::Property::LOWER_BOUND, 0.0f );
+    slider.SetProperty( Slider::Property::UPPER_BOUND, 3.0f );
+    slider.SetProperty( Slider::Property::VALUE, mDurationSeconds );
+    slider.SetProperty( Slider::Property::VALUE_PRECISION, 2 );
+    slider.SetProperty( Slider::Property::SHOW_POPUP, true );
+    slider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
     slider.ValueChangedSignal().Connect( this, &ItemViewExample::SliderValueChange );
     tableView.AddChild( slider, TableView::CellPosition( 0, 0 ) );
-    tableView.SetRelativeHeight( 0, 0.5f );
 
-    TextStyle defaultTextStyle;
-    defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
-    defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
-    defaultTextStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
-    defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
-    defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
-
-    TextView text = TextView::New( "Duration" );
+    TextLabel text = TextLabel::New( "Duration" );
     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
-    text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
-    text.SetStyleToCurrentText( defaultTextStyle );
-    text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
-    text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
-    text.SetZ( -0.9f );
+    text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
+    text.SetResizePolicy( ResizePolicy::FIXED, Dimension::HEIGHT );
+    text.SetSize( Vector2( 0.0f, LABEL_TEXT_SIZE_Y ) );
     slider.Add( text );
 
     Actor textContainer = Actor::New();
-    mAlphaFunctionText = TextView::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
+    textContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+    mAlphaFunctionText = TextLabel::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
     mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
     mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
-    mAlphaFunctionText.SetTextAlignment( Toolkit::Alignment::VerticalCenter );
     textContainer.Add( mAlphaFunctionText );
     tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
-    tableView.SetRelativeHeight( 0, 0.5f );
 
     mTapDetector = TapGestureDetector::New();
     mTapDetector.Attach(mAlphaFunctionText);
     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
 
-    text = TextView::New( "Alpha Function" );
+    text = TextLabel::New( "Alpha Function" );
     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
-    text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
-    text.SetStyleToCurrentText( defaultTextStyle );
+    text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
     text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
-    text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
     textContainer.Add( text );
 
     mMenu.Show();
@@ -1046,7 +1045,7 @@ private:
 
     if( mAlphaFunctionText )
     {
-      mAlphaFunctionText.SetText( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
+      mAlphaFunctionText.SetProperty( TextLabel::Property::TEXT, std::string(ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex]) );
     }
 
     if( mItemView )
@@ -1106,14 +1105,15 @@ private:
   Mode mMode;
   bool mMenuShown;
 
-  Toolkit::View mView;
+  Toolkit::Control mView;
   unsigned int mOrientation;
 
   Toolkit::ToolBar mToolBar;
-  TextView mTitleActor;             ///< The Toolbar's Title.
+  TextLabel mTitleActor;             ///< The Toolbar's Title.
 
   ItemView mItemView;
   Image mBorderImage;
+  Atlas mImageAtlas;
   unsigned int mCurrentLayout;
   float mDurationSeconds;
 
@@ -1130,7 +1130,8 @@ private:
   Toolkit::PushButton mReplaceButton;
 
   unsigned int mAlphaFuncIndex;
-  TextView mAlphaFunctionText;
+  TextLabel mAlphaFunctionText;
+  BufferImage mWhiteImage;
 };
 
 void RunTest(Application& app)