Chromium Code Reviews| 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 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 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 bool _transformMatrixIsDirty; | 31 |
| 32 List<Node> _eventTargets; | |
| 32 | 33 |
| 33 // Setup | 34 // Setup |
| 34 | 35 |
| 35 SpriteBox(Node rootNode, [SpriteBoxTransformMode mode = SpriteBoxTransformMode .nativePoints, double width=1024.0, double height=1024.0]) { | 36 SpriteBox(Node rootNode, [SpriteBoxTransformMode mode = SpriteBoxTransformMode .nativePoints, double width=1024.0, double height=1024.0]) { |
| 36 assert(rootNode != null); | 37 assert(rootNode != null); |
| 37 assert(rootNode._spriteBox == null); | 38 assert(rootNode._spriteBox == null); |
| 38 | 39 |
| 39 // Setup root node | 40 // Setup root node |
| 40 _rootNode = rootNode; | 41 _rootNode = rootNode; |
| 41 | 42 |
| 42 // Assign SpriteBox reference to all the nodes | 43 // Assign SpriteBox reference to all the nodes |
| 43 _addSpriteBoxReference(_rootNode); | 44 _addSpriteBoxReference(_rootNode); |
| 44 | 45 |
| 45 // Setup transform mode | 46 // Setup transform mode |
| 46 transformMode = mode; | 47 transformMode = mode; |
| 47 _systemWidth = width; | 48 _systemWidth = width; |
| 48 _systemHeight = height; | 49 _systemHeight = height; |
| 49 | 50 |
| 50 _transformMatrixIsDirty = true; | |
| 51 | |
| 52 _scheduleTick(); | 51 _scheduleTick(); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void _addSpriteBoxReference(Node node) { | 54 void _addSpriteBoxReference(Node node) { |
| 56 node._spriteBox = this; | 55 node._spriteBox = this; |
| 57 for (Node child in node._children) { | 56 for (Node child in node._children) { |
| 58 _addSpriteBoxReference(child); | 57 _addSpriteBoxReference(child); |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 // Properties | 61 // Properties |
| 63 | 62 |
| 64 double get systemWidth => _systemWidth; | 63 double get systemWidth => _systemWidth; |
| 65 double get systemHeight => _systemHeight; | 64 double get systemHeight => _systemHeight; |
| 66 | 65 |
| 67 Node get rootNode => _rootNode; | 66 Node get rootNode => _rootNode; |
| 68 | 67 |
| 69 void performLayout() { | 68 void performLayout() { |
| 70 size = constraints.constrain(Size.infinite); | 69 size = constraints.constrain(Size.infinite); |
| 71 _transformMatrixIsDirty = true; | 70 _invalidateTransformMatrix(); |
| 72 _callSpriteBoxPerformedLayout(_rootNode); | 71 _callSpriteBoxPerformedLayout(_rootNode); |
| 73 } | 72 } |
| 74 | 73 |
| 75 // Event handling | 74 // Event handling |
| 76 | 75 |
| 77 void handleEvent(Event event, BoxHitTestEntry entry) { | 76 void _addEventTargets(Node node, List<Node> eventTargets) { |
| 77 if (node.userInteractionEnabled) { | |
| 78 eventTargets.add(node); | |
| 79 } | |
| 80 for (Node child in node.children) { | |
| 81 _addEventTargets(child, eventTargets); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void handleEvent(Event event, SpriteBoxHitTestEntry entry) { | |
| 86 if (event is PointerEvent) { | |
| 87 | |
| 88 if (event.type == 'pointerdown') { | |
| 89 // Build list of event targets | |
| 90 if (_eventTargets == null) { | |
| 91 _eventTargets = []; | |
| 92 _addEventTargets(_rootNode, _eventTargets); | |
| 93 } | |
| 94 | |
| 95 // Find the once that are hit by the pointer | |
| 96 List<Node> nodeTargets = []; | |
| 97 for (int i = _eventTargets.length - 1; i >= 0; i--) { | |
| 98 Node node = _eventTargets[i]; | |
| 99 | |
| 100 // Check if the node is ready to handle a pointer | |
| 101 if (node.handleMultiplePointers || node._handlingPointer == null) { | |
| 102 // Do the hit test | |
| 103 Point posInNodeSpace = node.convertPointToNodeSpace(entry.localPosit ion); | |
| 104 if (node.hitTest(posInNodeSpace)) { | |
|
abarth-chromium
2015/06/18 21:19:21
Do you want to move this work into the |hitTest| f
| |
| 105 nodeTargets.add(node); | |
| 106 node._handlingPointer = event.pointer; | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 entry.nodeTargets = nodeTargets; | |
| 112 } | |
| 113 | |
| 114 // Pass the event down to nodes that were hit by the pointerdown | |
| 115 List<Node> targets = entry.nodeTargets; | |
| 116 for (Node node in targets) { | |
| 117 // Check if this event should be dispatched | |
| 118 if (node.handleMultiplePointers || event.pointer == node._handlingPointe r) { | |
| 119 // Dispatch event | |
| 120 bool consumedEvent = node.handleEvent(new SpriteBoxEvent(new Point(eve nt.x, event.y), event.type, event.pointer)); | |
| 121 if (consumedEvent == null || consumedEvent) break; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 // De-register pointer for nodes that doesn't handle multiple pointers | |
| 126 for (Node node in targets) { | |
| 127 if (event.type == 'pointerup' || event.type == 'pointercancel') { | |
| 128 node._handlingPointer = null; | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 bool hitTest(HitTestResult result, { Point position }) { | |
| 135 result.add(new SpriteBoxHitTestEntry(this, position)); | |
| 136 return true; | |
| 78 } | 137 } |
| 79 | 138 |
| 80 // Rendering | 139 // Rendering |
| 81 | 140 |
| 82 Matrix4 get transformMatrix { | 141 Matrix4 get transformMatrix { |
| 83 // Get cached matrix if available | 142 // Get cached matrix if available |
| 84 if (!_transformMatrixIsDirty && _transformMatrix != null) { | 143 if (_transformMatrix != null) { |
| 85 return _transformMatrix; | 144 return _transformMatrix; |
| 86 } | 145 } |
| 87 | 146 |
| 88 _transformMatrix = new Matrix4.identity(); | 147 _transformMatrix = new Matrix4.identity(); |
| 89 | 148 |
| 90 // Calculate matrix | 149 // Calculate matrix |
| 91 double scaleX = 1.0; | 150 double scaleX = 1.0; |
| 92 double scaleY = 1.0; | 151 double scaleY = 1.0; |
| 93 double offsetX = 0.0; | 152 double offsetX = 0.0; |
| 94 double offsetY = 0.0; | 153 double offsetY = 0.0; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 assert(false); | 197 assert(false); |
| 139 break; | 198 break; |
| 140 } | 199 } |
| 141 | 200 |
| 142 _transformMatrix.translate(offsetX, offsetY); | 201 _transformMatrix.translate(offsetX, offsetY); |
| 143 _transformMatrix.scale(scaleX, scaleY); | 202 _transformMatrix.scale(scaleX, scaleY); |
| 144 | 203 |
| 145 return _transformMatrix; | 204 return _transformMatrix; |
| 146 } | 205 } |
| 147 | 206 |
| 207 void _invalidateTransformMatrix() { | |
| 208 _transformMatrix = null; | |
| 209 _rootNode._invalidateToBoxTransformMatrix(); | |
| 210 } | |
| 211 | |
| 148 void paint(RenderObjectDisplayList canvas) { | 212 void paint(RenderObjectDisplayList canvas) { |
| 149 canvas.save(); | 213 canvas.save(); |
| 150 | 214 |
| 151 // Move to correct coordinate space before drawing | 215 // Move to correct coordinate space before drawing |
| 152 canvas.concat(transformMatrix.storage); | 216 canvas.concat(transformMatrix.storage); |
| 153 | 217 |
| 154 // Draw the sprite tree | 218 // Draw the sprite tree |
| 155 _rootNode.visit(canvas); | 219 _rootNode.visit(canvas); |
| 156 | 220 |
| 157 canvas.restore(); | 221 canvas.restore(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 for (Node child in node.children) { | 282 for (Node child in node.children) { |
| 219 _addNodesAtPosition(child, position, list); | 283 _addNodesAtPosition(child, position, list); |
| 220 } | 284 } |
| 221 // Do the hit test | 285 // Do the hit test |
| 222 Point posInNodeSpace = node.convertPointToNodeSpace(position); | 286 Point posInNodeSpace = node.convertPointToNodeSpace(position); |
| 223 if (node.hitTest(posInNodeSpace)) { | 287 if (node.hitTest(posInNodeSpace)) { |
| 224 list.add(node); | 288 list.add(node); |
| 225 } | 289 } |
| 226 } | 290 } |
| 227 } | 291 } |
| 292 | |
| 293 class SpriteBoxHitTestEntry extends BoxHitTestEntry { | |
| 294 List<Node> nodeTargets; | |
| 295 SpriteBoxHitTestEntry(RenderBox target, Point localPosition) : super(target, l ocalPosition); | |
| 296 } | |
| 297 | |
| 298 class SpriteBoxEvent { | |
| 299 Point boxPosition; | |
| 300 String type; | |
| 301 int pointer; | |
| 302 | |
| 303 SpriteBoxEvent(this.boxPosition, this.type, this.pointer); | |
| 304 } | |
| OLD | NEW |