OLD | NEW |
1 part of sprites; | 1 part of sprites; |
2 | 2 |
3 enum SpriteBoxTransformMode { | 3 enum SpriteBoxTransformMode { |
4 nativePoints, | 4 nativePoints, |
5 letterbox, | 5 letterbox, |
6 stretch, | 6 stretch, |
7 scaleToFit, | 7 scaleToFit, |
8 fixedWidth, | 8 fixedWidth, |
9 fixedHeight, | 9 fixedHeight, |
10 } | 10 } |
11 | 11 |
12 class SpriteBox extends RenderBox { | 12 class SpriteBox extends RenderBox { |
13 | 13 |
14 // Member variables | 14 // Member variables |
15 | 15 |
16 // Root node for drawing | 16 // Root node for drawing |
17 Node _rootNode; | 17 NodeWithSize _rootNode; |
18 | 18 |
19 // Tracking of frame rate and updates | 19 // Tracking of frame rate and updates |
20 double _lastTimeStamp; | 20 double _lastTimeStamp; |
21 int _numFrames = 0; | 21 int _numFrames = 0; |
22 double _frameRate = 0.0; | 22 double _frameRate = 0.0; |
23 | 23 |
24 // Transformation mode | 24 // Transformation mode |
25 SpriteBoxTransformMode transformMode; | 25 SpriteBoxTransformMode transformMode; |
26 double _systemWidth; | 26 // double _systemWidth; |
27 double _systemHeight; | 27 // double _systemHeight; |
28 | 28 |
29 // Cached transformation matrix | 29 // Cached transformation matrix |
30 Matrix4 _transformMatrix; | 30 Matrix4 _transformMatrix; |
31 | 31 |
32 List<Node> _eventTargets; | 32 List<Node> _eventTargets; |
33 | 33 |
34 // Setup | 34 // Setup |
35 | 35 |
36 SpriteBox(Node rootNode, [SpriteBoxTransformMode mode = SpriteBoxTransformMode
.nativePoints, double width=1024.0, double height=1024.0]) { | 36 SpriteBox(NodeWithSize rootNode, [SpriteBoxTransformMode mode = SpriteBoxTrans
formMode.nativePoints]) { |
37 assert(rootNode != null); | 37 assert(rootNode != null); |
38 assert(rootNode._spriteBox == null); | 38 assert(rootNode._spriteBox == null); |
39 | 39 |
40 // Setup root node | 40 // Setup root node |
41 _rootNode = rootNode; | 41 _rootNode = rootNode; |
42 | 42 |
43 // Assign SpriteBox reference to all the nodes | 43 // Assign SpriteBox reference to all the nodes |
44 _addSpriteBoxReference(_rootNode); | 44 _addSpriteBoxReference(_rootNode); |
45 | 45 |
46 // Setup transform mode | 46 // Setup transform mode |
47 transformMode = mode; | 47 transformMode = mode; |
48 _systemWidth = width; | 48 // _systemWidth = rootNode.size.width; |
49 _systemHeight = height; | 49 // _systemHeight = rootNode.size.height; |
50 | 50 |
51 _scheduleTick(); | 51 _scheduleTick(); |
52 } | 52 } |
53 | 53 |
54 void _addSpriteBoxReference(Node node) { | 54 void _addSpriteBoxReference(Node node) { |
55 node._spriteBox = this; | 55 node._spriteBox = this; |
56 for (Node child in node._children) { | 56 for (Node child in node._children) { |
57 _addSpriteBoxReference(child); | 57 _addSpriteBoxReference(child); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 // Properties | 61 // Properties |
62 | 62 |
63 double get systemWidth => _systemWidth; | 63 double get systemWidth => rootNode.size.width; |
64 double get systemHeight => _systemHeight; | 64 double get systemHeight => rootNode.size.height; |
65 | 65 |
66 Node get rootNode => _rootNode; | 66 NodeWithSize get rootNode => _rootNode; |
67 | 67 |
68 void performLayout() { | 68 void performLayout() { |
69 size = constraints.constrain(Size.infinite); | 69 size = constraints.constrain(Size.infinite); |
70 _invalidateTransformMatrix(); | 70 _invalidateTransformMatrix(); |
71 _callSpriteBoxPerformedLayout(_rootNode); | 71 _callSpriteBoxPerformedLayout(_rootNode); |
72 } | 72 } |
73 | 73 |
74 // Event handling | 74 // Event handling |
75 | 75 |
76 void _addEventTargets(Node node, List<Node> eventTargets) { | 76 void _addEventTargets(Node node, List<Node> eventTargets) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 145 } |
146 | 146 |
147 _transformMatrix = new Matrix4.identity(); | 147 _transformMatrix = new Matrix4.identity(); |
148 | 148 |
149 // Calculate matrix | 149 // Calculate matrix |
150 double scaleX = 1.0; | 150 double scaleX = 1.0; |
151 double scaleY = 1.0; | 151 double scaleY = 1.0; |
152 double offsetX = 0.0; | 152 double offsetX = 0.0; |
153 double offsetY = 0.0; | 153 double offsetY = 0.0; |
154 | 154 |
| 155 double systemWidth = rootNode.size.width; |
| 156 double systemHeight = rootNode.size.height; |
| 157 |
155 switch(transformMode) { | 158 switch(transformMode) { |
156 case SpriteBoxTransformMode.stretch: | 159 case SpriteBoxTransformMode.stretch: |
157 scaleX = size.width/_systemWidth; | 160 scaleX = size.width/systemWidth; |
158 scaleY = size.height/_systemHeight; | 161 scaleY = size.height/systemHeight; |
159 break; | 162 break; |
160 case SpriteBoxTransformMode.letterbox: | 163 case SpriteBoxTransformMode.letterbox: |
161 scaleX = size.width/_systemWidth; | 164 scaleX = size.width/systemWidth; |
162 scaleY = size.height/_systemHeight; | 165 scaleY = size.height/systemHeight; |
163 if (scaleX > scaleY) { | 166 if (scaleX > scaleY) { |
164 scaleY = scaleX; | 167 scaleY = scaleX; |
165 offsetY = (size.height - scaleY * _systemHeight)/2.0; | 168 offsetY = (size.height - scaleY * systemHeight)/2.0; |
166 } | 169 } |
167 else { | 170 else { |
168 scaleX = scaleY; | 171 scaleX = scaleY; |
169 offsetX = (size.width - scaleX * _systemWidth)/2.0; | 172 offsetX = (size.width - scaleX * systemWidth)/2.0; |
170 } | 173 } |
171 break; | 174 break; |
172 case SpriteBoxTransformMode.scaleToFit: | 175 case SpriteBoxTransformMode.scaleToFit: |
173 scaleX = size.width/_systemWidth; | 176 scaleX = size.width/systemWidth; |
174 scaleY = size.height/_systemHeight; | 177 scaleY = size.height/systemHeight; |
175 if (scaleX < scaleY) { | 178 if (scaleX < scaleY) { |
176 scaleY = scaleX; | 179 scaleY = scaleX; |
177 offsetY = (size.height - scaleY * _systemHeight)/2.0; | 180 offsetY = (size.height - scaleY * systemHeight)/2.0; |
178 } | 181 } |
179 else { | 182 else { |
180 scaleX = scaleY; | 183 scaleX = scaleY; |
181 offsetX = (size.width - scaleX * _systemWidth)/2.0; | 184 offsetX = (size.width - scaleX * systemWidth)/2.0; |
182 } | 185 } |
183 break; | 186 break; |
184 case SpriteBoxTransformMode.fixedWidth: | 187 case SpriteBoxTransformMode.fixedWidth: |
185 scaleX = size.width/_systemWidth; | 188 scaleX = size.width/systemWidth; |
186 scaleY = scaleX; | 189 scaleY = scaleX; |
187 _systemHeight = size.height/scaleX; | 190 systemHeight = size.height/scaleX; |
| 191 rootNode.size = new Size(systemWidth, systemHeight); |
188 break; | 192 break; |
189 case SpriteBoxTransformMode.fixedHeight: | 193 case SpriteBoxTransformMode.fixedHeight: |
190 scaleY = size.height/_systemHeight; | 194 scaleY = size.height/systemHeight; |
191 scaleX = scaleY; | 195 scaleX = scaleY; |
192 _systemWidth = size.width/scaleY; | 196 systemWidth = size.width/scaleY; |
| 197 rootNode.size = new Size(systemWidth, systemHeight); |
193 break; | 198 break; |
194 case SpriteBoxTransformMode.nativePoints: | 199 case SpriteBoxTransformMode.nativePoints: |
195 break; | 200 break; |
196 default: | 201 default: |
197 assert(false); | 202 assert(false); |
198 break; | 203 break; |
199 } | 204 } |
200 | 205 |
201 _transformMatrix.translate(offsetX, offsetY); | 206 _transformMatrix.translate(offsetX, offsetY); |
202 _transformMatrix.scale(scaleX, scaleY); | 207 _transformMatrix.scale(scaleX, scaleY); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 SpriteBoxHitTestEntry(RenderBox target, Point localPosition) : super(target, l
ocalPosition); | 300 SpriteBoxHitTestEntry(RenderBox target, Point localPosition) : super(target, l
ocalPosition); |
296 } | 301 } |
297 | 302 |
298 class SpriteBoxEvent { | 303 class SpriteBoxEvent { |
299 Point boxPosition; | 304 Point boxPosition; |
300 String type; | 305 String type; |
301 int pointer; | 306 int pointer; |
302 | 307 |
303 SpriteBoxEvent(this.boxPosition, this.type, this.pointer); | 308 SpriteBoxEvent(this.boxPosition, this.type, this.pointer); |
304 } | 309 } |
OLD | NEW |