2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2020-05-20 08:56:12 +08:00
|
|
|
Copyright (c) 2020 c4games.com.
|
2021-09-05 20:45:32 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
https://adxe.org
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __UIWIDGET_H__
|
|
|
|
#define __UIWIDGET_H__
|
|
|
|
|
|
|
|
#include "2d/CCProtectedNode.h"
|
|
|
|
#include "ui/UILayoutParameter.h"
|
|
|
|
#include "ui/GUIDefine.h"
|
|
|
|
#include "ui/GUIExport.h"
|
|
|
|
#include "base/CCMap.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class EventListenerTouchOneByOne;
|
|
|
|
class Camera;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
class LayoutComponent;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*@brief Base class for all ui widgets.
|
|
|
|
* This class inherent from `ProtectedNode` and `LayoutParameterProtocol`.
|
|
|
|
* If you want to implements your own ui widget, you should subclass it.
|
|
|
|
*/
|
|
|
|
class CC_GUI_DLL Widget : public ProtectedNode, public LayoutParameterProtocol
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Widget focus direction.
|
|
|
|
*/
|
|
|
|
enum class FocusDirection
|
|
|
|
{
|
|
|
|
LEFT,
|
|
|
|
RIGHT,
|
|
|
|
UP,
|
|
|
|
DOWN
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Widget position type for layout.
|
|
|
|
*/
|
|
|
|
enum class PositionType
|
|
|
|
{
|
|
|
|
ABSOLUTE,
|
|
|
|
PERCENT
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Widget size type for layout.
|
|
|
|
*/
|
|
|
|
enum class SizeType
|
|
|
|
{
|
|
|
|
ABSOLUTE,
|
|
|
|
PERCENT
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Touch event type.
|
|
|
|
*/
|
|
|
|
enum class TouchEventType
|
|
|
|
{
|
|
|
|
BEGAN,
|
|
|
|
MOVED,
|
|
|
|
ENDED,
|
|
|
|
CANCELED
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Texture resource type.
|
|
|
|
* - LOCAL: It means the texture is loaded from image.
|
|
|
|
* - PLIST: It means the texture is loaded from texture atlas.
|
|
|
|
*/
|
|
|
|
enum class TextureResType
|
|
|
|
{
|
|
|
|
LOCAL = 0,
|
|
|
|
PLIST = 1
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Widget bright style.
|
|
|
|
*/
|
|
|
|
enum class BrightStyle
|
|
|
|
{
|
|
|
|
NONE = -1,
|
|
|
|
NORMAL,
|
|
|
|
HIGHLIGHT
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget touch event callback.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
typedef std::function<void(Ref*, Widget::TouchEventType)> ccWidgetTouchCallback;
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Widget click event callback.
|
|
|
|
*/
|
|
|
|
typedef std::function<void(Ref*)> ccWidgetClickCallback;
|
|
|
|
/**
|
|
|
|
* Widget custom event callback.
|
|
|
|
* It is mainly used together with Cocos Studio.
|
|
|
|
*/
|
|
|
|
typedef std::function<void(Ref*, int)> ccWidgetEventCallback;
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
* @js ctor
|
|
|
|
* @lua new
|
|
|
|
*/
|
|
|
|
Widget();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Default destructor
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~Widget();
|
|
|
|
/**
|
|
|
|
* Create and return a empty Widget instance pointer.
|
|
|
|
*/
|
|
|
|
static Widget* create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget is enabled
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
* true if the widget is enabled, widget may be touched , false if the widget is disabled, widget cannot be touched.
|
|
|
|
*
|
|
|
|
* Note: If you want to change the widget's appearance to disabled state, you should also call `setBright(false)`.
|
|
|
|
*
|
|
|
|
* The default value is true, a widget is default to enable touch.
|
|
|
|
*
|
|
|
|
* @param enabled Set to true to enable touch, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual void setEnabled(bool enabled);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the widget is enabled or not.
|
|
|
|
*
|
|
|
|
* @return true if the widget is enabled, false if the widget is disabled.
|
|
|
|
*/
|
|
|
|
bool isEnabled() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget is bright
|
|
|
|
*
|
|
|
|
* The default value is true, a widget is default to bright
|
|
|
|
*
|
|
|
|
* @param bright true if the widget is bright, false if the widget is dark.
|
|
|
|
*/
|
|
|
|
void setBright(bool bright);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the widget is bright
|
|
|
|
*
|
|
|
|
* @return true if the widget is bright, false if the widget is dark.
|
|
|
|
*/
|
|
|
|
bool isBright() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget is touch enabled.
|
|
|
|
*
|
|
|
|
* The default value is false, a widget is default to touch disabled.
|
|
|
|
*
|
|
|
|
* @param enabled True if the widget is touch enabled, false if the widget is touch disabled.
|
|
|
|
*/
|
|
|
|
virtual void setTouchEnabled(bool enabled);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To set the bright style of widget.
|
|
|
|
*
|
|
|
|
* @see BrightStyle
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param style BrightStyle::NORMAL means the widget is in normal state, BrightStyle::HIGHLIGHT means the widget
|
|
|
|
* is in highlight state.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
void setBrightStyle(BrightStyle style);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the widget is touch enabled
|
|
|
|
*
|
|
|
|
* @return true if the widget is touch enabled, false if the widget is touch disabled.
|
|
|
|
*/
|
|
|
|
bool isTouchEnabled() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the widget is highlighted
|
|
|
|
*
|
|
|
|
* @return true if the widget is highlighted, false if the widget is not highlighted.
|
|
|
|
*/
|
|
|
|
bool isHighlighted() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget is highlighted
|
|
|
|
*
|
|
|
|
* The default value is false, a widget is default to not highlighted
|
|
|
|
*
|
|
|
|
* @param highlight true if the widget is highlighted, false if the widget is not highlighted.
|
|
|
|
*/
|
|
|
|
void setHighlighted(bool highlight);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the left boundary position of this widget in parent's coordination system.
|
|
|
|
* @return The left boundary position of this widget.
|
|
|
|
*/
|
|
|
|
float getLeftBoundary() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the bottom boundary position of this widget in parent's coordination system.
|
|
|
|
* @return The bottom boundary position of this widget.
|
|
|
|
*/
|
|
|
|
float getBottomBoundary() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the right boundary position of this widget in parent's coordination system.
|
|
|
|
* @return The right boundary position of this widget.
|
|
|
|
*/
|
|
|
|
float getRightBoundary() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the top boundary position of this widget in parent's coordination system.
|
|
|
|
* @return The top boundary position of this widget.
|
|
|
|
*/
|
|
|
|
float getTopBoundary() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void visit(cocos2d::Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a callback to touch vent listener.
|
|
|
|
*@param callback The callback in `ccWidgetEventCallback.`
|
|
|
|
*/
|
|
|
|
void addTouchEventListener(const ccWidgetTouchCallback& callback);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set a click event handler to the widget.
|
|
|
|
* @param callback The callback in `ccWidgetClickCallback`.
|
|
|
|
*/
|
|
|
|
void addClickEventListener(const ccWidgetClickCallback& callback);
|
|
|
|
/**
|
|
|
|
* Set a event handler to the widget in order to use cocostudio editor and framework
|
|
|
|
* @param callback The callback in `ccWidgetEventCallback`.
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual void addCCSEventListener(const ccWidgetEventCallback& callback);
|
|
|
|
/**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the position (x,y) of the widget in OpenGL coordinates
|
|
|
|
*
|
|
|
|
* Usually we use p(x,y) to compose a Vec2 object.
|
|
|
|
* The original point (0,0) is at the left-bottom corner of screen.
|
|
|
|
*
|
|
|
|
* @param pos The position (x,y) of the widget in OpenGL coordinates
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void setPosition(const Vec2& pos) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the percent(x,y) of the widget in OpenGL coordinates
|
|
|
|
*
|
|
|
|
* @param percent The percent (x,y) of the widget in OpenGL coordinates
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setPositionPercent(const Vec2& percent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the percent (x,y) of the widget in OpenGL coordinates
|
|
|
|
*
|
|
|
|
* @see setPosition(const Vec2&)
|
|
|
|
*
|
|
|
|
* @return The percent (x,y) of the widget in OpenGL coordinates
|
|
|
|
*/
|
|
|
|
const Vec2& getPositionPercent();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the position type of the widget
|
|
|
|
*
|
|
|
|
* @see `PositionType`
|
|
|
|
*
|
|
|
|
* @param type the position type of widget
|
|
|
|
*/
|
|
|
|
void setPositionType(PositionType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the position type of the widget
|
|
|
|
*
|
|
|
|
* @see `PositionType`
|
|
|
|
*
|
|
|
|
* @return type the position type of widget
|
|
|
|
*/
|
|
|
|
PositionType getPositionType() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget should be flipped horizontally or not.
|
|
|
|
*
|
|
|
|
* @param flippedX true if the widget should be flipped horizontally, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual void setFlippedX(bool flippedX);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the flag which indicates whether the widget is flipped horizontally or not.
|
|
|
|
*
|
|
|
|
* It not only flips the texture of the widget, but also the texture of the widget's children.
|
|
|
|
* Also, flipping relies on widget's anchor point.
|
|
|
|
* Internally, it just use setScaleX(-1) to flip the widget.
|
|
|
|
*
|
|
|
|
* @return true if the widget is flipped horizontally, false otherwise.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool isFlippedX() const { return _flippedX; };
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the widget should be flipped vertically or not.
|
|
|
|
*
|
|
|
|
* @param flippedY true if the widget should be flipped vertically, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual void setFlippedY(bool flippedY);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the flag which indicates whether the widget is flipped vertically or not.
|
|
|
|
*
|
|
|
|
* It not only flips the texture of the widget, but also the texture of the widget's children.
|
|
|
|
* Also, flipping relies on widget's anchor point.
|
|
|
|
* Internally, it just use setScaleY(-1) to flip the widget.
|
|
|
|
*
|
|
|
|
* @return true if the widget is flipped vertically, false otherwise.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool isFlippedY() const { return _flippedY; };
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// override the setScale function of Node
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void setScaleX(float scaleX) override;
|
|
|
|
virtual void setScaleY(float scaleY) override;
|
|
|
|
virtual void setScale(float scale) override;
|
|
|
|
virtual void setScale(float scalex, float scaley) override;
|
|
|
|
using Node::setScaleZ;
|
|
|
|
virtual float getScaleX() const override;
|
|
|
|
virtual float getScaleY() const override;
|
|
|
|
virtual float getScale() const override;
|
|
|
|
using Node::getScaleZ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks a point if in parent's area.
|
|
|
|
*
|
|
|
|
* @param pt A point in `Vec2`.
|
|
|
|
* @return true if the point is in parent's area, false otherwise.
|
|
|
|
*/
|
|
|
|
bool isClippingParentContainsPoint(const Vec2& pt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the touch began point of widget when widget is selected.
|
|
|
|
* @return the touch began point.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& getTouchBeganPosition() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Gets the touch move point of widget when widget is selected.
|
|
|
|
* @return the touch move point.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& getTouchMovePosition() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Gets the touch end point of widget when widget is selected.
|
|
|
|
* @return the touch end point.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& getTouchEndPosition() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the size that is widget's size
|
2021-10-23 23:27:14 +08:00
|
|
|
* @param contentSize A content size in `Vec2`.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-10-23 23:27:14 +08:00
|
|
|
virtual void setContentSize(const Vec2& contentSize) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the percent that is widget's percent size
|
|
|
|
*
|
|
|
|
* @param percent that is widget's percent size
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void setSizePercent(const Vec2& percent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the size type of widget.
|
|
|
|
*
|
|
|
|
* @see `SizeType`
|
|
|
|
*
|
|
|
|
* @param type that is widget's size type
|
|
|
|
*/
|
|
|
|
void setSizeType(SizeType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the size type of widget.
|
|
|
|
*
|
|
|
|
* @see `SizeType`
|
|
|
|
*/
|
|
|
|
SizeType getSizeType() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the user defined widget size.
|
|
|
|
*@return User defined size.
|
|
|
|
*/
|
2021-10-23 23:27:14 +08:00
|
|
|
const Vec2& getCustomSize() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Get the content size of widget.
|
|
|
|
* @warning This API exists mainly for keeping back compatibility.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @return
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual const Vec2& getLayoutSize() { return _contentSize; };
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get size percent of widget.
|
|
|
|
*
|
|
|
|
* @return Percent size.
|
|
|
|
*/
|
|
|
|
const Vec2& getSizePercent();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks a point is in widget's content space.
|
|
|
|
* This function is used for determining touch area of widget.
|
|
|
|
*
|
|
|
|
* @param pt The point in `Vec2`.
|
|
|
|
* @param camera The camera look at widget, used to convert GL screen point to near/far plane.
|
|
|
|
* @param p Point to a Vec3 for store the intersect point, if don't need them set to nullptr.
|
|
|
|
* @return true if the point is in widget's content space, false otherwise.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool hitTest(const Vec2& pt, const Camera* camera, Vec3* p) const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called when touch began event is issued.
|
|
|
|
*@param touch The touch info.
|
|
|
|
*@param unusedEvent The touch event info.
|
|
|
|
*@return True if user want to handle touches, false otherwise.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool onTouchBegan(Touch* touch, Event* unusedEvent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called when touch moved event is issued.
|
|
|
|
*@param touch The touch info.
|
|
|
|
*@param unusedEvent The touch event info.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void onTouchMoved(Touch* touch, Event* unusedEvent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called when touch ended event is issued.
|
|
|
|
*@param touch The touch info.
|
|
|
|
*@param unusedEvent The touch event info.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void onTouchEnded(Touch* touch, Event* unusedEvent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called when touch cancelled event is issued.
|
|
|
|
*@param touch The touch info.
|
|
|
|
*@param unusedEvent The touch event info.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void onTouchCancelled(Touch* touch, Event* unusedEvent);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a LayoutParameter to widget.
|
|
|
|
*
|
|
|
|
* @see LayoutParameter
|
|
|
|
* @param parameter LayoutParameter pointer
|
|
|
|
*/
|
|
|
|
void setLayoutParameter(LayoutParameter* parameter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets LayoutParameter of widget.
|
|
|
|
*
|
|
|
|
* @see LayoutParameter
|
|
|
|
* @return LayoutParameter
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
LayoutParameter* getLayoutParameter() const override;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle whether ignore user defined content size for widget.
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set true will ignore user defined content size which means
|
2019-11-23 20:27:39 +08:00
|
|
|
* the widget size is always equal to the return value of `getVirtualRendererSize`.
|
|
|
|
*
|
|
|
|
* @param ignore set member variable _ignoreSize to ignore
|
|
|
|
*/
|
|
|
|
virtual void ignoreContentAdaptWithSize(bool ignore);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query whether the widget ignores user defined content size or not
|
|
|
|
*
|
|
|
|
* @return True means ignore user defined content size, false otherwise.
|
|
|
|
*/
|
|
|
|
bool isIgnoreContentAdaptWithSize() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets position of widget in world space.
|
|
|
|
*
|
|
|
|
* @return Position of widget in world space.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 getWorldPosition() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the inner Renderer node of widget.
|
|
|
|
*
|
|
|
|
* For example, a button's Virtual Renderer is it's texture renderer.
|
|
|
|
*
|
|
|
|
* @return Node pointer.
|
|
|
|
*/
|
|
|
|
virtual Node* getVirtualRenderer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the virtual renderer's size
|
|
|
|
*@return Widget virtual renderer size.
|
|
|
|
*/
|
2021-10-23 23:27:14 +08:00
|
|
|
virtual Vec2 getVirtualRendererSize() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string representation of widget class name
|
|
|
|
* @return get the class description.
|
|
|
|
*/
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new widget copy of the original one.
|
|
|
|
* @return A cloned widget copy of original.
|
|
|
|
*/
|
|
|
|
Widget* clone();
|
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual void onEnter() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual void onExit() override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all children's contents size and position recursively.
|
2021-10-23 23:27:14 +08:00
|
|
|
* @see `updateSizeAndPosition(const Vec2&)`
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
void updateSizeAndPosition();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all children's contents size and position recursively.
|
|
|
|
*/
|
2021-10-23 23:27:14 +08:00
|
|
|
void updateSizeAndPosition(const Vec2& parentSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set the tag of action.
|
|
|
|
*@param tag A integer tag value.
|
|
|
|
*/
|
|
|
|
void setActionTag(int tag);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the action tag.
|
|
|
|
*@return Action tag.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
int getActionTag() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Allow widget touch events to propagate to its parents. Set false will disable propagation
|
|
|
|
* @param isPropagate True to allow propagation, false otherwise.
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
|
|
|
void setPropagateTouchEvents(bool isPropagate);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Return whether the widget is propagate touch events to its parents or not
|
|
|
|
* @return whether touch event propagation is allowed or not.
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool isPropagateTouchEvents() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Toggle widget swallow touch option.
|
|
|
|
* @brief Specify widget to swallow touches or not
|
|
|
|
* @param swallow True to swallow touch, false otherwise.
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
|
|
|
void setSwallowTouches(bool swallow);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Return whether the widget is swallowing touch or not
|
|
|
|
* @return Whether touch is swallowed.
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isSwallowTouches() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Query whether widget is focused or not.
|
|
|
|
*@return whether the widget is focused or not
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFocused() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Toggle widget focus status.
|
|
|
|
*@param focus pass true to let the widget get focus or pass false to let the widget lose focus
|
|
|
|
*/
|
|
|
|
void setFocused(bool focus);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Query widget's focus enable state.
|
|
|
|
*@return true represent the widget could accept focus, false represent the widget couldn't accept focus
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFocusEnabled() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Allow widget to accept focus.
|
|
|
|
*@param enable pass true/false to enable/disable the focus ability of a widget
|
|
|
|
*/
|
|
|
|
void setFocusEnabled(bool enable);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* When a widget is in a layout, you could call this method to get the next focused widget within a specified
|
|
|
|
*direction. If the widget is not in a layout, it will return itself
|
2019-11-23 20:27:39 +08:00
|
|
|
*@param direction the direction to look for the next focused widget in a layout
|
|
|
|
*@param current the current focused widget
|
|
|
|
*@return the next focused widget in a layout
|
|
|
|
*/
|
|
|
|
virtual Widget* findNextFocusedWidget(FocusDirection direction, Widget* current);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* when a widget calls this method, it will get focus immediately.
|
|
|
|
*/
|
|
|
|
void requestFocus();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a current focused widget in your UI scene.
|
|
|
|
* No matter what widget object you call this method on , it will return you the exact one focused widget.
|
|
|
|
*/
|
|
|
|
static Widget* getCurrentFocusedWidget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call this method with parameter true to enable the Android Dpad focus navigation feature
|
|
|
|
*@param enable set true to enable dpad focus navigation, otherwise disenable dpad focus navigation
|
|
|
|
*/
|
|
|
|
static void enableDpadNavigation(bool enable);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* When a widget lose/get focus, this method will be called. Be Caution when you provide your own version,
|
2019-11-23 20:27:39 +08:00
|
|
|
* you must call widget->setFocused(true/false) to change the focus state of the current focused widget;
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
std::function<void(Widget*, Widget*)> onFocusChanged;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* use this function to manually specify the next focused widget regards to each direction
|
|
|
|
*/
|
|
|
|
std::function<Widget*(FocusDirection)> onNextFocusedWidget;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
*Toggle use unify size.
|
|
|
|
*@param enable True to use unify size, false otherwise.
|
|
|
|
*/
|
|
|
|
void setUnifySizeEnabled(bool enable);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Query whether unify size enable state.
|
2021-10-24 14:09:59 +08:00
|
|
|
*@return true represent the widget use Unify size, false represent the widget couldn't use Unify size
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isUnifySizeEnabled() const;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set callback name.
|
|
|
|
*@param callbackName A string representation of callback name.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setCallbackName(std::string_view callbackName) { _callbackName = callbackName; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query callback name.
|
|
|
|
*@return The callback name.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getCallbackName() const { return _callbackName; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set callback type.
|
|
|
|
* @param callbackType A string representation of callback type.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setCallbackType(std::string_view callbackType) { _callbackType = callbackType; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query callback type.
|
|
|
|
*@return Callback type string.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getCallbackType() const { return _callbackType; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle layout component enable.
|
|
|
|
*@param enable Layout Component of a widget
|
|
|
|
*/
|
|
|
|
void setLayoutComponentEnabled(bool enable);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Query whether layout component is enabled or not.
|
2019-11-23 20:27:39 +08:00
|
|
|
*@return true represent the widget use Layout Component, false represent the widget couldn't use Layout Component.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isLayoutComponentEnabled() const;
|
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS :
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
friend class PageView;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// initializes state of widget.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual bool init() override;
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* @brief Sends the touch event to widget's parent, if a widget wants to handle touch event under another widget,
|
2019-11-23 20:27:39 +08:00
|
|
|
* it must override this function.
|
|
|
|
* @param event the touch event type, it could be BEGAN/MOVED/CANCELED/ENDED
|
|
|
|
* @param parent
|
|
|
|
* @param point
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void interceptTouchEvent(TouchEventType event, Widget* sender, Touch* touch);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
*@brief Propagate touch events to its parents
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void propagateTouchEvent(TouchEventType event, Widget* sender, Touch* touch);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* This method is called when a focus change event happens
|
|
|
|
*@param widgetLostFocus The widget which lose its focus
|
|
|
|
*@param widgetGetFocus The widget which get its focus
|
|
|
|
*/
|
|
|
|
void onFocusChange(Widget* widgetLostFocus, Widget* widgetGetFocus);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Dispatch a EventFocus through a EventDispatcher
|
|
|
|
*@param widgetLoseFocus The widget which lose its focus
|
|
|
|
*@param widgetGetFocus he widget which get its focus
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void dispatchFocusEvent(Widget* widgetLoseFocus, Widget* widgetGetFocus);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
// call back function called when size changed.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onSizeChanged();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// initializes renderer of widget.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void initRenderer();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// call back function called widget's state changed to normal.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onPressStateChangedToNormal();
|
2021-12-25 10:04:45 +08:00
|
|
|
// call back function called widget's state changed to selected.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onPressStateChangedToPressed();
|
2021-12-25 10:04:45 +08:00
|
|
|
// call back function called widget's state changed to dark.
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onPressStateChangedToDisabled();
|
|
|
|
|
|
|
|
void pushDownEvent();
|
|
|
|
void moveEvent();
|
|
|
|
|
|
|
|
virtual void releaseUpEvent();
|
|
|
|
virtual void cancelUpEvent();
|
|
|
|
|
|
|
|
virtual void adaptRenderers(){};
|
|
|
|
void updateChildrenDisplayedRGBA();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void copyProperties(Widget* model);
|
|
|
|
virtual Widget* createCloneInstance();
|
|
|
|
virtual void copySpecialProperties(Widget* model);
|
|
|
|
virtual void copyClonedWidgetChildren(Widget* model);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget* getWidgetParent();
|
2021-10-23 23:27:14 +08:00
|
|
|
void updateContentSizeWithTextureSize(const Vec2& size);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isAncestorsEnabled();
|
|
|
|
Widget* getAncestorWidget(Node* node);
|
|
|
|
bool isAncestorsVisible(Node* node);
|
|
|
|
|
|
|
|
void cleanupWidget();
|
|
|
|
LayoutComponent* getOrCreateLayoutComponent();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool _usingLayoutComponent;
|
|
|
|
bool _unifySize;
|
|
|
|
bool _enabled;
|
|
|
|
bool _bright;
|
|
|
|
bool _touchEnabled;
|
|
|
|
bool _highlight;
|
|
|
|
bool _affectByClipping;
|
|
|
|
bool _ignoreSize;
|
|
|
|
bool _propagateTouchEvents;
|
|
|
|
|
|
|
|
BrightStyle _brightStyle;
|
|
|
|
SizeType _sizeType;
|
|
|
|
PositionType _positionType;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// used for search widget by action tag in UIHelper class
|
2019-11-23 20:27:39 +08:00
|
|
|
int _actionTag;
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 _customSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Vec2 _sizePercent;
|
|
|
|
Vec2 _positionPercent;
|
|
|
|
|
|
|
|
bool _hitted;
|
|
|
|
// weak reference of the camera which made the widget passed the hit test when response touch begin event
|
|
|
|
// it's useful in the next touch move/end events
|
2021-12-25 10:04:45 +08:00
|
|
|
const Camera* _hittedByCamera;
|
2019-11-23 20:27:39 +08:00
|
|
|
EventListenerTouchOneByOne* _touchListener;
|
|
|
|
Vec2 _touchBeganPosition;
|
|
|
|
Vec2 _touchMovePosition;
|
|
|
|
Vec2 _touchEndPosition;
|
|
|
|
|
|
|
|
bool _flippedX;
|
|
|
|
bool _flippedY;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// use map to enable switch back and forth for user layout parameters
|
|
|
|
Map<int, LayoutParameter*> _layoutParameterDictionary;
|
2019-11-23 20:27:39 +08:00
|
|
|
LayoutParameter::Type _layoutParameterType;
|
|
|
|
|
|
|
|
bool _focused;
|
|
|
|
bool _focusEnabled;
|
|
|
|
/**
|
|
|
|
* store the only one focused widget
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static Widget* _focusedWidget; // both layout & widget will be stored in this variable
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Ref* _touchEventListener;
|
2019-11-23 20:27:39 +08:00
|
|
|
ccWidgetTouchCallback _touchEventCallback;
|
|
|
|
ccWidgetClickCallback _clickEventListener;
|
|
|
|
ccWidgetEventCallback _ccEventCallback;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string _callbackType;
|
|
|
|
std::string _callbackName;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
class FocusNavigationController;
|
|
|
|
static FocusNavigationController* _focusNavigationController;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
// end of ui group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
#endif /* defined(__Widget__) */
|