axmol/cocos/gui/UIListView.cpp

467 lines
12 KiB
C++

/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "gui/UIListView.h"
#include "gui/UIHelper.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
NS_CC_BEGIN
namespace gui {
ListView::ListView():
_model(nullptr),
_gravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL),
_itemsMargin(0.0f),
_listViewEventListener(nullptr),
_listViewEventSelector(nullptr),
_curSelectedIndex(0),
_refreshViewDirty(true)
{
}
ListView::~ListView()
{
_listViewEventListener = nullptr;
_listViewEventSelector = nullptr;
_items.clear();
}
ListView* ListView::create()
{
ListView* widget = new ListView();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool ListView::init()
{
if (ScrollView::init())
{
setLayoutType(LAYOUT_LINEAR_VERTICAL);
return true;
}
return false;
}
void ListView::setItemModel(Widget *model)
{
if (!model)
{
return;
}
CC_SAFE_RELEASE_NULL(_model);
_model = model;
CC_SAFE_RETAIN(_model);
}
void ListView::updateInnerContainerSize()
{
switch (_direction)
{
case SCROLLVIEW_DIR_VERTICAL:
{
int length = _items.size();
float totalHeight = (length - 1) * _itemsMargin;
for (auto& item : _items)
{
totalHeight += item->getSize().height;
}
float finalWidth = _size.width;
float finalHeight = totalHeight;
setInnerContainerSize(Size(finalWidth, finalHeight));
break;
}
case SCROLLVIEW_DIR_HORIZONTAL:
{
int length = _items.size();
float totalWidth = (length - 1) * _itemsMargin;
for (auto& item : _items)
{
totalWidth += item->getSize().width;
}
float finalWidth = totalWidth;
float finalHeight = _size.height;
setInnerContainerSize(Size(finalWidth, finalHeight));
break;
}
default:
break;
}
}
void ListView::remedyLayoutParameter(Widget *item)
{
if (!item)
{
return;
}
switch (_direction) {
case SCROLLVIEW_DIR_VERTICAL:
{
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (!llp)
{
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT:
defaultLp->setGravity(LINEAR_GRAVITY_LEFT);
break;
case LISTVIEW_GRAVITY_RIGHT:
defaultLp->setGravity(LINEAR_GRAVITY_RIGHT);
break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL:
defaultLp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL);
break;
default:
break;
}
if (getIndex(item) == 0)
{
defaultLp->setMargin(MarginZero);
}
else
{
defaultLp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
}
item->setLayoutParameter(defaultLp);
}
else
{
if (getIndex(item) == 0)
{
llp->setMargin(MarginZero);
}
else
{
llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
}
switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT:
llp->setGravity(LINEAR_GRAVITY_LEFT);
break;
case LISTVIEW_GRAVITY_RIGHT:
llp->setGravity(LINEAR_GRAVITY_RIGHT);
break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL:
llp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL);
break;
default:
break;
}
}
break;
}
case SCROLLVIEW_DIR_HORIZONTAL:
{
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (!llp)
{
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
switch (_gravity) {
case LISTVIEW_GRAVITY_TOP:
defaultLp->setGravity(LINEAR_GRAVITY_TOP);
break;
case LISTVIEW_GRAVITY_BOTTOM:
defaultLp->setGravity(LINEAR_GRAVITY_BOTTOM);
break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL:
defaultLp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL);
break;
default:
break;
}
if (getIndex(item) == 0)
{
defaultLp->setMargin(MarginZero);
}
else
{
defaultLp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
}
item->setLayoutParameter(defaultLp);
}
else
{
if (getIndex(item) == 0)
{
llp->setMargin(MarginZero);
}
else
{
llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
}
switch (_gravity) {
case LISTVIEW_GRAVITY_TOP:
llp->setGravity(LINEAR_GRAVITY_TOP);
break;
case LISTVIEW_GRAVITY_BOTTOM:
llp->setGravity(LINEAR_GRAVITY_BOTTOM);
break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL:
llp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL);
break;
default:
break;
}
}
break;
}
default:
break;
}
}
void ListView::pushBackDefaultItem()
{
if (!_model)
{
return;
}
Widget* newItem = _model->clone();
_items.pushBack(newItem);
remedyLayoutParameter(newItem);
addChild(newItem);
_refreshViewDirty = true;
}
void ListView::insertDefaultItem(int index)
{
if (!_model)
{
return;
}
Widget* newItem = _model->clone();
_items.insert(index, newItem);
remedyLayoutParameter(newItem);
addChild(newItem);
_refreshViewDirty = true;
}
void ListView::pushBackCustomItem(Widget* item)
{
_items.pushBack(item);
remedyLayoutParameter(item);
addChild(item);
_refreshViewDirty = true;
}
void ListView::insertCustomItem(Widget* item, int index)
{
_items.insert(index, item);
remedyLayoutParameter(item);
addChild(item);
_refreshViewDirty = true;
}
void ListView::removeItem(int index)
{
Widget* item = getItem(index);
if (!item)
{
return;
}
_items.eraseObject(item);
removeChild(item);
_refreshViewDirty = true;
}
void ListView::removeLastItem()
{
removeItem(_items.size() -1);
}
Widget* ListView::getItem(unsigned int index)
{
if ((int)index < 0 || index >= _items.size())
{
return nullptr;
}
return _items.at(index);
}
Vector<Widget*>& ListView::getItems()
{
return _items;
}
unsigned int ListView::getIndex(Widget *item) const
{
if (!item)
{
return -1;
}
return _items.getIndex(item);
}
void ListView::setGravity(ListViewGravity gravity)
{
if (_gravity == gravity)
{
return;
}
_gravity = gravity;
_refreshViewDirty = true;
}
void ListView::setItemsMargin(float margin)
{
if (_itemsMargin == margin)
{
return;
}
_itemsMargin = margin;
_refreshViewDirty = true;
}
void ListView::setDirection(SCROLLVIEW_DIR dir)
{
switch (dir)
{
case SCROLLVIEW_DIR_VERTICAL:
setLayoutType(LAYOUT_LINEAR_VERTICAL);
break;
case SCROLLVIEW_DIR_HORIZONTAL:
setLayoutType(LAYOUT_LINEAR_HORIZONTAL);
break;
case SCROLLVIEW_DIR_BOTH:
return;
default:
return;
break;
}
ScrollView::setDirection(dir);
}
void ListView::requestRefreshView()
{
_refreshViewDirty = true;
}
void ListView::refreshView()
{
int length = _items.size();
for (int i=0; i<length; i++)
{
Widget* item = _items.at(i);
item->setZOrder(i);
remedyLayoutParameter(item);
}
updateInnerContainerSize();
}
void ListView::sortAllChildren()
{
ScrollView::sortAllChildren();
if (_refreshViewDirty)
{
refreshView();
_refreshViewDirty = false;
}
}
void ListView::addEventListenerListView(Object *target, SEL_ListViewEvent selector)
{
_listViewEventListener = target;
_listViewEventSelector = selector;
}
void ListView::selectedItemEvent()
{
if (_listViewEventListener && _listViewEventSelector)
{
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM);
}
}
void ListView::interceptTouchEvent(int handleState, Widget *sender, const Point &touchPoint)
{
ScrollView::interceptTouchEvent(handleState, sender, touchPoint);
if (handleState != 1)
{
Widget* parent = sender;
while (parent)
{
if (parent && parent->getParent() == _innerContainer)
{
_curSelectedIndex = getIndex(parent);
break;
}
parent = dynamic_cast<Widget*>(parent->getParent());
}
selectedItemEvent();
}
}
int ListView::getCurSelectedIndex() const
{
return _curSelectedIndex;
}
void ListView::onSizeChanged()
{
ScrollView::onSizeChanged();
_refreshViewDirty = true;
}
std::string ListView::getDescription() const
{
return "ListViewEx";
}
Widget* ListView::createCloneInstance()
{
return ListView::create();
}
void ListView::copyClonedWidgetChildren(Widget* model)
{
auto& arrayItems = getItems();
for (auto& item : arrayItems)
{
pushBackCustomItem(item->clone());
}
}
void ListView::copySpecialProperties(Widget *widget)
{
ListView* listViewEx = dynamic_cast<ListView*>(widget);
if (listViewEx)
{
ScrollView::copySpecialProperties(widget);
setItemModel(listViewEx->_model);
setItemsMargin(listViewEx->_itemsMargin);
setGravity(listViewEx->_gravity);
}
}
}
NS_CC_END