@@ -13,56 +13,171 @@ var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
1313 * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
1414 * This method should not be called directly. It is a utility function of the Render module.
1515 *
16- * @method Phaser.GameObjects.Rectangle #renderCanvas
16+ * @method Phaser.GameObjects.Grid #renderCanvas
1717 * @since 3.13.0
1818 * @private
1919 *
2020 * @param {Phaser.Renderer.Canvas.CanvasRenderer } renderer - A reference to the current active Canvas renderer.
21- * @param {Phaser.GameObjects.Rectangle } src - The Game Object being rendered in this call.
21+ * @param {Phaser.GameObjects.Grid } src - The Game Object being rendered in this call.
2222 * @param {number } interpolationPercentage - Reserved for future use and custom pipelines.
2323 * @param {Phaser.Cameras.Scene2D.Camera } camera - The Camera that is rendering the Game Object.
2424 * @param {Phaser.GameObjects.Components.TransformMatrix } parentMatrix - This transform matrix is defined if the game object is nested
2525 */
26- var RectangleCanvasRenderer = function ( renderer , src , interpolationPercentage , camera , parentMatrix )
26+ var GridCanvasRenderer = function ( renderer , src , interpolationPercentage , camera , parentMatrix )
2727{
2828 var ctx = renderer . currentContext ;
2929
3030 if ( SetTransform ( renderer , ctx , src , camera , parentMatrix ) )
3131 {
32- var dx = src . _displayOriginX ;
33- var dy = src . _displayOriginY ;
32+ var dx = - src . _displayOriginX ;
33+ var dy = - src . _displayOriginY ;
3434
35- if ( src . isFilled )
35+ var alpha = camera . alpha * src . alpha ;
36+
37+ // Work out the grid size
38+
39+ var width = src . width ;
40+ var height = src . height ;
41+
42+ var cellWidth = src . cellWidth ;
43+ var cellHeight = src . cellHeight ;
44+
45+ var gridWidth = Math . ceil ( width / cellWidth ) ;
46+ var gridHeight = Math . ceil ( height / cellHeight ) ;
47+
48+ var cellWidthA = cellWidth ;
49+ var cellHeightA = cellHeight ;
50+
51+ var cellWidthB = cellWidth - ( ( gridWidth * cellWidth ) - width ) ;
52+ var cellHeightB = cellHeight - ( ( gridHeight * cellHeight ) - height ) ;
53+
54+ var showCells = src . showCells ;
55+ var showAltCells = src . showAltCells ;
56+ var showOutline = src . showOutline ;
57+
58+ var x = 0 ;
59+ var y = 0 ;
60+ var r = 0 ;
61+ var cw = 0 ;
62+ var ch = 0 ;
63+
64+ if ( showOutline )
65+ {
66+ // To make room for the grid lines (in case alpha < 1)
67+ cellWidthA -- ;
68+ cellHeightA -- ;
69+
70+ if ( cellWidthB === cellWidth )
71+ {
72+ cellWidthB -- ;
73+ }
74+
75+ if ( cellHeightB === cellHeight )
76+ {
77+ cellHeightB -- ;
78+ }
79+ }
80+
81+ if ( showCells && src . fillAlpha > 0 )
3682 {
3783 FillStyleCanvas ( ctx , src ) ;
38-
39- ctx . fillRect (
40- - dx ,
41- - dy ,
42- src . width ,
43- src . height
44- ) ;
84+
85+ for ( y = 0 ; y < gridHeight ; y ++ )
86+ {
87+ if ( showAltCells )
88+ {
89+ r = y % 2 ;
90+ }
91+
92+ for ( x = 0 ; x < gridWidth ; x ++ )
93+ {
94+ if ( showAltCells && r )
95+ {
96+ r = 0 ;
97+ continue ;
98+ }
99+
100+ r ++ ;
101+
102+ cw = ( x < gridWidth - 1 ) ? cellWidthA : cellWidthB ;
103+ ch = ( y < gridHeight - 1 ) ? cellHeightA : cellHeightB ;
104+
105+ ctx . fillRect (
106+ dx + x * cellWidth ,
107+ dy + y * cellHeight ,
108+ cw ,
109+ ch
110+ ) ;
111+ }
112+ }
113+ }
114+
115+ if ( showAltCells && src . altFillAlpha > 0 )
116+ {
117+ FillStyleCanvas ( ctx , src , src . altFillColor , src . altFillAlpha * alpha ) ;
118+
119+ for ( y = 0 ; y < gridHeight ; y ++ )
120+ {
121+ if ( showAltCells )
122+ {
123+ r = y % 2 ;
124+ }
125+
126+ for ( x = 0 ; x < gridWidth ; x ++ )
127+ {
128+ if ( showAltCells && ! r )
129+ {
130+ r = 1 ;
131+ continue ;
132+ }
133+
134+ r = 0 ;
135+
136+ cw = ( x < gridWidth - 1 ) ? cellWidthA : cellWidthB ;
137+ ch = ( y < gridHeight - 1 ) ? cellHeightA : cellHeightB ;
138+
139+ ctx . fillRect (
140+ dx + x * cellWidth ,
141+ dy + y * cellHeight ,
142+ cw ,
143+ ch
144+ ) ;
145+ }
146+ }
45147 }
46148
47- if ( src . isStroked )
149+ if ( showOutline && src . outlineFillAlpha > 0 )
48150 {
49- LineStyleCanvas ( ctx , src ) ;
151+ LineStyleCanvas ( ctx , src , src . outlineFillColor , src . outlineFillAlpha * alpha ) ;
152+
153+ for ( x = 1 ; x < gridWidth ; x ++ )
154+ {
155+ var x1 = x * cellWidth ;
156+
157+ ctx . beginPath ( ) ;
158+
159+ ctx . moveTo ( x1 + dx , dy ) ;
160+ ctx . lineTo ( x1 + dx , height + dy ) ;
161+
162+ ctx . stroke ( ) ;
163+ }
164+
165+ for ( y = 1 ; y < gridHeight ; y ++ )
166+ {
167+ var y1 = y * cellHeight ;
50168
51- ctx . beginPath ( ) ;
169+ ctx . beginPath ( ) ;
52170
53- ctx . rect (
54- - dx ,
55- - dy ,
56- src . width ,
57- src . height
58- ) ;
171+ ctx . moveTo ( dx , y1 + dy ) ;
172+ ctx . lineTo ( dx + width , y1 + dy ) ;
59173
60- ctx . stroke ( ) ;
174+ ctx . stroke ( ) ;
175+ }
61176 }
62177
63178 // Restore the context saved in SetTransform
64179 ctx . restore ( ) ;
65180 }
66181} ;
67182
68- module . exports = RectangleCanvasRenderer ;
183+ module . exports = GridCanvasRenderer ;
0 commit comments