#ifndef __UIWIDGET_H__
#define __UIWIDGET_H__
#include "ui/CCProtectedNode.h"
#include "ui/UILayoutDefine.h"
#include "ui/UILayoutParameter.h"
#include "ui/GUIDefine.h"
NS_CC_BEGIN
namespace ui {
typedef enum
{
BRIGHT_NONE = -1,
BRIGHT_NORMAL,//正常
BRIGHT_HIGHLIGHT//高亮 按钮点击之后
}BrightStyle;//明亮的风格
typedef enum
{
WidgetTypeWidget, //control 基本 UIWidget 默认
WidgetTypeContainer //container 容器 layout 实例化
}WidgetType;//ui 类型
typedef enum
{
UI_TEX_TYPE_LOCAL = 0,//means local file
UI_TEX_TYPE_PLIST = 1// means sprite frame
}TextureResType;//贴图类型
enum class TouchEventType
{
TOUCH_EVENT_BEGAN,//开始
TOUCH_EVENT_MOVED,//移动
TOUCH_EVENT_ENDED,//结束
TOUCH_EVENT_CANCELED//取消
};//触摸事件类型
typedef enum
{
SIZE_ABSOLUTE,//绝对值
SIZE_PERCENT//百分比值
}SizeType;//size值类型
typedef enum
{
POSITION_ABSOLUTE,//绝对 真实位置
POSITION_PERCENT//相对父节点位置百分比
}PositionType;//位置类型
typedef enum
{
WIDGET_SIZE,//size
WIDGET_POSITION//position
}WidgetInfoType;//位置类型
//哦 这里说明一下 回调函数被我修改了下 主要是这样修改之后可以用到C++11的lambda表达式
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//不必在使用的时候多声明一个回调函数了 比较方便 希望引擎组可以采用这种方式,
[Cocos2dx]源码分析之 2d/ui/Widget
。 typedef std::function
//函数指针 指向 返回值 void 参数列表(Ref*,TouchEventType) 类型的函数
//typedef void (Ref::*SEL_TouchEvent)(Ref*,TouchEventType);
//宏定义 给函数指针SEL_TouchEvent取别名为toucheventselector
#define toucheventselector(_SELECTOR) (SEL_TouchEvent)(&_SELECTOR)
class Widget : public ProtectedNode
{
public:
Widget(void);
virtual ~Widget();
static Widget* create();
/**设置 是否 触摸&&可见 默认true*/
virtual void setEnabled(bool enabled);
bool isEnabled() const;
/**设置是否明亮 默认true*/
void setBright(bool bright);
/**是否明亮*/
bool isBright() const;
/**是否可以触摸(响应事件)默认true*/
virtual void setTouchEnabled(bool enabled);
/**设置明亮风格 正常 or 明亮 默认正常*/
void setBrightStyle(BrightStyle. style);
/**是否可以触摸*/
bool isTouchEnabled() const;
/**是否被选中 默认false*/
bool isFocused() const;
/**是否被选中 默认false*/
void setFocused(bool fucosed);
/**获取此控件距离左节点的下边界长度*/
float getLeftInParent();
/**获取此控件距离父节点的下边界长度*/
float getBottomInParent();
/**获取此控件距离父节点的右边界长度*/
float getRightInParent();
/**获取此控件距离父节点的上边界长度*/
float getTopInParent();
/**获取一个孩子从容器根据它的名字*/
virtual Widget* getChildByName(const char* name);
/**渲染重载*/
virtual void visit(cocos2d::Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override;
/**添加回调事件*/
void addTouchEventListener(SEL_TouchEvent selector);
/**打印属性 debug*/
virtual void logInfo(WidgetInfoType _type);
//cocos2d 属性
/**
基类有的函数为virtual 有的不是
不带virtual 的是所有子类都拥有这个函数的功能,
电脑资料
《[Cocos2dx]源码分析之 2d/ui/Widget》(https://www.unjs.com)。带virtual 的是拥有上述功能之外 重载父类的函数 想拓展功能用的。
带virtual 的函数可以使用多态 即父类指针指向子类成员
*/
/**
*更改在OpenGL坐标系中widget的位置(X,Y)
*原来的点(0,0)是在屏幕的左下角。
*@参数位置在OpenGL坐标widget的位置(X,Y)
*/
virtual void setPosition(const Point &pos) override;
/**同上绝对位置变为百分比*/
void setPositionPercent(const Point &percent);
/**得到在OpenGL坐标系中widget的百分比位置(X,Y)*/
const Point& getPositionPercent();
/**设置位置类型*/
void setPositionType(PositionType type);
/**得到位置类型*/
PositionType getPositionType() const;
/**设置该widget是否水平翻转*/
virtual void setFlippedX(bool flippedX);
/**该widget是否水平翻转*/
virtual bool isFlippedX(){return _flippedX;};
/**该widget是否垂直翻转*/
virtual void setFlippedY(bool flippedY);
/**该widget是否垂直翻转*/
virtual bool isFlippedY(){return _flippedY;};
/**设置颜色*/
virtual void setColor(const Color3B& color) override;
/**设置透明度*/
virtual void setOpacity(GLubyte opacity) override;
/**得到颜色值*/
const Color3B& getColor() const override {return _color;};
/**得到透明度*/
GLubyte getOpacity() const override {return _opacity;};
/**弃用的属性*/
/** @deprecated Use isFlippedX() instead */
CC_DEPRECATED_ATTRIBUTE bool isFlipX() { return isFlippedX(); };
/** @deprecated Use setFlippedX() instead */
CC_DEPRECATED_ATTRIBUTE void setFlipX(bool flipX) { setFlippedX(flipX); };
/** @deprecated Use isFlippedY() instead */
CC_DEPRECATED_ATTRIBUTE bool isFlipY() { return isFlippedY(); };
/** @deprecated Use setFlippedY() instead */
CC_DEPRECATED_ATTRIBUTE void setFlipY(bool flipY) { setFlippedY(flipY); };
/**当该widget失去焦点会调用*/
void didNotSelectSelf();
/**检查一个点是否在父节点区域*/
bool clippingParentAreaContainPoint(const Point &pt);
/** 发送触摸事件到widget的父节点*/
virtual void checkChildInfo(int handleState,Widget* sender,const Point &touchPoint);
/** Gets the touch began point of widget when widget is selected.*/
const Point& getTouchStartPos();
/** Gets the touch move point of widget when widget is selected*/
const Point& getTouchMovePos();
/** Gets the touch end point of widget when widget is selected.*/
const Point& getTouchEndPos();
/**设置name*/
void setName(const char* name);
/**得到name*/
const char* getName() const;
/**得到控件类型*/
WidgetType getWidgetType() const;
/**设置控件大小*/
virtual void setSize(const Size &size);
virtual void setSizeW(const int &sizeW);
virtual void setSizeH(const int &sizeH);
/**设置size百分比*/
virtual void setSizePercent(const Point &percent);
/**size类型*/
void setSizeType(SizeType type);
/**得到size类型*/
SizeType getSizeType() const;
/**得到size*/
const Size& getSize() const;
/**得到Customsize*/
const Size& getCustomSize() const;
/**得到layout size*/
virtual const Size& getLayoutSize() {return _size;};//虚函数 供子类重写?
/**得到size百分比*/
const Point& getSizePercent() const;
/***/
virtual bool hitTest(const Point &pt);
/**触摸事件*/
virtual bool onTouchBegan(Touch *touch, Event *unusedEvent);
virtual void onTouchMoved(Touch *touch, Event *unusedEvent);
virtual void onTouchEnded(Touch *touch, Event *unusedEvent);
virtual void onTouchCancelled(Touch *touch, Event *unusedEvent);
/**设置布局参数*/
void setLayoutParameter(LayoutParameter* parameter);
/**得到布局参数*/
LayoutParameter* getLayoutParameter(LayoutParameterType type);
/**是否忽略size使用texture size 默认true*/
virtual void ignoreContentAdaptWithSize(bool ignore);
/**是否忽略size使用texture size*/
bool isIgnoreContentAdaptWithSize() const;
/**得到世界位置*/
Point getWorldPosition();
/**得到虚拟渲染器 例如 一个button的虚拟渲染器是他的 texture renderer 贴图渲染器*/
virtual Node* getVirtualRenderer();
/**得到虚拟渲染器的size 默认是_contentSize 不同的子类有自己的重载*/
virtual const Size& getVirtualRendererSize() const;
/**
* 得到改控件对应的"class name".
*/
virtual std::string getDescription() const override;
/**克隆替身*/
Widget* clone();
virtual void onEnter() override;
virtual void onExit() override;
/**更新size和position*/
void updateSizeAndPosition();
void updateSizeAndPosition(const Size& parentSize);
/*temp action*/
void setActionTag(int tag);
int getActionTag();
CC_CONSTRUCTOR_ACCESS:
//initializes state of widget.
virtual bool init() override;
protected:
//call back function called when size changed.
virtual void onSizeChanged();
//initializes renderer of widget.
virtual void initRenderer();
//回复正常调用.
virtual void onPressStateChangedToNormal();
//选中会调用
virtual void onPressStateChangedToPressed();
//变暗会调用
virtual void onPressStateChangedToDisabled();
//不同的事件
void pushDownEvent();
void moveEvent();
void releaseUpEvent();
void cancelUpEvent();
//空的虚函数为了多态
virtual void updateTextureColor(){};
virtual void updateTextureOpacity(){};
virtual void updateTextureRGBA(){};
virtual void updateFlippedX(){};
virtual void updateFlippedY(){};
void updateColorToRenderer(Node* renderer);
void updateOpacityToRenderer(Node* renderer);
void updateRGBAToRenderer(Node* renderer);
void copyProperties(Widget* model);
virtual Widget* createCloneInstance();
virtual void copySpecialProperties(Widget* model);
virtual void copyClonedWidgetChildren(Widget* model);
Widget* getWidgetParent();
void updateContentSizeWithTextureSize(const Size& size);
/**多态 适用于所有的子类重载 实现自己的逻辑*/
virtual void adaptRenderers(){};
protected:
bool _enabled; ///< 是否可见&&可触摸 最高属性
bool _bright; ///< 是否光亮
bool _touchEnabled; ///< 是否响应触摸
bool _touchPassedEnabled; ///< 触摸时间是不是按下
bool _focus; ///< 是不是焦点
BrightStyle. _brightStyle; ///< 明亮风格
Point _touchStartPos; ///< touch began point
Point _touchMovePos; ///< touch moved point
Point _touchEndPos; ///< touch ended point
SEL_TouchEvent _touchEventSelector;///回调函数类型
std::string _name;///类名
WidgetType _widgetType;///控件类型
int _actionTag;///执行动作的tag
Size _size;///untransformed size
Size _customSize;///自定义size
bool _ignoreSize;///是否忽略自定义的大小 而取资源原本的大小 untransformed img size 默认_size = _customSize
bool _affectByClipping;
SizeType _sizeType;///size 类型
Point _sizePercent;///相对父节点size百分比
PositionType _positionType;///位置类型
Point _positionPercent;///相对父节点的位置百分比
bool _reorderWidgetChildDirty;
bool _hitted;
EventListenerTouchOneByOne* _touchListener;///单点触摸
Color3B _color;///颜色值
GLubyte _opacity;///透明度
bool _flippedX;//是否镜像X轴
bool _flippedY;//是否镜像Y轴
Map
};
}
NS_CC_END
#endif /* defined(__Widget__) */