forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.js
More file actions
294 lines (263 loc) · 8.41 KB
/
Copy pathShape.js
File metadata and controls
294 lines (263 loc) · 8.41 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var Line = require('../../geom/line/Line');
/**
* @classdesc
* The Shape Game Object is a base class for the various different shapes, such as the Arc, Star or Polygon.
* You cannot add a Shape directly to your Scene, it is meant as a base for your own custom Shape classes.
*
* @class Shape
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.13.0
*
* @extends Phaser.GameObjects.Components.AlphaSingle
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.ComputedSize
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {string} [type] - The internal type of the Shape.
* @param {any} [data] - The data of the source shape geometry, if any.
*/
var Shape = new Class({
Extends: GameObject,
Mixins: [
Components.AlphaSingle,
Components.BlendMode,
Components.ComputedSize,
Components.Depth,
Components.GetBounds,
Components.Mask,
Components.Origin,
Components.Pipeline,
Components.ScrollFactor,
Components.Transform,
Components.Visible
],
initialize:
function Shape (scene, type, data)
{
if (type === undefined) { type = 'Shape'; }
GameObject.call(this, scene, type);
/**
* The source Shape data. Typically a geometry object.
* You should not manipulate this directly.
*
* @name Phaser.GameObjects.Shape#geom
* @type {any}
* @readonly
* @since 3.13.0
*/
this.geom = data;
/**
* Holds the polygon path data for filled rendering.
*
* @name Phaser.GameObjects.Shape#pathData
* @type {number[]}
* @readonly
* @since 3.13.0
*/
this.pathData = [];
/**
* Holds the earcut polygon path index data for filled rendering.
*
* @name Phaser.GameObjects.Shape#pathIndexes
* @type {integer[]}
* @readonly
* @since 3.13.0
*/
this.pathIndexes = [];
/**
* The fill color used by this Shape.
*
* @name Phaser.GameObjects.Shape#fillColor
* @type {number}
* @since 3.13.0
*/
this.fillColor = 0xffffff;
/**
* The fill alpha value used by this Shape.
*
* @name Phaser.GameObjects.Shape#fillAlpha
* @type {number}
* @since 3.13.0
*/
this.fillAlpha = 1;
/**
* The stroke color used by this Shape.
*
* @name Phaser.GameObjects.Shape#strokeColor
* @type {number}
* @since 3.13.0
*/
this.strokeColor = 0xffffff;
/**
* The stroke alpha value used by this Shape.
*
* @name Phaser.GameObjects.Shape#strokeAlpha
* @type {number}
* @since 3.13.0
*/
this.strokeAlpha = 1;
/**
* The stroke line width used by this Shape.
*
* @name Phaser.GameObjects.Shape#lineWidth
* @type {number}
* @since 3.13.0
*/
this.lineWidth = 1;
/**
* Controls if this Shape is filled or not.
* Note that some Shapes do not support being filled (such as Line shapes)
*
* @name Phaser.GameObjects.Shape#isFilled
* @type {boolean}
* @since 3.13.0
*/
this.isFilled = false;
/**
* Controls if this Shape is stroked or not.
* Note that some Shapes do not support being stroked (such as Iso Box shapes)
*
* @name Phaser.GameObjects.Shape#isStroked
* @type {boolean}
* @since 3.13.0
*/
this.isStroked = false;
/**
* Controls if this Shape path is closed during rendering when stroked.
* Note that some Shapes are always closed when stroked (such as Ellipse shapes)
*
* @name Phaser.GameObjects.Shape#closePath
* @type {boolean}
* @since 3.13.0
*/
this.closePath = true;
/**
* Private internal value.
* A Line used when parsing internal path data to avoid constant object re-creation.
*
* @name Phaser.GameObjects.Curve#_tempLine
* @type {Phaser.Geom.Line}
* @private
* @since 3.13.0
*/
this._tempLine = new Line();
this.initPipeline();
},
/**
* Sets the fill color and alpha for this Shape.
*
* If you wish for the Shape to not be filled then call this method with no arguments, or just set `isFilled` to `false`.
*
* Note that some Shapes do not support fill colors, such as the Line shape.
*
* This call can be chained.
*
* @method Phaser.GameObjects.Shape#setFillStyle
* @since 3.13.0
*
* @param {number} [color] - The color used to fill this shape. If not provided the Shape will not be filled.
* @param {number} [alpha=1] - The alpha value used when filling this shape, if a fill color is given.
*
* @return {this} This Game Object instance.
*/
setFillStyle: function (color, alpha)
{
if (alpha === undefined) { alpha = 1; }
if (color === undefined)
{
this.isFilled = false;
}
else
{
this.fillColor = color;
this.fillAlpha = alpha;
this.isFilled = true;
}
return this;
},
/**
* Sets the stroke color and alpha for this Shape.
*
* If you wish for the Shape to not be stroked then call this method with no arguments, or just set `isStroked` to `false`.
*
* Note that some Shapes do not support being stroked, such as the Iso Box shape.
*
* This call can be chained.
*
* @method Phaser.GameObjects.Shape#setStrokeStyle
* @since 3.13.0
*
* @param {number} [lineWidth] - The width of line to stroke with. If not provided or undefined the Shape will not be stroked.
* @param {number} [color] - The color used to stroke this shape. If not provided the Shape will not be stroked.
* @param {number} [alpha=1] - The alpha value used when stroking this shape, if a stroke color is given.
*
* @return {this} This Game Object instance.
*/
setStrokeStyle: function (lineWidth, color, alpha)
{
if (alpha === undefined) { alpha = 1; }
if (lineWidth === undefined)
{
this.isStroked = false;
}
else
{
this.lineWidth = lineWidth;
this.strokeColor = color;
this.strokeAlpha = alpha;
this.isStroked = true;
}
return this;
},
/**
* Sets if this Shape path is closed during rendering when stroked.
* Note that some Shapes are always closed when stroked (such as Ellipse shapes)
*
* This call can be chained.
*
* @method Phaser.GameObjects.Shape#setClosePath
* @since 3.13.0
*
* @param {boolean} value - Set to `true` if the Shape should be closed when stroked, otherwise `false`.
*
* @return {this} This Game Object instance.
*/
setClosePath: function (value)
{
this.closePath = value;
return this;
},
/**
* Internal destroy handler, called as part of the destroy process.
*
* @method Phaser.GameObjects.Shape#preDestroy
* @protected
* @since 3.13.0
*/
preDestroy: function ()
{
this.geom = null;
this._tempLine = null;
this.pathData = [];
this.pathIndexes = [];
}
});
module.exports = Shape;