[dali_1.2.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / scrollable / item-view / item-layout.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 177c1d8..a9f2769
@@ -1,54 +1,88 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2015 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
+// CLASS HEADER
 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
 
+// EXTERNAL INCLUDES
+#include <dali/public-api/animation/animation.h>
+#include <dali/public-api/animation/constraint.h>
+#include <dali/public-api/animation/time-period.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
+#include <dali-toolkit/devel-api/controls/scrollable/item-view/default-item-layout-property.h>
+
 namespace Dali
 {
 
 namespace Toolkit
 {
 
+struct ItemLayout::Impl
+{
+  Vector3 mItemSize;                              ///< The size of an item in the layout
+  ControlOrientation::Type mOrientation;          ///< the orientation of the layout.
+  Property::Map mProperties;
+  bool mHasLayoutChanged;
+};
+
 ItemLayout::ItemLayout()
-: mOrientation(ControlOrientation::Up)
+: mImpl( new Impl )
 {
+  mImpl->mOrientation = ControlOrientation::Up;
 }
 
 ItemLayout::~ItemLayout()
 {
+  delete mImpl;
 }
 
 void ItemLayout::SetOrientation(ControlOrientation::Type orientation)
 {
-  mOrientation = orientation;
+  mImpl->mOrientation = orientation;
 }
 
 ControlOrientation::Type ItemLayout::GetOrientation() const
 {
-  return mOrientation;
+  return mImpl->mOrientation;
 }
 
-float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
+void ItemLayout::GetItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
 {
-  ItemLayout::Vector3Function positionConstraint;
-  Vector3 itemPosition = Vector3::ZERO;
-  if (GetPositionConstraint(itemID, positionConstraint))
+  // If item-size has not been set then get the default size
+  if ( mImpl->mItemSize == Vector3::ZERO )
   {
-    itemPosition = positionConstraint(Vector3::ZERO, currentLayoutPosition + itemID, 0.0f, layoutSize);
+    GetDefaultItemSize( itemId, layoutSize, itemSize );
   }
+  else
+  {
+    itemSize = mImpl->mItemSize;
+  }
+}
+
+void ItemLayout::SetItemSize( const Vector3& itemSize )
+{
+  mImpl->mItemSize = itemSize;
+}
+
+float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize)
+{
+  Vector3 itemPosition = GetItemPosition( itemID, currentLayoutPosition, layoutSize );
   Vector3 itemSize;
   GetItemSize(itemID, layoutSize, itemSize);
   Vector3 onScreenArea = (layoutSize - itemSize) * 0.5f;
@@ -64,176 +98,81 @@ float ItemLayout::GetClosestOnScreenLayoutPosition(int itemID, float currentLayo
   return currentLayoutPosition;
 }
 
-void ItemLayout::GetXAxisScrollHint(Vector2& scrollHint) const
+int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
 {
-  scrollHint = Vector2::ZERO;
-  Radian scrollAngle(GetScrollDirection());
-  Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
-  switch(mOrientation)
+  switch( direction )
   {
-    case ControlOrientation::Up:
-    {
-      if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
-      {
-        // we probably want x scrolling
-        if(scrollDirection.x > 0.0f)
-        {
-          // normal positive scrolling
-          scrollHint = Vector2::XAXIS;
-        }
-        else
-        {
-          scrollHint = -Vector2::XAXIS;
-        }
-      }
-      break;
-    }
-    case ControlOrientation::Down:
+    case Control::KeyboardFocus::LEFT:
+    case Control::KeyboardFocus::UP:
     {
-      if(fabsf(scrollDirection.y) < Math::MACHINE_EPSILON_1)
+      itemID--;
+      if( itemID < 0 )
       {
-        // we probably want x scrolling
-        if(scrollDirection.x > 0.0f)
-        {
-          // normal positive scrolling
-          scrollHint = -Vector2::XAXIS;
-        }
-        else
-        {
-          scrollHint = Vector2::XAXIS;
-        }
+        itemID = loopEnabled ? maxItems - 1 : 0;
       }
       break;
     }
-    case ControlOrientation::Left:
+    case Control::KeyboardFocus::RIGHT:
+    case Control::KeyboardFocus::DOWN:
     {
-      // we probably want x scrolling
-      if(scrollDirection.x > 0.0f)
-      {
-        // normal positive scrolling
-        scrollHint = Vector2::XAXIS;
-      }
-      else
+      itemID++;
+      if( itemID >= maxItems )
       {
-        scrollHint = -Vector2::XAXIS;
+        itemID = loopEnabled ? 0 : maxItems - 1;
       }
       break;
     }
-    case ControlOrientation::Right:
+    default:
     {
-      // we probably want x scrolling
-      if(scrollDirection.x > 0.0f)
-      {
-        // normal positive scrolling
-        scrollHint = -Vector2::XAXIS;
-      }
-      else
-      {
-        scrollHint = Vector2::XAXIS;
-      }
       break;
     }
   }
+  return itemID;
 }
 
-void ItemLayout::GetYAxisScrollHint(Vector2& scrollHint) const
+float ItemLayout::GetFlickSpeedFactor() const
 {
-  scrollHint = Vector2::ZERO;
-  Radian scrollAngle(GetScrollDirection());
-  Vector2 scrollDirection(sinf(scrollAngle), cosf(scrollAngle));
-  switch(mOrientation)
-  {
-    case ControlOrientation::Up:
-    {
-      // we probably want x scrolling
-      if(scrollDirection.y > 0.0f)
-      {
-        // normal positive scrolling
-        scrollHint = Vector2::YAXIS;
-      }
-      else
-      {
-        scrollHint = -Vector2::YAXIS;
-      }
-      break;
-    }
-    case ControlOrientation::Down:
-    {
-      // we probably want x scrolling
-      if(scrollDirection.y > 0.0f)
-      {
-        // normal positive scrolling
-        scrollHint = -Vector2::YAXIS;
-      }
-      else
-      {
-        scrollHint = Vector2::YAXIS;
-      }
-      break;
-    }
-    case ControlOrientation::Left:
-    {
-      if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
-      {
-        // we probably want x scrolling
-        if(scrollDirection.y > 0.0f)
-        {
-          // normal positive scrolling
-          scrollHint = -Vector2::YAXIS;
-        }
-        else
-        {
-          scrollHint = Vector2::YAXIS;
-        }
-      }
-      break;
-    }
-    case ControlOrientation::Right:
-    {
-      if(fabsf(scrollDirection.x) < Math::MACHINE_EPSILON_1)
-      {
-        // we probably want x scrolling
-        if(scrollDirection.y > 0.0f)
-        {
-          // normal positive scrolling
-          scrollHint = Vector2::YAXIS;
-        }
-        else
-        {
-          scrollHint = -Vector2::YAXIS;
-        }
-      }
-      break;
-    }
-  }
+  // By default, the speed factor while dragging and swiping is the same.
+  return GetScrollSpeedFactor();
 }
 
-int ItemLayout::GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
+void ItemLayout::SetLayoutProperties(const Property::Map& properties)
 {
-  switch( direction )
+  for( unsigned int idx = 0, mapCount = properties.Count(); idx < mapCount; ++idx )
   {
-    case Control::Left:
-    case Control::Up:
+    KeyValuePair propertyPair( properties.GetKeyValue( idx ) );
+
+    if(propertyPair.first == DefaultItemLayoutProperty::ITEM_SIZE)
     {
-      itemID--;
-      if( itemID < 0 )
-      {
-        itemID = loopEnabled ? maxItems - 1 : 0;
-      }
-      break;
+      SetItemSize(propertyPair.second.Get<Vector3>());
     }
-    case Control::Right:
-    case Control::Down:
+    else if(propertyPair.first == DefaultItemLayoutProperty::ORIENTATION)
     {
-      itemID++;
-      if( itemID >= maxItems )
+      //Up, Left, Down, Right
+      int orientationType = propertyPair.second.Get<int>();
+      if(orientationType <= ControlOrientation::Right && orientationType >= ControlOrientation::Up)
       {
-        itemID = loopEnabled ? 0 : maxItems - 1;
+        SetOrientation(ControlOrientation::Type(orientationType));
       }
-      break;
     }
   }
-  return itemID;
+  mImpl->mHasLayoutChanged = true;
+  mImpl->mProperties = properties;
+}
+
+Property::Map ItemLayout::GetLayoutProperties()
+{
+  return mImpl->mProperties;
+}
+
+bool ItemLayout::HasLayoutChanged()
+{
+  return mImpl->mHasLayoutChanged;
+}
+
+void ItemLayout::ResetLayoutChangedFlag()
+{
+  mImpl->mHasLayoutChanged = false;
 }
 
 } // namespace Toolkit