@vpmedia/phaser 1.114.0 → 1.115.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/dist/index.js +400 -377
  2. package/dist/index.js.map +1 -1
  3. package/dist/phaser/core/animation_manager.d.ts +2 -1
  4. package/dist/phaser/core/animation_manager.d.ts.map +1 -1
  5. package/dist/phaser/core/array_set.d.ts +1 -1
  6. package/dist/phaser/core/array_set.d.ts.map +1 -1
  7. package/dist/phaser/core/cache.d.ts +1 -1
  8. package/dist/phaser/core/cache.d.ts.map +1 -1
  9. package/dist/phaser/core/factory.d.ts +1 -1
  10. package/dist/phaser/core/factory.d.ts.map +1 -1
  11. package/dist/phaser/core/game.d.ts +2 -0
  12. package/dist/phaser/core/game.d.ts.map +1 -1
  13. package/dist/phaser/core/input.d.ts +5 -5
  14. package/dist/phaser/core/input.d.ts.map +1 -1
  15. package/dist/phaser/core/input_event.d.ts +35 -0
  16. package/dist/phaser/core/input_event.d.ts.map +1 -0
  17. package/dist/phaser/core/input_handler.d.ts +2 -2
  18. package/dist/phaser/core/input_handler.d.ts.map +1 -1
  19. package/dist/phaser/core/input_mouse.d.ts +27 -26
  20. package/dist/phaser/core/input_mouse.d.ts.map +1 -1
  21. package/dist/phaser/core/input_mspointer.d.ts +16 -15
  22. package/dist/phaser/core/input_mspointer.d.ts.map +1 -1
  23. package/dist/phaser/core/input_pointer.d.ts +9 -8
  24. package/dist/phaser/core/input_pointer.d.ts.map +1 -1
  25. package/dist/phaser/core/input_touch.d.ts +18 -17
  26. package/dist/phaser/core/input_touch.d.ts.map +1 -1
  27. package/dist/phaser/core/loader.d.ts +64 -30
  28. package/dist/phaser/core/loader.d.ts.map +1 -1
  29. package/dist/phaser/core/scale_manager.d.ts +1 -1
  30. package/dist/phaser/core/scale_manager.d.ts.map +1 -1
  31. package/dist/phaser/core/scene_manager.d.ts +14 -4
  32. package/dist/phaser/core/scene_manager.d.ts.map +1 -1
  33. package/dist/phaser/core/signal.d.ts +8 -8
  34. package/dist/phaser/core/signal.d.ts.map +1 -1
  35. package/dist/phaser/core/signal_binding.d.ts +2 -2
  36. package/dist/phaser/core/signal_binding.d.ts.map +1 -1
  37. package/dist/phaser/core/sound.d.ts +1 -1
  38. package/dist/phaser/core/sound.d.ts.map +1 -1
  39. package/dist/phaser/core/sound_manager.d.ts +3 -3
  40. package/dist/phaser/core/sound_manager.d.ts.map +1 -1
  41. package/dist/phaser/core/sound_sprite.d.ts +1 -1
  42. package/dist/phaser/core/sound_sprite.d.ts.map +1 -1
  43. package/dist/phaser/core/timer.d.ts +5 -5
  44. package/dist/phaser/core/timer.d.ts.map +1 -1
  45. package/dist/phaser/core/timer_event.d.ts +2 -2
  46. package/dist/phaser/core/timer_event.d.ts.map +1 -1
  47. package/dist/phaser/core/tween.d.ts +4 -4
  48. package/dist/phaser/core/tween.d.ts.map +1 -1
  49. package/dist/phaser/core/tween_data.d.ts +2 -1
  50. package/dist/phaser/core/tween_data.d.ts.map +1 -1
  51. package/dist/phaser/core/tween_manager.d.ts +1 -1
  52. package/dist/phaser/core/tween_manager.d.ts.map +1 -1
  53. package/dist/phaser/display/button.d.ts +1 -1
  54. package/dist/phaser/display/button.d.ts.map +1 -1
  55. package/dist/phaser/display/canvas/buffer.d.ts +3 -3
  56. package/dist/phaser/display/canvas/buffer.d.ts.map +1 -1
  57. package/dist/phaser/display/canvas/graphics.d.ts +2 -2
  58. package/dist/phaser/display/canvas/graphics.d.ts.map +1 -1
  59. package/dist/phaser/display/canvas/masker.d.ts +5 -2
  60. package/dist/phaser/display/canvas/masker.d.ts.map +1 -1
  61. package/dist/phaser/display/canvas/renderer.d.ts +6 -5
  62. package/dist/phaser/display/canvas/renderer.d.ts.map +1 -1
  63. package/dist/phaser/display/canvas/tinter.d.ts +2 -1
  64. package/dist/phaser/display/canvas/tinter.d.ts.map +1 -1
  65. package/dist/phaser/display/canvas/util.d.ts +11 -4
  66. package/dist/phaser/display/canvas/util.d.ts.map +1 -1
  67. package/dist/phaser/display/display_object.d.ts +4 -3
  68. package/dist/phaser/display/display_object.d.ts.map +1 -1
  69. package/dist/phaser/display/graphics.d.ts +47 -27
  70. package/dist/phaser/display/graphics.d.ts.map +1 -1
  71. package/dist/phaser/display/graphics_data.d.ts +20 -11
  72. package/dist/phaser/display/graphics_data.d.ts.map +1 -1
  73. package/dist/phaser/display/image.d.ts +4 -3
  74. package/dist/phaser/display/image.d.ts.map +1 -1
  75. package/dist/phaser/display/render_session.d.ts +44 -0
  76. package/dist/phaser/display/render_session.d.ts.map +1 -0
  77. package/dist/phaser/display/sprite_util.d.ts +4 -3
  78. package/dist/phaser/display/sprite_util.d.ts.map +1 -1
  79. package/dist/phaser/display/text.d.ts +64 -30
  80. package/dist/phaser/display/text.d.ts.map +1 -1
  81. package/dist/phaser/display/webgl/base_texture.d.ts +3 -3
  82. package/dist/phaser/display/webgl/base_texture.d.ts.map +1 -1
  83. package/dist/phaser/display/webgl/earcut.d.ts +25 -25
  84. package/dist/phaser/display/webgl/earcut.d.ts.map +1 -1
  85. package/dist/phaser/display/webgl/earcut_node.d.ts +2 -2
  86. package/dist/phaser/display/webgl/earcut_node.d.ts.map +1 -1
  87. package/dist/phaser/display/webgl/fast_sprite_batch.d.ts +3 -2
  88. package/dist/phaser/display/webgl/fast_sprite_batch.d.ts.map +1 -1
  89. package/dist/phaser/display/webgl/filter_manager.d.ts +1 -1
  90. package/dist/phaser/display/webgl/filter_manager.d.ts.map +1 -1
  91. package/dist/phaser/display/webgl/graphics.d.ts +2 -1
  92. package/dist/phaser/display/webgl/graphics.d.ts.map +1 -1
  93. package/dist/phaser/display/webgl/graphics_data.d.ts +9 -9
  94. package/dist/phaser/display/webgl/graphics_data.d.ts.map +1 -1
  95. package/dist/phaser/display/webgl/mask_manager.d.ts +3 -2
  96. package/dist/phaser/display/webgl/mask_manager.d.ts.map +1 -1
  97. package/dist/phaser/display/webgl/render_texture.d.ts +3 -3
  98. package/dist/phaser/display/webgl/render_texture.d.ts.map +1 -1
  99. package/dist/phaser/display/webgl/renderer.d.ts +5 -4
  100. package/dist/phaser/display/webgl/renderer.d.ts.map +1 -1
  101. package/dist/phaser/display/webgl/shader/normal.d.ts +3 -1
  102. package/dist/phaser/display/webgl/shader/normal.d.ts.map +1 -1
  103. package/dist/phaser/display/webgl/shader_manager.d.ts +10 -4
  104. package/dist/phaser/display/webgl/shader_manager.d.ts.map +1 -1
  105. package/dist/phaser/display/webgl/sprite_batch.d.ts +4 -3
  106. package/dist/phaser/display/webgl/sprite_batch.d.ts.map +1 -1
  107. package/dist/phaser/display/webgl/stencil_manager.d.ts +4 -3
  108. package/dist/phaser/display/webgl/stencil_manager.d.ts.map +1 -1
  109. package/dist/phaser/display/webgl/texture.d.ts +6 -6
  110. package/dist/phaser/display/webgl/texture.d.ts.map +1 -1
  111. package/package.json +1 -1
  112. package/src/phaser/core/animation_manager.ts +2 -2
  113. package/src/phaser/core/array_set.ts +1 -1
  114. package/src/phaser/core/cache.ts +1 -1
  115. package/src/phaser/core/factory.ts +1 -1
  116. package/src/phaser/core/game.ts +2 -0
  117. package/src/phaser/core/input.ts +7 -6
  118. package/src/phaser/core/input_event.ts +34 -0
  119. package/src/phaser/core/input_handler.ts +2 -11
  120. package/src/phaser/core/input_mouse.ts +53 -36
  121. package/src/phaser/core/input_mspointer.ts +54 -29
  122. package/src/phaser/core/input_pointer.test.ts +149 -0
  123. package/src/phaser/core/input_pointer.ts +20 -21
  124. package/src/phaser/core/input_touch.ts +48 -31
  125. package/src/phaser/core/loader.test.ts +139 -0
  126. package/src/phaser/core/loader.ts +146 -83
  127. package/src/phaser/core/scale_manager.ts +1 -1
  128. package/src/phaser/core/scene_manager.ts +16 -8
  129. package/src/phaser/core/signal.ts +8 -8
  130. package/src/phaser/core/signal_binding.ts +2 -2
  131. package/src/phaser/core/sound.ts +4 -4
  132. package/src/phaser/core/sound_manager.ts +17 -19
  133. package/src/phaser/core/sound_sprite.ts +2 -2
  134. package/src/phaser/core/timer.ts +6 -6
  135. package/src/phaser/core/timer_event.ts +2 -2
  136. package/src/phaser/core/tween.ts +6 -6
  137. package/src/phaser/core/tween_data.ts +2 -1
  138. package/src/phaser/core/tween_manager.ts +1 -1
  139. package/src/phaser/display/button.ts +1 -1
  140. package/src/phaser/display/canvas/buffer.ts +3 -3
  141. package/src/phaser/display/canvas/graphics.ts +24 -20
  142. package/src/phaser/display/canvas/masker.ts +5 -2
  143. package/src/phaser/display/canvas/renderer.ts +10 -11
  144. package/src/phaser/display/canvas/tinter.ts +4 -3
  145. package/src/phaser/display/canvas/util.ts +15 -5
  146. package/src/phaser/display/display_object.ts +6 -5
  147. package/src/phaser/display/graphics.test.ts +122 -0
  148. package/src/phaser/display/graphics.ts +73 -64
  149. package/src/phaser/display/graphics_data.ts +22 -12
  150. package/src/phaser/display/image.ts +4 -3
  151. package/src/phaser/display/render_session.ts +45 -0
  152. package/src/phaser/display/sprite_util.ts +10 -8
  153. package/src/phaser/display/text.test.ts +27 -1
  154. package/src/phaser/display/text.ts +181 -117
  155. package/src/phaser/display/webgl/base_texture.ts +3 -3
  156. package/src/phaser/display/webgl/earcut.test.ts +132 -0
  157. package/src/phaser/display/webgl/earcut.ts +84 -74
  158. package/src/phaser/display/webgl/earcut_node.ts +5 -5
  159. package/src/phaser/display/webgl/fast_sprite_batch.ts +4 -4
  160. package/src/phaser/display/webgl/filter_manager.ts +1 -1
  161. package/src/phaser/display/webgl/graphics.ts +26 -17
  162. package/src/phaser/display/webgl/graphics_data.ts +9 -9
  163. package/src/phaser/display/webgl/mask_manager.ts +3 -2
  164. package/src/phaser/display/webgl/render_texture.ts +3 -3
  165. package/src/phaser/display/webgl/renderer.ts +7 -6
  166. package/src/phaser/display/webgl/shader/normal.ts +23 -6
  167. package/src/phaser/display/webgl/shader_manager.ts +15 -6
  168. package/src/phaser/display/webgl/sprite_batch.ts +4 -3
  169. package/src/phaser/display/webgl/stencil_manager.ts +10 -3
  170. package/src/phaser/display/webgl/texture.ts +6 -6
