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 // Root node for drawing | 14 // Root node for drawing |
15 TransformNode _rootNode; | 15 TransformNode _rootNode; |
16 | 16 |
17 // Tracking of frame rate and updates | 17 // Tracking of frame rate and updates |
18 double _lastTimeStamp; | 18 double _lastTimeStamp; |
19 int _numFrames = 0; | 19 int _numFrames = 0; |
20 | 20 |
21 SpriteBoxTransformMode transformMode; | 21 SpriteBoxTransformMode transformMode; |
22 double systemWidth; | 22 double _systemWidth; |
23 double systemHeight; | 23 double _systemHeight; |
24 | 24 |
25 SpriteBox(TransformNode rootNode, [SpriteBoxTransformMode mode = SpriteBoxTran
sformMode.nativePoints, double width=1024.0, double height=1024.0]) { | 25 SpriteBox(TransformNode rootNode, [SpriteBoxTransformMode mode = SpriteBoxTran
sformMode.nativePoints, double width=1024.0, double height=1024.0]) { |
26 // Setup root node | 26 // Setup root node |
27 _rootNode = rootNode; | 27 _rootNode = rootNode; |
28 | 28 |
29 // Setup transform mode | 29 // Setup transform mode |
30 transformMode = mode; | 30 transformMode = mode; |
31 systemWidth = width; | 31 _systemWidth = width; |
32 systemHeight = height; | 32 _systemHeight = height; |
33 | 33 |
34 _scheduleTick(); | 34 _scheduleTick(); |
35 } | 35 } |
36 | 36 |
| 37 double get systemWidth => _systemWidth; |
| 38 double get systemHeight => _systemHeight; |
| 39 |
37 void performLayout() { | 40 void performLayout() { |
38 size = constraints.constrain(new Size.infinite()); | 41 size = constraints.constrain(new Size.infinite()); |
39 } | 42 } |
40 | 43 |
41 void handlePointer(PointerEvent event) { | 44 void handlePointer(PointerEvent event) { |
42 switch (event.type) { | 45 switch (event.type) { |
43 case 'pointerdown': | 46 case 'pointerdown': |
44 print("pointerdown"); | 47 print("pointerdown"); |
45 break; | 48 break; |
46 } | 49 } |
47 } | 50 } |
48 | 51 |
49 void paint(RenderNodeDisplayList canvas) { | 52 void paint(RenderNodeDisplayList canvas) { |
50 // Move to correct coordinate space before drawing | 53 // Move to correct coordinate space before drawing |
51 double scaleX = 1.0; | 54 double scaleX = 1.0; |
52 double scaleY = 1.0; | 55 double scaleY = 1.0; |
53 double offsetX = 0.0; | 56 double offsetX = 0.0; |
54 double offsetY = 0.0; | 57 double offsetY = 0.0; |
55 | 58 |
56 switch(transformMode) { | 59 switch(transformMode) { |
57 case SpriteBoxTransformMode.stretch: | 60 case SpriteBoxTransformMode.stretch: |
58 scaleX = size.width/systemWidth; | 61 scaleX = size.width/_systemWidth; |
59 scaleY = size.height/systemHeight; | 62 scaleY = size.height/_systemHeight; |
60 break; | 63 break; |
61 case SpriteBoxTransformMode.letterbox: | 64 case SpriteBoxTransformMode.letterbox: |
62 scaleX = size.width/systemWidth; | 65 scaleX = size.width/_systemWidth; |
63 scaleY = size.height/systemHeight; | 66 scaleY = size.height/_systemHeight; |
64 if (scaleX > scaleY) { | 67 if (scaleX > scaleY) { |
65 scaleY = scaleX; | 68 scaleY = scaleX; |
66 offsetY = (size.height - scaleY * systemHeight)/2.0; | 69 offsetY = (size.height - scaleY * _systemHeight)/2.0; |
67 } | 70 } |
68 else { | 71 else { |
69 scaleX = scaleY; | 72 scaleX = scaleY; |
70 offsetX = (size.width - scaleX * systemWidth)/2.0; | 73 offsetX = (size.width - scaleX * _systemWidth)/2.0; |
71 } | 74 } |
72 break; | 75 break; |
| 76 case SpriteBoxTransformMode.scaleToFit: |
| 77 scaleX = size.width/_systemWidth; |
| 78 scaleY = size.height/_systemHeight; |
| 79 if (scaleX < scaleY) { |
| 80 scaleY = scaleX; |
| 81 offsetY = (size.height - scaleY * _systemHeight)/2.0; |
| 82 } |
| 83 else { |
| 84 scaleX = scaleY; |
| 85 offsetX = (size.width - scaleX * _systemWidth)/2.0; |
| 86 } |
| 87 break; |
| 88 case SpriteBoxTransformMode.fixedWidth: |
| 89 scaleX = size.width/_systemWidth; |
| 90 scaleY = scaleX; |
| 91 _systemHeight = size.height/scaleX; |
| 92 print("systemHeight: $_systemHeight"); |
| 93 break; |
| 94 case SpriteBoxTransformMode.fixedHeight: |
| 95 scaleY = size.height/_systemHeight; |
| 96 scaleX = scaleY; |
| 97 _systemWidth = size.width/scaleY; |
| 98 break; |
73 case SpriteBoxTransformMode.nativePoints: | 99 case SpriteBoxTransformMode.nativePoints: |
74 break; | 100 break; |
75 default: | 101 default: |
76 assert(false); | 102 assert(false); |
77 break; | 103 break; |
78 } | 104 } |
79 | 105 |
80 canvas.save(); | 106 canvas.save(); |
81 | 107 |
82 canvas.translate(offsetX, offsetY); | 108 canvas.translate(offsetX, offsetY); |
(...skipping 21 matching lines...) Expand all Loading... |
104 // Count the number of frames we've been running | 130 // Count the number of frames we've been running |
105 _numFrames += 1; | 131 _numFrames += 1; |
106 | 132 |
107 // Print frame rate | 133 // Print frame rate |
108 if (_numFrames % 60 == 0) print("delta: ${delta} fps: ${1.0/delta}"); | 134 if (_numFrames % 60 == 0) print("delta: ${delta} fps: ${1.0/delta}"); |
109 | 135 |
110 _rootNode.update(delta); | 136 _rootNode.update(delta); |
111 _scheduleTick(); | 137 _scheduleTick(); |
112 } | 138 } |
113 } | 139 } |
OLD | NEW |