-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathGGraph.cs
More file actions
275 lines (246 loc) · 9.56 KB
/
GGraph.cs
File metadata and controls
275 lines (246 loc) · 9.56 KB
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
using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI.Utils;
namespace FairyGUI
{
/// <summary>
/// GGraph class.
/// 对应编辑器里的图形对象。图形有两个用途,一是用来显示简单的图形,例如矩形等;二是作为一个占位的用途,
/// 可以将本对象替换为其他对象,或者在它的前后添加其他对象,相当于一个位置和深度的占位;还可以直接将内容设置
/// 为原生对象。
/// </summary>
public class GGraph : GObject, IColorGear
{
Shape _shape;
public GGraph()
{
}
override protected void CreateDisplayObject()
{
_shape = new Shape();
_shape.gOwner = this;
displayObject = _shape;
}
/// <summary>
/// Replace this object to another object in the display list.
/// 在显示列表中,将指定对象取代这个图形对象。这个图形对象相当于一个占位的用途。
/// </summary>
/// <param name="target">Target object.</param>
public void ReplaceMe(GObject target)
{
if (parent == null)
throw new Exception("parent not set");
target.name = this.name;
target.alpha = this.alpha;
target.rotation = this.rotation;
target.visible = this.visible;
target.touchable = this.touchable;
target.grayed = this.grayed;
target.SetXY(this.x, this.y);
target.SetSize(this.width, this.height);
int index = parent.GetChildIndex(this);
parent.AddChildAt(target, index);
target.relations.CopyFrom(this.relations);
parent.RemoveChild(this, true);
}
/// <summary>
/// Add another object before this object.
/// 在显示列表中,将另一个对象插入到这个对象的前面。
/// </summary>
/// <param name="target">Target object.</param>
public void AddBeforeMe(GObject target)
{
if (parent == null)
throw new Exception("parent not set");
int index = parent.GetChildIndex(this);
parent.AddChildAt(target, index);
}
/// <summary>
/// Add another object after this object.
/// 在显示列表中,将另一个对象插入到这个对象的后面。
/// </summary>
/// <param name="target">Target object.</param>
public void AddAfterMe(GObject target)
{
if (parent == null)
throw new Exception("parent not set");
int index = parent.GetChildIndex(this);
index++;
parent.AddChildAt(target, index);
}
/// <summary>
/// 设置内容为一个原生对象。这个图形对象相当于一个占位的用途。
/// </summary>
/// <param name="obj">原生对象</param>
public void SetNativeObject(DisplayObject obj)
{
if (displayObject == obj)
return;
if (_shape != null)
{
if (_shape.parent != null)
_shape.parent.RemoveChild(displayObject, true);
else
_shape.Dispose();
_shape.gOwner = null;
_shape = null;
}
if (displayObject != null) displayObject.Dispose();
displayObject = obj;
if (displayObject != null)
{
displayObject.alpha = this.alpha;
displayObject.rotation = this.rotation;
displayObject.visible = this.visible;
displayObject.touchable = this.touchable;
displayObject.gOwner = this;
}
if (parent != null)
parent.ChildStateChanged(this);
HandlePositionChanged();
}
/// <summary>
///
/// </summary>
public Color color
{
get
{
if (_shape != null)
return _shape.color;
else
return Color.clear;
}
set
{
if (_shape != null && _shape.color != value)
{
_shape.color = value;
UpdateGear(4);
}
}
}
/// <summary>
/// Get the shape object. It can be used for drawing.
/// 获取图形的原生对象,可用于绘制图形。
/// </summary>
public Shape shape
{
get { return _shape; }
}
/// <summary>
/// Draw a rectangle.
/// 画矩形。
/// </summary>
/// <param name="aWidth">Width</param>
/// <param name="aHeight">Height</param>
/// <param name="lineSize">Line size</param>
/// <param name="lineColor">Line color</param>
/// <param name="fillColor">Fill color</param>
public void DrawRect(float aWidth, float aHeight, int lineSize, Color lineColor, Color fillColor)
{
this.SetSize(aWidth, aHeight);
_shape.DrawRect(lineSize, lineColor, fillColor);
}
/// <summary>
///
/// </summary>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
/// <param name="fillColor"></param>
/// <param name="corner"></param>
public void DrawRoundRect(float aWidth, float aHeight, Color fillColor, float[] corner)
{
this.SetSize(aWidth, aHeight);
this.shape.DrawRoundRect(0, Color.white, fillColor, corner[0], corner[1], corner[2], corner[3]);
}
/// <summary>
///
/// </summary>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
/// <param name="fillColor"></param>
public void DrawEllipse(float aWidth, float aHeight, Color fillColor)
{
this.SetSize(aWidth, aHeight);
_shape.DrawEllipse(fillColor);
}
/// <summary>
///
/// </summary>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
/// <param name="points"></param>
/// <param name="fillColor"></param>
public void DrawPolygon(float aWidth, float aHeight, IList<Vector2> points, Color fillColor)
{
this.SetSize(aWidth, aHeight);
_shape.DrawPolygon(points, fillColor);
}
/// <summary>
///
/// </summary>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
/// <param name="points"></param>
/// <param name="fillColor"></param>
/// <param name="lineSize"></param>
/// <param name="lineColor"></param>
public void DrawPolygon(float aWidth, float aHeight, IList<Vector2> points, Color fillColor, float lineSize, Color lineColor)
{
this.SetSize(aWidth, aHeight);
_shape.DrawPolygon(points, fillColor, lineSize, lineColor);
}
override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
{
base.Setup_BeforeAdd(buffer, beginPos);
buffer.Seek(beginPos, 5);
int type = buffer.ReadByte();
if (type != 0)
{
int lineSize = buffer.ReadInt();
Color lineColor = buffer.ReadColor();
Color fillColor = buffer.ReadColor();
bool roundedRect = buffer.ReadBool();
Vector4 cornerRadius = new Vector4();
if (roundedRect)
{
for (int i = 0; i < 4; i++)
cornerRadius[i] = buffer.ReadFloat();
}
if (type == 1)
{
if (roundedRect)
_shape.DrawRoundRect(lineSize, lineColor, fillColor, cornerRadius.x, cornerRadius.y, cornerRadius.z, cornerRadius.w);
else
_shape.DrawRect(lineSize, lineColor, fillColor);
}
else if (type == 2)
_shape.DrawEllipse(lineSize, fillColor, lineColor, fillColor, 0, 360);
else if (type == 3)
{
int cnt = buffer.ReadShort() / 2;
Vector2[] points = new Vector2[cnt];
for (int i = 0; i < cnt; i++)
points[i].Set(buffer.ReadFloat(), buffer.ReadFloat());
_shape.DrawPolygon(points, fillColor, lineSize, lineColor);
}
else if (type == 4)
{
int sides = buffer.ReadShort();
float startAngle = buffer.ReadFloat();
int cnt = buffer.ReadShort();
float[] distances = null;
if (cnt > 0)
{
distances = new float[cnt];
for (int i = 0; i < cnt; i++)
distances[i] = buffer.ReadFloat();
}
_shape.DrawRegularPolygon(sides, lineSize, fillColor, lineColor, fillColor, startAngle, distances);
}
}
}
}
}