2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-09-05 20:45:32 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventListenerKeyboard.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventFocus.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "ui/UILayoutComponent.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "ui/UIScale9Sprite.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
class Widget::FocusNavigationController
|
|
|
|
{
|
|
|
|
void enableFocusNavigation(bool flag);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FocusNavigationController()
|
|
|
|
: _keyboardListener(nullptr)
|
|
|
|
, _firstFocusedWidget(nullptr)
|
|
|
|
, _enableFocusNavigation(false)
|
|
|
|
, _keyboardEventPriority(1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// no-op
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
~FocusNavigationController();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
void setFirstFocusedWidget(Widget* widget);
|
|
|
|
|
|
|
|
void onKeypadKeyPressed(EventKeyboard::KeyCode, Event*);
|
|
|
|
|
|
|
|
void addKeyboardEventListener();
|
|
|
|
void removeKeyboardEventListener();
|
|
|
|
|
|
|
|
friend class Widget;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerKeyboard* _keyboardListener;
|
|
|
|
Widget* _firstFocusedWidget;
|
|
|
|
bool _enableFocusNavigation;
|
2019-11-23 20:27:39 +08:00
|
|
|
const int _keyboardEventPriority;
|
|
|
|
};
|
|
|
|
|
|
|
|
Widget::FocusNavigationController::~FocusNavigationController()
|
|
|
|
{
|
|
|
|
this->removeKeyboardEventListener();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::FocusNavigationController::onKeypadKeyPressed(EventKeyboard::KeyCode keyCode, Event* /*event*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_enableFocusNavigation && _firstFocusedWidget)
|
|
|
|
{
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_DOWN)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_firstFocusedWidget =
|
|
|
|
_firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::DOWN, _firstFocusedWidget);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_UP)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_firstFocusedWidget =
|
|
|
|
_firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::UP, _firstFocusedWidget);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_LEFT)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_firstFocusedWidget =
|
|
|
|
_firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::LEFT, _firstFocusedWidget);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_RIGHT)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_firstFocusedWidget =
|
|
|
|
_firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::RIGHT, _firstFocusedWidget);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::enableFocusNavigation(bool flag)
|
|
|
|
{
|
|
|
|
if (_enableFocusNavigation == flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_enableFocusNavigation = flag;
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
this->addKeyboardEventListener();
|
|
|
|
else
|
|
|
|
this->removeKeyboardEventListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::setFirstFocusedWidget(Widget* widget)
|
|
|
|
{
|
|
|
|
_firstFocusedWidget = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::addKeyboardEventListener()
|
|
|
|
{
|
|
|
|
if (nullptr == _keyboardListener)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_keyboardListener = EventListenerKeyboard::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
_keyboardListener->onKeyReleased = AX_CALLBACK_2(Widget::FocusNavigationController::onKeypadKeyPressed, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatcher->addEventListenerWithFixedPriority(_keyboardListener, _keyboardEventPriority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::FocusNavigationController::removeKeyboardEventListener()
|
|
|
|
{
|
|
|
|
if (nullptr != _keyboardListener)
|
|
|
|
{
|
|
|
|
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->removeEventListener(_keyboardListener);
|
|
|
|
_keyboardListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* Widget::_focusedWidget = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget::FocusNavigationController* Widget::_focusNavigationController = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::Widget()
|
|
|
|
: _usingLayoutComponent(false)
|
|
|
|
, _unifySize(false)
|
|
|
|
, _enabled(true)
|
|
|
|
, _bright(true)
|
|
|
|
, _touchEnabled(false)
|
|
|
|
, _highlight(false)
|
|
|
|
, _affectByClipping(false)
|
|
|
|
, _ignoreSize(false)
|
|
|
|
, _propagateTouchEvents(true)
|
|
|
|
, _brightStyle(BrightStyle::NONE)
|
|
|
|
, _sizeType(SizeType::ABSOLUTE)
|
|
|
|
, _positionType(PositionType::ABSOLUTE)
|
|
|
|
, _actionTag(0)
|
|
|
|
, _customSize(Vec2::ZERO)
|
|
|
|
, _hitted(false)
|
|
|
|
, _hittedByCamera(nullptr)
|
|
|
|
, _touchListener(nullptr)
|
|
|
|
, _flippedX(false)
|
|
|
|
, _flippedY(false)
|
|
|
|
, _layoutParameterType(LayoutParameter::Type::NONE)
|
|
|
|
, _focused(false)
|
|
|
|
, _focusEnabled(true)
|
|
|
|
, _touchEventListener(nullptr)
|
|
|
|
, _ccEventCallback(nullptr)
|
|
|
|
, _callbackType("")
|
|
|
|
, _callbackName("")
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Widget::~Widget()
|
|
|
|
{
|
|
|
|
this->cleanupWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::cleanupWidget()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// clean up _touchListener
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_touchListener);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// cleanup focused widget and focus navigation controller
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_focusedWidget == this)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// delete
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_focusNavigationController);
|
2019-11-23 20:27:39 +08:00
|
|
|
_focusedWidget = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Widget* widget = new Widget();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(widget);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::init()
|
|
|
|
{
|
|
|
|
if (ProtectedNode::init())
|
|
|
|
{
|
|
|
|
initRenderer();
|
|
|
|
setBright(true);
|
2022-07-16 10:43:05 +08:00
|
|
|
onFocusChanged = AX_CALLBACK_2(Widget::onFocusChange, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
onNextFocusedWidget = nullptr;
|
|
|
|
this->setAnchorPoint(Vec2(0.5f, 0.5f));
|
|
|
|
|
|
|
|
ignoreContentAdaptWithSize(true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onEnter()
|
|
|
|
{
|
|
|
|
if (!_usingLayoutComponent)
|
|
|
|
updateSizeAndPosition();
|
|
|
|
ProtectedNode::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onExit()
|
|
|
|
{
|
|
|
|
unscheduleUpdate();
|
|
|
|
ProtectedNode::onExit();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_visible)
|
|
|
|
{
|
|
|
|
adaptRenderers();
|
|
|
|
ProtectedNode::visit(renderer, parentTransform, parentFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::getWidgetParent()
|
|
|
|
{
|
|
|
|
return dynamic_cast<Widget*>(getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_enabled = enabled;
|
|
|
|
setBright(enabled);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::initRenderer() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
LayoutComponent* Widget::getOrCreateLayoutComponent()
|
|
|
|
{
|
|
|
|
auto layoutComponent = this->getComponent(__LAYOUT_COMPONENT_NAME);
|
|
|
|
if (nullptr == layoutComponent)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutComponent* component = LayoutComponent::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addComponent(component);
|
|
|
|
layoutComponent = component;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (LayoutComponent*)layoutComponent;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::setContentSize(const Vec2& contentSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 previousSize = ProtectedNode::getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (previousSize.equals(contentSize))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ProtectedNode::setContentSize(contentSize);
|
|
|
|
|
|
|
|
_customSize = contentSize;
|
|
|
|
if (_unifySize)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// unify size logic
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (_ignoreSize)
|
|
|
|
{
|
|
|
|
ProtectedNode::setContentSize(getVirtualRendererSize());
|
|
|
|
}
|
|
|
|
if (!_usingLayoutComponent && _running)
|
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 pSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
_sizePercent.set(spx, spy);
|
|
|
|
}
|
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::setSizePercent(const Vec2& percent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
component->setUsingPercentContentSize(true);
|
|
|
|
component->setPercentContentSize(percent);
|
|
|
|
component->refreshLayout();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sizePercent = percent;
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 cSize = _customSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_running)
|
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cSize = Vec2(widgetParent->getContentSize().width * percent.x,
|
|
|
|
widgetParent->getContentSize().height * percent.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
cSize = Vec2(_parent->getContentSize().width * percent.x, _parent->getContentSize().height * percent.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
this->setContentSize(getVirtualRendererSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentSize(cSize);
|
|
|
|
}
|
|
|
|
_customSize = cSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::updateSizeAndPosition()
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 pSize = _parent->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
updateSizeAndPosition(pSize);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::updateSizeAndPosition(const Vec2& parentSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (_sizeType)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case SizeType::ABSOLUTE:
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
this->setContentSize(getVirtualRendererSize());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
this->setContentSize(_customSize);
|
|
|
|
}
|
|
|
|
float spx = 0.0f;
|
|
|
|
float spy = 0.0f;
|
|
|
|
if (parentSize.width > 0.0f)
|
|
|
|
{
|
|
|
|
spx = _customSize.width / parentSize.width;
|
|
|
|
}
|
|
|
|
if (parentSize.height > 0.0f)
|
|
|
|
{
|
|
|
|
spy = _customSize.height / parentSize.height;
|
|
|
|
}
|
|
|
|
_sizePercent.set(spx, spy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SizeType::PERCENT:
|
|
|
|
{
|
|
|
|
Vec2 cSize = Vec2(parentSize.width * _sizePercent.x, parentSize.height * _sizePercent.y);
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
this->setContentSize(getVirtualRendererSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentSize(cSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_customSize = cSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// update position & position percent
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec2 absPos = getPosition();
|
|
|
|
switch (_positionType)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case PositionType::ABSOLUTE:
|
|
|
|
{
|
|
|
|
if (parentSize.width <= 0.0f || parentSize.height <= 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_positionPercent.setZero();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_positionPercent.set(absPos.x / parentSize.width, absPos.y / parentSize.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PositionType::PERCENT:
|
|
|
|
{
|
|
|
|
absPos.set(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
setPosition(absPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setSizeType(SizeType type)
|
|
|
|
{
|
|
|
|
_sizeType = type;
|
|
|
|
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
|
|
|
|
if (_sizeType == Widget::SizeType::PERCENT)
|
|
|
|
{
|
|
|
|
component->setUsingPercentContentSize(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
component->setUsingPercentContentSize(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Widget::SizeType Widget::getSizeType() const
|
|
|
|
{
|
|
|
|
return _sizeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
this->setContentSize(_customSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_ignoreSize == ignore)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ignoreSize = ignore;
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 s = getVirtualRendererSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
this->setContentSize(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentSize(_customSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isIgnoreContentAdaptWithSize() const
|
|
|
|
{
|
|
|
|
return _ignoreSize;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
const Vec2& Widget::getCustomSize() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _customSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& Widget::getSizePercent()
|
|
|
|
{
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
2021-12-25 10:04:45 +08:00
|
|
|
_sizePercent = component->getPercentContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _sizePercent;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 Widget::getWorldPosition() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return convertToWorldSpace(Vec2(_anchorPoint.x * _contentSize.width, _anchorPoint.y * _contentSize.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Widget::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onSizeChanged()
|
|
|
|
{
|
|
|
|
if (!_usingLayoutComponent)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& child : getChildren())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* widgetChild = dynamic_cast<Widget*>(child);
|
|
|
|
if (widgetChild)
|
|
|
|
{
|
|
|
|
widgetChild->updateSizeAndPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 Widget::getVirtualRendererSize() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _contentSize;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::updateContentSizeWithTextureSize(const Vec2& size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
this->setContentSize(size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
this->setContentSize(size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentSize(_customSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setTouchEnabled(bool enable)
|
|
|
|
{
|
|
|
|
if (enable == _touchEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_touchEnabled = enable;
|
|
|
|
if (_touchEnabled)
|
|
|
|
{
|
|
|
|
_touchListener = EventListenerTouchOneByOne::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(_touchListener);
|
2019-11-23 20:27:39 +08:00
|
|
|
_touchListener->setSwallowTouches(true);
|
2022-07-16 10:43:05 +08:00
|
|
|
_touchListener->onTouchBegan = AX_CALLBACK_2(Widget::onTouchBegan, this);
|
|
|
|
_touchListener->onTouchMoved = AX_CALLBACK_2(Widget::onTouchMoved, this);
|
|
|
|
_touchListener->onTouchEnded = AX_CALLBACK_2(Widget::onTouchEnded, this);
|
|
|
|
_touchListener->onTouchCancelled = AX_CALLBACK_2(Widget::onTouchCancelled, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_touchListener);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isTouchEnabled() const
|
|
|
|
{
|
|
|
|
return _touchEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isHighlighted() const
|
|
|
|
{
|
|
|
|
return _highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setHighlighted(bool highlight)
|
|
|
|
{
|
|
|
|
if (highlight == _highlight)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_highlight = highlight;
|
|
|
|
if (_bright)
|
|
|
|
{
|
|
|
|
if (_highlight)
|
|
|
|
{
|
|
|
|
setBrightStyle(BrightStyle::HIGHLIGHT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setBrightStyle(BrightStyle::NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
onPressStateChangedToDisabled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setBright(bool bright)
|
|
|
|
{
|
|
|
|
_bright = bright;
|
|
|
|
if (_bright)
|
|
|
|
{
|
|
|
|
_brightStyle = BrightStyle::NONE;
|
|
|
|
setBrightStyle(BrightStyle::NORMAL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
onPressStateChangedToDisabled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setBrightStyle(BrightStyle style)
|
|
|
|
{
|
|
|
|
if (_brightStyle == style)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_brightStyle = style;
|
|
|
|
switch (_brightStyle)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case BrightStyle::NORMAL:
|
|
|
|
onPressStateChangedToNormal();
|
|
|
|
break;
|
|
|
|
case BrightStyle::HIGHLIGHT:
|
|
|
|
onPressStateChangedToPressed();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::onPressStateChangedToNormal() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::onPressStateChangedToPressed() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::onPressStateChangedToDisabled() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void Widget::updateChildrenDisplayedRGBA()
|
|
|
|
{
|
|
|
|
this->setColor(this->getColor());
|
|
|
|
this->setOpacity(this->getOpacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::getAncestorWidget(Node* node)
|
|
|
|
{
|
|
|
|
if (nullptr == node)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* parent = node->getParent();
|
|
|
|
if (nullptr == parent)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Widget* parentWidget = dynamic_cast<Widget*>(parent);
|
|
|
|
if (parentWidget)
|
|
|
|
{
|
|
|
|
return parentWidget;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this->getAncestorWidget(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isAncestorsVisible(Node* node)
|
|
|
|
{
|
|
|
|
if (nullptr == node)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Node* parent = node->getParent();
|
|
|
|
|
|
|
|
if (parent && !parent->isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this->isAncestorsVisible(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isAncestorsEnabled()
|
|
|
|
{
|
|
|
|
Widget* parentWidget = this->getAncestorWidget(this);
|
|
|
|
if (parentWidget == nullptr)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (parentWidget && !parentWidget->isEnabled())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentWidget->isAncestorsEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setPropagateTouchEvents(bool isPropagate)
|
|
|
|
{
|
|
|
|
_propagateTouchEvents = isPropagate;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isPropagateTouchEvents() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _propagateTouchEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setSwallowTouches(bool swallow)
|
|
|
|
{
|
|
|
|
if (_touchListener)
|
|
|
|
{
|
|
|
|
_touchListener->setSwallowTouches(swallow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isSwallowTouches() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_touchListener)
|
|
|
|
{
|
|
|
|
return _touchListener->isSwallowTouches();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::onTouchBegan(Touch* touch, Event* /*unusedEvent*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_hitted = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_touchBeganPosition = touch->getLocation();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto camera = Camera::getVisitingCamera();
|
|
|
|
if (hitTest(_touchBeganPosition, camera, nullptr))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isClippingParentContainsPoint(_touchBeganPosition))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_hittedByCamera = camera;
|
2021-12-25 10:04:45 +08:00
|
|
|
_hitted = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!_hitted)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setHighlighted(true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
|
|
|
{
|
|
|
|
this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);
|
|
|
|
}
|
|
|
|
|
|
|
|
pushDownEvent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void Widget::propagateTouchEvent(ax::ui::Widget::TouchEventType event,
|
|
|
|
ax::ui::Widget* sender,
|
|
|
|
ax::Touch* touch)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
widgetParent->_hittedByCamera = _hittedByCamera;
|
|
|
|
widgetParent->interceptTouchEvent(event, sender, touch);
|
|
|
|
widgetParent->_hittedByCamera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::onTouchMoved(Touch* touch, Event* /*unusedEvent*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_touchMovePosition = touch->getLocation();
|
|
|
|
|
|
|
|
setHighlighted(hitTest(_touchMovePosition, _hittedByCamera, nullptr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
|
|
|
{
|
|
|
|
this->propagateTouchEvent(TouchEventType::MOVED, this, touch);
|
|
|
|
}
|
|
|
|
|
|
|
|
moveEvent();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::onTouchEnded(Touch* touch, Event* /*unusedEvent*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_touchEndPosition = touch->getLocation();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
|
|
|
{
|
|
|
|
this->propagateTouchEvent(TouchEventType::ENDED, this, touch);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool highlight = _highlight;
|
|
|
|
setHighlighted(false);
|
|
|
|
|
|
|
|
if (highlight)
|
|
|
|
{
|
|
|
|
releaseUpEvent();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cancelUpEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onTouchCancelled(Touch* touch, Event* /*unusedEvent*/)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Propagate touch events to its parents
|
|
|
|
*/
|
|
|
|
if (_propagateTouchEvents)
|
|
|
|
{
|
|
|
|
this->propagateTouchEvent(TouchEventType::CANCELED, this, touch);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
setHighlighted(false);
|
|
|
|
cancelUpEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::pushDownEvent()
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
|
|
|
_touchEventCallback(this, TouchEventType::BEGAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::moveEvent()
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
|
|
|
_touchEventCallback(this, TouchEventType::MOVED);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::releaseUpEvent()
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
|
|
|
|
if (isFocusEnabled())
|
|
|
|
{
|
|
|
|
requestFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
|
|
|
_touchEventCallback(this, TouchEventType::ENDED);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_clickEventListener)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_clickEventListener(this);
|
|
|
|
}
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::cancelUpEvent()
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
if (_touchEventCallback)
|
|
|
|
{
|
|
|
|
_touchEventCallback(this, TouchEventType::CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::addTouchEventListener(const ccWidgetTouchCallback& callback)
|
|
|
|
{
|
|
|
|
this->_touchEventCallback = callback;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::addClickEventListener(const ccWidgetClickCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
this->_clickEventListener = callback;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::addCCSEventListener(const ccWidgetEventCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
this->_ccEventCallback = callback;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::hitTest(const Vec2& pt, const Camera* camera, Vec3* p) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Rect rect;
|
|
|
|
rect.size = getContentSize();
|
|
|
|
return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, p);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isClippingParentContainsPoint(const Vec2& pt)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_affectByClipping = false;
|
|
|
|
Node* parent = getParent();
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget* clippingParent = nullptr;
|
|
|
|
while (parent)
|
|
|
|
{
|
|
|
|
Layout* layoutParent = dynamic_cast<Layout*>(parent);
|
|
|
|
if (layoutParent)
|
|
|
|
{
|
|
|
|
if (layoutParent->isClippingEnabled())
|
|
|
|
{
|
|
|
|
_affectByClipping = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
clippingParent = layoutParent;
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent = parent->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_affectByClipping)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clippingParent)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool bRet = false;
|
2019-11-23 20:27:39 +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))
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
return clippingParent->isClippingParentContainsPoint(pt);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void Widget::interceptTouchEvent(ax::ui::Widget::TouchEventType event, ax::ui::Widget* sender, Touch* touch)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
|
|
|
widgetParent->_hittedByCamera = _hittedByCamera;
|
2021-12-25 10:04:45 +08:00
|
|
|
widgetParent->interceptTouchEvent(event, sender, touch);
|
2019-11-23 20:27:39 +08:00
|
|
|
widgetParent->_hittedByCamera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::setPosition(const Vec2& pos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!_usingLayoutComponent && _running)
|
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 pSize = widgetParent->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (pSize.width <= 0.0f || pSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_positionPercent.setZero();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_positionPercent.set(pos.x / pSize.width, pos.y / pSize.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProtectedNode::setPosition(pos);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::setPositionPercent(const Vec2& percent)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
component->setPositionPercentX(percent.x);
|
|
|
|
component->setPositionPercentY(percent.y);
|
|
|
|
component->refreshLayout();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_positionPercent = percent;
|
|
|
|
if (_running)
|
|
|
|
{
|
|
|
|
Widget* widgetParent = getWidgetParent();
|
|
|
|
if (widgetParent)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 parentSize = widgetParent->getContentSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec2 absPos(parentSize.width * _positionPercent.x, parentSize.height * _positionPercent.y);
|
|
|
|
setPosition(absPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& Widget::getPositionPercent()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
float percentX = component->getPositionPercentX();
|
|
|
|
float percentY = component->getPositionPercentY();
|
|
|
|
|
|
|
|
_positionPercent.set(percentX, percentY);
|
|
|
|
}
|
|
|
|
return _positionPercent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setPositionType(PositionType type)
|
|
|
|
{
|
|
|
|
_positionType = type;
|
|
|
|
|
|
|
|
if (_usingLayoutComponent)
|
|
|
|
{
|
|
|
|
auto component = this->getOrCreateLayoutComponent();
|
|
|
|
if (type == Widget::PositionType::ABSOLUTE)
|
|
|
|
{
|
|
|
|
component->setPositionPercentXEnabled(false);
|
|
|
|
component->setPositionPercentYEnabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
component->setPositionPercentXEnabled(true);
|
|
|
|
component->setPositionPercentYEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::PositionType Widget::getPositionType() const
|
|
|
|
{
|
|
|
|
return _positionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isBright() const
|
|
|
|
{
|
|
|
|
return _bright;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::isEnabled() const
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Widget::getLeftBoundary() const
|
|
|
|
{
|
|
|
|
return getBoundingBox().origin.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Widget::getBottomBoundary() const
|
|
|
|
{
|
|
|
|
return getBoundingBox().origin.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Widget::getRightBoundary() const
|
|
|
|
{
|
|
|
|
return getLeftBoundary() + getBoundingBox().size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Widget::getTopBoundary() const
|
|
|
|
{
|
|
|
|
return getBottomBoundary() + getBoundingBox().size.height;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& Widget::getTouchBeganPosition() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _touchBeganPosition;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& Widget::getTouchMovePosition() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _touchMovePosition;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& Widget::getTouchEndPosition() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _touchEndPosition;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::setLayoutParameter(LayoutParameter* parameter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!parameter)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_layoutParameterDictionary.insert((int)parameter->getLayoutType(), parameter);
|
|
|
|
_layoutParameterType = parameter->getLayoutType();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutParameter* Widget::getLayoutParameter() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at((int)_layoutParameterType));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& subWidget : modelChildren)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
addChild(child->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::copySpecialProperties(Widget* /*model*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Widget::copyProperties(Widget* widget)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
this->setContentSize(widget->_contentSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
_customSize = widget->_customSize;
|
|
|
|
_sizeType = widget->getSizeType();
|
|
|
|
_sizePercent = widget->_sizePercent;
|
|
|
|
_positionType = widget->_positionType;
|
2019-11-23 20:27:39 +08:00
|
|
|
_positionPercent = widget->_positionPercent;
|
|
|
|
setPosition(widget->getPosition());
|
|
|
|
setAnchorPoint(widget->getAnchorPoint());
|
|
|
|
setScaleX(widget->getScaleX());
|
|
|
|
setScaleY(widget->getScaleY());
|
|
|
|
setRotation(widget->getRotation());
|
|
|
|
setRotationSkewX(widget->getRotationSkewX());
|
|
|
|
setRotationSkewY(widget->getRotationSkewY());
|
|
|
|
setFlippedX(widget->isFlippedX());
|
|
|
|
setFlippedY(widget->isFlippedY());
|
|
|
|
setColor(widget->getColor());
|
|
|
|
setOpacity(widget->getOpacity());
|
|
|
|
setCascadeColorEnabled(widget->isCascadeColorEnabled());
|
|
|
|
setCascadeOpacityEnabled(widget->isCascadeOpacityEnabled());
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchEventCallback = widget->_touchEventCallback;
|
|
|
|
_touchEventListener = widget->_touchEventListener;
|
|
|
|
_clickEventListener = widget->_clickEventListener;
|
|
|
|
_focused = widget->_focused;
|
|
|
|
_focusEnabled = widget->_focusEnabled;
|
2019-11-23 20:27:39 +08:00
|
|
|
_propagateTouchEvents = widget->_propagateTouchEvents;
|
|
|
|
|
|
|
|
copySpecialProperties(widget);
|
|
|
|
|
|
|
|
Map<int, LayoutParameter*>& layoutParameterDic = widget->_layoutParameterDictionary;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : layoutParameterDic)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setLayoutParameter(iter.second->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setFlippedX(bool flippedX)
|
|
|
|
{
|
|
|
|
|
|
|
|
float realScale = this->getScaleX();
|
2021-12-25 10:04:45 +08:00
|
|
|
_flippedX = flippedX;
|
2019-11-23 20:27:39 +08:00
|
|
|
this->setScaleX(realScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setFlippedY(bool flippedY)
|
|
|
|
{
|
|
|
|
float realScale = this->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
_flippedY = flippedY;
|
2019-11-23 20:27:39 +08:00
|
|
|
this->setScaleY(realScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setScaleX(float scaleX)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_flippedX)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
scaleX = scaleX * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleX(scaleX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setScaleY(float scaleY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_flippedY)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
scaleY = scaleY * -1;
|
|
|
|
}
|
|
|
|
Node::setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setScale(float scale)
|
|
|
|
{
|
|
|
|
this->setScaleX(scale);
|
|
|
|
this->setScaleY(scale);
|
|
|
|
this->setScaleZ(scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setScale(float scaleX, float scaleY)
|
|
|
|
{
|
|
|
|
this->setScaleX(scaleX);
|
|
|
|
this->setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float Widget::getScaleX() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleX();
|
|
|
|
if (_flippedX)
|
|
|
|
{
|
|
|
|
originalScale = originalScale * -1.0f;
|
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float Widget::getScaleY() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float originalScale = Node::getScaleY();
|
|
|
|
if (_flippedY)
|
|
|
|
{
|
|
|
|
originalScale = originalScale * -1.0f;
|
|
|
|
}
|
|
|
|
return originalScale;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float Widget::getScale() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(this->getScaleX() == this->getScaleY(), "scaleX should be equal to scaleY.");
|
2019-11-23 20:27:39 +08:00
|
|
|
return this->getScaleX();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*temp action*/
|
|
|
|
void Widget::setActionTag(int tag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_actionTag = tag;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int Widget::getActionTag() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _actionTag;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setFocused(bool focus)
|
|
|
|
{
|
|
|
|
_focused = focus;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// make sure there is only one focusedWidget
|
|
|
|
if (focus)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_focusedWidget = this;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_focusNavigationController)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_focusNavigationController->setFirstFocusedWidget(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isFocused() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _focused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setFocusEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_focusEnabled = enable;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isFocusEnabled() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _focusEnabled;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* Widget::findNextFocusedWidget(FocusDirection direction, Widget* current)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == onNextFocusedWidget || nullptr == onNextFocusedWidget(direction))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (this->isFocused() || dynamic_cast<Layout*>(current))
|
|
|
|
{
|
|
|
|
Node* parent = this->getParent();
|
|
|
|
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(parent);
|
|
|
|
if (nullptr == layout)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// the outer layout's default behaviour is : loop focus
|
2019-11-23 20:27:39 +08:00
|
|
|
if (dynamic_cast<Layout*>(current))
|
|
|
|
{
|
|
|
|
return current->findNextFocusedWidget(direction, current);
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* nextWidget = layout->findNextFocusedWidget(direction, current);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* getFocusWidget = onNextFocusedWidget(direction);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->dispatchFocusEvent(this, getFocusWidget);
|
|
|
|
return getFocusWidget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void Widget::dispatchFocusEvent(ax::ui::Widget* widgetLoseFocus, ax::ui::Widget* widgetGetFocus)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// if the widgetLoseFocus doesn't get focus, it will use the previous focused widget instead
|
2019-11-23 20:27:39 +08:00
|
|
|
if (widgetLoseFocus && !widgetLoseFocus->isFocused())
|
|
|
|
{
|
|
|
|
widgetLoseFocus = _focusedWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widgetGetFocus != widgetLoseFocus)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (widgetGetFocus)
|
|
|
|
{
|
|
|
|
widgetGetFocus->onFocusChanged(widgetLoseFocus, widgetGetFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widgetLoseFocus)
|
|
|
|
{
|
|
|
|
widgetLoseFocus->onFocusChanged(widgetLoseFocus, widgetGetFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventFocus event(widgetLoseFocus, widgetGetFocus);
|
2021-04-22 22:01:47 +08:00
|
|
|
auto dispatcher = _director->getEventDispatcher();
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatcher->dispatchEvent(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::requestFocus()
|
|
|
|
{
|
|
|
|
if (this == _focusedWidget)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->dispatchFocusEvent(_focusedWidget, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onFocusChange(Widget* widgetLostFocus, Widget* widgetGetFocus)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// only change focus when there is indeed a get&lose happens
|
2019-11-23 20:27:39 +08:00
|
|
|
if (widgetLostFocus)
|
|
|
|
{
|
|
|
|
widgetLostFocus->setFocused(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widgetGetFocus)
|
|
|
|
{
|
|
|
|
widgetGetFocus->setFocused(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::getCurrentFocusedWidget()
|
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::enableDpadNavigation(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
if (nullptr == _focusNavigationController)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_focusNavigationController = new FocusNavigationController;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_focusedWidget)
|
|
|
|
{
|
|
|
|
_focusNavigationController->setFirstFocusedWidget(_focusedWidget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_focusNavigationController);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nullptr != _focusNavigationController)
|
|
|
|
{
|
|
|
|
_focusNavigationController->enableFocusNavigation(enable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isUnifySizeEnabled() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _unifySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setUnifySizeEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_unifySize = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setLayoutComponentEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_usingLayoutComponent = enable;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Widget::isLayoutComponentEnabled() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _usingLayoutComponent;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|