axmol/cocos/gui/UIListView.cpp

422 lines
11 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
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"
2013-11-06 16:04:06 +08:00
#include "gui/UIHelper.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
namespace gui {
2013-09-13 22:20:20 +08:00
2013-11-06 16:04:06 +08:00
UIListView::UIListView():
_model(NULL),
_items(NULL),
_gravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL),
_itemsMargin(0.0f)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
}
UIListView::~UIListView()
{
2013-11-06 16:04:06 +08:00
_items->removeAllObjects();
CC_SAFE_RELEASE(_items);
2013-09-13 22:20:20 +08:00
}
UIListView* UIListView::create()
{
UIListView* widget = new UIListView();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
bool UIListView::init()
{
2013-11-06 16:04:06 +08:00
if (UIScrollView::init())
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_items = cocos2d::Array::create();
CC_SAFE_RETAIN(_items);
setLayoutType(LAYOUT_LINEAR_VERTICAL);
2013-09-13 22:20:20 +08:00
return true;
}
return false;
}
2013-11-06 16:04:06 +08:00
void UIListView::setItemModel(UIWidget *model)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!model)
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
}
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
}
2013-11-06 16:04:06 +08:00
void UIListView::updateInnerContainerSize()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
switch (_direction) {
case SCROLLVIEW_DIR_VERTICAL:
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
int childrenCount = _items->count();
float totalHeight = _model->getSize().height * childrenCount + (childrenCount - 1) * _itemsMargin;
float finalWidth = _size.width;
float finalHeight = totalHeight;
setInnerContainerSize(cocos2d::Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
int childrenCount = _items->count();
float totalWidth = _model->getSize().width * childrenCount + (childrenCount - 1) * _itemsMargin;
float finalWidth = totalWidth;
float finalHeight = _size.height;
setInnerContainerSize(cocos2d::Size(finalWidth, finalHeight));
2013-09-13 22:20:20 +08:00
break;
}
default:
break;
}
}
2013-11-06 16:04:06 +08:00
void UIListView::remedyLayoutParameter(UIWidget *item)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!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
}
2013-11-06 16:04:06 +08:00
switch (_direction) {
case SCROLLVIEW_DIR_VERTICAL:
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILinearLayoutParameter* llp = (UILinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (!llp)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILinearLayoutParameter* defaultLp = UILinearLayoutParameter::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)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
defaultLp->setMargin(UIMarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
defaultLp->setMargin(UIMargin(0.0f, _itemsMargin, 0.0f, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
item->setLayoutParameter(defaultLp);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
llp->setMargin(UIMarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
llp->setMargin(UIMargin(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;
2013-09-13 22:20:20 +08:00
}
}
break;
2013-11-06 16:04:06 +08:00
}
case SCROLLVIEW_DIR_HORIZONTAL:
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILinearLayoutParameter* llp = (UILinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR));
if (!llp)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UILinearLayoutParameter* defaultLp = UILinearLayoutParameter::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)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
defaultLp->setMargin(UIMarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
defaultLp->setMargin(UIMargin(_itemsMargin, 0.0f, 0.0f, 0.0f));
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
item->setLayoutParameter(defaultLp);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (getIndex(item) == 0)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
llp->setMargin(UIMarginZero);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
else
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
llp->setMargin(UIMargin(_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;
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;
}
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::pushBackDefaultItem()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
UIWidget* newItem = _model->clone();
_items->addObject(newItem);
remedyLayoutParameter(newItem);
addChild(newItem);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::insertDefaultItem(int index)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_items)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
if (!_model)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
UIWidget* newItem = _model->clone();
_items->insertObject(newItem, index);
remedyLayoutParameter(newItem);
addChild(newItem);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::pushBackCustomItem(UIWidget* item)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_items->addObject(item);
remedyLayoutParameter(item);
addChild(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::insertCustomItem(UIWidget* item, int index)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_items->insertObject(item, index);
remedyLayoutParameter(item);
addChild(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::removeItem(int index)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_items)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
UIWidget* item = getItem(index);
if (!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
}
2013-11-06 16:04:06 +08:00
_items->removeObject(item);
removeChild(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::removeLastItem()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
removeItem(_items->count() -1);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
UIWidget* UIListView::getItem(unsigned int index)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if ((int)index < 0 || index >= _items->count())
{
return NULL;
}
return (UIWidget*)(_items->data->arr[index]);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
cocos2d::Array* UIListView::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-11-06 16:04:06 +08:00
unsigned int UIListView::getIndex(UIWidget *item) const
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_items)
{
return -1;
}
if (!item)
{
return -1;
}
return _items->getIndexOfObject(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::setGravity(ListViewGravity gravity)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (_gravity == gravity)
{
return;
}
_gravity = gravity;
refreshView();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::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;
refreshView();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::setDirection(SCROLLVIEW_DIR 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
{
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_VERTICAL:
setLayoutType(LAYOUT_LINEAR_VERTICAL);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_HORIZONTAL:
setLayoutType(LAYOUT_LINEAR_HORIZONTAL);
2013-09-13 22:20:20 +08:00
break;
2013-11-06 16:04:06 +08:00
case SCROLLVIEW_DIR_BOTH:
return;
2013-09-13 22:20:20 +08:00
default:
2013-11-06 16:04:06 +08:00
return;
2013-09-13 22:20:20 +08:00
break;
}
2013-11-06 16:04:06 +08:00
UIScrollView::setDirection(dir);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::refreshView()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (!_items)
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
}
2013-11-06 16:04:06 +08:00
cocos2d::ccArray* arrayItems = _items->data;
int length = arrayItems->num;
for (int i=0; i<length; i++)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UIWidget* item = (UIWidget*)(arrayItems->arr[i]);
item->setZOrder(i);
remedyLayoutParameter(item);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
updateInnerContainerSize();
doLayout();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::onSizeChanged()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UIScrollView::onSizeChanged();
refreshView();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
const char* UIListView::getDescription() const
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return "ListViewEx";
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
UIWidget* UIListView::createCloneInstance()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return UIListView::create();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIListView::copyClonedWidgetChildren(UIWidget* model)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
cocos2d::ccArray* arrayItems = dynamic_cast<UIListView*>(model)->getItems()->data;
int length = arrayItems->num;
for (int i=0; i<length; i++)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UIWidget* item = (UIWidget*)(arrayItems->arr[i]);
pushBackCustomItem(item->clone());
2013-09-13 22:20:20 +08:00
}
}
2013-11-06 16:04:06 +08:00
void UIListView::copySpecialProperties(UIWidget *widget)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UIListView* listViewEx = dynamic_cast<UIListView*>(widget);
if (listViewEx)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
UIScrollView::copySpecialProperties(widget);
setItemModel(listViewEx->_model);
setItemsMargin(listViewEx->_itemsMargin);
setGravity(listViewEx->_gravity);
2013-09-13 22:20:20 +08:00
}
}
}