/
GraphicsComponent.ts
421 lines (370 loc) · 12.1 KB
/
GraphicsComponent.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { Vector, vec } from '../Math/vector';
import { Graphic } from './Graphic';
import { HasTick } from './Animation';
import { ExcaliburGraphicsContext } from './Context/ExcaliburGraphicsContext';
import { BoundingBox } from '../Collision/Index';
import { Component } from '../EntityComponentSystem/Component';
import { Material } from './Context/material';
import { Logger } from '../Util/Log';
import { WatchVector } from '../Math/watch-vector';
import { TransformComponent } from '../EntityComponentSystem';
import { GraphicsGroup } from '../Graphics/GraphicsGroup';
/**
* Type guard for checking if a Graphic HasTick (used for graphics that change over time like animations)
* @param graphic
*/
export function hasGraphicsTick(graphic: Graphic): graphic is Graphic & HasTick {
return !!(graphic as unknown as HasTick).tick;
}
export interface GraphicsShowOptions {
offset?: Vector;
anchor?: Vector;
}
export interface GraphicsComponentOptions {
onPostDraw?: (ex: ExcaliburGraphicsContext, elapsed: number) => void;
onPreDraw?: (ex: ExcaliburGraphicsContext, elapsed: number) => void;
onPreTransformDraw?: (ex: ExcaliburGraphicsContext, elapsed: number) => void;
onPostTransformDraw?: (ex: ExcaliburGraphicsContext, elapsed: number) => void;
/**
* Name of current graphic to use
*/
current?: string;
/**
* Optionally set a material to use on the graphic
*/
material?: Material;
/**
* Optionally copy instances of graphics by calling .clone(), you may set this to false to avoid sharing graphics when added to the
* component for performance reasons. By default graphics are not copied and are shared when added to the component.
*/
copyGraphics?: boolean;
/**
* Optional visible flag, if the graphics component is not visible it will not be displayed
*/
visible?: boolean;
/**
* Optional opacity
*/
opacity?: number;
/**
* List of graphics and optionally the options per graphic
*/
graphics?: { [graphicName: string]: Graphic | { graphic: Graphic; options: GraphicsShowOptions } };
/**
* Optional offset in absolute pixels to shift all graphics in this component from each graphic's anchor (default is top left corner)
*/
offset?: Vector;
/**
* Optional anchor
*/
anchor?: Vector;
}
/**
* Component to manage drawings, using with the position component
*/
export class GraphicsComponent extends Component {
private _logger = Logger.getInstance();
private _current: string = 'default';
private _graphics: Record<string, Graphic> = {};
private _options: Record<string, GraphicsShowOptions> = {};
public material: Material | null = null;
/**
* Draws after the entity transform has been applied, but before graphics component graphics have been drawn
*/
public onPreDraw: (ctx: ExcaliburGraphicsContext, elapsedMilliseconds: number) => void;
/**
* Draws after the entity transform has been applied, and after graphics component graphics has been drawn
*/
public onPostDraw: (ctx: ExcaliburGraphicsContext, elapsedMilliseconds: number) => void;
/**
* Draws before the entity transform has been applied before any any graphics component drawing
*/
public onPreTransformDraw: (ctx: ExcaliburGraphicsContext, elapsedMilliseconds: number) => void;
/**
* Draws after the entity transform has been applied, and after all graphics component drawing
*/
public onPostTransformDraw: (ctx: ExcaliburGraphicsContext, elapsedMilliseconds: number) => void;
/**
* Sets or gets wether any drawing should be visible in this component
*/
public visible: boolean = true;
/**
* Sets or gets wither all drawings should have an opacity applied
*/
public opacity: number = 1;
private _offset: Vector = Vector.Zero;
/**
* Offset to apply to graphics by default
*/
public get offset(): Vector {
return new WatchVector(this._offset, () => {
this.recalculateBounds();
});
}
public set offset(value: Vector) {
this._offset = value;
this.recalculateBounds();
}
private _anchor: Vector = Vector.Half;
/**
* Anchor to apply to graphics by default
*/
public get anchor(): Vector {
return new WatchVector(this._anchor, () => {
this.recalculateBounds();
});
}
public set anchor(value: Vector) {
this._anchor = value;
this.recalculateBounds();
}
/**
* Flip all graphics horizontally along the y-axis
*/
public flipHorizontal: boolean = false;
/**
* Flip all graphics vertically along the x-axis
*/
public flipVertical: boolean = false;
/**
* If set to true graphics added to the component will be copied. This can effect performance, but is useful if you don't want
* changes to a graphic to effect all the places it is used.
*/
public copyGraphics: boolean = false;
constructor(options?: GraphicsComponentOptions) {
super();
// Defaults
options = {
visible: this.visible,
graphics: {},
...options
};
const {
current,
anchor,
opacity,
visible,
graphics,
offset,
copyGraphics,
onPreDraw,
onPostDraw,
onPreTransformDraw,
onPostTransformDraw
} = options;
for (const [key, graphicOrOptions] of Object.entries(graphics)) {
if (graphicOrOptions instanceof Graphic) {
this._graphics[key] = graphicOrOptions;
} else {
this._graphics[key] = graphicOrOptions.graphic;
this._options[key] = graphicOrOptions.options;
}
}
this.offset = offset ?? this.offset;
this.opacity = opacity ?? this.opacity;
this.anchor = anchor ?? this.anchor;
this.copyGraphics = copyGraphics ?? this.copyGraphics;
this.onPreDraw = onPreDraw ?? this.onPreDraw;
this.onPostDraw = onPostDraw ?? this.onPostDraw;
this.onPreDraw = onPreTransformDraw ?? this.onPreTransformDraw;
this.onPostTransformDraw = onPostTransformDraw ?? this.onPostTransformDraw;
this.visible = !!visible;
this._current = current ?? this._current;
if (current && this._graphics[current]) {
this.use(current);
}
}
public getGraphic(name: string): Graphic | undefined {
return this._graphics[name];
}
public getOptions(name: string): GraphicsShowOptions | undefined {
return this._options[name];
}
/**
* Get registered graphics names
*/
public getNames(): string[] {
return Object.keys(this._graphics);
}
/**
* Returns the currently displayed graphic
*/
public get current(): Graphic | undefined {
return this._graphics[this._current];
}
/**
* Returns the currently displayed graphic offsets
*/
public get currentOptions(): GraphicsShowOptions | undefined {
return this._options[this._current];
}
/**
* Returns all graphics associated with this component
*/
public get graphics(): { [graphicName: string]: Graphic } {
return this._graphics;
}
/**
* Returns all graphics options associated with this component
*/
public get options(): { [graphicName: string]: GraphicsShowOptions } {
return this._options;
}
/**
* Adds a named graphic to this component, if the name is "default" or not specified, it will be shown by default without needing to call
* @param graphic
*/
public add(graphic: Graphic, options?: GraphicsShowOptions): Graphic;
public add(name: string, graphic: Graphic, options?: GraphicsShowOptions): Graphic;
public add(nameOrGraphic: string | Graphic, graphicOrOptions?: Graphic | GraphicsShowOptions, options?: GraphicsShowOptions): Graphic {
let name = 'default';
let graphicToSet: Graphic = null;
let optionsToSet: GraphicsShowOptions | undefined = undefined;
if (typeof nameOrGraphic === 'string' && graphicOrOptions instanceof Graphic) {
name = nameOrGraphic;
graphicToSet = graphicOrOptions;
optionsToSet = options;
}
if (nameOrGraphic instanceof Graphic && !(graphicOrOptions instanceof Graphic)) {
graphicToSet = nameOrGraphic;
optionsToSet = graphicOrOptions;
}
this._graphics[name] = this.copyGraphics ? graphicToSet.clone() : graphicToSet;
this._options[name] = this.copyGraphics ? { ...optionsToSet } : optionsToSet;
if (name === 'default') {
this.use('default');
}
return graphicToSet;
}
/**
* Removes a registered graphic, if the removed graphic is the current it will switch to the default
* @param name
*/
public remove(name: string) {
delete this._graphics[name];
delete this._options[name];
if (this._current === name) {
this._current = 'default';
this.recalculateBounds();
}
}
/**
* Shows a graphic, will be removed
* @param nameOrGraphic
* @param options
* @deprecated will be removed in v0.30.0, use `graphics.use(...)`
*/
public show<T extends Graphic = Graphic>(nameOrGraphic: string | T, options?: GraphicsShowOptions): T {
return this.use(nameOrGraphic, options);
}
/**
* Use a graphic only, will set the default graphic. Returns the new [[Graphic]]
*
* Optionally override the stored options
* @param nameOrGraphic
* @param options
*/
public use<T extends Graphic = Graphic>(nameOrGraphic: string | T, options?: GraphicsShowOptions): T {
if (nameOrGraphic instanceof Graphic) {
let graphic = nameOrGraphic as Graphic;
if (this.copyGraphics) {
graphic = nameOrGraphic.clone();
}
this._current = 'default';
this._graphics[this._current] = graphic;
this._options[this._current] = options;
} else {
this._current = nameOrGraphic;
this._options[this._current] = options;
if (!(this._current in this._graphics)) {
this._logger.warn(
`Graphic ${this._current} is not registered with the graphics component owned by ${this.owner?.name}. Nothing will be drawn.`
);
}
}
this.recalculateBounds();
return this.current as T;
}
/**
* Hide currently shown graphic
*/
public hide(): void {
this._current = 'ex.none';
}
private _localBounds: BoundingBox = null;
public set localBounds(bounds: BoundingBox) {
this._localBounds = bounds;
}
public recalculateBounds() {
let bb = new BoundingBox();
const graphic = this._graphics[this._current];
const options = this._options[this._current];
if (!graphic) {
this._localBounds = bb;
return;
}
let anchor = this.anchor;
let offset = this.offset;
if (options?.anchor) {
anchor = options.anchor;
}
if (options?.offset) {
offset = options.offset;
}
const bounds = graphic.localBounds;
const offsetX = -bounds.width * anchor.x + offset.x;
const offsetY = -bounds.height * anchor.y + offset.y;
if (graphic instanceof GraphicsGroup && !graphic.useAnchor) {
bb = graphic?.localBounds.combine(bb);
} else {
bb = graphic?.localBounds.translate(vec(offsetX, offsetY)).combine(bb);
}
this._localBounds = bb;
}
/**
* Get local bounds of graphics component
*/
public get localBounds(): BoundingBox {
if (!this._localBounds || this._localBounds.hasZeroDimensions()) {
this.recalculateBounds();
}
return this._localBounds;
}
/**
* Get world bounds of graphics component
*/
public get bounds(): BoundingBox {
let bounds = this.localBounds;
if (this.owner) {
const tx = this.owner.get(TransformComponent);
if (tx) {
bounds = bounds.transform(tx.get().matrix);
}
}
return bounds;
}
/**
* Update underlying graphics if necessary, called internally
* @param elapsed
* @internal
*/
public update(elapsed: number, idempotencyToken: number = 0) {
const graphic = this.current;
if (graphic && hasGraphicsTick(graphic)) {
graphic.tick(elapsed, idempotencyToken);
}
}
public clone(): GraphicsComponent {
const graphics = new GraphicsComponent();
graphics._graphics = { ...this._graphics };
graphics._options = { ...this._options };
graphics.offset = this.offset.clone();
graphics.opacity = this.opacity;
graphics.anchor = this.anchor.clone();
graphics.copyGraphics = this.copyGraphics;
graphics.onPreDraw = this.onPreDraw;
graphics.onPostDraw = this.onPostDraw;
graphics.visible = this.visible;
return graphics;
}
}