2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UIWidget.h"
|
|
|
|
#include "ui/UILayout.h"
|
|
|
|
#include "ui/UIHelper.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventListenerKeyboard.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventFocus.h"
|
2014-08-26 18:19:28 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
#include "ui/UILayoutComponent.h"
|
2014-12-23 15:03:31 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
2015-06-18 10:23:52 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2016-07-25 17:31:54 +08:00
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "ui/UIScale9Sprite.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
class Widget::FocusNavigationController
|
|
|
|
{
|
|
|
|
void enableFocusNavigation(bool flag);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
FocusNavigationController():
|
|
|
|
_keyboardListener(nullptr),
|
|
|
|
_firstFocusedWidget(nullptr),
|
|
|
|
_enableFocusNavigation(false),
|
|
|
|
_keyboardEventPriority(1)
|
|
|
|
{
|
|
|
|
//no-op
|
|
|
|
}
|
|
|
|
~FocusNavigationController();
|
|
|
|
protected:
|
|
|
|
void setFirstFocsuedWidget(Widget* widget);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
void onKeypadKeyPressed(EventKeyboard::KeyCode, Event*);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
void addKeyboardEventListener();
|
|
|
|
void removeKeyboardEventListener();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
friend class Widget;
|
|
|
|
private:
|
|
|
|
EventListenerKeyboard* _keyboardListener ;
|
|
|
|
Widget* _firstFocusedWidget ;
|
|
|
|
bool _enableFocusNavigation ;
|
|
|
|
const int _keyboardEventPriority;
|
|
|
|
};
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
Widget::FocusNavigationController::~FocusNavigationController()
|
|
|
|
{
|
|
|
|
this->removeKeyboardEventListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::onKeypadKeyPressed(EventKeyboard::KeyCode keyCode, Event *event)
|
|
|
|
{
|
|
|
|
if (_enableFocusNavigation && _firstFocusedWidget)
|
|
|
|
{
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_DOWN)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::DOWN, _firstFocusedWidget);
|
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_UP)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::UP, _firstFocusedWidget);
|
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_LEFT)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::LEFT, _firstFocusedWidget);
|
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_RIGHT)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::RIGHT, _firstFocusedWidget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::enableFocusNavigation(bool flag)
|
|
|
|
{
|
|
|
|
if (_enableFocusNavigation == flag)
|
|
|
|
return;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
_enableFocusNavigation = flag;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
if (flag)
|
|
|
|
this->addKeyboardEventListener();
|
|
|
|
else
|
|
|
|
this->removeKeyboardEventListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::setFirstFocsuedWidget(Widget* widget)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::addKeyboardEventListener()
|
|
|
|
{
|
|
|
|
if (nullptr == _keyboardListener)
|
|
|
|
{
|
|
|
|
_keyboardListener = EventListenerKeyboard::create();
|
|
|
|
_keyboardListener->onKeyReleased = CC_CALLBACK_2(Widget::FocusNavigationController::onKeypadKeyPressed, this);
|
|
|
|
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->addEventListenerWithFixedPriority(_keyboardListener, _keyboardEventPriority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::removeKeyboardEventListener()
|
|
|
|
{
|
|
|
|
if (nullptr != _keyboardListener)
|
|
|
|
{
|
|
|
|
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->removeEventListener(_keyboardListener);
|
|
|
|
_keyboardListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
Widget* Widget::_focusedWidget = nullptr;
|
2014-05-26 22:57:50 +08:00
|
|
|
Widget::FocusNavigationController* Widget::_focusNavigationController = nullptr;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Widget::Widget():
|
2014-12-23 20:29:53 +08:00
|
|
|
_usingLayoutComponent(false),
|
2014-10-09 18:28:09 +08:00
|
|
|
_unifySize(false),
|
2014-03-11 17:13:54 +08:00
|
|
|
_enabled(true),
|
|
|
|
_bright(true),
|
|
|
|
_touchEnabled(false),
|
2014-05-04 10:19:11 +08:00
|
|
|
_highlight(false),
|
2014-10-30 22:25:46 +08:00
|
|
|
_affectByClipping(false),
|
|
|
|
_ignoreSize(false),
|
|
|
|
_propagateTouchEvents(true),
|
2014-05-09 15:23:34 +08:00
|
|
|
_brightStyle(BrightStyle::NONE),
|
2014-10-30 22:25:46 +08:00
|
|
|
_sizeType(SizeType::ABSOLUTE),
|
|
|
|
_positionType(PositionType::ABSOLUTE),
|
2014-03-11 17:13:54 +08:00
|
|
|
_actionTag(0),
|
|
|
|
_customSize(Size::ZERO),
|
|
|
|
_hitted(false),
|
2015-06-18 10:23:52 +08:00
|
|
|
_hittedByCamera(nullptr),
|
2014-03-11 17:13:54 +08:00
|
|
|
_touchListener(nullptr),
|
|
|
|
_flippedX(false),
|
2014-05-04 11:23:20 +08:00
|
|
|
_flippedY(false),
|
2014-10-30 22:25:46 +08:00
|
|
|
_layoutParameterType(LayoutParameter::Type::NONE),
|
2014-05-04 11:23:20 +08:00
|
|
|
_focused(false),
|
2014-05-16 10:33:28 +08:00
|
|
|
_focusEnabled(true),
|
2014-10-30 22:25:46 +08:00
|
|
|
_touchEventListener(nullptr),
|
2014-11-27 16:49:19 +08:00
|
|
|
_touchEventSelector(nullptr),
|
|
|
|
_ccEventCallback(nullptr),
|
|
|
|
_callbackType(""),
|
|
|
|
_callbackName("")
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget::~Widget()
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
this->cleanupWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::cleanupWidget()
|
|
|
|
{
|
|
|
|
//clean up _touchListener
|
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
|
|
|
CC_SAFE_RELEASE_NULL(_touchListener);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-26 22:57:50 +08:00
|
|
|
//cleanup focused widget and focus navigation controller
|
|
|
|
if (_focusedWidget == this)
|
2014-05-26 18:17:47 +08:00
|
|
|
{
|
|
|
|
//delete
|
|
|
|
CC_SAFE_DELETE(_focusNavigationController);
|
2014-05-05 11:25:30 +08:00
|
|
|
_focusedWidget = nullptr;
|
2014-05-07 18:27:05 +08:00
|
|
|
}
|
2014-05-26 22:57:50 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Widget* widget = new (std::nothrow) Widget();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::init()
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
if (ProtectedNode::init())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
initRenderer();
|
|
|
|
setBright(true);
|
2014-06-06 16:48:49 +08:00
|
|
|
onFocusChanged = CC_CALLBACK_2(Widget::onFocusChange,this);
|
|
|
|
onNextFocusedWidget = nullptr;
|
|
|
|
this->setAnchorPoint(Vec2(0.5f, 0.5f));
|
2014-06-25 11:10:51 +08:00
|
|
|
|
2014-06-20 14:55:42 +08:00
|
|
|
ignoreContentAdaptWithSize(true);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onEnter()
|
|
|
|
{
|
2015-07-28 16:04:28 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
if (!_usingLayoutComponent)
|
|
|
|
updateSizeAndPosition();
|
2014-03-24 15:25:44 +08:00
|
|
|
ProtectedNode::onEnter();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onExit()
|
|
|
|
{
|
2015-07-28 16:04:28 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2015-07-28 18:24:03 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
|
2015-07-28 16:04:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
unscheduleUpdate();
|
2014-03-24 15:25:44 +08:00
|
|
|
ProtectedNode::onExit();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Widget::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:32:30 +08:00
|
|
|
if (_visible)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
adaptRenderers();
|
2014-05-31 07:42:05 +08:00
|
|
|
ProtectedNode::visit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::getWidgetParent()
|
|
|
|
{
|
|
|
|
return dynamic_cast<Widget*>(getParent());
|
|
|
|
}
|
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
void Widget::setEnabled(bool enabled)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
_enabled = enabled;
|
2015-08-10 07:42:35 +08:00
|
|
|
setBright(enabled);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Widget::initRenderer()
|
|
|
|
{
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
LayoutComponent* Widget::getOrCreateLayoutComponent()
|
|
|
|
{
|
|
|
|
auto layoutComponent = this->getComponent(__LAYOUT_COMPONENT_NAME);
|
|
|
|
if (nullptr == layoutComponent)
|
|
|
|
{
|
|
|
|
LayoutComponent *component = LayoutComponent::create();
|
|
|
|
this->addComponent(component);
|
|
|
|
layoutComponent = component;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
return (LayoutComponent*)layoutComponent;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-06-20 11:18:53 +08:00
|
|
|
void Widget::setContentSize(const cocos2d::Size &contentSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-09-15 13:37:32 +08:00
|
|
|
Size previousSize = ProtectedNode::getContentSize();
|
|
|
|
if(previousSize.equals(contentSize))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 14:55:42 +08:00
|
|
|
ProtectedNode::setContentSize(contentSize);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-06-20 11:18:53 +08:00
|
|
|
_customSize = contentSize;
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
//unify Size logic
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
else if (_ignoreSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
ProtectedNode::setContentSize(getVirtualRendererSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-12-23 20:29:53 +08:00
|
|
|
if (!_usingLayoutComponent && _running)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
Size pSize;
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
pSize = widgetParent->getContentSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2015-04-20 01:40:52 +08:00
|
|
|
_sizePercent.set(spx, spy);
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
2014-06-20 11:18:53 +08:00
|
|
|
void Widget::setSize(const Size &size)
|
|
|
|
{
|
|
|
|
this->setContentSize(size);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Widget::setSizePercent(const Vec2 &percent)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_usingLayoutComponent)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
2014-12-22 15:23:26 +08:00
|
|
|
component->setUsingPercentContentSize(true);
|
2014-12-23 20:29:53 +08:00
|
|
|
component->setPercentContentSize(percent);
|
|
|
|
component->refreshLayout();
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
_sizePercent = percent;
|
|
|
|
Size cSize = _customSize;
|
|
|
|
if (_running)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
cSize = Size(widgetParent->getContentSize().width * percent.x, widgetParent->getContentSize().height * percent.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cSize = Size(_parent->getContentSize().width * percent.x, _parent->getContentSize().height * percent.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
this->setContentSize(getVirtualRendererSize());
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
this->setContentSize(cSize);
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
2014-12-23 20:29:53 +08:00
|
|
|
_customSize = cSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::updateSizeAndPosition()
|
2014-03-13 10:11:41 +08:00
|
|
|
{
|
2014-06-20 14:03:33 +08:00
|
|
|
Size pSize = _parent->getContentSize();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-13 10:11:41 +08:00
|
|
|
updateSizeAndPosition(pSize);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-13 10:11:41 +08:00
|
|
|
void Widget::updateSizeAndPosition(const cocos2d::Size &parentSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
switch (_sizeType)
|
|
|
|
{
|
2014-05-09 09:37:51 +08:00
|
|
|
case SizeType::ABSOLUTE:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(getVirtualRendererSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(_customSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-03-13 10:11:41 +08:00
|
|
|
float spx = 0.0f;
|
|
|
|
float spy = 0.0f;
|
|
|
|
if (parentSize.width > 0.0f)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-13 10:11:41 +08:00
|
|
|
spx = _customSize.width / parentSize.width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-03-13 10:11:41 +08:00
|
|
|
if (parentSize.height > 0.0f)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-13 10:11:41 +08:00
|
|
|
spy = _customSize.height / parentSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 01:40:52 +08:00
|
|
|
_sizePercent.set(spx, spy);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-09 09:37:51 +08:00
|
|
|
case SizeType::PERCENT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-13 10:11:41 +08:00
|
|
|
Size cSize = Size(parentSize.width * _sizePercent.x , parentSize.height * _sizePercent.y);
|
|
|
|
if (_ignoreSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(getVirtualRendererSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(cSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-03-13 10:11:41 +08:00
|
|
|
_customSize = cSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-03-13 10:11:41 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-07-24 11:52:42 +08:00
|
|
|
//update position & position percent
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 absPos = getPosition();
|
2014-03-11 17:13:54 +08:00
|
|
|
switch (_positionType)
|
|
|
|
{
|
2014-05-08 18:25:49 +08:00
|
|
|
case PositionType::ABSOLUTE:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-13 10:11:41 +08:00
|
|
|
if (parentSize.width <= 0.0f || parentSize.height <= 0.0f)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionPercent.setZero();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionPercent.set(absPos.x / parentSize.width, absPos.y / parentSize.height);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-08 18:25:49 +08:00
|
|
|
case PositionType::PERCENT:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
absPos.set(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setPosition(absPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setSizeType(SizeType type)
|
|
|
|
{
|
|
|
|
_sizeType = type;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_sizeType == Widget::SizeType::PERCENT)
|
|
|
|
{
|
|
|
|
component->setUsingPercentContentSize(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
component->setUsingPercentContentSize(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 09:37:51 +08:00
|
|
|
Widget::SizeType Widget::getSizeType() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _sizeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
this->setContentSize(_customSize);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-20 17:24:04 +08:00
|
|
|
if (_ignoreSize == ignore)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
_ignoreSize = ignore;
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
Size s = getVirtualRendererSize();
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(s);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(_customSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isIgnoreContentAdaptWithSize() const
|
|
|
|
{
|
|
|
|
return _ignoreSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& Widget::getSize() const
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
return this->getContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
const Size& Widget::getCustomSize() const
|
|
|
|
{
|
|
|
|
return _customSize;
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
const Vec2& Widget::getSizePercent()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
2014-12-24 11:12:14 +08:00
|
|
|
_sizePercent = component->getPercentContentSize();
|
2014-12-23 20:29:53 +08:00
|
|
|
}
|
2014-12-24 11:12:14 +08:00
|
|
|
|
2014-10-17 19:13:37 +08:00
|
|
|
return _sizePercent;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
Vec2 Widget::getWorldPosition()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return convertToWorldSpace(Vec2(_anchorPoint.x * _contentSize.width, _anchorPoint.y * _contentSize.height));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* Widget::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onSizeChanged()
|
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
if (!_usingLayoutComponent)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
for (auto& child : getChildren())
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
Widget* widgetChild = dynamic_cast<Widget*>(child);
|
|
|
|
if (widgetChild)
|
|
|
|
{
|
|
|
|
widgetChild->updateSizeAndPosition();
|
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 14:39:22 +08:00
|
|
|
Size Widget::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
return _contentSize;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
void Widget::updateContentSizeWithTextureSize(const cocos2d::Size &size)
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
this->setContentSize(size);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(size);
|
2014-04-17 14:08:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(_customSize);
|
2014-04-17 14:08:25 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setTouchEnabled(bool enable)
|
|
|
|
{
|
|
|
|
if (enable == _touchEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_touchEnabled = enable;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isTouchEnabled() const
|
|
|
|
{
|
|
|
|
return _touchEnabled;
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:04:52 +08:00
|
|
|
bool Widget::isHighlighted() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-04 10:19:11 +08:00
|
|
|
return _highlight;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-23 15:37:33 +08:00
|
|
|
void Widget::setHighlighted(bool highlight)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
if (highlight == _highlight)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-23 15:37:33 +08:00
|
|
|
_highlight = highlight;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_bright)
|
|
|
|
{
|
2014-05-04 10:19:11 +08:00
|
|
|
if (_highlight)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-09 15:23:34 +08:00
|
|
|
setBrightStyle(BrightStyle::HIGHLIGHT);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-09 15:23:34 +08:00
|
|
|
setBrightStyle(BrightStyle::NORMAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
onPressStateChangedToDisabled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setBright(bool bright)
|
|
|
|
{
|
|
|
|
_bright = bright;
|
|
|
|
if (_bright)
|
|
|
|
{
|
2014-05-09 15:23:34 +08:00
|
|
|
_brightStyle = BrightStyle::NONE;
|
|
|
|
setBrightStyle(BrightStyle::NORMAL);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
onPressStateChangedToDisabled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setBrightStyle(BrightStyle style)
|
|
|
|
{
|
|
|
|
if (_brightStyle == style)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_brightStyle = style;
|
|
|
|
switch (_brightStyle)
|
|
|
|
{
|
2014-05-09 15:23:34 +08:00
|
|
|
case BrightStyle::NORMAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
onPressStateChangedToNormal();
|
|
|
|
break;
|
2014-05-09 15:23:34 +08:00
|
|
|
case BrightStyle::HIGHLIGHT:
|
2014-03-11 17:13:54 +08:00
|
|
|
onPressStateChangedToPressed();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onPressStateChangedToNormal()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onPressStateChangedToPressed()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onPressStateChangedToDisabled()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-11 15:04:00 +08:00
|
|
|
void Widget::updateChildrenDisplayedRGBA()
|
|
|
|
{
|
|
|
|
this->setColor(this->getColor());
|
|
|
|
this->setOpacity(this->getOpacity());
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-13 16:37:41 +08:00
|
|
|
Widget* Widget::getAncensterWidget(Node* node)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-13 18:41:38 +08:00
|
|
|
if (nullptr == node)
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-13 16:37:41 +08:00
|
|
|
Node* parent = node->getParent();
|
2014-05-13 18:41:38 +08:00
|
|
|
if (nullptr == parent)
|
|
|
|
{
|
2014-05-13 17:12:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-13 16:37:41 +08:00
|
|
|
Widget* parentWidget = dynamic_cast<Widget*>(parent);
|
2014-05-13 18:41:38 +08:00
|
|
|
if (parentWidget)
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return parentWidget;
|
|
|
|
}
|
2014-05-13 18:41:38 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return this->getAncensterWidget(parent->getParent());
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-13 16:37:41 +08:00
|
|
|
bool Widget::isAncestorsVisible(Node* node)
|
|
|
|
{
|
2014-05-13 18:41:38 +08:00
|
|
|
if (nullptr == node)
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Node* parent = node->getParent();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-13 18:41:38 +08:00
|
|
|
if (parent && !parent->isVisible())
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this->isAncestorsVisible(parent);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-14 09:23:56 +08:00
|
|
|
bool Widget::isAncestorsEnabled()
|
2014-05-13 16:37:41 +08:00
|
|
|
{
|
|
|
|
Widget* parentWidget = this->getAncensterWidget(this);
|
2014-05-13 18:41:38 +08:00
|
|
|
if (parentWidget == nullptr)
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-13 18:41:38 +08:00
|
|
|
if (parentWidget && !parentWidget->isEnabled())
|
|
|
|
{
|
2014-05-13 16:37:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-14 09:23:56 +08:00
|
|
|
return parentWidget->isAncestorsEnabled();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
void Widget::setPropagateTouchEvents(bool isPropagate)
|
|
|
|
{
|
|
|
|
_propagateTouchEvents = isPropagate;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
bool Widget::isPropagateTouchEvents()const
|
|
|
|
{
|
|
|
|
return _propagateTouchEvents;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
void Widget::setSwallowTouches(bool swallow)
|
|
|
|
{
|
|
|
|
if (_touchListener)
|
|
|
|
{
|
|
|
|
_touchListener->setSwallowTouches(swallow);
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
bool Widget::isSwallowTouches()const
|
|
|
|
{
|
|
|
|
if (_touchListener)
|
|
|
|
{
|
|
|
|
return _touchListener->isSwallowTouches();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
_hitted = false;
|
2014-05-14 09:23:56 +08:00
|
|
|
if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
_touchBeganPosition = touch->getLocation();
|
2015-06-18 10:23:52 +08:00
|
|
|
auto camera = Camera::getVisitingCamera();
|
|
|
|
if(hitTest(_touchBeganPosition, camera, nullptr))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
if (isClippingParentContainsPoint(_touchBeganPosition)) {
|
2015-09-17 11:20:36 +08:00
|
|
|
_hittedByCamera = camera;
|
2015-06-18 10:23:52 +08:00
|
|
|
_hitted = true;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!_hitted)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
setHighlighted(true);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-15 15:28:28 +08:00
|
|
|
this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
pushDownEvent();
|
2014-05-13 16:37:41 +08:00
|
|
|
return true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
void Widget::propagateTouchEvent(cocos2d::ui::Widget::TouchEventType event, cocos2d::ui::Widget *sender, cocos2d::Touch *touch)
|
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2015-07-07 10:42:04 +08:00
|
|
|
widgetParent->_hittedByCamera = _hittedByCamera;
|
2014-08-15 15:28:28 +08:00
|
|
|
widgetParent->interceptTouchEvent(event, sender, touch);
|
2015-07-07 10:42:04 +08:00
|
|
|
widgetParent->_hittedByCamera = nullptr;
|
2014-08-15 15:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void Widget::onTouchMoved(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
_touchMovePosition = touch->getLocation();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
setHighlighted(hitTest(_touchMovePosition, _hittedByCamera, nullptr));
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-15 15:28:28 +08:00
|
|
|
this->propagateTouchEvent(TouchEventType::MOVED, this, touch);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
moveEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onTouchEnded(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
_touchEndPosition = touch->getLocation();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-15 15:28:28 +08:00
|
|
|
this->propagateTouchEvent(TouchEventType::ENDED, this, touch);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-06-10 11:30:39 +08:00
|
|
|
bool highlight = _highlight;
|
|
|
|
setHighlighted(false);
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-06-10 11:30:39 +08:00
|
|
|
if (highlight)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
releaseUpEvent();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cancelUpEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onTouchCancelled(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
2014-05-06 12:04:52 +08:00
|
|
|
setHighlighted(false);
|
2014-03-11 17:13:54 +08:00
|
|
|
cancelUpEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::pushDownEvent()
|
|
|
|
{
|
2014-07-23 18:52:03 +08:00
|
|
|
this->retain();
|
2014-08-15 15:28:28 +08:00
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
2014-05-09 11:28:36 +08:00
|
|
|
_touchEventCallback(this, TouchEventType::BEGAN);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_touchEventListener && _touchEventSelector)
|
|
|
|
{
|
|
|
|
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_BEGAN);
|
|
|
|
}
|
2014-07-23 18:52:03 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::moveEvent()
|
|
|
|
{
|
2014-07-23 18:52:03 +08:00
|
|
|
this->retain();
|
2014-08-15 15:28:28 +08:00
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
2014-05-09 11:28:36 +08:00
|
|
|
_touchEventCallback(this, TouchEventType::MOVED);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_touchEventListener && _touchEventSelector)
|
|
|
|
{
|
|
|
|
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_MOVED);
|
|
|
|
}
|
2014-07-23 18:52:03 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::releaseUpEvent()
|
|
|
|
{
|
2014-07-23 18:52:03 +08:00
|
|
|
this->retain();
|
2015-09-30 17:31:33 +08:00
|
|
|
|
|
|
|
if (isFocusEnabled())
|
|
|
|
{
|
|
|
|
requestFocus();
|
|
|
|
}
|
|
|
|
|
2014-08-15 15:28:28 +08:00
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
2014-05-09 11:28:36 +08:00
|
|
|
_touchEventCallback(this, TouchEventType::ENDED);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_touchEventListener && _touchEventSelector)
|
|
|
|
{
|
|
|
|
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_ENDED);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-21 10:21:07 +08:00
|
|
|
if (_clickEventListener) {
|
|
|
|
_clickEventListener(this);
|
|
|
|
}
|
2014-07-23 18:52:03 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::cancelUpEvent()
|
|
|
|
{
|
2014-07-23 18:52:03 +08:00
|
|
|
this->retain();
|
2014-05-09 11:28:36 +08:00
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
|
|
|
_touchEventCallback(this, TouchEventType::CANCELED);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_touchEventListener && _touchEventSelector)
|
|
|
|
{
|
|
|
|
(_touchEventListener->*_touchEventSelector)(this,TOUCH_EVENT_CANCELED);
|
|
|
|
}
|
2014-07-23 18:52:03 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::addTouchEventListener(Ref *target, SEL_TouchEvent selector)
|
|
|
|
{
|
|
|
|
_touchEventListener = target;
|
|
|
|
_touchEventSelector = selector;
|
2014-05-09 11:28:36 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-21 10:21:07 +08:00
|
|
|
void Widget::addTouchEventListener(const ccWidgetTouchCallback& callback)
|
2014-05-09 11:28:36 +08:00
|
|
|
{
|
|
|
|
this->_touchEventCallback = callback;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-08-21 10:21:07 +08:00
|
|
|
void Widget::addClickEventListener(const ccWidgetClickCallback &callback)
|
|
|
|
{
|
|
|
|
this->_clickEventListener = callback;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-27 16:49:19 +08:00
|
|
|
void Widget::addCCSEventListener(const ccWidgetEventCallback &callback)
|
|
|
|
{
|
|
|
|
this->_ccEventCallback = callback;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2015-06-18 10:23:52 +08:00
|
|
|
bool Widget::hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
Rect rect;
|
|
|
|
rect.size = getContentSize();
|
|
|
|
return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, p);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
bool Widget::isClippingParentContainsPoint(const Vec2 &pt)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_affectByClipping = false;
|
2015-12-23 16:05:52 +08:00
|
|
|
Node* parent = getParent();
|
2014-03-11 17:13:54 +08:00
|
|
|
Widget* clippingParent = nullptr;
|
|
|
|
while (parent)
|
|
|
|
{
|
|
|
|
Layout* layoutParent = dynamic_cast<Layout*>(parent);
|
|
|
|
if (layoutParent)
|
|
|
|
{
|
|
|
|
if (layoutParent->isClippingEnabled())
|
|
|
|
{
|
|
|
|
_affectByClipping = true;
|
|
|
|
clippingParent = layoutParent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 16:05:52 +08:00
|
|
|
parent = parent->getParent();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_affectByClipping)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (clippingParent)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2015-09-17 11:20:36 +08:00
|
|
|
auto camera = Camera::getVisitingCamera();
|
|
|
|
// Camera isn't null means in touch begin process, otherwise use _hittedByCamera instead.
|
|
|
|
if (clippingParent->hitTest(pt, (camera ? camera : _hittedByCamera), nullptr))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
if (bRet)
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
return clippingParent->isClippingParentContainsPoint(pt);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void Widget::interceptTouchEvent(cocos2d::ui::Widget::TouchEventType event, cocos2d::ui::Widget *sender, Touch *touch)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2015-07-07 10:42:04 +08:00
|
|
|
widgetParent->_hittedByCamera = _hittedByCamera;
|
2014-06-06 16:48:49 +08:00
|
|
|
widgetParent->interceptTouchEvent(event,sender,touch);
|
2015-07-07 10:42:04 +08:00
|
|
|
widgetParent->_hittedByCamera = nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-05-22 15:23:13 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Widget::setPosition(const Vec2 &pos)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
if (!_usingLayoutComponent && _running)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
Size pSize = widgetParent->getContentSize();
|
|
|
|
if (pSize.width <= 0.0f || pSize.height <= 0.0f)
|
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionPercent.setZero();
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionPercent.set(pos.x / pSize.width, pos.y / pSize.height);
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 15:25:44 +08:00
|
|
|
ProtectedNode::setPosition(pos);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Widget::setPositionPercent(const Vec2 &percent)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_usingLayoutComponent)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
component->setPositionPercentX(percent.x);
|
|
|
|
component->setPositionPercentY(percent.y);
|
|
|
|
component->refreshLayout();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_positionPercent = percent;
|
|
|
|
if (_running)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
Size parentSize = widgetParent->getContentSize();
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 absPos(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
|
2014-12-23 20:29:53 +08:00
|
|
|
setPosition(absPos);
|
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-12-24 11:12:14 +08:00
|
|
|
const Vec2& Widget::getPositionPercent(){
|
2014-12-22 15:23:26 +08:00
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
float percentX = component->getPositionPercentX();
|
|
|
|
float percentY = component->getPositionPercentY();
|
2014-12-22 15:23:26 +08:00
|
|
|
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionPercent.set(percentX, percentY);
|
2014-12-23 20:29:53 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
return _positionPercent;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setPositionType(PositionType type)
|
|
|
|
{
|
|
|
|
_positionType = type;
|
2014-12-23 20:29:53 +08:00
|
|
|
|
|
|
|
if (_usingLayoutComponent)
|
2014-12-22 15:23:26 +08:00
|
|
|
{
|
2014-12-23 20:29:53 +08:00
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
if (type == Widget::PositionType::ABSOLUTE)
|
|
|
|
{
|
|
|
|
component->setPositionPercentXEnabled(false);
|
|
|
|
component->setPositionPercentYEnabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
component->setPositionPercentXEnabled(true);
|
|
|
|
component->setPositionPercentYEnabled(true);
|
|
|
|
}
|
2014-12-22 15:23:26 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 18:25:49 +08:00
|
|
|
Widget::PositionType Widget::getPositionType() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _positionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isBright() const
|
|
|
|
{
|
|
|
|
return _bright;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isEnabled() const
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:51:25 +08:00
|
|
|
float Widget::getLeftBoundary() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
return getPosition().x - getAnchorPoint().x * _contentSize.width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:51:25 +08:00
|
|
|
float Widget::getBottomBoundary() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
return getPosition().y - getAnchorPoint().y * _contentSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:51:25 +08:00
|
|
|
float Widget::getRightBoundary() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
return getLeftBoundary() + _contentSize.width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:51:25 +08:00
|
|
|
float Widget::getTopBoundary() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
return getBottomBoundary() + _contentSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
const Vec2& Widget::getTouchBeganPosition()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
return _touchBeganPosition;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
const Vec2& Widget::getTouchMovePosition()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
return _touchMovePosition;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
const Vec2& Widget::getTouchEndPosition()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
return _touchEndPosition;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setLayoutParameter(LayoutParameter *parameter)
|
|
|
|
{
|
|
|
|
if (!parameter)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-09 17:38:05 +08:00
|
|
|
_layoutParameterDictionary.insert((int)parameter->getLayoutType(), parameter);
|
2014-05-16 10:33:28 +08:00
|
|
|
_layoutParameterType = parameter->getLayoutType();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
LayoutParameter* Widget::getLayoutParameter()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
return dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at((int)_layoutParameterType));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-19 17:43:27 +08:00
|
|
|
LayoutParameter* Widget::getLayoutParameter(LayoutParameter::Type type)
|
|
|
|
{
|
|
|
|
return dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at((int)type));
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
std::string Widget::getDescription() const
|
|
|
|
{
|
|
|
|
return "Widget";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::clone()
|
|
|
|
{
|
|
|
|
Widget* clonedWidget = createCloneInstance();
|
|
|
|
clonedWidget->copyProperties(this);
|
|
|
|
clonedWidget->copyClonedWidgetChildren(this);
|
|
|
|
return clonedWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::createCloneInstance()
|
|
|
|
{
|
|
|
|
return Widget::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::copyClonedWidgetChildren(Widget* model)
|
|
|
|
{
|
|
|
|
auto& modelChildren = model->getChildren();
|
|
|
|
|
|
|
|
for (auto& subWidget : modelChildren)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
addChild(child->clone());
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-12-23 15:03:31 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
GLProgramState* Widget::getNormalGLProgramState()const
|
|
|
|
{
|
|
|
|
GLProgramState *glState = nullptr;
|
2016-07-25 17:31:54 +08:00
|
|
|
|
|
|
|
// ETC1 Gray supports.
|
|
|
|
// currently, Only AbstractCheckButton, ui::Slider called this function, them's VirtualRender is Sprite
|
|
|
|
auto virtualRender = const_cast<Widget*>(this)->getVirtualRenderer();
|
|
|
|
Texture2D* virtualTexture = nullptr;
|
|
|
|
if (auto sp = dynamic_cast<Sprite*>(virtualRender))
|
|
|
|
{
|
|
|
|
virtualTexture = sp->getTexture();
|
|
|
|
}
|
|
|
|
else if (auto scale9sp = dynamic_cast<Scale9Sprite*>(virtualRender))
|
|
|
|
{
|
|
|
|
virtualTexture = scale9sp->getSprite() != nullptr ? scale9sp->getSprite()->getTexture() : nullptr;
|
|
|
|
}
|
|
|
|
glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, virtualTexture);
|
2014-12-23 15:03:31 +08:00
|
|
|
return glState;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLProgramState* Widget::getGrayGLProgramState()const
|
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
GLProgramState *glState = nullptr;
|
2016-07-25 17:31:54 +08:00
|
|
|
|
|
|
|
// ETC1 Gray supports.
|
|
|
|
// currently, Only AbstractCheckButton, ui::Slider called this function, them's VirtualRender is Sprite
|
|
|
|
auto virtualRender = const_cast<Widget*>(this)->getVirtualRenderer();
|
|
|
|
Texture2D* virtualTexture = nullptr;
|
|
|
|
if (auto sp = dynamic_cast<cocos2d::Sprite*>(virtualRender))
|
|
|
|
{
|
|
|
|
virtualTexture = sp->getTexture();
|
|
|
|
}
|
|
|
|
else if (auto scale9sp = dynamic_cast<Scale9Sprite*>(virtualRender))
|
|
|
|
{
|
|
|
|
virtualTexture = scale9sp->getSprite() != nullptr ? scale9sp->getSprite()->getTexture() : nullptr;
|
|
|
|
}
|
|
|
|
glState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_GRAYSCALE, virtualTexture);
|
2014-12-23 15:03:31 +08:00
|
|
|
return glState;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::copySpecialProperties(Widget* model)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::copyProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
setEnabled(widget->isEnabled());
|
|
|
|
setVisible(widget->isVisible());
|
|
|
|
setBright(widget->isBright());
|
|
|
|
setTouchEnabled(widget->isTouchEnabled());
|
|
|
|
setLocalZOrder(widget->getLocalZOrder());
|
|
|
|
setTag(widget->getTag());
|
|
|
|
setName(widget->getName());
|
|
|
|
setActionTag(widget->getActionTag());
|
|
|
|
_ignoreSize = widget->_ignoreSize;
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(widget->_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_customSize = widget->_customSize;
|
|
|
|
_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());
|
2014-03-14 15:38:43 +08:00
|
|
|
setRotationSkewX(widget->getRotationSkewX());
|
|
|
|
setRotationSkewY(widget->getRotationSkewY());
|
2014-03-11 17:13:54 +08:00
|
|
|
setFlippedX(widget->isFlippedX());
|
|
|
|
setFlippedY(widget->isFlippedY());
|
|
|
|
setColor(widget->getColor());
|
|
|
|
setOpacity(widget->getOpacity());
|
2016-06-15 14:39:18 +08:00
|
|
|
setCascadeColorEnabled(widget->isCascadeColorEnabled());
|
|
|
|
setCascadeOpacityEnabled(widget->isCascadeOpacityEnabled());
|
2014-05-23 15:03:46 +08:00
|
|
|
_touchEventCallback = widget->_touchEventCallback;
|
|
|
|
_touchEventListener = widget->_touchEventListener;
|
|
|
|
_touchEventSelector = widget->_touchEventSelector;
|
2014-08-21 10:21:07 +08:00
|
|
|
_clickEventListener = widget->_clickEventListener;
|
2014-05-23 15:03:46 +08:00
|
|
|
_focused = widget->_focused;
|
|
|
|
_focusEnabled = widget->_focusEnabled;
|
2014-08-15 15:28:28 +08:00
|
|
|
_propagateTouchEvents = widget->_propagateTouchEvents;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-23 15:03:46 +08:00
|
|
|
copySpecialProperties(widget);
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Map<int, LayoutParameter*>& layoutParameterDic = widget->_layoutParameterDictionary;
|
|
|
|
for (auto iter = layoutParameterDic.begin(); iter != layoutParameterDic.end(); ++iter)
|
|
|
|
{
|
|
|
|
setLayoutParameter(iter->second->clone());
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setFlippedX(bool flippedX)
|
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
float realScale = this->getScaleX();
|
|
|
|
_flippedX = flippedX;
|
|
|
|
this->setScaleX(realScale);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setFlippedY(bool flippedY)
|
|
|
|
{
|
|
|
|
float realScale = this->getScaleY();
|
|
|
|
_flippedY = flippedY;
|
|
|
|
this->setScaleY(realScale);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setScaleX(float scaleX)
|
|
|
|
{
|
|
|
|
if (_flippedX) {
|
|
|
|
scaleX = scaleX * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleX(scaleX);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setScaleY(float scaleY)
|
|
|
|
{
|
|
|
|
if (_flippedY) {
|
|
|
|
scaleY = scaleY * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleY(scaleY);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setScale(float scale)
|
|
|
|
{
|
|
|
|
this->setScaleX(scale);
|
|
|
|
this->setScaleY(scale);
|
|
|
|
this->setScaleZ(scale);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
void Widget::setScale(float scaleX, float scaleY)
|
|
|
|
{
|
|
|
|
this->setScaleX(scaleX);
|
|
|
|
this->setScaleY(scaleY);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
float Widget::getScaleX()const
|
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleX();
|
|
|
|
if (_flippedX)
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
originalScale = originalScale * -1.0f;
|
2014-11-05 20:22:35 +08:00
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
float Widget::getScaleY()const
|
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleY();
|
|
|
|
if (_flippedY)
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
originalScale = originalScale * -1.0f;
|
2014-11-05 20:22:35 +08:00
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-11-05 20:22:35 +08:00
|
|
|
float Widget::getScale()const
|
|
|
|
{
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(this->getScaleX() == this->getScaleY(), "scaleX should be equal to scaleY.");
|
2014-11-05 20:22:35 +08:00
|
|
|
return this->getScaleX();
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*temp action*/
|
|
|
|
void Widget::setActionTag(int tag)
|
|
|
|
{
|
|
|
|
_actionTag = tag;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
int Widget::getActionTag()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _actionTag;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
void Widget::setFocused(bool focus)
|
|
|
|
{
|
|
|
|
_focused = focus;
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
//make sure there is only one focusedWidget
|
|
|
|
if (focus) {
|
2014-05-07 10:23:57 +08:00
|
|
|
_focusedWidget = this;
|
2014-05-26 18:38:03 +08:00
|
|
|
if (_focusNavigationController) {
|
|
|
|
_focusNavigationController->setFirstFocsuedWidget(this);
|
|
|
|
}
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
bool Widget::isFocused()const
|
2014-05-04 11:23:20 +08:00
|
|
|
{
|
|
|
|
return _focused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setFocusEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_focusEnabled = enable;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
bool Widget::isFocusEnabled()const
|
2014-05-04 11:23:20 +08:00
|
|
|
{
|
|
|
|
return _focusEnabled;
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
Widget* Widget::findNextFocusedWidget(FocusDirection direction, Widget* current)
|
2014-05-04 11:23:20 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (nullptr == onNextFocusedWidget || nullptr == onNextFocusedWidget(direction) ) {
|
2014-05-26 22:57:50 +08:00
|
|
|
if (this->isFocused() || dynamic_cast<Layout*>(current))
|
2014-05-07 14:06:44 +08:00
|
|
|
{
|
2014-05-04 11:23:20 +08:00
|
|
|
Node* parent = this->getParent();
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
Layout* layout = dynamic_cast<Layout*>(parent);
|
2014-05-07 14:06:44 +08:00
|
|
|
if (nullptr == layout)
|
|
|
|
{
|
2014-05-04 11:23:20 +08:00
|
|
|
//the outer layout's default behaviour is : loop focus
|
2014-05-07 14:06:44 +08:00
|
|
|
if (dynamic_cast<Layout*>(current))
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return current->findNextFocusedWidget(direction, current);
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
|
|
|
return current;
|
2014-05-07 14:06:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
Widget *nextWidget = layout->findNextFocusedWidget(direction, current);
|
2014-05-04 11:23:20 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
2014-05-07 14:06:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-04 11:23:20 +08:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 14:06:44 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
Widget *getFocusWidget = onNextFocusedWidget(direction);
|
2014-05-04 11:23:20 +08:00
|
|
|
this->dispatchFocusEvent(this, getFocusWidget);
|
|
|
|
return getFocusWidget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::dispatchFocusEvent(cocos2d::ui::Widget *widgetLoseFocus, cocos2d::ui::Widget *widgetGetFocus)
|
|
|
|
{
|
2014-05-06 17:28:12 +08:00
|
|
|
//if the widgetLoseFocus doesn't get focus, it will use the previous focused widget instead
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widgetLoseFocus && !widgetLoseFocus->isFocused())
|
|
|
|
{
|
2014-05-06 17:28:12 +08:00
|
|
|
widgetLoseFocus = _focusedWidget;
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-06 17:28:12 +08:00
|
|
|
if (widgetGetFocus != widgetLoseFocus)
|
|
|
|
{
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widgetGetFocus)
|
|
|
|
{
|
2014-05-06 17:28:12 +08:00
|
|
|
widgetGetFocus->onFocusChanged(widgetLoseFocus, widgetGetFocus);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widgetLoseFocus)
|
|
|
|
{
|
2014-05-06 17:28:12 +08:00
|
|
|
widgetLoseFocus->onFocusChanged(widgetLoseFocus, widgetGetFocus);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-06 17:28:12 +08:00
|
|
|
EventFocus event(widgetLoseFocus, widgetGetFocus);
|
|
|
|
auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&event);
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 10:23:57 +08:00
|
|
|
void Widget::requestFocus()
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
if (this == _focusedWidget)
|
|
|
|
{
|
2014-05-07 10:23:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-07 10:23:57 +08:00
|
|
|
this->dispatchFocusEvent(_focusedWidget, this);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-04 11:23:20 +08:00
|
|
|
void Widget::onFocusChange(Widget* widgetLostFocus, Widget* widgetGetFocus)
|
|
|
|
{
|
2014-05-06 17:28:12 +08:00
|
|
|
//only change focus when there is indeed a get&lose happens
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widgetLostFocus)
|
|
|
|
{
|
2014-05-04 11:23:20 +08:00
|
|
|
widgetLostFocus->setFocused(false);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widgetGetFocus)
|
|
|
|
{
|
2014-05-04 11:23:20 +08:00
|
|
|
widgetGetFocus->setFocused(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 17:31:33 +08:00
|
|
|
Widget* Widget::getCurrentFocusedWidget()
|
2014-05-04 11:23:20 +08:00
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-26 18:17:47 +08:00
|
|
|
void Widget::enableDpadNavigation(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
2014-05-26 18:25:54 +08:00
|
|
|
if (nullptr == _focusNavigationController)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_focusNavigationController = new (std::nothrow) FocusNavigationController;
|
2015-01-15 15:18:14 +08:00
|
|
|
if (_focusedWidget)
|
|
|
|
{
|
2014-05-26 18:38:03 +08:00
|
|
|
_focusNavigationController->setFirstFocsuedWidget(_focusedWidget);
|
|
|
|
}
|
2014-05-26 18:17:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_focusNavigationController);
|
|
|
|
}
|
2015-04-20 17:00:40 +08:00
|
|
|
|
2015-01-15 15:18:14 +08:00
|
|
|
if (nullptr != _focusNavigationController)
|
|
|
|
{
|
|
|
|
_focusNavigationController->enableFocusNavigation(enable);
|
|
|
|
}
|
2014-05-26 18:17:47 +08:00
|
|
|
}
|
2014-05-07 18:27:05 +08:00
|
|
|
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
bool Widget::isUnifySizeEnabled()const
|
|
|
|
{
|
|
|
|
return _unifySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setUnifySizeEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_unifySize = enable;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-23 20:29:53 +08:00
|
|
|
void Widget::setLayoutComponentEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_usingLayoutComponent = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isLayoutComponentEnabled()const
|
|
|
|
{
|
|
|
|
return _usingLayoutComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
NS_CC_END
|