axmol/extensions/CocoStudio/GUI/BaseClasses/UIWidget.cpp

1219 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 "UIWidget.h"
#include "../System/UILayer.h"
#include "../Layouts/Layout.h"
#include "../System/UIHelper.h"
NS_CC_EXT_BEGIN
2013-09-16 15:32:52 +08:00
#define DYNAMIC_CAST_CCBLENDPROTOCOL dynamic_cast<cocos2d::BlendProtocol*>(_renderer)
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
#define DYNAMIC_CAST_CCRGBAPROTOCOL dynamic_cast<cocos2d::RGBAProtocol*>(_renderer)
2013-09-13 22:20:20 +08:00
2013-09-16 15:32:52 +08:00
#define DYNAMIC_CAST_CCNODERGBA dynamic_cast<GUIRenderer*>(_renderer)
2013-09-13 22:20:20 +08:00
UIWidget::UIWidget():
2013-09-16 15:32:52 +08:00
_enabled(true),
_visible(true),
_bright(true),
_touchEnabled(false),
_touchPassedEnabled(false),
_focus(false),
_widgetZOrder(0),
_anchorPoint(Point(0.5f, 0.5f)),
_widgetParent(NULL),
_brightStyle(BRIGHT_NONE),
_updateEnabled(false),
_renderer(NULL),
_touchStartPos(Point::ZERO),
_touchMovePos(Point::ZERO),
_touchEndPos(Point::ZERO),
_touchEventListener(NULL),
_touchEventSelector(NULL),
_widgetTag(-1),
_name("default"),
_widgetType(WidgetTypeWidget),
_actionTag(0),
_size(Size::ZERO),
_customSize(Size::ZERO),
_layoutParameter(NULL),
_ignoreSize(false),
_children(NULL),
_affectByClipping(false),
_scheduler(NULL),
_sizeType(SIZE_ABSOLUTE),
_sizePercent(Point::ZERO),
_positionType(POSITION_ABSOLUTE),
_positionPercent(Point::ZERO),
2013-09-17 21:14:45 +08:00
_isRunning(false)
2013-09-13 22:20:20 +08:00
{
}
UIWidget::~UIWidget()
{
releaseResoures();
setParent(NULL);
2013-09-16 15:32:52 +08:00
CC_SAFE_RELEASE_NULL(_layoutParameter);
CC_SAFE_RELEASE(_scheduler);
2013-09-13 22:20:20 +08:00
}
UIWidget* UIWidget::create()
{
UIWidget* widget = new UIWidget();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}
bool UIWidget::init()
{
2013-09-16 15:32:52 +08:00
_children = Array::create();
_children->retain();
2013-09-13 22:20:20 +08:00
initRenderer();
2013-09-16 15:32:52 +08:00
_renderer->retain();
_renderer->setZOrder(_widgetZOrder);
2013-09-13 22:20:20 +08:00
RGBAProtocol* renderRGBA = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (renderRGBA)
{
renderRGBA->setCascadeColorEnabled(true);
renderRGBA->setCascadeOpacityEnabled(true);
}
setBright(true);
ignoreContentAdaptWithSize(true);
2013-09-16 15:32:52 +08:00
_scheduler = Director::getInstance()->getScheduler();
CC_SAFE_RETAIN(_scheduler);
2013-09-13 22:20:20 +08:00
return true;
}
void UIWidget::releaseResoures()
{
setUpdateEnabled(false);
removeAllChildren();
2013-09-16 15:32:52 +08:00
_children->release();
_renderer->removeAllChildrenWithCleanup(true);
_renderer->removeFromParentAndCleanup(true);
_renderer->release();
2013-09-13 22:20:20 +08:00
}
void UIWidget::onEnter()
{
2013-09-16 15:32:52 +08:00
arrayMakeObjectsPerformSelector(_children, onEnter, UIWidget*);
_isRunning = true;
2013-09-13 22:20:20 +08:00
updateSizeAndPosition();
}
void UIWidget::onExit()
{
2013-09-16 15:32:52 +08:00
_isRunning = false;
arrayMakeObjectsPerformSelector(_children, onExit, UIWidget*);
2013-09-13 22:20:20 +08:00
}
bool UIWidget::addChild(UIWidget *child)
{
if (!child)
{
return false;
}
2013-09-16 15:32:52 +08:00
if (_children->containsObject(child))
2013-09-13 22:20:20 +08:00
{
return false;
}
child->setParent(this);
2013-09-16 15:32:52 +08:00
int childrenCount = _children->data->num;
2013-09-13 22:20:20 +08:00
if (childrenCount <= 0)
{
2013-09-16 15:32:52 +08:00
_children->addObject(child);
2013-09-13 22:20:20 +08:00
}
else
{
bool seekSucceed = false;
2013-09-16 15:32:52 +08:00
ccArray* arrayChildren = _children->data;
2013-09-13 22:20:20 +08:00
for (int i=childrenCount-1; i>=0; --i)
{
UIWidget* widget = (UIWidget*)(arrayChildren->arr[i]);
if (child->getZOrder() >= widget->getZOrder())
{
if (i == childrenCount-1)
{
2013-09-16 15:32:52 +08:00
_children->addObject(child);
2013-09-13 22:20:20 +08:00
seekSucceed = true;
break;
}
else
{
2013-09-16 15:32:52 +08:00
_children->insertObject(child, i+1);
2013-09-13 22:20:20 +08:00
seekSucceed = true;
break;
}
}
}
if (!seekSucceed)
{
2013-09-16 15:32:52 +08:00
_children->insertObject(child,0);
2013-09-13 22:20:20 +08:00
}
}
child->getRenderer()->setZOrder(child->getZOrder());
2013-09-16 15:32:52 +08:00
_renderer->addChild(child->getRenderer());
if (_isRunning)
2013-09-13 22:20:20 +08:00
{
child->onEnter();
}
return true;
}
bool UIWidget::removeChild(UIWidget *child)
{
if (!child)
{
return false;
}
2013-09-16 15:32:52 +08:00
if (_children->containsObject(child))
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_isRunning)
2013-09-13 22:20:20 +08:00
{
child->onExit();
}
child->disableUpdate();
child->setParent(NULL);
2013-09-16 15:32:52 +08:00
_renderer->removeChild(child->getRenderer());
_children->removeObject(child);
2013-09-13 22:20:20 +08:00
return true;
}
return false;
}
void UIWidget::removeFromParent()
{
2013-09-16 15:32:52 +08:00
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->removeChild(this);
2013-09-13 22:20:20 +08:00
}
}
void UIWidget::removeAllChildren()
{
2013-09-16 15:32:52 +08:00
if (!_children || _children->count() <= 0)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 15:32:52 +08:00
int times = _children->data->num;
2013-09-13 22:20:20 +08:00
for (int i=0; i<times; ++i)
{
2013-09-16 15:32:52 +08:00
UIWidget* lastChild = (UIWidget*)(_children->getLastObject());
2013-09-13 22:20:20 +08:00
removeChild(lastChild);
}
}
void UIWidget::reorderChild(UIWidget* child)
{
CC_SAFE_RETAIN(child);
2013-09-16 15:32:52 +08:00
_children->removeObject(child);
int childrenCount = _children->data->num;
2013-09-13 22:20:20 +08:00
if (childrenCount <= 0)
{
2013-09-16 15:32:52 +08:00
_children->addObject(child);
2013-09-13 22:20:20 +08:00
}
else
{
bool seekSucceed = false;
2013-09-16 15:32:52 +08:00
ccArray* arrayChildren = _children->data;
2013-09-13 22:20:20 +08:00
for (int i=childrenCount-1; i>=0; --i)
{
UIWidget* widget = (UIWidget*)(arrayChildren->arr[i]);
if (child->getZOrder() >= widget->getZOrder())
{
if (i == childrenCount-1)
{
2013-09-16 15:32:52 +08:00
_children->addObject(child);
2013-09-13 22:20:20 +08:00
seekSucceed = true;
break;
}
else
{
2013-09-16 15:32:52 +08:00
_children->insertObject(child, i+1);
2013-09-13 22:20:20 +08:00
seekSucceed = true;
break;
}
}
}
if (!seekSucceed)
{
2013-09-16 15:32:52 +08:00
_children->insertObject(child,0);
2013-09-13 22:20:20 +08:00
}
}
CC_SAFE_RELEASE(child);
}
void UIWidget::disableUpdate()
{
2013-09-16 15:32:52 +08:00
if (_scheduler)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_scheduler->unscheduleUpdateForTarget(this);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
int childrenCount = _children->data->num;
ccArray* arrayChildren = _children->data;
2013-09-13 22:20:20 +08:00
for (int i=0; i<childrenCount; i++)
{
UIWidget* child = (UIWidget*)(arrayChildren->arr[i]);
child->disableUpdate();
}
}
void UIWidget::setEnabled(bool enabled)
{
2013-09-16 15:32:52 +08:00
_enabled = enabled;
2013-09-13 22:20:20 +08:00
GUIRenderer* renderer = DYNAMIC_CAST_CCNODERGBA;
if (renderer)
{
renderer->setEnabled(enabled);
}
else
{
2013-09-16 15:32:52 +08:00
dynamic_cast<RectClippingNode*>(_renderer)->setEnabled(enabled);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
ccArray* arrayChildren = _children->data;
2013-09-13 22:20:20 +08:00
int childrenCount = arrayChildren->num;
for (int i = 0; i < childrenCount; i++)
{
UIWidget* child = dynamic_cast<UIWidget*>(arrayChildren->arr[i]);
child->setEnabled(enabled);
}
}
UIWidget* UIWidget::getChildByName(const char *name)
{
return CCUIHELPER->seekWidgetByName(this, name);
}
UIWidget* UIWidget::getChildByTag(int tag)
{
return CCUIHELPER->seekWidgetByTag(this, tag);
}
Array* UIWidget::getChildren()
{
2013-09-16 15:32:52 +08:00
return _children;
2013-09-13 22:20:20 +08:00
}
void UIWidget::initRenderer()
{
2013-09-16 15:32:52 +08:00
_renderer = GUIRenderer::create();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setSize(const Size &size)
{
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-09-16 15:32:52 +08:00
if (_isRunning)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_sizePercent = (_widgetParent == NULL) ? Point::ZERO : Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height);
2013-09-13 22:20:20 +08:00
}
onSizeChanged();
}
void UIWidget::setSizePercent(const Point &percent)
{
2013-09-16 15:32:52 +08:00
_sizePercent = percent;
if (!_isRunning)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 15:32:52 +08:00
Size cSize = (_widgetParent == NULL) ? Size::ZERO : Size(_widgetParent->getSize().width * percent.x , _widgetParent->getSize().height * percent.y);
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();
}
void UIWidget::updateSizeAndPosition()
{
2013-09-16 15:32:52 +08:00
switch (_sizeType)
2013-09-13 22:20:20 +08:00
{
case SIZE_ABSOLUTE:
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-09-16 15:32:52 +08:00
_sizePercent = (_widgetParent == NULL) ? Point::ZERO : Point(_customSize.width / _widgetParent->getSize().width, _customSize.height / _widgetParent->getSize().height);
2013-09-13 22:20:20 +08:00
break;
case SIZE_PERCENT:
{
2013-09-16 15:32:52 +08:00
Size cSize = (_widgetParent == NULL) ? Size::ZERO : Size(_widgetParent->getSize().width * _sizePercent.x , _widgetParent->getSize().height * _sizePercent.y);
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
}
break;
default:
break;
}
onSizeChanged();
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-09-16 15:32:52 +08:00
_positionPercent = (_widgetParent == NULL) ? Point::ZERO : Point(absPos.x / _widgetParent->getSize().width, absPos.y / _widgetParent->getSize().height);
2013-09-13 22:20:20 +08:00
break;
case POSITION_PERCENT:
{
2013-09-16 15:32:52 +08:00
Size parentSize = _widgetParent->getSize();
absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
2013-09-13 22:20:20 +08:00
}
break;
default:
break;
}
2013-09-16 15:32:52 +08:00
_renderer->setPosition(absPos);
2013-09-13 22:20:20 +08:00
}
void UIWidget::setSizeType(SizeType type)
{
2013-09-16 15:32:52 +08:00
_sizeType = type;
2013-09-13 22:20:20 +08:00
}
SizeType UIWidget::getSizeType() const
{
2013-09-16 15:32:52 +08:00
return _sizeType;
2013-09-13 22:20:20 +08:00
}
void UIWidget::ignoreContentAdaptWithSize(bool ignore)
{
2013-09-16 15:32:52 +08:00
_ignoreSize = ignore;
if (_ignoreSize)
2013-09-13 22:20:20 +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();
}
bool UIWidget::isIgnoreContentAdaptWithSize() const
{
2013-09-16 15:32:52 +08:00
return _ignoreSize;
2013-09-13 22:20:20 +08:00
}
const Size& UIWidget::getSize() const
{
2013-09-16 15:32:52 +08:00
return _size;
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getSizePercent() const
{
2013-09-16 15:32:52 +08:00
return _sizePercent;
2013-09-13 22:20:20 +08:00
}
Point UIWidget::getWorldPosition()
{
2013-09-16 15:32:52 +08:00
return _renderer->convertToWorldSpace(Point::ZERO);
2013-09-13 22:20:20 +08:00
}
Point UIWidget::convertToWorldSpace(const Point& pt)
{
2013-09-16 15:32:52 +08:00
return _renderer->convertToWorldSpace(pt);
2013-09-13 22:20:20 +08:00
}
Node* UIWidget::getVirtualRenderer()
{
2013-09-16 15:32:52 +08:00
return _renderer;
2013-09-13 22:20:20 +08:00
}
void UIWidget::onSizeChanged()
{
}
const Size& UIWidget::getContentSize() const
{
2013-09-16 15:32:52 +08:00
return _size;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setZOrder(int z)
{
2013-09-16 15:32:52 +08:00
_widgetZOrder = z;
_renderer->setZOrder(z);
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->reorderChild(this);
2013-09-13 22:20:20 +08:00
}
}
int UIWidget::getZOrder()
{
2013-09-16 15:32:52 +08:00
return _widgetZOrder;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setTouchEnabled(bool enable)
{
2013-09-16 15:32:52 +08:00
_touchEnabled = enable;
2013-09-13 22:20:20 +08:00
}
bool UIWidget::isTouchEnabled() const
{
2013-09-16 15:32:52 +08:00
return _touchEnabled;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setUpdateEnabled(bool enable)
{
2013-09-16 15:32:52 +08:00
_updateEnabled = enable;
2013-09-13 22:20:20 +08:00
if (enable)
{
2013-09-16 15:32:52 +08:00
if (_scheduler)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_scheduler->scheduleUpdateForTarget(this, 0, false);
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 15:32:52 +08:00
if (_scheduler)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_scheduler->unscheduleUpdateForTarget(this);
2013-09-13 22:20:20 +08:00
}
}
}
bool UIWidget::isUpdateEnabled()
{
2013-09-16 15:32:52 +08:00
return _updateEnabled;
2013-09-13 22:20:20 +08:00
}
bool UIWidget::isFocused() const
{
2013-09-16 15:32:52 +08:00
return _focus;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setFocused(bool fucos)
{
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();
}
}
void UIWidget::setBright(bool bright)
{
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();
}
}
void UIWidget::setBrightStyle(BrightStyle style)
{
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;
}
}
void UIWidget::onPressStateChangedToNormal()
{
}
void UIWidget::onPressStateChangedToPressed()
{
}
void UIWidget::onPressStateChangedToDisabled()
{
}
void UIWidget::didNotSelectSelf()
{
}
bool UIWidget::onTouchBegan(const Point &touchPoint)
{
setFocused(true);
2013-09-16 15:32:52 +08:00
_touchStartPos.x = touchPoint.x;
_touchStartPos.y = touchPoint.y;
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(0,this,touchPoint);
2013-09-13 22:20:20 +08:00
}
pushDownEvent();
2013-09-16 15:32:52 +08:00
return _touchPassedEnabled;
2013-09-13 22:20:20 +08:00
}
void UIWidget::onTouchMoved(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
_touchMovePos.x = touchPoint.x;
_touchMovePos.y = touchPoint.y;
2013-09-13 22:20:20 +08:00
setFocused(hitTest(touchPoint));
2013-09-16 15:32:52 +08:00
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(1,this,touchPoint);
2013-09-13 22:20:20 +08:00
}
moveEvent();
}
void UIWidget::onTouchEnded(const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
_touchEndPos.x = touchPoint.x;
_touchEndPos.y = touchPoint.y;
bool focus = _focus;
2013-09-13 22:20:20 +08:00
setFocused(false);
2013-09-16 15:32:52 +08:00
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(2,this,touchPoint);
2013-09-13 22:20:20 +08:00
}
if (focus)
{
releaseUpEvent();
}
else
{
cancelUpEvent();
}
}
void UIWidget::onTouchCancelled(const Point &touchPoint)
{
setFocused(false);
cancelUpEvent();
}
void UIWidget::onTouchLongClicked(const Point &touchPoint)
{
longClickEvent();
}
void UIWidget::pushDownEvent()
{
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
}
}
void UIWidget::moveEvent()
{
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
}
}
void UIWidget::releaseUpEvent()
{
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
}
}
void UIWidget::cancelUpEvent()
{
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
}
}
void UIWidget::longClickEvent()
{
}
void UIWidget::addTouchEventListener(Object *target, SEL_TouchEvent selector)
{
2013-09-16 15:32:52 +08:00
_touchEventListener = target;
_touchEventSelector = selector;
2013-09-13 22:20:20 +08:00
}
Node* UIWidget::getRenderer()
{
2013-09-16 15:32:52 +08:00
return _renderer;
2013-09-13 22:20:20 +08:00
}
void UIWidget::addRenderer(Node* renderer, int zOrder)
{
2013-09-16 15:32:52 +08:00
_renderer->addChild(renderer, zOrder);
2013-09-13 22:20:20 +08:00
}
void UIWidget::removeRenderer(Node* renderer, bool cleanup)
{
2013-09-16 15:32:52 +08:00
_renderer->removeChild(renderer,cleanup);
2013-09-13 22:20:20 +08:00
}
bool UIWidget::hitTest(const Point &pt)
{
2013-09-16 15:32:52 +08:00
Point nsp = _renderer->convertToNodeSpace(pt);
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;
}
bool UIWidget::clippingParentAreaContainPoint(const Point &pt)
{
2013-09-16 15:32:52 +08:00
_affectByClipping = false;
2013-09-13 22:20:20 +08:00
UIWidget* parent = getParent();
UIWidget* clippingParent = NULL;
while (parent)
{
Layout* layoutParent = dynamic_cast<Layout*>(parent);
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;
}
}
parent = parent->getParent();
}
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;
}
void UIWidget::checkChildInfo(int handleState, UIWidget *sender, const Point &touchPoint)
{
2013-09-16 15:32:52 +08:00
if (_widgetParent)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(handleState,sender,touchPoint);
2013-09-13 22:20:20 +08:00
}
}
void UIWidget::setPosition(const Point &pos)
{
2013-09-16 15:32:52 +08:00
if (_isRunning)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
_positionPercent = (_widgetParent == NULL) ? Point::ZERO : Point(pos.x / _widgetParent->getSize().width, pos.y / _widgetParent->getSize().height);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
_renderer->setPosition(pos);
2013-09-13 22:20:20 +08:00
}
void UIWidget::setPositionPercent(const Point &percent)
{
2013-09-16 15:32:52 +08:00
_positionPercent = percent;
if (_isRunning)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
Size parentSize = _widgetParent->getSize();
Point absPos = Point(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
_renderer->setPosition(absPos);
2013-09-13 22:20:20 +08:00
}
}
void UIWidget::setAnchorPoint(const Point &pt)
{
2013-09-16 15:32:52 +08:00
_anchorPoint = pt;
_renderer->setAnchorPoint(pt);
2013-09-13 22:20:20 +08:00
}
void UIWidget::updateAnchorPoint()
{
2013-09-16 15:32:52 +08:00
setAnchorPoint(_anchorPoint);
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getPosition()
{
2013-09-16 15:32:52 +08:00
return _renderer->getPosition();
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getPositionPercent()
{
2013-09-16 15:32:52 +08:00
return _positionPercent;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setPositionType(PositionType type)
{
2013-09-16 15:32:52 +08:00
_positionType = type;
2013-09-13 22:20:20 +08:00
}
PositionType UIWidget::getPositionType() const
{
2013-09-16 15:32:52 +08:00
return _positionType;
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getAnchorPoint()
{
2013-09-16 15:32:52 +08:00
return _anchorPoint;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setScale(float scale)
{
2013-09-16 15:32:52 +08:00
_renderer->setScale(scale);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getScale()
{
2013-09-16 15:32:52 +08:00
return _renderer->getScale();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setScaleX(float scaleX)
{
2013-09-16 15:32:52 +08:00
_renderer->setScaleX(scaleX);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getScaleX()
{
2013-09-16 15:32:52 +08:00
return _renderer->getScaleX();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setScaleY(float scaleY)
{
2013-09-16 15:32:52 +08:00
_renderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getScaleY()
{
2013-09-16 15:32:52 +08:00
return _renderer->getScaleY();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setRotation(float rotation)
{
2013-09-16 15:32:52 +08:00
_renderer->setRotation(rotation);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getRotation()
{
2013-09-16 15:32:52 +08:00
return _renderer->getRotation();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setRotationX(float rotationX)
{
2013-09-16 15:32:52 +08:00
_renderer->setRotationX(rotationX);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getRotationX()
{
2013-09-16 15:32:52 +08:00
return _renderer->getRotationX();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setRotationY(float rotationY)
{
2013-09-16 15:32:52 +08:00
_renderer->setRotationY(rotationY);
2013-09-13 22:20:20 +08:00
}
float UIWidget::getRotationY()
{
2013-09-16 15:32:52 +08:00
return _renderer->getRotationY();
2013-09-13 22:20:20 +08:00
}
void UIWidget::setVisible(bool visible)
{
2013-09-16 15:32:52 +08:00
_visible = visible;
_renderer->setVisible(visible);
2013-09-13 22:20:20 +08:00
}
bool UIWidget::isVisible() const
{
2013-09-16 15:32:52 +08:00
return _visible;
2013-09-13 22:20:20 +08:00
}
bool UIWidget::isBright() const
{
2013-09-16 15:32:52 +08:00
return _bright;
2013-09-13 22:20:20 +08:00
}
bool UIWidget::isEnabled() const
{
2013-09-16 15:32:52 +08:00
return _enabled;
2013-09-13 22:20:20 +08:00
}
float UIWidget::getLeftInParent()
{
float leftPos = 0.0f;
2013-09-16 15:32:52 +08:00
switch (_widgetType)
2013-09-13 22:20:20 +08:00
{
case WidgetTypeWidget:
2013-09-16 15:32:52 +08:00
leftPos = getPosition().x - getAnchorPoint().x * _size.width;
2013-09-13 22:20:20 +08:00
break;
case WidgetTypeContainer:
leftPos = getPosition().x;
break;
default:
break;
}
return leftPos;
}
float UIWidget::getBottomInParent()
{
float bottomPos = 0.0f;
2013-09-16 15:32:52 +08:00
switch (_widgetType)
2013-09-13 22:20:20 +08:00
{
case WidgetTypeWidget:
2013-09-16 15:32:52 +08:00
bottomPos = getPosition().y - getAnchorPoint().y * _size.height;
2013-09-13 22:20:20 +08:00
break;
case WidgetTypeContainer:
bottomPos = getPosition().y;
break;
default:
break;
}
return bottomPos;
}
float UIWidget::getRightInParent()
{
2013-09-16 15:32:52 +08:00
return getLeftInParent() + _size.width;
2013-09-13 22:20:20 +08:00
}
float UIWidget::getTopInParent()
{
2013-09-16 15:32:52 +08:00
return getBottomInParent() + _size.height;
2013-09-13 22:20:20 +08:00
}
UIWidget* UIWidget::getParent()
{
2013-09-16 15:32:52 +08:00
return _widgetParent;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setParent(UIWidget* parent)
{
2013-09-16 15:32:52 +08:00
_widgetParent = parent;
2013-09-13 22:20:20 +08:00
}
Action* UIWidget::runAction(Action *action)
{
2013-09-16 15:32:52 +08:00
return _renderer->runAction(action);
2013-09-13 22:20:20 +08:00
}
void UIWidget::setActionManager(ActionManager *actionManager)
{
2013-09-16 15:32:52 +08:00
_renderer->setActionManager(actionManager);
2013-09-13 22:20:20 +08:00
}
ActionManager* UIWidget::getActionManager()
{
2013-09-16 15:32:52 +08:00
return _renderer->getActionManager();
2013-09-13 22:20:20 +08:00
}
void UIWidget::stopAllActions()
{
2013-09-16 15:32:52 +08:00
_renderer->stopAllActions();
2013-09-13 22:20:20 +08:00
}
void UIWidget::stopAction(Action *action)
{
2013-09-16 15:32:52 +08:00
_renderer->stopAction(action);
2013-09-13 22:20:20 +08:00
}
void UIWidget::stopActionByTag(int tag)
{
2013-09-16 15:32:52 +08:00
_renderer->stopActionByTag(tag);
2013-09-13 22:20:20 +08:00
}
Action* UIWidget::getActionByTag(int tag)
{
2013-09-16 15:32:52 +08:00
return _renderer->getActionByTag(tag);
2013-09-13 22:20:20 +08:00
}
void UIWidget::setColor(const Color3B &color)
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
rgbap->setColor(color);
}
}
const Color3B& UIWidget::getColor()
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
return rgbap->getColor();
}
return Color3B::WHITE;
}
void UIWidget::setOpacity(int opacity)
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
rgbap->setOpacity(opacity);
}
}
int UIWidget::getOpacity()
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
return rgbap->getOpacity();
}
return 255;
}
bool UIWidget::isCascadeOpacityEnabled()
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
return rgbap->isCascadeOpacityEnabled();
}
return false;
}
void UIWidget::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
rgbap->setCascadeOpacityEnabled(cascadeOpacityEnabled);
}
}
bool UIWidget::isCascadeColorEnabled()
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
return rgbap->isCascadeColorEnabled();
}
return false;
}
void UIWidget::setCascadeColorEnabled(bool cascadeColorEnabled)
{
RGBAProtocol* rgbap = DYNAMIC_CAST_CCRGBAPROTOCOL;
if (rgbap)
{
rgbap->setCascadeColorEnabled(cascadeColorEnabled);
}
}
void UIWidget::setBlendFunc(BlendFunc blendFunc)
{
BlendProtocol * blendNode = DYNAMIC_CAST_CCBLENDPROTOCOL;
if (blendNode)
{
blendNode->setBlendFunc(blendFunc);
}
}
const Point& UIWidget::getTouchStartPos()
{
2013-09-16 15:32:52 +08:00
return _touchStartPos;
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getTouchMovePos()
{
2013-09-16 15:32:52 +08:00
return _touchMovePos;
2013-09-13 22:20:20 +08:00
}
const Point& UIWidget::getTouchEndPos()
{
2013-09-16 15:32:52 +08:00
return _touchEndPos;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setTag(int tag)
{
2013-09-16 15:32:52 +08:00
_widgetTag = tag;
2013-09-13 22:20:20 +08:00
}
int UIWidget::getTag() const
{
2013-09-16 15:32:52 +08:00
return _widgetTag;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setName(const char* name)
{
2013-09-16 15:32:52 +08:00
_name = name;
2013-09-13 22:20:20 +08:00
}
const char* UIWidget::getName() const
{
2013-09-16 15:32:52 +08:00
return _name.c_str();
2013-09-13 22:20:20 +08:00
}
WidgetType UIWidget::getWidgetType() const
{
2013-09-16 15:32:52 +08:00
return _widgetType;
2013-09-13 22:20:20 +08:00
}
void UIWidget::setLayoutParameter(LayoutParameter *parameter)
{
2013-09-16 15:32:52 +08:00
if (_layoutParameter)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
CC_SAFE_RELEASE_NULL(_layoutParameter);
2013-09-13 22:20:20 +08:00
}
2013-09-16 15:32:52 +08:00
_layoutParameter = parameter;
CC_SAFE_RETAIN(_layoutParameter);
2013-09-13 22:20:20 +08:00
}
LayoutParameter* UIWidget::getLayoutParameter()
{
2013-09-16 15:32:52 +08:00
return _layoutParameter;
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UIWidget::getDescription() const
{
return "Widget";
}
2013-09-13 22:20:20 +08:00
/*temp action*/
void UIWidget::setActionTag(int tag)
{
2013-09-16 15:32:52 +08:00
_actionTag = tag;
2013-09-13 22:20:20 +08:00
}
int UIWidget::getActionTag()
{
2013-09-16 15:32:52 +08:00
return _actionTag;
2013-09-13 22:20:20 +08:00
}
GUIRenderer::GUIRenderer():
2013-09-16 15:32:52 +08:00
_enabled(true)
2013-09-13 22:20:20 +08:00
{
}
GUIRenderer::~GUIRenderer()
{
}
GUIRenderer* GUIRenderer::create()
{
GUIRenderer* renderer = new GUIRenderer();
if (renderer && renderer->init())
{
renderer->autorelease();
}
else
{
CC_SAFE_DELETE(renderer);
}
return renderer;
}
void GUIRenderer::setEnabled(bool enabled)
{
2013-09-16 15:32:52 +08:00
_enabled = enabled;
2013-09-13 22:20:20 +08:00
}
bool GUIRenderer::isEnabled() const
{
2013-09-16 15:32:52 +08:00
return _enabled;
2013-09-13 22:20:20 +08:00
}
void GUIRenderer::visit()
{
2013-09-16 15:32:52 +08:00
if (!_enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
NodeRGBA::visit();
}
NS_CC_EXT_END