Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(927)

Side by Side Diff: sky/examples/game/lib/node.dart

Issue 1179333002: Playable demo game and bug fixes in sprites (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 part of sprites; 1 part of sprites;
2 2
3 double degrees2radians(double degrees) => degrees * Math.PI/180.8; 3 double convertDegrees2Radians(double degrees) => degrees * Math.PI/180.8;
4 4
5 double radians2degrees(double radians) => radians * 180.0/Math.PI; 5 double convertRadians2Degrees(double radians) => radians * 180.0/Math.PI;
6 6
7 class Node { 7 class Node {
8 8
9 // Member variables 9 // Member variables
10 10
11 SpriteBox _spriteBox; 11 SpriteBox _spriteBox;
12 Node _parent; 12 Node _parent;
13 13
14 Point _position; 14 Point _position;
15 double _rotation; 15 double _rotation;
16 16
17 bool _isMatrixDirty; 17 bool _isMatrixDirty;
18 Matrix4 _transformMatrix; 18 Matrix4 _transformMatrix;
19 Matrix4 _transformMatrixFromWorld; 19 Matrix4 _transformMatrixFromWorld;
20 20
21 double _scaleX; 21 double _scaleX;
22 double _scaleY; 22 double _scaleY;
23 23
24 bool visible; 24 bool visible;
25 25
26 double _zPosition; 26 double _zPosition;
27 int _addedOrder; 27 int _addedOrder;
28 int _childrenLastAddedOrder; 28 int _childrenLastAddedOrder;
29 bool _childrenNeedSorting; 29 bool _childrenNeedSorting;
30 30
31 bool paused = false;
32
31 List<Node>_children; 33 List<Node>_children;
32 34
33 // Constructors 35 // Constructors
34 36
35 Node() { 37 Node() {
36 _rotation = 0.0; 38 _rotation = 0.0;
37 _position = new Point(0.0, 0.0); 39 _position = Point.origin;
38 _scaleX = _scaleY = 1.0; 40 _scaleX = _scaleY = 1.0;
39 _isMatrixDirty = false; 41 _isMatrixDirty = false;
40 _transformMatrix = new Matrix4.identity(); 42 _transformMatrix = new Matrix4.identity();
41 _children = []; 43 _children = [];
42 _childrenNeedSorting = false; 44 _childrenNeedSorting = false;
43 _childrenLastAddedOrder = 0; 45 _childrenLastAddedOrder = 0;
46 _zPosition = 0.0;
44 visible = true; 47 visible = true;
45 } 48 }
46 49
47 // Property setters and getters 50 // Property setters and getters
48 51
49 SpriteBox get spriteBox => _spriteBox; 52 SpriteBox get spriteBox => _spriteBox;
50 53
51 Node get parent => _parent; 54 Node get parent => _parent;
52 55
53 double get rotation => _rotation; 56 double get rotation => _rotation;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 103
101 void removeChild(Node child) { 104 void removeChild(Node child) {
102 if (_children.remove(child)) { 105 if (_children.remove(child)) {
103 child._parent = null; 106 child._parent = null;
104 child._spriteBox = null; 107 child._spriteBox = null;
105 } 108 }
106 } 109 }
107 110
108 void removeFromParent() { 111 void removeFromParent() {
109 assert(_parent != null); 112 assert(_parent != null);
110 _parent.removeFromParent(); 113 _parent.removeChild(this);
111 } 114 }
112 115
113 void removeAllChildren() { 116 void removeAllChildren() {
114 for (Node child in _children) { 117 for (Node child in _children) {
115 child._parent = null; 118 child._parent = null;
116 child._spriteBox = null; 119 child._spriteBox = null;
117 } 120 }
118 _children = []; 121 _children = [];
119 _childrenNeedSorting = false; 122 _childrenNeedSorting = false;
120 } 123 }
121 124
122 // Calculating the transformation matrix 125 // Calculating the transformation matrix
123 126
124 Matrix4 get transformMatrix { 127 Matrix4 get transformMatrix {
125 if (!_isMatrixDirty) { 128 if (!_isMatrixDirty) {
126 return _transformMatrix; 129 return _transformMatrix;
127 } 130 }
128 131
129 double cx, sx, cy, sy; 132 double cx, sx, cy, sy;
130 133
131 if (_rotation == 0.0) { 134 if (_rotation == 0.0) {
132 cx = 1.0; 135 cx = 1.0;
133 sx = 0.0; 136 sx = 0.0;
134 cy = 1.0; 137 cy = 1.0;
135 sy = 0.0; 138 sy = 0.0;
136 } 139 }
137 else { 140 else {
138 double radiansX = degrees2radians(_rotation); 141 double radiansX = convertDegrees2Radians(_rotation);
139 double radiansY = degrees2radians(_rotation); 142 double radiansY = convertDegrees2Radians(_rotation);
140 143
141 cx = Math.cos(radiansX); 144 cx = Math.cos(radiansX);
142 sx = Math.sin(radiansX); 145 sx = Math.sin(radiansX);
143 cy = Math.cos(radiansY); 146 cy = Math.cos(radiansY);
144 sy = Math.sin(radiansY); 147 sy = Math.sin(radiansY);
145 } 148 }
146 149
147 // Create transformation matrix for scale, position and rotation 150 // Create transformation matrix for scale, position and rotation
148 _transformMatrix.setValues(cy * _scaleX, sy * _scaleX, 0.0, 0.0, 151 _transformMatrix.setValues(cy * _scaleX, sy * _scaleX, 0.0, 0.0,
149 -sx * _scaleY, cx * _scaleY, 0.0, 0.0, 152 -sx * _scaleY, cx * _scaleY, 0.0, 0.0,
150 0.0, 0.0, 1.0, 0.0, 153 0.0, 0.0, 1.0, 0.0,
151 _position.x, _position.y, 0.0, 1.0 154 _position.x, _position.y, 0.0, 1.0
152 ); 155 );
153 156
154 return _transformMatrix; 157 return _transformMatrix;
155 } 158 }
156 159
157 // Transforms to other nodes 160 // Transforms to other nodes
158 161
159 Matrix4 _nodeToBoxMatrix() { 162 Matrix4 _nodeToBoxMatrix() {
163 assert(_spriteBox != null);
164
160 Matrix4 t = transformMatrix; 165 Matrix4 t = transformMatrix;
161 166
167 // Apply transforms from parents
162 Node p = this.parent; 168 Node p = this.parent;
163 while (p != null) { 169 while (p != null) {
164 t = new Matrix4.copy(p.transformMatrix).multiply(t); 170 t = new Matrix4.copy(p.transformMatrix).multiply(t);
165 p = p.parent; 171 p = p.parent;
166 } 172 }
173
174 // Apply transform from sprite box
175 t = new Matrix4.copy(_spriteBox.transformMatrix).multiply(t);
176
167 return t; 177 return t;
168 } 178 }
169 179
170 Matrix4 _boxToNodeMatrix() { 180 Matrix4 _boxToNodeMatrix() {
181 assert(_spriteBox != null);
182
171 Matrix4 t = _nodeToBoxMatrix(); 183 Matrix4 t = _nodeToBoxMatrix();
172 t.invert(); 184 t.invert();
185
173 return t; 186 return t;
174 } 187 }
175 188
176 Point convertPointToNodeSpace(Point boxPoint) { 189 Point convertPointToNodeSpace(Point boxPoint) {
177 assert(boxPoint != null); 190 assert(boxPoint != null);
178 assert(_spriteBox != null); 191 assert(_spriteBox != null);
179 192
180 Vector4 v =_boxToNodeMatrix().transform(new Vector4(boxPoint.x, boxPoint.y, 0.0, 1.0)); 193 Vector4 v =_boxToNodeMatrix().transform(new Vector4(boxPoint.x, boxPoint.y, 0.0, 1.0));
181 return new Point(v[0], v[1]); 194 return new Point(v[0], v[1]);
182 } 195 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 229
217 prePaint(canvas); 230 prePaint(canvas);
218 paint(canvas); 231 paint(canvas);
219 visitChildren(canvas); 232 visitChildren(canvas);
220 postPaint(canvas); 233 postPaint(canvas);
221 } 234 }
222 235
223 void prePaint(PictureRecorder canvas) { 236 void prePaint(PictureRecorder canvas) {
224 canvas.save(); 237 canvas.save();
225 238
226 // TODO: Can this be done more efficiently?
227 // Get the transformation matrix and apply transform 239 // Get the transformation matrix and apply transform
228 List<double> matrix = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0]; 240 canvas.concat(transformMatrix.storage);
abarth-chromium 2015/06/13 00:03:20 \o/
229 this.transformMatrix.copyIntoArray(matrix);
230 Float32List list32 = new Float32List.fromList(matrix);
231 canvas.concat(list32);
232 } 241 }
233 242
234 void paint(PictureRecorder canvas) { 243 void paint(PictureRecorder canvas) {
235 244
236 } 245 }
237 246
238 void visitChildren(PictureRecorder canvas) { 247 void visitChildren(PictureRecorder canvas) {
239 // Sort children primarily by zPosition, secondarily by added order 248 // Sort children primarily by zPosition, secondarily by added order
240 if (_childrenNeedSorting) { 249 if (_childrenNeedSorting) {
241 _children.sort((Node a, Node b) { 250 _children.sort((Node a, Node b) {
242 if (a._zPosition == b._zPosition) { 251 if (a._zPosition == b._zPosition) {
243 return b._addedOrder - a._addedOrder; 252 return a._addedOrder - b._addedOrder;
244 } 253 }
245 else if (a._zPosition > b._zPosition) { 254 else if (a._zPosition > b._zPosition) {
246 return 1; 255 return 1;
247 } 256 }
248 else { 257 else {
249 return -1; 258 return -1;
250 } 259 }
251 }); 260 });
252 _childrenNeedSorting = false; 261 _childrenNeedSorting = false;
253 } 262 }
254 263
255 // Visit each child 264 // Visit each child
256 _children.forEach((child) => child.visit(canvas)); 265 _children.forEach((child) => child.visit(canvas));
257 } 266 }
258 267
259 void postPaint(PictureRecorder canvas) { 268 void postPaint(PictureRecorder canvas) {
260 canvas.restore(); 269 canvas.restore();
261 } 270 }
262 271
263 // Receiving update calls 272 // Receiving update calls
264 273
265 void update(double dt) { 274 void update(double dt) {
275 }
266 276
277 void spriteBoxPerformedLayout() {
267 } 278 }
268 } 279 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698