axmol/cocos/ui/UIListView.cpp

980 lines
27 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2013-09-13 22:20:20 +08:00
2014-03-11 17:13:54 +08:00
#include "ui/UIListView.h"
#include "ui/UIHelper.h"
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
static const float DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM = 1.0f;
namespace ui {
2014-03-04 16:51:35 +08:00
IMPLEMENT_CLASS_GUI_INFO(ListView)
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
ListView::ListView():
2013-11-14 11:37:46 +08:00
_model(nullptr),
2014-05-12 09:51:23 +08:00
_gravity(Gravity::CENTER_VERTICAL),
_magneticType(MagneticType::NONE),
_magneticAllowedOutOfBoundary(true),
2013-11-08 20:29:49 +08:00
_itemsMargin(0.0f),
_curSelectedIndex(-1),
_innerContainerDoLayoutDirty(true),
_listViewEventListener(nullptr),
_listViewEventSelector(nullptr),
2014-05-12 09:51:23 +08:00
_eventCallback(nullptr)
2013-09-13 22:20:20 +08:00
{
2014-06-24 15:51:14 +08:00
this->setTouchEnabled(true);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
ListView::~ListView()
2013-09-13 22:20:20 +08:00
{
2013-11-14 11:37:46 +08:00
_listViewEventListener = nullptr;
_listViewEventSelector = nullptr;
2013-12-23 15:02:52 +08:00
_items.clear();
2014-03-31 11:34:26 +08:00
CC_SAFE_RELEASE(_model);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
ListView* ListView::create()
2013-09-13 22:20:20 +08:00
{
ListView* widget = new (std::nothrow) ListView();
2013-09-13 22:20:20 +08:00
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool ListView::init()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
if (ScrollView::init())
2013-09-13 22:20:20 +08:00
{
setDirection(Direction::VERTICAL);
2013-09-13 22:20:20 +08:00
return true;
}
return false;
}
2013-12-23 15:02:52 +08:00
void ListView::setItemModel(Widget *model)
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
if (nullptr == model)
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
CCLOG("Can't set a nullptr to item model!");
2013-11-06 16:04:06 +08:00
return;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
CC_SAFE_RELEASE_NULL(_model);
_model = model;
CC_SAFE_RETAIN(_model);
2013-09-13 22:20:20 +08:00
}
void ListView::handleReleaseLogic(Touch *touch)
{
ScrollView::handleReleaseLogic(touch);
if(!_autoScrolling)
{
startMagneticScroll();
}
}
2013-12-23 15:02:52 +08:00
void ListView::updateInnerContainerSize()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
switch (_direction)
2013-09-13 22:20:20 +08:00
{
2014-05-12 11:08:10 +08:00
case Direction::VERTICAL:
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
size_t length = _items.size();
2013-12-23 15:02:52 +08:00
float totalHeight = (length - 1) * _itemsMargin;
2013-12-26 16:17:52 +08:00
for (auto& item : _items)
2013-12-23 15:02:52 +08:00
{
2014-06-20 11:18:53 +08:00
totalHeight += item->getContentSize().height;
2013-12-23 15:02:52 +08:00
}
float finalWidth = _contentSize.width;
2013-11-06 16:04:06 +08:00
float finalHeight = totalHeight;
2013-12-23 15:02:52 +08:00
setInnerContainerSize(Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
2014-05-12 11:08:10 +08:00
case Direction::HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
size_t length = _items.size();
2013-12-23 15:02:52 +08:00
float totalWidth = (length - 1) * _itemsMargin;
2013-12-26 16:17:52 +08:00
for (auto& item : _items)
2013-12-23 15:02:52 +08:00
{
2014-06-20 11:18:53 +08:00
totalWidth += item->getContentSize().width;
2013-12-23 15:02:52 +08:00
}
2013-11-06 16:04:06 +08:00
float finalWidth = totalWidth;
float finalHeight = _contentSize.height;
2013-12-23 15:02:52 +08:00
setInnerContainerSize(Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
default:
break;
}
}
2015-01-19 11:44:32 +08:00
void ListView::remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex)
{
2015-01-19 15:27:10 +08:00
CCASSERT(nullptr != layoutParameter, "Layout parameter can't be nullptr!");
switch (_gravity)
2015-01-19 11:44:32 +08:00
{
case Gravity::LEFT:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
break;
case Gravity::RIGHT:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
break;
case Gravity::CENTER_HORIZONTAL:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
break;
default:
break;
}
2015-01-19 15:27:10 +08:00
if (0 == itemIndex)
2015-01-19 11:44:32 +08:00
{
layoutParameter->setMargin(Margin::ZERO);
}
else
{
layoutParameter->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
}
}
void ListView::remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex)
{
2015-01-19 15:27:10 +08:00
CCASSERT(nullptr != layoutParameter, "Layout parameter can't be nullptr!");
2015-01-19 11:44:32 +08:00
switch (_gravity)
{
case Gravity::TOP:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::TOP);
break;
case Gravity::BOTTOM:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
break;
case Gravity::CENTER_VERTICAL:
layoutParameter->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
break;
default:
break;
}
2015-01-19 15:27:10 +08:00
if (0 == itemIndex)
2015-01-19 11:44:32 +08:00
{
layoutParameter->setMargin(Margin::ZERO);
}
else
{
layoutParameter->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
}
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
void ListView::remedyLayoutParameter(Widget *item)
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
CCASSERT(nullptr != item, "ListView Item can't be nullptr!");
2015-01-19 11:44:32 +08:00
LinearLayoutParameter* linearLayoutParameter = (LinearLayoutParameter*)(item->getLayoutParameter());
bool isLayoutParameterExists = true;
if (!linearLayoutParameter)
{
linearLayoutParameter = LinearLayoutParameter::create();
isLayoutParameterExists = false;
}
ssize_t itemIndex = getIndex(item);
switch (_direction)
{
2014-05-12 11:08:10 +08:00
case Direction::VERTICAL:
2013-09-13 22:20:20 +08:00
{
2015-01-19 11:44:32 +08:00
this->remedyVerticalLayoutParameter(linearLayoutParameter, itemIndex);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
}
2014-05-12 11:08:10 +08:00
case Direction::HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2015-01-19 11:44:32 +08:00
this->remedyHorizontalLayoutParameter(linearLayoutParameter, itemIndex);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
}
2013-09-13 22:20:20 +08:00
default:
break;
}
2015-01-19 11:44:32 +08:00
if (!isLayoutParameterExists)
{
item->setLayoutParameter(linearLayoutParameter);
}
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::pushBackDefaultItem()
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
if (nullptr == _model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-12-23 15:02:52 +08:00
Widget* newItem = _model->clone();
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(newItem);
addChild(newItem);
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
2013-12-28 14:34:52 +08:00
void ListView::insertDefaultItem(ssize_t index)
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
if (nullptr == _model)
2013-09-13 22:20:20 +08:00
{
return;
}
insertCustomItem(_model->clone(), index);
2013-09-13 22:20:20 +08:00
}
2014-06-25 16:17:16 +08:00
2013-12-23 15:02:52 +08:00
void ListView::pushBackCustomItem(Widget* item)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(item);
addChild(item);
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
void ListView::addChild(cocos2d::Node *child, int zOrder, int tag)
{
ScrollView::addChild(child, zOrder, tag);
Widget* widget = dynamic_cast<Widget*>(child);
2015-01-19 15:27:10 +08:00
if (nullptr != widget)
{
_items.pushBack(widget);
_outOfBoundaryAmountDirty = true;
}
2014-06-25 11:27:48 +08:00
}
2014-06-25 16:17:16 +08:00
void ListView::addChild(cocos2d::Node *child)
{
ListView::addChild(child, child->getLocalZOrder(), child->getName());
}
void ListView::addChild(cocos2d::Node *child, int zOrder)
{
ListView::addChild(child, zOrder, child->getName());
}
2014-06-25 11:27:48 +08:00
void ListView::addChild(Node* child, int zOrder, const std::string &name)
{
ScrollView::addChild(child, zOrder, name);
Widget* widget = dynamic_cast<Widget*>(child);
2015-01-19 15:27:10 +08:00
if (nullptr != widget)
2014-06-25 11:27:48 +08:00
{
_items.pushBack(widget);
_outOfBoundaryAmountDirty = true;
2014-06-25 11:27:48 +08:00
}
}
void ListView::removeChild(cocos2d::Node *child, bool cleaup)
{
Widget* widget = dynamic_cast<Widget*>(child);
2015-01-19 15:27:10 +08:00
if (nullptr != widget)
{
if (-1 != _curSelectedIndex)
{
auto removedIndex = getIndex(widget);
if (_curSelectedIndex > removedIndex)
{
_curSelectedIndex -= 1;
}
else if (_curSelectedIndex == removedIndex)
{
_curSelectedIndex = -1;
}
}
_items.eraseObject(widget);
_outOfBoundaryAmountDirty = true;
}
ScrollView::removeChild(child, cleaup);
}
void ListView::removeAllChildren()
{
this->removeAllChildrenWithCleanup(true);
}
void ListView::removeAllChildrenWithCleanup(bool cleanup)
{
ScrollView::removeAllChildrenWithCleanup(cleanup);
_items.clear();
_curSelectedIndex = -1;
_outOfBoundaryAmountDirty = true;
}
2013-09-13 22:20:20 +08:00
2013-12-28 14:34:52 +08:00
void ListView::insertCustomItem(Widget* item, ssize_t index)
2013-09-13 22:20:20 +08:00
{
if (-1 != _curSelectedIndex)
{
if (_curSelectedIndex >= index)
{
_curSelectedIndex += 1;
}
}
2013-12-23 15:02:52 +08:00
_items.insert(index, item);
_outOfBoundaryAmountDirty = true;
ScrollView::addChild(item);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(item);
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
2013-12-28 14:34:52 +08:00
void ListView::removeItem(ssize_t index)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* item = getItem(index);
2015-01-19 15:27:10 +08:00
if (nullptr == item)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return;
2013-09-13 22:20:20 +08:00
}
removeChild(item, true);
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::removeLastItem()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
removeItem(_items.size() -1);
2013-09-13 22:20:20 +08:00
}
void ListView::removeAllItems()
{
removeAllChildren();
}
2013-09-13 22:20:20 +08:00
Widget* ListView::getItem(ssize_t index) const
2013-09-13 22:20:20 +08:00
{
2013-12-28 14:34:52 +08:00
if (index < 0 || index >= _items.size())
2013-11-06 16:04:06 +08:00
{
2013-11-14 11:37:46 +08:00
return nullptr;
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
return _items.at(index);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Vector<Widget*>& ListView::getItems()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return _items;
2013-09-13 22:20:20 +08:00
}
2013-12-28 14:34:52 +08:00
ssize_t ListView::getIndex(Widget *item) const
2013-09-13 22:20:20 +08:00
{
2015-01-19 15:27:10 +08:00
if (nullptr == item)
2013-11-06 16:04:06 +08:00
{
return -1;
}
2013-12-23 15:02:52 +08:00
return _items.getIndex(item);
2013-09-13 22:20:20 +08:00
}
2014-05-12 09:51:23 +08:00
void ListView::setGravity(Gravity gravity)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_gravity == gravity)
{
return;
}
_gravity = gravity;
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
void ListView::setMagneticType(MagneticType magneticType)
{
_magneticType = magneticType;
_outOfBoundaryAmountDirty = true;
startMagneticScroll();
}
ListView::MagneticType ListView::getMagneticType() const
{
return _magneticType;
}
void ListView::setMagneticAllowedOutOfBoundary(bool magneticAllowedOutOfBoundary)
{
_magneticAllowedOutOfBoundary = magneticAllowedOutOfBoundary;
}
bool ListView::getMagneticAllowedOutOfBoundary() const
{
return _magneticAllowedOutOfBoundary;
}
2013-12-23 15:02:52 +08:00
void ListView::setItemsMargin(float margin)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_itemsMargin == margin)
{
return;
}
_itemsMargin = margin;
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
2014-03-01 22:34:25 +08:00
2014-05-27 10:40:20 +08:00
float ListView::getItemsMargin()const
2014-03-01 22:34:25 +08:00
{
return _itemsMargin;
}
2013-09-13 22:20:20 +08:00
2014-05-12 11:08:10 +08:00
void ListView::setDirection(Direction dir)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
switch (dir)
2013-09-13 22:20:20 +08:00
{
2015-01-30 17:02:20 +08:00
case Direction::NONE:
case Direction::BOTH:
break;
2014-05-12 11:08:10 +08:00
case Direction::VERTICAL:
setLayoutType(Type::VERTICAL);
2013-09-13 22:20:20 +08:00
break;
2014-05-12 11:08:10 +08:00
case Direction::HORIZONTAL:
setLayoutType(Type::HORIZONTAL);
2013-09-13 22:20:20 +08:00
break;
default:
2013-11-06 16:04:06 +08:00
return;
2013-09-13 22:20:20 +08:00
break;
}
2013-12-23 15:02:52 +08:00
ScrollView::setDirection(dir);
2013-09-13 22:20:20 +08:00
}
2013-12-26 14:57:30 +08:00
void ListView::refreshView()
2013-12-26 14:57:30 +08:00
{
forceDoLayout();
2013-12-26 14:57:30 +08:00
}
2013-09-13 22:20:20 +08:00
void ListView::requestDoLayout()
{
_innerContainerDoLayoutDirty = true;
}
void ListView::doLayout()
2013-09-13 22:20:20 +08:00
{
if(!_innerContainerDoLayoutDirty)
{
return;
}
2013-12-28 14:34:52 +08:00
ssize_t length = _items.size();
for (int i = 0; i < length; ++i)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* item = _items.at(i);
item->setLocalZOrder(i);
2013-11-06 16:04:06 +08:00
remedyLayoutParameter(item);
2013-09-13 22:20:20 +08:00
}
2015-09-09 15:25:00 +08:00
_innerContainer->forceDoLayout();
2013-11-06 16:04:06 +08:00
updateInnerContainerSize();
_innerContainerDoLayoutDirty = false;
2013-12-23 15:02:52 +08:00
}
void ListView::addEventListenerListView(Ref *target, SEL_ListViewEvent selector)
2013-11-08 20:29:49 +08:00
{
_listViewEventListener = target;
_listViewEventSelector = selector;
}
2014-06-23 12:09:49 +08:00
void ListView::addEventListener(const ccListViewCallback& callback)
2014-05-12 09:51:23 +08:00
{
_eventCallback = callback;
}
void ListView::selectedItemEvent(TouchEventType event)
2013-11-08 20:29:49 +08:00
{
this->retain();
switch (event)
2013-11-08 20:29:49 +08:00
{
case TouchEventType::BEGAN:
2014-05-12 09:51:23 +08:00
{
2014-03-11 11:53:44 +08:00
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);
}
2014-05-12 09:51:23 +08:00
if (_eventCallback) {
_eventCallback(this,EventType::ON_SELECTED_ITEM_START);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_START));
}
2014-05-12 09:51:23 +08:00
}
break;
2014-03-11 11:53:44 +08:00
default:
2014-05-12 09:51:23 +08:00
{
2014-03-11 11:53:44 +08:00
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);
}
2014-05-12 09:51:23 +08:00
if (_eventCallback) {
_eventCallback(this, EventType::ON_SELECTED_ITEM_END);
}
if (_ccEventCallback)
{
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_END));
}
2014-05-12 09:51:23 +08:00
}
break;
2013-11-08 20:29:49 +08:00
}
this->release();
2013-11-08 20:29:49 +08:00
}
2014-06-06 16:48:49 +08:00
void ListView::interceptTouchEvent(TouchEventType event, Widget *sender, Touch* touch)
2013-11-08 20:29:49 +08:00
{
2014-06-06 16:48:49 +08:00
ScrollView::interceptTouchEvent(event, sender, touch);
if (!_touchEnabled)
{
return;
}
if (event != TouchEventType::MOVED)
2013-11-08 20:29:49 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* parent = sender;
2013-11-08 20:29:49 +08:00
while (parent)
{
if (parent && (parent->getParent() == _innerContainer))
2013-11-08 20:29:49 +08:00
{
_curSelectedIndex = getIndex(parent);
break;
}
2013-12-23 15:02:52 +08:00
parent = dynamic_cast<Widget*>(parent->getParent());
2013-11-08 20:29:49 +08:00
}
if (sender->isHighlighted()) {
selectedItemEvent(event);
}
2013-11-08 20:29:49 +08:00
}
}
static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
{
Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
Size size = item->getContentSize();
return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
}
static Widget* findClosestItem(const Vec2& targetPosition, const Vector<Widget*>& items, const Vec2& itemAnchorPoint, ssize_t firstIndex, float distanceFromFirst, ssize_t lastIndex, float distanceFromLast)
{
CCASSERT(firstIndex >= 0 && lastIndex < items.size() && firstIndex <= lastIndex, "");
if (firstIndex == lastIndex)
{
return items.at(firstIndex);
}
if (lastIndex - firstIndex == 1)
{
if (distanceFromFirst <= distanceFromLast)
{
return items.at(firstIndex);
}
else
{
return items.at(lastIndex);
}
}
// Binary search
ssize_t midIndex = (firstIndex + lastIndex) / 2;
Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
float distanceFromMid = (targetPosition - itemPosition).length();
if (distanceFromFirst <= distanceFromLast)
{
// Left half
return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
}
else
{
// Right half
return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
}
}
Widget* ListView::getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint) const
{
if (_items.empty())
{
return nullptr;
}
// Find the closest item through binary search
ssize_t firstIndex = 0;
Vec2 firstPosition = calculateItemPositionWithAnchor(_items.at(firstIndex), itemAnchorPoint);
float distanceFromFirst = (targetPosition - firstPosition).length();
ssize_t lastIndex = _items.size() - 1;
Vec2 lastPosition = calculateItemPositionWithAnchor(_items.at(lastIndex), itemAnchorPoint);
float distanceFromLast = (targetPosition - lastPosition).length();
return findClosestItem(targetPosition, _items, itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
}
Widget* ListView::getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) const
{
// Calculate the target position
Size contentSize = getContentSize();
Vec2 targetPosition = -_innerContainer->getPosition();
targetPosition.x += contentSize.width * positionRatioInView.x;
targetPosition.y += contentSize.height * positionRatioInView.y;
return getClosestItemToPosition(targetPosition, itemAnchorPoint);
}
Widget* ListView::getCenterItemInCurrentView() const
{
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
}
Widget* ListView::getLeftmostItemInCurrentView() const
{
if (_direction == Direction::HORIZONTAL)
{
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_LEFT, Vec2::ANCHOR_MIDDLE);
}
return nullptr;
}
Widget* ListView::getRightmostItemInCurrentView() const
{
if (_direction == Direction::HORIZONTAL)
{
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_RIGHT, Vec2::ANCHOR_MIDDLE);
}
return nullptr;
}
Widget* ListView::getTopmostItemInCurrentView() const
{
if (_direction == Direction::VERTICAL)
{
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_TOP, Vec2::ANCHOR_MIDDLE);
}
return nullptr;
}
Widget* ListView::getBottommostItemInCurrentView() const
{
if (_direction == Direction::VERTICAL)
{
return getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE_BOTTOM, Vec2::ANCHOR_MIDDLE);
}
return nullptr;
}
void ListView::jumpToBottom()
{
doLayout();
ScrollView::jumpToBottom();
}
void ListView::jumpToTop()
{
doLayout();
ScrollView::jumpToTop();
}
void ListView::jumpToLeft()
{
doLayout();
ScrollView::jumpToLeft();
}
void ListView::jumpToRight()
{
doLayout();
ScrollView::jumpToRight();
}
void ListView::jumpToTopLeft()
{
doLayout();
ScrollView::jumpToTopLeft();
}
void ListView::jumpToTopRight()
{
doLayout();
ScrollView::jumpToTopRight();
}
void ListView::jumpToBottomLeft()
{
doLayout();
ScrollView::jumpToBottomLeft();
}
void ListView::jumpToBottomRight()
{
doLayout();
ScrollView::jumpToBottomRight();
}
void ListView::jumpToPercentVertical(float percent)
{
doLayout();
ScrollView::jumpToPercentVertical(percent);
}
void ListView::jumpToPercentHorizontal(float percent)
{
doLayout();
ScrollView::jumpToPercentHorizontal(percent);
}
void ListView::jumpToPercentBothDirection(const Vec2& percent)
{
doLayout();
ScrollView::jumpToPercentBothDirection(percent);
}
2015-09-09 15:25:00 +08:00
static Vec2 calculateItemDestination(const Size& contentSize, Widget* item, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
{
Vec2 positionInView;
positionInView.x += contentSize.width * positionRatioInView.x;
positionInView.y += contentSize.height * positionRatioInView.y;
Vec2 itemPosition = calculateItemPositionWithAnchor(item, itemAnchorPoint);
return -(itemPosition - positionInView);
}
void ListView::jumpToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
2015-09-09 15:25:00 +08:00
{
Widget* item = getItem(itemIndex);
if (item == nullptr)
{
return;
}
doLayout();
2015-09-09 15:25:00 +08:00
Vec2 destination = calculateItemDestination(getContentSize(), item, positionRatioInView, itemAnchorPoint);
destination = flattenVectorByDirection(destination);
Vec2 delta = destination - getInnerContainerPosition();
Vec2 outOfBoundary = getHowMuchOutOfBoundary(delta);
destination += outOfBoundary;
moveChildrenToPosition(destination);
}
void ListView::scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
{
scrollToItem(itemIndex, positionRatioInView, itemAnchorPoint, DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM);
}
void ListView::scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec)
{
Widget* item = getItem(itemIndex);
if (item == nullptr)
{
return;
}
2015-09-09 15:25:00 +08:00
Vec2 destination = calculateItemDestination(getContentSize(), item, positionRatioInView, itemAnchorPoint);
startAutoScrollToDestination(destination, timeInSec, true);
}
2013-12-28 14:34:52 +08:00
ssize_t ListView::getCurSelectedIndex() const
2013-11-08 20:29:49 +08:00
{
return _curSelectedIndex;
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
void ListView::onSizeChanged()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
ScrollView::onSizeChanged();
requestDoLayout();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
std::string ListView::getDescription() const
2013-09-13 22:20:20 +08:00
{
2013-12-27 17:22:51 +08:00
return "ListView";
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Widget* ListView::createCloneInstance()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
return ListView::create();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void ListView::copyClonedWidgetChildren(Widget* model)
2013-09-13 22:20:20 +08:00
{
2013-12-27 17:22:51 +08:00
auto& arrayItems = static_cast<ListView*>(model)->getItems();
2013-12-26 16:17:52 +08:00
for (auto& item : arrayItems)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
pushBackCustomItem(item->clone());
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void ListView::copySpecialProperties(Widget *widget)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
ListView* listViewEx = dynamic_cast<ListView*>(widget);
2013-11-06 16:04:06 +08:00
if (listViewEx)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
ScrollView::copySpecialProperties(widget);
2013-11-06 16:04:06 +08:00
setItemModel(listViewEx->_model);
setItemsMargin(listViewEx->_itemsMargin);
setGravity(listViewEx->_gravity);
2014-05-23 15:03:46 +08:00
_listViewEventListener = listViewEx->_listViewEventListener;
_listViewEventSelector = listViewEx->_listViewEventSelector;
_eventCallback = listViewEx->_eventCallback;
2013-09-13 22:20:20 +08:00
}
}
Vec2 ListView::getHowMuchOutOfBoundary(const Vec2& addition)
{
if(!_magneticAllowedOutOfBoundary || _items.empty())
{
return ScrollView::getHowMuchOutOfBoundary(addition);
}
else if(_magneticType == MagneticType::NONE || _magneticType == MagneticType::BOTH_END)
{
return ScrollView::getHowMuchOutOfBoundary(addition);
}
else if(addition == Vec2::ZERO && !_outOfBoundaryAmountDirty)
{
return _outOfBoundaryAmount;
}
// If it is allowed to be out of boundary by magnetic, adjust the boundaries according to the magnetic type.
float leftBoundary = _leftBoundary;
float rightBoundary = _rightBoundary;
float topBoundary = _topBoundary;
float bottomBoundary = _bottomBoundary;
{
ssize_t lastItemIndex = _items.size() - 1;
Size contentSize = getContentSize();
Vec2 firstItemAdjustment, lastItemAdjustment;
if(_magneticType == MagneticType::CENTER)
{
firstItemAdjustment = (contentSize - _items.at(0)->getContentSize()) / 2;
lastItemAdjustment = (contentSize - _items.at(lastItemIndex)->getContentSize()) / 2;
}
else if(_magneticType == MagneticType::LEFT)
{
lastItemAdjustment = contentSize - _items.at(lastItemIndex)->getContentSize();
}
else if(_magneticType == MagneticType::RIGHT)
{
firstItemAdjustment = contentSize - _items.at(0)->getContentSize();
}
else if(_magneticType == MagneticType::TOP)
{
lastItemAdjustment = contentSize - _items.at(lastItemIndex)->getContentSize();
}
else if(_magneticType == MagneticType::BOTTOM)
{
firstItemAdjustment = contentSize - _items.at(0)->getContentSize();
}
leftBoundary += firstItemAdjustment.x;
rightBoundary -= lastItemAdjustment.x;
topBoundary -= firstItemAdjustment.y;
bottomBoundary += lastItemAdjustment.y;
}
// Calculate the actual amount
Vec2 outOfBoundaryAmount;
if(_innerContainer->getLeftBoundary() + addition.x > leftBoundary)
{
outOfBoundaryAmount.x = leftBoundary - (_innerContainer->getLeftBoundary() + addition.x);
}
else if(_innerContainer->getRightBoundary() + addition.x < rightBoundary)
{
outOfBoundaryAmount.x = rightBoundary - (_innerContainer->getRightBoundary() + addition.x);
}
if(_innerContainer->getTopBoundary() + addition.y < topBoundary)
{
outOfBoundaryAmount.y = topBoundary - (_innerContainer->getTopBoundary() + addition.y);
}
else if(_innerContainer->getBottomBoundary() + addition.y > bottomBoundary)
{
outOfBoundaryAmount.y = bottomBoundary - (_innerContainer->getBottomBoundary() + addition.y);
}
if(addition == Vec2::ZERO)
{
_outOfBoundaryAmount = outOfBoundaryAmount;
_outOfBoundaryAmountDirty = false;
}
return outOfBoundaryAmount;
}
static Vec2 getAnchorPointByMagneticType(ListView::MagneticType magneticType)
{
switch(magneticType)
{
case ListView::MagneticType::NONE: return Vec2::ZERO;
case ListView::MagneticType::BOTH_END: return Vec2::ANCHOR_TOP_LEFT;
case ListView::MagneticType::CENTER: return Vec2::ANCHOR_MIDDLE;
case ListView::MagneticType::LEFT: return Vec2::ANCHOR_MIDDLE_LEFT;
case ListView::MagneticType::RIGHT: return Vec2::ANCHOR_MIDDLE_RIGHT;
case ListView::MagneticType::TOP: return Vec2::ANCHOR_MIDDLE_TOP;
case ListView::MagneticType::BOTTOM: return Vec2::ANCHOR_MIDDLE_BOTTOM;
}
return Vec2::ZERO;
}
void ListView::startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity)
{
Vec2 adjustedDeltaMove = deltaMove;
if(!_items.empty() && _magneticType != MagneticType::NONE)
{
adjustedDeltaMove = flattenVectorByDirection(adjustedDeltaMove);
// If the destination is out of boundary, do nothing here. Because it will be handled by bouncing back.
if(getHowMuchOutOfBoundary(adjustedDeltaMove) == Vec2::ZERO)
{
MagneticType magType = _magneticType;
if(magType == MagneticType::BOTH_END)
{
if(_direction == Direction::HORIZONTAL)
{
magType = (adjustedDeltaMove.x > 0 ? MagneticType::LEFT : MagneticType::RIGHT);
}
else if(_direction == Direction::VERTICAL)
{
magType = (adjustedDeltaMove.y > 0 ? MagneticType::BOTTOM : MagneticType::TOP);
}
}
// Adjust the delta move amount according to the magnetic type
Vec2 magneticAnchorPoint = getAnchorPointByMagneticType(magType);
Vec2 magneticPosition = -_innerContainer->getPosition();
magneticPosition.x += getContentSize().width * magneticAnchorPoint.x;
magneticPosition.y += getContentSize().height * magneticAnchorPoint.y;
Widget* pTargetItem = getClosestItemToPosition(magneticPosition - adjustedDeltaMove, magneticAnchorPoint);
Vec2 itemPosition = calculateItemPositionWithAnchor(pTargetItem, magneticAnchorPoint);
adjustedDeltaMove = magneticPosition - itemPosition;
}
}
ScrollView::startAttenuatingAutoScroll(adjustedDeltaMove, initialVelocity);
}
void ListView::startMagneticScroll()
{
if(_items.empty() || _magneticType == MagneticType::NONE)
{
return;
}
// Find the closest item
Vec2 magneticAnchorPoint = getAnchorPointByMagneticType(_magneticType);
Vec2 magneticPosition = -_innerContainer->getPosition();
magneticPosition.x += getContentSize().width * magneticAnchorPoint.x;
magneticPosition.y += getContentSize().height * magneticAnchorPoint.y;
Widget* pTargetItem = getClosestItemToPosition(magneticPosition, magneticAnchorPoint);
scrollToItem(getIndex(pTargetItem), magneticAnchorPoint, magneticAnchorPoint);
}
2013-09-13 22:20:20 +08:00
}
NS_CC_END