OLD | NEW |
1 part of sprites; | 1 part of sprites; |
2 | 2 |
| 3 /// The super class of any [Node] that has a size. |
| 4 /// |
| 5 /// NodeWithSize adds the ability for a node to have a size and a pivot point. |
3 abstract class NodeWithSize extends Node { | 6 abstract class NodeWithSize extends Node { |
| 7 |
| 8 /// Changing the size will affect the size of the rendering of the node. |
| 9 /// |
| 10 /// myNode.size = new Size(1024.0, 1024.0); |
4 Size size; | 11 Size size; |
| 12 |
| 13 /// The normalized point which the node is transformed around. |
| 14 /// |
| 15 /// // Position myNode from is middle top |
| 16 /// myNode.pivot = new Point(0.5, 0.0); |
5 Point pivot; | 17 Point pivot; |
6 | 18 |
7 NodeWithSize() { | 19 /// Creates a new NodeWithSize. |
8 size = Size.zero; | 20 /// |
9 pivot = Point.origin; | 21 /// The default [size] is zero and the default [pivot] point is the origin. Su
bclasses may change the default values. |
10 } | 22 /// |
11 | 23 /// var myNodeWithSize = new NodeWithSize(new Size(1024.0, 1024.0)); |
12 NodeWithSize.withSize(Size this.size, [Point this.pivot]) { | 24 NodeWithSize([Size this.size, Point this.pivot]) { |
| 25 if (size == null) size = Size.zero; |
13 if (pivot == null) pivot = Point.origin; | 26 if (pivot == null) pivot = Point.origin; |
14 } | 27 } |
15 | 28 |
| 29 /// Call this method in your [paint] method if you want the origin of your dra
wing to be the top left corner of the |
| 30 /// node's bounding box. |
| 31 /// |
| 32 /// If you use this method you will need to save and restore your canvas at th
e beginning and |
| 33 /// end of your [paint] method. |
| 34 /// |
| 35 /// void paint(PictureRecorder canvas) { |
| 36 /// canvas.save(); |
| 37 /// applyTransformForPivot(canvas); |
| 38 /// |
| 39 /// // Do painting here |
| 40 /// |
| 41 /// canvas.restore(); |
| 42 /// } |
16 void applyTransformForPivot(PictureRecorder canvas) { | 43 void applyTransformForPivot(PictureRecorder canvas) { |
17 if (pivot.x != 0 || pivot.y != 0) { | 44 if (pivot.x != 0 || pivot.y != 0) { |
18 double pivotInPointsX = size.width * pivot.x; | 45 double pivotInPointsX = size.width * pivot.x; |
19 double pivotInPointsY = size.height * pivot.y; | 46 double pivotInPointsY = size.height * pivot.y; |
20 canvas.translate(-pivotInPointsX, -pivotInPointsY); | 47 canvas.translate(-pivotInPointsX, -pivotInPointsY); |
21 } | 48 } |
22 } | 49 } |
23 | 50 |
24 bool isPointInside (Point nodePoint) { | 51 bool isPointInside (Point nodePoint) { |
25 | 52 |
26 double minX = -size.width * pivot.x; | 53 double minX = -size.width * pivot.x; |
27 double minY = -size.height * pivot.y; | 54 double minY = -size.height * pivot.y; |
28 double maxX = minX + size.width; | 55 double maxX = minX + size.width; |
29 double maxY = minY + size.height; | 56 double maxY = minY + size.height; |
30 return (nodePoint.x >= minX && nodePoint.x < maxX && | 57 return (nodePoint.x >= minX && nodePoint.x < maxX && |
31 nodePoint.y >= minY && nodePoint.y < maxY); | 58 nodePoint.y >= minY && nodePoint.y < maxY); |
32 } | 59 } |
33 } | 60 } |
OLD | NEW |