axmol/cocos/gui/UIWidget.cpp

1114 lines
24 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/UIWidget.h"
2013-11-06 16:04:06 +08:00
#include "gui/UILayout.h"
#include "gui/UIHelper.h"
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
namespace gui {
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
Widget::Widget():
2013-09-16 15:32:52 +08:00
_enabled(true),
_bright(true),
_touchEnabled(false),
_touchPassedEnabled(false),
_focus(false),
_brightStyle(BRIGHT_NONE),
_updateEnabled(false),
2013-12-23 15:02:52 +08:00
_touchStartPos(Point::ZERO),
_touchMovePos(Point::ZERO),
_touchEndPos(Point::ZERO),
2013-11-14 11:37:46 +08:00
_touchEventListener(nullptr),
_touchEventSelector(nullptr),
2013-09-16 15:32:52 +08:00
_name("default"),
_widgetType(WidgetTypeWidget),
_actionTag(0),
2013-12-23 15:02:52 +08:00
_size(Size::ZERO),
_customSize(Size::ZERO),
2013-09-16 15:32:52 +08:00
_ignoreSize(false),
_affectByClipping(false),
_sizeType(SIZE_ABSOLUTE),
2013-12-23 15:02:52 +08:00
_sizePercent(Point::ZERO),
2013-09-16 15:32:52 +08:00
_positionType(POSITION_ABSOLUTE),
2013-12-23 15:02:52 +08:00
_positionPercent(Point::ZERO),
_reorderWidgetChildDirty(true),
_hitted(false),
_touchListener(nullptr)
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
Widget::~Widget()
2013-09-13 22:20:20 +08:00
{
2013-11-14 11:37:46 +08:00
_touchEventListener = nullptr;
_touchEventSelector = nullptr;
2013-12-23 15:02:52 +08:00
_widgetChildren.clear();
2014-01-03 20:29:16 +08:00
setTouchEnabled(false);
_nodes.clear();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Widget* Widget::create()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widget = new Widget();
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 Widget::init()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
if (Node::init())
{
initRenderer();
setCascadeColorEnabled(true);
setCascadeOpacityEnabled(true);
setBright(true);
ignoreContentAdaptWithSize(true);
setAnchorPoint(Point(0.5f, 0.5f));
return true;
}
return false;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::onEnter()
2013-09-13 22:20:20 +08:00
{
updateSizeAndPosition();
2013-12-23 15:02:52 +08:00
Node::onEnter();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::onExit()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Node::onExit();
2013-09-13 22:20:20 +08:00
}
2013-11-08 14:58:20 +08:00
2013-12-23 15:02:52 +08:00
void Widget::visit()
2013-11-08 14:58:20 +08:00
{
2013-12-23 15:02:52 +08:00
if (_enabled)
{
Node::visit();
}
2013-11-08 14:58:20 +08:00
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
void Widget::addChild(Node *child)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Node::addChild(child);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::addChild(Node * child, int zOrder)
{
Node::addChild(child, zOrder);
}
void Widget::addChild(Node* child, int zOrder, int tag)
{
2014-01-03 20:35:14 +08:00
CCASSERT(dynamic_cast<Widget*>(child) != nullptr, "Widget only supports Widgets as children");
2013-12-23 15:02:52 +08:00
Node::addChild(child, zOrder, tag);
_widgetChildren.pushBack(child);
}
void Widget::sortAllChildren()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_reorderWidgetChildDirty = _reorderChildDirty;
Node::sortAllChildren();
if( _reorderWidgetChildDirty )
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
std::sort( std::begin(_widgetChildren), std::end(_widgetChildren), nodeComparisonLess );
_reorderWidgetChildDirty = false;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
}
Node* Widget::getChildByTag(int aTag)
{
CCASSERT( aTag != Node::INVALID_TAG, "Invalid tag");
for (auto& child : _widgetChildren)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
if(child && child->getTag() == aTag)
return child;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Vector<Node*>& Widget::getChildren()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
return _widgetChildren;
}
const Vector<Node*>& Widget::getChildren() const
{
return _widgetChildren;
}
ssize_t Widget::getChildrenCount() const
2013-12-23 15:02:52 +08:00
{
return _widgetChildren.size();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Widget* Widget::getWidgetParent()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
return dynamic_cast<Widget*>(getParent());
}
void Widget::removeFromParent()
{
removeFromParentAndCleanup(true);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::removeFromParentAndCleanup(bool cleanup)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Node::removeFromParentAndCleanup(cleanup);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::removeChild(Node *child, bool cleanup)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Node::removeChild(child, cleanup);
_widgetChildren.eraseObject(child);
}
void Widget::removeChildByTag(int tag, bool cleanup)
{
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
Node *child = getChildByTag(tag);
2014-01-03 20:35:14 +08:00
if (child == nullptr)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
2013-09-13 22:20:20 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
removeChild(child, cleanup);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::removeAllChildren()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
removeAllChildrenWithCleanup(true);
}
void Widget::removeAllChildrenWithCleanup(bool cleanup)
{
for (auto& child : _widgetChildren)
{
if (child)
{
Node::removeChild(child);
}
}
_widgetChildren.clear();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setEnabled(bool enabled)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_enabled = enabled;
for (auto& child : _widgetChildren)
{
if (child)
{
static_cast<Widget*>(child)->setEnabled(enabled);
}
}
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Widget* Widget::getChildByName(const char *name)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
for (auto& child : _widgetChildren)
{
if (child)
{
Widget* widgetChild = static_cast<Widget*>(child);
if (strcmp(widgetChild->getName(), name) == 0)
{
return widgetChild;
}
}
}
return nullptr;
2013-09-13 22:20:20 +08:00
}
2014-01-03 20:29:16 +08:00
void Widget::addNode(Node* node)
{
addNode(node, node->getZOrder(), node->getTag());
}
void Widget::addNode(Node * node, int zOrder)
{
addNode(node, zOrder, node->getTag());
}
void Widget::addNode(Node* node, int zOrder, int tag)
{
2014-01-03 20:35:14 +08:00
CCAssert(dynamic_cast<Widget*>(node) == nullptr, "Widget only supports Nodes as renderer");
2014-01-03 20:29:16 +08:00
Node::addChild(node, zOrder, tag);
_nodes.pushBack(node);
}
Node* Widget::getNodeByTag(int tag)
{
CCAssert( tag != Node::INVALID_TAG, "Invalid tag");
for (auto& node : _nodes)
{
if(node && node->getTag() == tag)
return node;
}
return nullptr;
}
Vector<Node*>& Widget::getNodes()
{
return _nodes;
}
void Widget::removeNode(Node* node)
{
Node::removeChild(node);
_nodes.eraseObject(node);
}
void Widget::removeNodeByTag(int tag)
{
CCAssert( tag != Node::INVALID_TAG, "Invalid tag");
Node *node = this->getNodeByTag(tag);
2014-01-03 20:35:14 +08:00
if (node == nullptr)
2014-01-03 20:29:16 +08:00
{
CCLOG("cocos2d: removeNodeByTag(tag = %d): child not found!", tag);
}
else
{
this->removeNode(node);
}
}
void Widget::removeAllNodes()
{
for (auto& node : _nodes)
{
if (node)
{
Node::removeChild(node);
}
}
_nodes.clear();
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
void Widget::initRenderer()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::setSize(const Size &size)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_customSize = size;
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_size = getContentSize();
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 15:32:52 +08:00
_size = size;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
if (_running)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
Size pSize;
if (widgetParent)
{
pSize = widgetParent->getSize();
}
else
{
pSize = _parent->getContentSize();
}
2013-11-14 14:46:44 +08:00
float spx = 0.0f;
float spy = 0.0f;
if (pSize.width > 0.0f)
{
spx = _customSize.width / pSize.width;
}
if (pSize.height > 0.0f)
{
spy = _customSize.height / pSize.height;
}
2013-12-23 15:02:52 +08:00
_sizePercent = Point(spx, spy);
2013-09-13 22:20:20 +08:00
}
onSizeChanged();
}
2013-12-23 15:02:52 +08:00
void Widget::setSizePercent(const Point &percent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_sizePercent = percent;
2013-12-23 15:02:52 +08:00
Size cSize = _customSize;
if (_running)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
cSize = Size(widgetParent->getSize().width * percent.x , widgetParent->getSize().height * percent.y);
}
else
{
cSize = Size(_parent->getContentSize().width * percent.x , _parent->getContentSize().height * percent.y);
}
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_size = getContentSize();
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 15:32:52 +08:00
_size = cSize;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
_customSize = cSize;
2013-09-13 22:20:20 +08:00
onSizeChanged();
}
2013-12-23 15:02:52 +08:00
void Widget::updateSizeAndPosition()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
switch (_sizeType)
2013-09-13 22:20:20 +08:00
{
case SIZE_ABSOLUTE:
2013-11-14 14:46:44 +08:00
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_size = getContentSize();
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 15:32:52 +08:00
_size = _customSize;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-11-14 14:46:44 +08:00
{
2013-12-23 15:02:52 +08:00
Size pSize = widgetParent->getSize();
2013-11-14 14:46:44 +08:00
float spx = 0.0f;
float spy = 0.0f;
if (pSize.width > 0.0f)
{
spx = _customSize.width / pSize.width;
}
if (pSize.height > 0.0f)
{
spy = _customSize.height / pSize.height;
}
2013-12-23 15:02:52 +08:00
_sizePercent = Point(spx, spy);
}
else
{
Size pSize = _parent->getContentSize();
float spx = 0.0f;
float spy = 0.0f;
if (pSize.width > 0.0f)
{
spx = _customSize.width / pSize.width;
}
if (pSize.height > 0.0f)
{
spy = _customSize.height / pSize.height;
}
_sizePercent = Point(spx, spy);
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
break;
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
case SIZE_PERCENT:
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Size cSize = Size(widgetParent->getSize().width * _sizePercent.x , widgetParent->getSize().height * _sizePercent.y);
if (_ignoreSize)
{
_size = getContentSize();
}
else
{
_size = cSize;
}
_customSize = cSize;
2013-09-13 22:20:20 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
Size cSize = Size(_parent->getContentSize().width * _sizePercent.x , _parent->getContentSize().height * _sizePercent.y);
if (_ignoreSize)
{
_size = getContentSize();
}
else
{
_size = cSize;
}
_customSize = cSize;
2013-09-13 22:20:20 +08:00
}
}
break;
default:
break;
}
onSizeChanged();
2013-12-23 15:02:52 +08:00
Point absPos = getPosition();
2013-09-16 15:32:52 +08:00
switch (_positionType)
2013-09-13 22:20:20 +08:00
{
case POSITION_ABSOLUTE:
2013-11-14 14:46:44 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-11-14 14:46:44 +08:00
{
2013-12-23 15:02:52 +08:00
Size pSize = widgetParent->getSize();
2013-11-14 14:46:44 +08:00
if (pSize.width <= 0.0f || pSize.height <= 0.0f)
{
2013-12-23 15:02:52 +08:00
_positionPercent = Point::ZERO;
2013-11-14 14:46:44 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
_positionPercent = Point(absPos.x / pSize.width, absPos.y / pSize.height);
2013-11-14 14:46:44 +08:00
}
}
else
{
2013-12-23 15:02:52 +08:00
Size pSize = _parent->getContentSize();
if (pSize.width <= 0.0f || pSize.height <= 0.0f)
{
_positionPercent = Point::ZERO;
}
else
{
_positionPercent = Point(absPos.x / pSize.width, absPos.y / pSize.height);
}
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
break;
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
case POSITION_PERCENT:
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-11-14 14:46:44 +08:00
{
2013-12-23 15:02:52 +08:00
Size parentSize = widgetParent->getSize();
absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
2013-11-14 14:46:44 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
Size parentSize = _parent->getContentSize();
absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
break;
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
default:
break;
}
2013-12-23 15:02:52 +08:00
setPosition(absPos);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setSizeType(SizeType type)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_sizeType = type;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
SizeType Widget::getSizeType() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _sizeType;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::ignoreContentAdaptWithSize(bool ignore)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_ignoreSize = ignore;
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Size s = getContentSize();
2013-09-16 15:32:52 +08:00
_size = s;
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 15:32:52 +08:00
_size = _customSize;
2013-09-13 22:20:20 +08:00
}
onSizeChanged();
}
2013-12-23 15:02:52 +08:00
bool Widget::isIgnoreContentAdaptWithSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _ignoreSize;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Size& Widget::getSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _size;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Point& Widget::getSizePercent() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _sizePercent;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Point Widget::getWorldPosition()
2013-09-13 22:20:20 +08:00
{
2013-12-24 11:18:25 +08:00
return convertToWorldSpace(Point::ZERO);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Node* Widget::getVirtualRenderer()
2013-09-13 22:20:20 +08:00
{
2013-12-24 20:22:14 +08:00
return this;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::onSizeChanged()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
for (auto& child : getChildren())
{
if (child)
{
static_cast<Widget*>(child)->updateSizeAndPosition();
}
}
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Size& Widget::getContentSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _size;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setTouchEnabled(bool enable)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
if (enable == _touchEnabled)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
return;
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
_touchEnabled = enable;
2013-12-23 15:02:52 +08:00
if (_touchEnabled)
{
_touchListener = EventListenerTouchOneByOne::create();
CC_SAFE_RETAIN(_touchListener);
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(Widget::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(Widget::onTouchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(Widget::onTouchEnded, this);
_touchListener->onTouchCancelled = CC_CALLBACK_2(Widget::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
}
else
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE_NULL(_touchListener);
}
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool Widget::isTouchEnabled() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _touchEnabled;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setUpdateEnabled(bool enable)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
if (enable == _updateEnabled)
{
return;
}
2013-09-16 15:32:52 +08:00
_updateEnabled = enable;
2013-09-13 22:20:20 +08:00
if (enable)
{
2013-12-23 15:02:52 +08:00
scheduleUpdate();
2013-09-13 22:20:20 +08:00
}
else
{
2013-12-23 15:02:52 +08:00
unscheduleUpdate();
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
bool Widget::isUpdateEnabled()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _updateEnabled;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool Widget::isFocused() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _focus;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setFocused(bool fucos)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (fucos == _focus)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 15:32:52 +08:00
_focus = fucos;
if (_bright)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_focus)
2013-09-13 22:20:20 +08:00
{
setBrightStyle(BRIGHT_HIGHLIGHT);
}
else
{
setBrightStyle(BRIGHT_NORMAL);
}
}
else
{
onPressStateChangedToDisabled();
}
}
2013-12-23 15:02:52 +08:00
void Widget::setBright(bool bright)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_bright = bright;
if (_bright)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_brightStyle = BRIGHT_NONE;
2013-09-13 22:20:20 +08:00
setBrightStyle(BRIGHT_NORMAL);
}
else
{
onPressStateChangedToDisabled();
}
}
2013-12-23 15:02:52 +08:00
void Widget::setBrightStyle(BrightStyle style)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_brightStyle == style)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 15:32:52 +08:00
_brightStyle = style;
switch (_brightStyle)
2013-09-13 22:20:20 +08:00
{
case BRIGHT_NORMAL:
onPressStateChangedToNormal();
break;
case BRIGHT_HIGHLIGHT:
onPressStateChangedToPressed();
break;
default:
break;
}
}
2013-12-23 15:02:52 +08:00
void Widget::onPressStateChangedToNormal()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::onPressStateChangedToPressed()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::onPressStateChangedToDisabled()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::didNotSelectSelf()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_touchStartPos = touch->getLocation();
_hitted = isEnabled()
& isTouchEnabled()
& hitTest(_touchStartPos)
& clippingParentAreaContainPoint(_touchStartPos);
if (!_hitted)
{
return false;
}
2013-09-13 22:20:20 +08:00
setFocused(true);
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
widgetParent->checkChildInfo(0,this,_touchStartPos);
2013-09-13 22:20:20 +08:00
}
pushDownEvent();
2013-12-23 15:02:52 +08:00
return !_touchPassedEnabled;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::onTouchMoved(Touch *touch, Event *unusedEvent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_touchMovePos = touch->getLocation();
setFocused(hitTest(_touchMovePos));
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
widgetParent->checkChildInfo(1,this,_touchMovePos);
2013-09-13 22:20:20 +08:00
}
moveEvent();
}
2013-12-23 15:02:52 +08:00
void Widget::onTouchEnded(Touch *touch, Event *unusedEvent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
_touchEndPos = touch->getLocation();
2013-09-16 15:32:52 +08:00
bool focus = _focus;
2013-09-13 22:20:20 +08:00
setFocused(false);
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
widgetParent->checkChildInfo(2,this,_touchEndPos);
2013-09-13 22:20:20 +08:00
}
if (focus)
{
releaseUpEvent();
}
else
{
cancelUpEvent();
}
}
2013-12-23 15:02:52 +08:00
void Widget::onTouchCancelled(Touch *touch, Event *unusedEvent)
2013-09-13 22:20:20 +08:00
{
setFocused(false);
cancelUpEvent();
}
2013-12-23 15:02:52 +08:00
void Widget::onTouchLongClicked(const Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
longClickEvent();
}
2013-12-23 15:02:52 +08:00
void Widget::pushDownEvent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_touchEventListener && _touchEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_BEGAN);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::moveEvent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_touchEventListener && _touchEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_MOVED);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::releaseUpEvent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_touchEventListener && _touchEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_ENDED);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::cancelUpEvent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_touchEventListener && _touchEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_CANCELED);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::longClickEvent()
2013-09-13 22:20:20 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::addTouchEventListener(Object *target, SEL_TouchEvent selector)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_touchEventListener = target;
_touchEventSelector = selector;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool Widget::hitTest(const Point &pt)
2013-09-13 22:20:20 +08:00
{
2013-12-24 11:18:25 +08:00
Point nsp = convertToNodeSpace(pt);
2013-12-23 15:02:52 +08:00
Rect bb = Rect(-_size.width * _anchorPoint.x, -_size.height * _anchorPoint.y, _size.width, _size.height);
2013-09-13 22:20:20 +08:00
if (nsp.x >= bb.origin.x && nsp.x <= bb.origin.x + bb.size.width && nsp.y >= bb.origin.y && nsp.y <= bb.origin.y + bb.size.height)
{
return true;
}
return false;
}
2013-12-23 15:02:52 +08:00
bool Widget::clippingParentAreaContainPoint(const Point &pt)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_affectByClipping = false;
2013-12-23 15:02:52 +08:00
Widget* parent = getWidgetParent();
Widget* clippingParent = nullptr;
2013-09-13 22:20:20 +08:00
while (parent)
{
2013-12-23 15:02:52 +08:00
Layout* layoutParent = dynamic_cast<Layout*>(parent);
2013-09-13 22:20:20 +08:00
if (layoutParent)
{
if (layoutParent->isClippingEnabled())
{
2013-09-16 15:32:52 +08:00
_affectByClipping = true;
2013-09-13 22:20:20 +08:00
clippingParent = layoutParent;
break;
}
}
2013-12-23 15:02:52 +08:00
parent = parent->getWidgetParent();
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
if (!_affectByClipping)
2013-09-13 22:20:20 +08:00
{
return true;
}
if (clippingParent)
{
bool bRet = false;
if (clippingParent->hitTest(pt))
{
bRet = true;
}
if (bRet)
{
return clippingParent->clippingParentAreaContainPoint(pt);
}
return false;
}
return true;
}
2013-12-23 15:02:52 +08:00
void Widget::checkChildInfo(int handleState, Widget *sender, const Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
widgetParent->checkChildInfo(handleState,sender,touchPoint);
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::setPosition(const Point &pos)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
if (_running)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
2013-11-14 14:46:44 +08:00
{
2013-12-23 15:02:52 +08:00
Size pSize = widgetParent->getSize();
if (pSize.width <= 0.0f || pSize.height <= 0.0f)
{
_positionPercent = Point::ZERO;
}
else
{
_positionPercent = Point(pos.x / pSize.width, pos.y / pSize.height);
}
2013-11-14 14:46:44 +08:00
}
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
Node::setPosition(pos);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setPositionPercent(const Point &percent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_positionPercent = percent;
2013-12-23 15:02:52 +08:00
if (_running)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
Size parentSize = widgetParent->getSize();
Point absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
2013-12-24 11:18:25 +08:00
setPosition(absPos);
2013-12-23 15:02:52 +08:00
}
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
void Widget::updateAnchorPoint()
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
setAnchorPoint(getAnchorPoint());
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Point& Widget::getPositionPercent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _positionPercent;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setPositionType(PositionType type)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_positionType = type;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
PositionType Widget::getPositionType() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _positionType;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool Widget::isBright() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _bright;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
bool Widget::isEnabled() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _enabled;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
float Widget::getLeftInParent()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return getPosition().x - getAnchorPoint().x * _size.width;;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
float Widget::getBottomInParent()
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
return getPosition().y - getAnchorPoint().y * _size.height;;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
float Widget::getRightInParent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return getLeftInParent() + _size.width;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
float Widget::getTopInParent()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return getBottomInParent() + _size.height;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Point& Widget::getTouchStartPos()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _touchStartPos;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Point& Widget::getTouchMovePos()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _touchMovePos;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const Point& Widget::getTouchEndPos()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _touchEndPos;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setName(const char* name)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_name = name;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
const char* Widget::getName() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _name.c_str();
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
WidgetType Widget::getWidgetType() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _widgetType;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::setLayoutParameter(LayoutParameter *parameter)
2013-09-13 22:20:20 +08:00
{
2013-11-13 20:01:57 +08:00
if (!parameter)
{
return;
}
2013-12-23 15:02:52 +08:00
_layoutParameterDictionary.insert(parameter->getLayoutType(), parameter);
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
LayoutParameter* Widget::getLayoutParameter(LayoutParameterType type)
2013-09-13 22:20:20 +08:00
{
2013-12-23 15:02:52 +08:00
return dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at(type));
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
std::string Widget::getDescription() const
2013-09-17 17:59:20 +08:00
{
return "Widget";
}
2013-12-23 15:02:52 +08:00
Widget* Widget::clone()
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* clonedWidget = createCloneInstance();
2013-11-06 16:04:06 +08:00
clonedWidget->copyProperties(this);
clonedWidget->copyClonedWidgetChildren(this);
return clonedWidget;
}
2013-12-23 15:02:52 +08:00
Widget* Widget::createCloneInstance()
2013-11-06 16:04:06 +08:00
{
2013-12-23 15:02:52 +08:00
return Widget::create();
2013-11-06 16:04:06 +08:00
}
2013-12-23 15:02:52 +08:00
void Widget::copyClonedWidgetChildren(Widget* model)
2013-11-06 16:04:06 +08:00
{
2013-12-26 16:17:52 +08:00
auto& modelChildren = model->getChildren();
for (auto& subWidget : modelChildren)
2013-11-06 16:04:06 +08:00
{
2013-12-26 16:17:52 +08:00
Widget* child = static_cast<Widget*>(subWidget);
2013-11-06 16:04:06 +08:00
addChild(child->clone());
}
}
2013-12-23 15:02:52 +08:00
void Widget::copySpecialProperties(Widget* model)
2013-11-06 16:04:06 +08:00
{
}
2013-12-23 15:02:52 +08:00
void Widget::copyProperties(Widget *widget)
2013-11-06 16:04:06 +08:00
{
setEnabled(widget->isEnabled());
setVisible(widget->isVisible());
setBright(widget->isBright());
setTouchEnabled(widget->isTouchEnabled());
_touchPassedEnabled = false;
setZOrder(widget->getZOrder());
setUpdateEnabled(widget->isUpdateEnabled());
setTag(widget->getTag());
setName(widget->getName());
setActionTag(widget->getActionTag());
_ignoreSize = widget->_ignoreSize;
_size = widget->_size;
_customSize = widget->_customSize;
copySpecialProperties(widget);
_sizeType = widget->getSizeType();
_sizePercent = widget->_sizePercent;
_positionType = widget->_positionType;
_positionPercent = widget->_positionPercent;
setPosition(widget->getPosition());
setAnchorPoint(widget->getAnchorPoint());
setScaleX(widget->getScaleX());
setScaleY(widget->getScaleY());
setRotation(widget->getRotation());
setRotationX(widget->getRotationX());
setRotationY(widget->getRotationY());
setFlipX(widget->isFlipX());
setFlipY(widget->isFlipY());
setColor(widget->getColor());
setOpacity(widget->getOpacity());
setCascadeOpacityEnabled(widget->isCascadeOpacityEnabled());
setCascadeColorEnabled(widget->isCascadeColorEnabled());
onSizeChanged();
}
2013-09-13 22:20:20 +08:00
/*temp action*/
2013-12-23 15:02:52 +08:00
void Widget::setActionTag(int tag)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_actionTag = tag;
2013-09-13 22:20:20 +08:00
}
2013-12-23 15:02:52 +08:00
int Widget::getActionTag()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
return _actionTag;
2013-09-13 22:20:20 +08:00
}
}
2013-12-23 15:02:52 +08:00
NS_CC_END