@@ -6,6 +6,7 @@ import { PI_2 } from '../util/math.js';
6
6
  import { renderCanvas, renderWebGL } from './sprite_util.js';
7
7
  import type { Graphics } from './graphics.js';
8
8
  import type { Game } from '../core/game.js';
9
+ import type { RenderSession } from './render_session.js';
9
10
 
10
11
  export class DisplayObject {
11
12
  /** @type {boolean} */
@@ -508,7 +509,7 @@ export class DisplayObject {
508
509
  * @param {object} renderSession - The WebGL rendering session.
509
510
  * @param {Matrix | null} _matrix - The transform matrix to render with.
510
511
  */
511
- public renderWebGL(renderSession: any, _matrix: Matrix | null = null) {
512
+ public renderWebGL(renderSession: RenderSession, _matrix: Matrix | null = null) {
512
513
  if (!this.visible || this.alpha <= 0) {
513
514
  return;
514
515
  }
@@ -520,7 +521,7 @@ export class DisplayObject {
520
521
  }
521
522
  if (this._mask) {
522
523
  renderSession.spriteBatch.stop();
523
- renderSession.maskManager.pushMask(this.mask, renderSession);
524
+ renderSession.maskManager.pushMask(this.mask!, renderSession);
524
525
  renderSession.spriteBatch.start();
525
526
  }
526
527
  for (const child of this.children) {
@@ -545,7 +546,7 @@ export class DisplayObject {
545
546
  * Renders this display object using Canvas.
546
547
  * @param {object} renderSession - The Canvas rendering session.
547
548
  */
548
- public renderCanvas(renderSession: any) {
549
+ public renderCanvas(renderSession: RenderSession) {
549
550
  if (!this.visible || this.alpha <= 0) {
550
551
  return;
551
552
  }
@@ -556,7 +557,7 @@ export class DisplayObject {
556
557
  child.renderCanvas(renderSession);
557
558
  }
558
559
  if (this._mask) {
559
- renderSession.maskManager.popMask(renderSession);
560
+ renderSession.maskManager.popMask(this._mask, renderSession);
560
561
  }
561
562
  }
562
563
 
@@ -618,7 +619,7 @@ export class DisplayObject {
618
619
  * Renders a cached sprite for this display object.
619
620
  * @param {object} renderSession - The rendering session.
620
621
  */
621
- public renderCachedSprite(renderSession: any) {
622
+ public renderCachedSprite(renderSession: RenderSession) {
622
623
  if (!this._cachedSprite) {
623
624
  return;
624
625
  }
@@ -0,0 +1,122 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Game } from '../core/game.js';
3
+ import { Point } from '../geom/point.js';
4
+ import { Polygon } from '../geom/polygon.js';
5
+ import { Rectangle } from '../geom/rectangle.js';
6
+ import { Graphics } from './graphics.js';
7
+
8
+ const createGame = (): Game => ({ renderer: { resolution: 1 } }) as unknown as Game;
9
+
10
+ const createGraphics = (): Graphics => new Graphics(createGame());
11
+
12
+ describe('Graphics', () => {
13
+ describe('containsPoint', () => {
14
+ // The index never advanced here, so any filled shape that did not contain the point spun
15
+ // forever. A vitest timeout is the only way a hang shows up as a failure.
16
+ it('returns false for a point outside a filled shape instead of hanging', { timeout: 2000 }, () => {
17
+ const graphics = createGraphics();
18
+ graphics.beginFill(0xff0000);
19
+ graphics.drawRect(0, 0, 10, 10);
20
+ graphics.endFill();
21
+ expect(graphics.containsPoint(new Point(100, 100), new Point())).toBe(false);
22
+ });
23
+
24
+ it('returns true for a point inside a filled shape', { timeout: 2000 }, () => {
25
+ const graphics = createGraphics();
26
+ graphics.beginFill(0xff0000);
27
+ graphics.drawRect(0, 0, 10, 10);
28
+ graphics.endFill();
29
+ expect(graphics.containsPoint(new Point(5, 5), new Point())).toBe(true);
30
+ });
31
+
32
+ it('walks past a non-matching shape to a later matching one', { timeout: 2000 }, () => {
33
+ const graphics = createGraphics();
34
+ graphics.beginFill(0xff0000);
35
+ graphics.drawRect(0, 0, 10, 10);
36
+ graphics.drawRect(100, 100, 10, 10);
37
+ graphics.endFill();
38
+ expect(graphics.containsPoint(new Point(105, 105), new Point())).toBe(true);
39
+ });
40
+
41
+ it('ignores unfilled shapes', { timeout: 2000 }, () => {
42
+ const graphics = createGraphics();
43
+ graphics.lineStyle(1, 0x00ff00);
44
+ graphics.drawRect(0, 0, 10, 10);
45
+ expect(graphics.containsPoint(new Point(5, 5), new Point())).toBe(false);
46
+ });
47
+
48
+ it('returns false when nothing has been drawn', { timeout: 2000 }, () => {
49
+ expect(createGraphics().containsPoint(new Point(0, 0), new Point())).toBe(false);
50
+ });
51
+ });
52
+
53
+ describe('drawShape', () => {
54
+ it('records a polygon as the current path', () => {
55
+ const graphics = createGraphics();
56
+ const data = graphics.drawShape(new Polygon([0, 0, 10, 0, 10, 10]));
57
+ expect(graphics.currentPath).toBe(data);
58
+ expect(data.shape).toBeInstanceOf(Polygon);
59
+ });
60
+
61
+ it('does not record a rectangle as the current path', () => {
62
+ const graphics = createGraphics();
63
+ graphics.drawShape(new Rectangle(0, 0, 10, 10));
64
+ expect(graphics.currentPath).toBeNull();
65
+ });
66
+
67
+ it('carries the current fill and line style onto the data', () => {
68
+ const graphics = createGraphics();
69
+ graphics.lineStyle(4, 0x00ff00, 0.5);
70
+ graphics.beginFill(0xff0000, 0.25);
71
+ const data = graphics.drawShape(new Rectangle(0, 0, 10, 10));
72
+ expect(data.lineWidth).toBe(4);
73
+ expect(data.lineColor).toBe(0x00ff00);
74
+ expect(data.lineAlpha).toBe(0.5);
75
+ expect(data.fillColor).toBe(0xff0000);
76
+ expect(data.fillAlpha).toBe(0.25);
77
+ expect(data.fill).toBe(true);
78
+ });
79
+
80
+ it('flattens a polygon so the builders see numbers', () => {
81
+ const graphics = createGraphics();
82
+ const data = graphics.drawShape(new Polygon([new Point(0, 0), new Point(10, 0), new Point(10, 10)]));
83
+ expect((data.shape as Polygon)._points.every((value) => typeof value === 'number')).toBe(true);
84
+ });
85
+ });
86
+
87
+ describe('path building', () => {
88
+ it('accumulates points through moveTo and lineTo', () => {
89
+ const graphics = createGraphics();
90
+ graphics.moveTo(0, 0);
91
+ graphics.lineTo(10, 0);
92
+ graphics.lineTo(10, 10);
93
+ expect(graphics.currentPath?.shape.points).toStrictEqual([0, 0, 10, 0, 10, 10]);
94
+ });
95
+
96
+ it('starts a path at the origin when lineTo comes first', () => {
97
+ const graphics = createGraphics();
98
+ graphics.lineTo(10, 10);
99
+ expect(graphics.currentPath?.shape.points).toStrictEqual([0, 0, 10, 10]);
100
+ });
101
+ });
102
+
103
+ describe('endFill', () => {
104
+ it('stops filling subsequent shapes', () => {
105
+ const graphics = createGraphics();
106
+ graphics.beginFill(0xff0000);
107
+ graphics.endFill();
108
+ expect(graphics.drawShape(new Rectangle(0, 0, 10, 10)).fill).toBe(false);
109
+ });
110
+ });
111
+
112
+ describe('clear', () => {
113
+ it('drops the recorded graphics data', () => {
114
+ const graphics = createGraphics();
115
+ graphics.beginFill(0xff0000);
116
+ graphics.drawRect(0, 0, 10, 10);
117
+ graphics.clear();
118
+ expect(graphics.graphicsData).toStrictEqual([]);
119
+ expect(graphics.currentPath).toBeNull();
120
+ });
121
+ });
122
+ });
@@ -1,14 +1,6 @@
1
1
  import type { Game } from '../core/game.js';
2
2
  import type { Matrix } from '../geom/matrix.js';
3
- import {
4
- BLEND_NORMAL,
5
- GEOM_CIRCLE,
6
- GEOM_ELLIPSE,
7
- GEOM_POLYGON,
8
- GEOM_RECTANGLE,
9
- GEOM_ROUNDED_RECTANGLE,
10
- GRAPHICS,
11
- } from '../core/const.js';
3
+ import { BLEND_NORMAL, GRAPHICS } from '../core/const.js';
12
4
  import { Circle } from '../geom/circle.js';
13
5
  import { Ellipse } from '../geom/ellipse.js';
14
6
  import { Point } from '../geom/point.js';
@@ -20,37 +12,54 @@ import { getEmptyRectangle } from '../geom/util/rectangle.js';
20
12
  import { CanvasBuffer } from './canvas/buffer.js';
21
13
  import { renderGraphics as renderCanvasGraphics } from './canvas/graphics.js';
22
14
  import { DisplayObject } from './display_object.js';
15
+ import type { GraphicsShape } from './graphics_data.js';
23
16
  import { GraphicsData } from './graphics_data.js';
24
17
  import { Image } from './image.js';
25
18
  import { renderCanvas as renderSpriteCanvas, renderWebGL as renderSpriteWebGL } from './sprite_util.js';
26
19
  import { renderGraphics as renderWebGLGraphics } from './webgl/graphics.js';
27
20
  import { textureFromCanvas } from './webgl/texture_util.js';
21
+ import type { RenderSession } from './render_session.js';
22
+ import type { GraphicsData as WebGLGraphicsData } from './webgl/graphics_data.js';
23
+ import type { IdentifiedWebGLRenderingContext } from './webgl/util.js';
24
+
25
+ /** Graphics attaches the buffer it draws into onto its cached sprite. */
26
+ export type CachedSprite = Image & { buffer: CanvasBuffer };
27
+
28
+ /** drawShape only records a current path for polygon data, so its shape is always a Polygon. */
29
+ export type PolygonPath = GraphicsData & { shape: Polygon };
30
+
31
+ /** Per-context WebGL state cached on a graphics object, keyed by gl context id. */
32
+ export type GraphicsWebGLBucket = {
33
+ lastIndex: number;
34
+ data: WebGLGraphicsData[];
35
+ gl: IdentifiedWebGLRenderingContext;
36
+ };
28
37
 
29
38
  export class Graphics extends DisplayObject {
30
- declare public type: any;
31
- declare public renderable: any;
32
- public fillAlpha!: any;
33
- public lineWidth!: any;
34
- public lineColor!: any;
35
- public lineAlpha!: any;
36
- public graphicsData!: any;
37
- declare public tint: any;
38
- public blendMode!: any;
39
- public currentPath!: any;
40
- public _webGL!: any;
41
- public isMask!: any;
42
- public boundsPadding!: any;
43
- public _localBounds!: any;
44
- public dirty!: any;
45
- public clearDirty!: any;
46
- public _boundsDirty!: any;
47
- public _cacheAsBitmap!: any;
48
- public webGLDirty!: any;
49
- public cachedSpriteDirty!: any;
50
- declare public _cachedSprite: any;
51
- public filling!: any;
52
- public fillColor!: any;
53
- public _prevTint!: any;
39
+ declare public type: number;
40
+ declare public renderable: boolean;
41
+ public fillAlpha!: number;
42
+ public lineWidth!: number;
43
+ public lineColor!: number;
44
+ public lineAlpha!: number;
45
+ public graphicsData!: GraphicsData[];
46
+ declare public tint: number;
47
+ public blendMode!: number;
48
+ public currentPath!: PolygonPath | null;
49
+ public _webGL!: Record<number, GraphicsWebGLBucket | undefined>;
50
+ public isMask!: boolean;
51
+ public boundsPadding!: number;
52
+ public _localBounds!: Rectangle;
53
+ public dirty!: boolean;
54
+ public clearDirty!: boolean;
55
+ public _boundsDirty!: boolean;
56
+ public _cacheAsBitmap!: boolean;
57
+ public webGLDirty!: boolean;
58
+ public cachedSpriteDirty!: boolean;
59
+ declare public _cachedSprite: CachedSprite | null;
60
+ public filling!: boolean;
61
+ public fillColor!: number | null;
62
+ public _prevTint!: number;
54
63
  /**
55
64
  * Creates a new Graphics object.
56
65
  * @param {Game} game - The game instance.
@@ -152,7 +161,7 @@ export class Graphics extends DisplayObject {
152
161
  if (!this.currentPath) {
153
162
  this.moveTo(0, 0);
154
163
  }
155
- this.currentPath.shape.points.push(x, y);
164
+ this.currentPath!.shape.points.push(x, y);
156
165
  this.dirty = true;
157
166
  this._boundsDirty = true;
158
167
  return this;
@@ -177,7 +186,7 @@ export class Graphics extends DisplayObject {
177
186
  let xa;
178
187
  let ya;
179
188
  const n = 20;
180
- const { points } = this.currentPath.shape;
189
+ const { points } = this.currentPath!.shape;
181
190
  if (points.length === 0) {
182
191
  this.moveTo(0, 0);
183
192
  }
@@ -219,7 +228,7 @@ export class Graphics extends DisplayObject {
219
228
  let dt3;
220
229
  let t2;
221
230
  let t3;
222
- const { points } = this.currentPath.shape;
231
+ const { points } = this.currentPath!.shape;
223
232
  const fromX = points.at(-2);
224
233
  const fromY = points.at(-1);
225
234
  let j = 0;
@@ -257,7 +266,7 @@ export class Graphics extends DisplayObject {
257
266
  } else {
258
267
  this.moveTo(x1, y1);
259
268
  }
260
- const { points } = this.currentPath.shape;
269
+ const { points } = this.currentPath!.shape;
261
270
  const fromX = points.at(-2);
262
271
  const fromY = points.at(-1);
263
272
  const a1 = fromY - y1;
@@ -335,7 +344,7 @@ export class Graphics extends DisplayObject {
335
344
  this.moveTo(startX, startY);
336
345
  }
337
346
  // currentPath will always exist after calling a moveTo
338
- const { points } = this.currentPath.shape;
347
+ const { points } = this.currentPath!.shape;
339
348
  const theta = sweep / (segs * 2);
340
349
  const theta2 = theta * 2;
341
350
  const cTheta = Math.cos(theta);
@@ -489,9 +498,9 @@ export class Graphics extends DisplayObject {
489
498
  * Renders the graphics object using WebGL.
490
499
  * @param {object} renderSession - The render session to use.
491
500
  */
492
- public override renderWebGL(renderSession: any) {
501
+ public override renderWebGL(renderSession: RenderSession) {
493
502
  // if the sprite is not visible or the alpha is 0 then no need to render this element
494
- if (!this.visible || this.alpha === 0 || this.isMask === true) {
503
+ if (!this.visible || this.alpha === 0 || this.isMask) {
495
504
  return;
496
505
  }
497
506
  if (this._cacheAsBitmap) {
@@ -502,8 +511,8 @@ export class Graphics extends DisplayObject {
502
511
  this.cachedSpriteDirty = false;
503
512
  this.dirty = false;
504
513
  }
505
- this._cachedSprite.worldAlpha = this.worldAlpha;
506
- (renderSpriteWebGL as any).call(this._cachedSprite, renderSession);
514
+ this._cachedSprite!.worldAlpha = this.worldAlpha;
515
+ renderSpriteWebGL(this._cachedSprite!, renderSession);
507
516
  } else {
508
517
  renderSession.spriteBatch.stop();
509
518
  renderSession.blendModeManager.setBlendMode(this.blendMode);
@@ -517,7 +526,7 @@ export class Graphics extends DisplayObject {
517
526
  if (this.blendMode !== renderSession.spriteBatch.currentBlendMode) {
518
527
  renderSession.spriteBatch.currentBlendMode = this.blendMode;
519
528
  const blendModeWebGL = globalThis.PhaserRegistry.blendModesWebGL[renderSession.spriteBatch.currentBlendMode]!;
520
- renderSession.spriteBatch.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]);
529
+ renderSession.spriteBatch.gl.blendFunc(blendModeWebGL[0]!, blendModeWebGL[1]!);
521
530
  }
522
531
  // check if the webgl graphic needs to be updated
523
532
  if (this.webGLDirty) {
@@ -549,9 +558,9 @@ export class Graphics extends DisplayObject {
549
558
  * Renders the graphics object using Canvas.
550
559
  * @param {object} renderSession - The render session to use.
551
560
  */
552
- public override renderCanvas(renderSession: any) {
561
+ public override renderCanvas(renderSession: RenderSession) {
553
562
  // if the sprite is not visible or the alpha is 0 then no need to render this element
554
- if (!this.visible || this.alpha === 0 || this.isMask === true) {
563
+ if (!this.visible || this.alpha === 0 || this.isMask) {
555
564
  return;
556
565
  }
557
566
  // if the tint has changed, set the graphics object to dirty.
@@ -567,14 +576,15 @@ export class Graphics extends DisplayObject {
567
576
  this.cachedSpriteDirty = false;
568
577
  this.dirty = false;
569
578
  }
570
- this._cachedSprite.alpha = this.alpha;
571
- renderSpriteCanvas(this._cachedSprite, renderSession);
579
+ this._cachedSprite!.alpha = this.alpha;
580
+ renderSpriteCanvas(this._cachedSprite!, renderSession);
572
581
  } else {
573
582
  const { context } = renderSession;
574
583
  const transform = this.worldTransform;
575
584
  if (this.blendMode !== renderSession.currentBlendMode) {
576
585
  renderSession.currentBlendMode = this.blendMode;
577
- context.globalCompositeOperation = globalThis.PhaserRegistry.blendModesCanvas[renderSession.currentBlendMode];
586
+ context.globalCompositeOperation =
587
+ globalThis.PhaserRegistry.blendModesCanvas[renderSession.currentBlendMode] ?? 'source-over';
578
588
  }
579
589
  if (this._mask) {
580
590
  renderSession.maskManager.pushMask(this._mask, renderSession);
@@ -596,7 +606,7 @@ export class Graphics extends DisplayObject {
596
606
  child.renderCanvas(renderSession);
597
607
  }
598
608
  if (this._mask) {
599
- renderSession.maskManager.popMask(renderSession);
609
+ renderSession.maskManager.popMask(this._mask, renderSession);
600
610
  }
601
611
  }
602
612
  }
@@ -689,8 +699,7 @@ export class Graphics extends DisplayObject {
689
699
  public containsPoint(point: Point, tempPoint: Point) {
690
700
  this.worldTransform.applyInverse(point, tempPoint);
691
701
  const { graphicsData } = this;
692
- for (let i = 0; i < graphicsData.length; i += 0) {
693
- const data = graphicsData[i];
702
+ for (const data of graphicsData) {
694
703
  if (data.fill && data.shape) {
695
704
  if (data.shape.contains(tempPoint.x, tempPoint.y)) {
696
705
  return true;
@@ -716,10 +725,9 @@ export class Graphics extends DisplayObject {
716
725
  let w;
717
726
  let h;
718
727
  for (const data of this.graphicsData) {
719
- const { type } = data;
720
728
  const { lineWidth } = data;
721
729
  shape = data.shape;
722
- if (type === GEOM_RECTANGLE || type === GEOM_ROUNDED_RECTANGLE) {
730
+ if (shape instanceof Rectangle || shape instanceof RoundedRectangle) {
723
731
  x = shape.x - lineWidth / 2;
724
732
  y = shape.y - lineWidth / 2;
725
733
  w = shape.width + lineWidth;
@@ -728,7 +736,7 @@ export class Graphics extends DisplayObject {
728
736
  maxX = x + w > maxX ? x + w : maxX;
729
737
  minY = y < minY ? y : minY;
730
738
  maxY = y + h > maxY ? y + h : maxY;
731
- } else if (type === GEOM_CIRCLE) {
739
+ } else if (shape instanceof Circle) {
732
740
  x = shape.x;
733
741
  y = shape.y;
734
742
  w = shape.radius + lineWidth / 2;
@@ -737,7 +745,7 @@ export class Graphics extends DisplayObject {
737
745
  maxX = x + w > maxX ? x + w : maxX;
738
746
  minY = y - h < minY ? y - h : minY;
739
747
  maxY = y + h > maxY ? y + h : maxY;
740
- } else if (type === GEOM_ELLIPSE) {
748
+ } else if (shape instanceof Ellipse) {
741
749
  x = shape.x;
742
750
  y = shape.y;
743
751
  w = shape.width + lineWidth / 2;
@@ -788,7 +796,7 @@ export class Graphics extends DisplayObject {
788
796
  if (!this._cachedSprite) {
789
797
  const canvasBuffer = new CanvasBuffer(bounds.width, bounds.height);
790
798
  const texture = textureFromCanvas(canvasBuffer.canvas);
791
- this._cachedSprite = new Image(this.game, 0, 0, texture);
799
+ this._cachedSprite = new Image(this.game, 0, 0, texture) as CachedSprite;
792
800
  this._cachedSprite.buffer = canvasBuffer;
793
801
  this._cachedSprite.worldTransform = this.worldTransform;
794
802
  } else {
@@ -810,7 +818,7 @@ export class Graphics extends DisplayObject {
810
818
  * Updates the cached sprite texture.
811
819
  */
812
820
  public updateCachedSpriteTexture() {
813
- const cachedSprite = this._cachedSprite;
821
+ const cachedSprite = this._cachedSprite!;
814
822
  const { texture } = cachedSprite;
815
823
  const { canvas } = cachedSprite.buffer;
816
824
  texture.baseTexture.width = canvas.width;
@@ -841,7 +849,7 @@ export class Graphics extends DisplayObject {
841
849
  * @param {object} shape - The shape to draw.
842
850
  * @returns {GraphicsData} The graphics data for the drawn shape.
843
851
  */
844
- public drawShape(shape: any) {
852
+ public drawShape(shape: GraphicsShape) {
845
853
  if (this.currentPath) {
846
854
  // check current path!
847
855
  if (this.currentPath.shape.points.length <= 2) {
@@ -850,9 +858,10 @@ export class Graphics extends DisplayObject {
850
858
  }
851
859
  this.currentPath = null;
852
860
  // Handle mixed-type polygons
853
- if (shape instanceof Polygon) {
854
- shape = shape.clone();
855
- shape.flatten();
861
+ let target = shape;
862
+ if (target instanceof Polygon) {
863
+ target = target.clone();
864
+ target.flatten();
856
865
  }
857
866
  const data = new GraphicsData(
858
867
  this.lineWidth,
@@ -861,12 +870,12 @@ export class Graphics extends DisplayObject {
861
870
  this.fillColor,
862
871
  this.fillAlpha,
863
872
  this.filling,
864
- shape
873
+ target
865
874
  );
866
875
  this.graphicsData.push(data);
867
- if (data.type === GEOM_POLYGON) {
876
+ if (data.shape instanceof Polygon) {
868
877
  data.shape.closed = this.filling;
869
- this.currentPath = data;
878
+ this.currentPath = data as PolygonPath;
870
879
  }
871
880
  this.dirty = true;
872
881
  this._boundsDirty = true;
@@ -1,16 +1,26 @@
1
+ import type { Circle } from '../geom/circle.js';
2
+ import type { Ellipse } from '../geom/ellipse.js';
3
+ import type { Polygon } from '../geom/polygon.js';
4
+ import type { Rectangle } from '../geom/rectangle.js';
5
+ import type { RoundedRectangle } from '../geom/rounded_rectangle.js';
1
6
  import { clone } from './graphics_data_util.js';
2
7
 
8
+ /** The geometry a graphics path can be built from. */
9
+ export type GraphicsShape = Circle | Ellipse | Polygon | Rectangle | RoundedRectangle;
10
+
3
11
  export class GraphicsData {
4
- public lineWidth!: any;
5
- public lineColor!: any;
6
- public lineAlpha!: any;
7
- public _lineTint!: any;
8
- public fillColor!: any;
9
- public fillAlpha!: any;
10
- public _fillTint!: any;
11
- public fill!: any;
12
- public shape!: any;
13
- public type!: any;
12
+ public lineWidth: number;
13
+ public lineColor: number;
14
+ public lineAlpha: number;
15
+ public _lineTint: number;
16
+ public fillColor: number | null;
17
+ public fillAlpha: number;
18
+ public _fillTint: number | null;
19
+ public fill: boolean;
20
+ public shape: GraphicsShape;
21
+ public type: number;
22
+ /** Flattened points the WebGL builders work from, filled in while updating the graphics. */
23
+ public points: number[] = [];
14
24
  /**
15
25
  * Creates a new GraphicsData object.
16
26
  * @param {number} lineWidth - The line width.
@@ -25,10 +35,10 @@ export class GraphicsData {
25
35
  lineWidth: number,
26
36
  lineColor: number,
27
37
  lineAlpha: number,
28
- fillColor: number,
38
+ fillColor: number | null,
29
39
  fillAlpha: number,
30
40
  fill: boolean,
31
- shape: any
41
+ shape: GraphicsShape
32
42
  ) {
33
43
  this.lineWidth = lineWidth;
34
44
  this.lineColor = lineColor;
@@ -9,6 +9,7 @@ import { Texture } from './webgl/texture.js';
9
9
  import type { Game } from '../core/game.js';
10
10
  import type { Frame } from '../core/frame.js';
11
11
  import type { Matrix } from '../geom/matrix.js';
12
+ import type { RenderSession } from './render_session.js';
12
13
 
13
14
  export class Image extends DisplayObject {
14
15
  public key!: any;
@@ -18,7 +19,7 @@ export class Image extends DisplayObject {
18
19
  public tint!: number;
19
20
  public cachedTint!: number;
20
21
  public tilingTexture!: Texture | null;
21
- public tintedTexture!: Texture | null;
22
+ public tintedTexture!: HTMLCanvasElement | null;
22
23
  public blendMode!: number;
23
24
  public shader!: object | null;
24
25
  public _frame!: any;
@@ -409,7 +410,7 @@ export class Image extends DisplayObject {
409
410
  * @param {object} renderSession - The WebGL rendering session.
410
411
  * @param {Matrix} matrix - The transformation matrix to use.
411
412
  */
412
- public override renderWebGL(renderSession: any, matrix: Matrix | null = null) {
413
+ public override renderWebGL(renderSession: RenderSession, matrix: Matrix | null = null) {
413
414
  renderWebGL(this, renderSession, matrix);
414
415
  }
415
416
 
@@ -418,7 +419,7 @@ export class Image extends DisplayObject {
418
419
  * @param {object} renderSession - The Canvas rendering session.
419
420
  * @param {Matrix} matrix - The transformation matrix to use.
420
421
  */
421
- public override renderCanvas(renderSession: any, matrix: Matrix | null = null) {
422
+ public override renderCanvas(renderSession: RenderSession, matrix: Matrix | null = null) {
422
423
  renderCanvas(this, renderSession, matrix);
423
424
  }
424
425
  }
@@ -0,0 +1,45 @@
1
+ import type { Point } from '../geom/point.js';
2
+ import type { Graphics } from './graphics.js';
3
+ import type { WebGLBlendModeManager } from './webgl/blend_manager.js';
4
+ import type { WebGLFilterManager } from './webgl/filter_manager.js';
5
+ import type { WebGLRenderer } from './webgl/renderer.js';
6
+ import type { WebGLShaderManager } from './webgl/shader_manager.js';
7
+ import type { WebGLSpriteBatch } from './webgl/sprite_batch.js';
8
+ import type { WebGLStencilManager } from './webgl/stencil_manager.js';
9
+ import type { IdentifiedWebGLRenderingContext } from './webgl/util.js';
10
+
11
+ /** The canvas and WebGL mask managers, which the renderers supply as namespace imports. */
12
+ export type MaskManager = {
13
+ pushMask: (maskData: Graphics, renderSession: RenderSession) => void;
14
+ popMask: (maskData: Graphics | null, renderSession: RenderSession) => void;
15
+ };
16
+
17
+ /**
18
+ * State threaded through every render call.
19
+ *
20
+ * The canvas and WebGL renderers each populate the subset their pipeline needs, so a session is
21
+ * only ever fully populated for one of the two. Members are declared non-optional because that is
22
+ * how the render paths consume them; the renderers assemble the session in one place each.
23
+ */
24
+ export type RenderSession = {
25
+ blendModeManager: WebGLBlendModeManager;
26
+ context: CanvasRenderingContext2D;
27
+ currentBlendMode: number;
28
+ drawCount: number;
29
+ filterManager: WebGLFilterManager;
30
+ flipY: number;
31
+ gl: IdentifiedWebGLRenderingContext;
32
+ maskManager: MaskManager;
33
+ offset: Point;
34
+ projection: Point;
35
+ renderer: WebGLRenderer;
36
+ resolution: number;
37
+ roundPixels: boolean;
38
+ scaleMode: number;
39
+ shaderManager: WebGLShaderManager;
40
+ shakeX: number;
41
+ shakeY: number;
42
+ smoothProperty: string | null;
43
+ spriteBatch: WebGLSpriteBatch;
44
+ stencilManager: WebGLStencilManager;
45
+ };