-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGGraph.cpp
296 lines (263 loc) · 7.96 KB
/
GGraph.cpp
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
287
288
289
290
291
292
293
294
295
296
#include "GGraph.h"
#include "utils/ByteBuffer.h"
#include "utils/ToolSet.h"
NS_FGUI_BEGIN
USING_NS_CC;
static void drawVertRect(cocos2d::DrawNode* shape, float x, float y, float width, float height, const cocos2d::Color4F& color)
{
float mx = x + width;
float my = y + height;
shape->drawTriangle(Vec2(x, y), Vec2(mx, y), Vec2(x, my), color);
shape->drawTriangle(Vec2(mx, y), Vec2(mx, my), Vec2(x, my), color);
}
GGraph::GGraph() : _shape(nullptr),
_type(0),
_lineSize(1),
_lineColor(Color4F::BLACK),
_fillColor(Color4F::WHITE),
_cornerRadius(nullptr),
_polygonPoints(nullptr),
_distances(nullptr)
{
_touchDisabled = true;
}
GGraph::~GGraph()
{
CC_SAFE_DELETE(_cornerRadius);
CC_SAFE_DELETE(_polygonPoints);
CC_SAFE_DELETE(_distances);
}
void GGraph::handleInit()
{
_shape = DrawNode::create();
_shape->retain();
_displayObject = _shape;
}
void GGraph::drawRect(float aWidth, float aHeight, int lineSize, const cocos2d::Color4F& lineColor, const cocos2d::Color4F& fillColor)
{
_type = 0; //avoid updateshape call in handleSizeChange
setSize(aWidth, aHeight);
_type = 1;
_lineSize = lineSize;
_lineColor = lineColor;
_fillColor = fillColor;
updateShape();
}
void GGraph::drawEllipse(float aWidth, float aHeight, int lineSize, const cocos2d::Color4F& lineColor, const cocos2d::Color4F& fillColor)
{
_type = 0; //avoid updateshape call in handleSizeChange
setSize(aWidth, aHeight);
_type = 2;
_lineSize = lineSize;
_lineColor = lineColor;
_fillColor = fillColor;
updateShape();
}
void GGraph::drawPolygon(int lineSize, const cocos2d::Color4F& lineColor, const cocos2d::Color4F& fillColor, const cocos2d::Vec2* points, int count)
{
_type = 3;
_lineSize = lineSize;
_lineColor = lineColor;
_fillColor = fillColor;
setPolygonPoints(points, count);
}
void GGraph::drawRegularPolygon(int lineSize, const cocos2d::Color4F& lineColor, const cocos2d::Color4F& fillColor,
int sides, float startAngle, const float* distances, int count)
{
_type = 4;
_lineSize = lineSize;
_lineColor = lineColor;
_fillColor = fillColor;
_sides = sides;
_startAngle = startAngle;
if (distances != nullptr)
{
if (_distances == nullptr)
_distances = new std::vector<float>();
else
_distances->clear();
for (int i = 0; i < count; i++)
_distances->push_back(*(distances + i));
}
else if (_distances != nullptr)
_distances->clear();
updateShape();
}
void GGraph::updateShape()
{
_shape->clear();
if (_type == 0)
{
_touchDisabled = false;
return;
}
switch (_type)
{
case 1:
{
if (_lineSize > 0)
{
float wl = _size.width - _lineSize;
float hl = _size.height - _lineSize;
drawVertRect(_shape, 0, 0, wl, _lineSize, _lineColor);
drawVertRect(_shape, wl, 0, _lineSize, hl, _lineColor);
drawVertRect(_shape, _lineSize, hl, wl, _lineSize, _lineColor);
drawVertRect(_shape, 0, _lineSize, _lineSize, hl, _lineColor);
drawVertRect(_shape, _lineSize, _lineSize, _size.width - _lineSize * 2, _size.height - _lineSize * 2, _fillColor);
}
else
drawVertRect(_shape, 0, 0, _size.width, _size.height, _fillColor);
break;
}
case 2:
{
if (_lineSize > 0)
{
_shape->setLineWidth(_lineSize);
_shape->drawCircle(Vec2(_size.width / 2, _size.height / 2), _size.width / 2, 0, 360, false, 1, _size.height / _size.width, _lineColor);
}
_shape->drawSolidCircle(Vec2(_size.width / 2, _size.height / 2), _size.width / 2, 0, 360, 1, _size.height / _size.width, _fillColor);
break;
}
case 3:
{
_shape->drawPolygon(_polygonPoints->data(), (int)_polygonPoints->size(), _fillColor, _lineSize * 0.5f, _lineColor);
break;
}
case 4:
{
float h = getHeight();
_polygonPointOffset = h;
if (_polygonPoints == nullptr)
_polygonPoints = new std::vector<Vec2>();
else
_polygonPoints->clear();
float radius = MIN(getWidth(), getHeight()) * 0.5f;
float angle = MATH_DEG_TO_RAD(_startAngle);
float deltaAngle = 2 * M_PI / _sides;
float dist;
for (int i = 0; i < _sides; i++)
{
if (_distances != nullptr && i < (int)_distances->size())
dist = (*_distances)[i];
else
dist = 1;
float xv = radius + radius * dist * cos(angle);
float yv = h - (radius + radius * dist * sin(angle));
_polygonPoints->push_back(Vec2(xv, yv));
angle += deltaAngle;
}
_shape->drawPolygon(_polygonPoints->data(), (int)_polygonPoints->size(), _fillColor, _lineSize * 0.5f, _lineColor);
break;
}
}
}
cocos2d::Color3B GGraph::getColor() const
{
return (Color3B)_fillColor;
}
void GGraph::setColor(const cocos2d::Color3B& value)
{
_fillColor = Color4F(value, _fillColor.a);
updateShape();
}
void GGraph::setPolygonPoints(const cocos2d::Vec2* points, int count)
{
if (_polygonPoints == nullptr)
_polygonPoints = new std::vector<Vec2>();
else
_polygonPoints->clear();
float h = getHeight();
_polygonPointOffset = h;
for (int i = 0; i < count; i++)
{
Vec2 pt = *(points + i);
pt.y = h - pt.y;
_polygonPoints->push_back(pt);
}
updateShape();
}
cocos2d::Value GGraph::getProp(ObjectPropID propId)
{
switch (propId)
{
case ObjectPropID::Color:
return Value(ToolSet::colorToInt(getColor()));
default:
return GObject::getProp(propId);
}
}
void GGraph::setProp(ObjectPropID propId, const cocos2d::Value& value)
{
switch (propId)
{
case ObjectPropID::Color:
setColor(ToolSet::intToColor(value.asUnsignedInt()));
break;
default:
GObject::setProp(propId, value);
break;
}
}
void GGraph::handleSizeChanged()
{
GObject::handleSizeChanged();
if (_type == 3 || _type == 4)
{
float h = getHeight();
int count = (int)_polygonPoints->size();
for (int i = 0; i < count; i++)
{
Vec2 pt = (*_polygonPoints)[i];
pt.y = h - (_polygonPointOffset - pt.y);
(*_polygonPoints)[i] = pt;
}
_polygonPointOffset = h;
}
updateShape();
}
void GGraph::setup_beforeAdd(ByteBuffer* buffer, int beginPos)
{
GObject::setup_beforeAdd(buffer, beginPos);
buffer->seek(beginPos, 5);
_type = buffer->readByte();
if (_type != 0)
{
_lineSize = buffer->readInt();
_lineColor = (Color4F)buffer->readColor();
_fillColor = (Color4F)buffer->readColor();
if (buffer->readBool())
{
_cornerRadius = new float[4];
for (int i = 0; i < 4; i++)
_cornerRadius[i] = buffer->readFloat();
}
if (_type == 3)
{
int cnt = buffer->readShort() / 2;
_polygonPoints = new std::vector<Vec2>(cnt);
float h = getHeight();
_polygonPointOffset = h;
for (int i = 0; i < cnt; i++)
{
float f1 = buffer->readFloat();
float f2 = h - buffer->readFloat();
(*_polygonPoints)[i] = Vec2(f1, f2);
}
}
else if (_type == 4)
{
_sides = buffer->readShort();
_startAngle = buffer->readFloat();
int cnt = buffer->readShort();
if (cnt > 0)
{
_distances = new std::vector<float>(cnt);
for (int i = 0; i < cnt; i++)
(*_distances)[i] = buffer->readFloat();
}
}
updateShape();
}
}
NS_FGUI_END