OLD | NEW |
(Empty) | |
| 1 part of sprites; |
| 2 |
| 3 double degrees2radians(double degrees) => degrees * Math.PI/180.8; |
| 4 |
| 5 double radians2degrees(double radians) => radians * 180.0/Math.PI; |
| 6 |
| 7 class TransformNode { |
| 8 Vector2 _position; |
| 9 double _rotation; |
| 10 |
| 11 bool _isMatrixDirty; |
| 12 Matrix3 _transform; |
| 13 Matrix3 _pivotTransform; |
| 14 |
| 15 double _width; |
| 16 double _height; |
| 17 |
| 18 Vector2 _pivot; |
| 19 |
| 20 List<TransformNode>children; |
| 21 |
| 22 |
| 23 TransformNode() { |
| 24 _width = 0.0; |
| 25 _height = 0.0; |
| 26 _rotation = 0.0; |
| 27 _pivot = new Vector2(0.0, 0.0); |
| 28 _position = new Vector2(0.0, 0.0); |
| 29 _isMatrixDirty = false; |
| 30 _transform = new Matrix3.identity(); |
| 31 _pivotTransform = new Matrix3.identity(); |
| 32 children = []; |
| 33 } |
| 34 |
| 35 double get rotation => _rotation; |
| 36 |
| 37 void set rotation(double rotation) { |
| 38 _rotation = rotation; |
| 39 _isMatrixDirty = true; |
| 40 } |
| 41 |
| 42 |
| 43 Vector2 get position => _position; |
| 44 |
| 45 void set position(Vector2 position) { |
| 46 _position = position; |
| 47 _isMatrixDirty = true; |
| 48 } |
| 49 |
| 50 double get width => _width; |
| 51 |
| 52 void set width(double width) { |
| 53 _width = width; |
| 54 _isMatrixDirty = true; |
| 55 } |
| 56 |
| 57 |
| 58 double get height => _height; |
| 59 |
| 60 void set height(double height) { |
| 61 _height = height; |
| 62 _isMatrixDirty = true; |
| 63 } |
| 64 |
| 65 Vector2 get pivot => _pivot; |
| 66 |
| 67 void set pivot(Vector2 pivot) { |
| 68 _pivot = pivot; |
| 69 _isMatrixDirty = true; |
| 70 } |
| 71 |
| 72 |
| 73 Matrix3 get transformMatrix { |
| 74 if (!_isMatrixDirty) { |
| 75 return _transform; |
| 76 } |
| 77 |
| 78 Vector2 pivotInPoints = new Vector2(_width * _pivot[0], _height * _pivot[1])
; |
| 79 |
| 80 double cx, sx, cy, sy; |
| 81 |
| 82 if (_rotation == 0) { |
| 83 cx = 1.0; |
| 84 sx = 0.0; |
| 85 cy = 1.0; |
| 86 sy = 0.0; |
| 87 } |
| 88 else { |
| 89 double radiansX = degrees2radians(_rotation); |
| 90 double radiansY = degrees2radians(_rotation); |
| 91 |
| 92 cx = Math.cos(radiansX); |
| 93 sx = Math.sin(radiansX); |
| 94 cy = Math.cos(radiansY); |
| 95 sy = Math.sin(radiansY); |
| 96 } |
| 97 |
| 98 // TODO: Add support for scale |
| 99 double scaleX = 1.0; |
| 100 double scaleY = 1.0; |
| 101 |
| 102 // Create transformation matrix for scale, position and rotation |
| 103 _transform.setValues(cy * scaleX, sy * scaleX, 0.0, |
| 104 -sx * scaleY, cx * scaleY, 0.0, |
| 105 _position[0], _position[1], 1.0); |
| 106 |
| 107 if (_pivot.x != 0 || _pivot.y != 0) { |
| 108 _pivotTransform.setValues(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, pivotInPoints[0],
pivotInPoints[1], 1.0); |
| 109 _transform.multiply(_pivotTransform); |
| 110 } |
| 111 |
| 112 return _transform; |
| 113 } |
| 114 |
| 115 void visit(PictureRecorder canvas) { |
| 116 prePaint(canvas); |
| 117 paint(canvas); |
| 118 visitChildren(canvas); |
| 119 postPaint(canvas); |
| 120 } |
| 121 |
| 122 void prePaint(PictureRecorder canvas) { |
| 123 canvas.save(); |
| 124 |
| 125 canvas.translate(_position[0], _position[1]); |
| 126 canvas.rotateDegrees(_rotation); |
| 127 canvas.translate(-_width*_pivot[0], -_height*_pivot[1]); |
| 128 |
| 129 // TODO: Use transformation matrix instead of individual calls |
| 130 // List<double> matrix = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; |
| 131 // this.transformMatrix.copyIntoArray(matrix); |
| 132 // canvas.concat(matrix); |
| 133 } |
| 134 |
| 135 void paint(PictureRecorder canvas) { |
| 136 |
| 137 } |
| 138 |
| 139 void visitChildren(PictureRecorder canvas) { |
| 140 children.forEach((child) => child.visit(canvas)); |
| 141 } |
| 142 |
| 143 void postPaint(PictureRecorder canvas) { |
| 144 canvas.restore(); |
| 145 } |
| 146 } |
OLD | NEW |