[dali_1.0.37] Merge branch 'tizen'
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
index a3cd43a..448735c 100644 (file)
@@ -99,6 +99,10 @@ const char* IMAGE_PATHS[] = {
 
 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
 
+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[] = { AlphaFunctions::Linear,
                                     AlphaFunctions::EaseIn,
                                     AlphaFunctions::EaseOut };
@@ -156,12 +160,6 @@ 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 );
 
 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
@@ -236,6 +234,8 @@ public:
    */
   void OnInit(Application& app)
   {
+    DemoHelper::RequestThemeChange();
+
     Stage stage = Dali::Stage::GetCurrent();
     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
 
@@ -275,9 +275,9 @@ public:
     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
-    mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
     mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
-    mDeleteButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
+    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 );
@@ -289,8 +289,8 @@ public:
     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mInsertButton.SetDrawMode( DrawMode::OVERLAY );
-    mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_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 );
@@ -303,8 +303,8 @@ public:
     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
     mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
-    mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_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 );
@@ -312,7 +312,9 @@ public:
     stage.Add( mReplaceButton );
 
     // Create the item view actor
+    mImageAtlas = CreateImageAtlas();
     mItemView = ItemView::New(*this);
+    mItemView.SetRelayoutEnabled( false );
     mItemView.SetParentOrigin(ParentOrigin::CENTER);
     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
 
@@ -876,8 +878,12 @@ public: // From ItemFactory
   virtual Actor NewItem(unsigned int itemId)
   {
     // Create an image actor for this item
-    Image image = ResourceImage::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
@@ -888,7 +894,7 @@ 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
-    borderActor.SetSizeMode( SIZE_FIXED_OFFSET_FROM_PARENT );
+    borderActor.SetResizePolicy( SIZE_FIXED_OFFSET_FROM_PARENT, ALL_DIMENSIONS );
     borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
     actor.Add(borderActor);
     actor.SetKeyboardFocusable( true );
@@ -899,6 +905,7 @@ public: // From ItemFactory
     // Add a checkbox child actor; invisible until edit-mode is enabled
 
     ImageActor checkbox = ImageActor::New( mWhiteImage );
+    checkbox.SetRelayoutEnabled( false );
     checkbox.SetName( "CheckBox" );
     checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
@@ -915,6 +922,7 @@ public: // From ItemFactory
     actor.Add( checkbox );
 
     ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
+    tick.SetRelayoutEnabled( false );
     tick.SetColorMode( USE_OWN_COLOR );
     tick.SetName( "Tick" );
     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
@@ -936,6 +944,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.
    */
@@ -943,15 +968,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()
@@ -962,12 +984,11 @@ 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( FILL_TO_PARENT, ALL_DIMENSIONS );
     mMenu.Add( tableView );
 
     Slider slider = Slider::New();
@@ -976,49 +997,39 @@ private:
     slider.SetProperty( Slider::Property::VALUE, mDurationSeconds );
     slider.SetProperty( Slider::Property::VALUE_PRECISION, 2 );
     slider.SetProperty( Slider::Property::SHOW_POPUP, true );
+    slider.SetResizePolicy( FILL_TO_PARENT, 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::Property::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::Property::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
-    text.SetZ( -0.9f );
+    text.SetResizePolicy( FILL_TO_PARENT, WIDTH );
+    text.SetResizePolicy( FIXED, 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.SetRelayoutEnabled( true );
+    textContainer.SetResizePolicy( FILL_TO_PARENT, 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( FILL_TO_PARENT, WIDTH );
     text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
-    text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::Property::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::Property::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
     textContainer.Add( text );
 
+    mMenu.MarkDirtyForRelayout();
     mMenu.Show();
     mMenuShown = true;
   }
@@ -1039,7 +1050,7 @@ private:
 
     if( mAlphaFunctionText )
     {
-      mAlphaFunctionText.SetText( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
+      mAlphaFunctionText.SetProperty( TextLabel::Property::TEXT, std::string(ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex]) );
     }
 
     if( mItemView )
@@ -1103,10 +1114,11 @@ private:
   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;
 
@@ -1123,7 +1135,7 @@ private:
   Toolkit::PushButton mReplaceButton;
 
   unsigned int mAlphaFuncIndex;
-  TextView mAlphaFunctionText;
+  TextLabel mAlphaFunctionText;
   BufferImage mWhiteImage;
 };