| OLD | NEW |
| 1 part of sprites; | 1 part of sprites; |
| 2 | 2 |
| 3 /// A texture represents a rectangular area of an image and is typically used to
draw a sprite to the screen. | 3 /// A texture represents a rectangular area of an image and is typically used to
draw a sprite to the screen. |
| 4 /// | 4 /// |
| 5 /// Normally you get a reference to a texture from a [SpriteSheet], but you can
also create one from an [Image]. | 5 /// Normally you get a reference to a texture from a [SpriteSheet], but you can
also create one from an [Image]. |
| 6 class Texture { | 6 class Texture { |
| 7 /// The image that this texture is a part of. | 7 /// The image that this texture is a part of. |
| 8 /// | 8 /// |
| 9 /// var textureImage = myTexture.image; | 9 /// var textureImage = myTexture.image; |
| 10 final Image image; | 10 final Image image; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 rotated = false, | 58 rotated = false, |
| 59 frame = new Rect.fromLTRB(0.0, 0.0, image.width.toDouble(), image.height.toD
ouble()), | 59 frame = new Rect.fromLTRB(0.0, 0.0, image.width.toDouble(), image.height.toD
ouble()), |
| 60 spriteSourceSize = new Rect.fromLTRB(0.0, 0.0, image.width.toDouble(), image
.height.toDouble()), | 60 spriteSourceSize = new Rect.fromLTRB(0.0, 0.0, image.width.toDouble(), image
.height.toDouble()), |
| 61 pivot = new Point(0.5, 0.5); | 61 pivot = new Point(0.5, 0.5); |
| 62 | 62 |
| 63 | 63 |
| 64 Texture._fromSpriteFrame(this.image, this.name, this.size, this.rotated, this.
trimmed, this.frame, | 64 Texture._fromSpriteFrame(this.image, this.name, this.size, this.rotated, this.
trimmed, this.frame, |
| 65 this.spriteSourceSize, this.pivot) { | 65 this.spriteSourceSize, this.pivot) { |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Texture textureFromRect(Rect rect, [String name = null]) { | 68 Texture textureFromRect(Rect rect, [String name = null]) { |
| 69 // assert(rect != null); | 69 assert(rect != null); |
| 70 // Rect frame = new Rect.fromLTRB(); | 70 assert(!rotated); |
| 71 // return new Texture._fromSpriteFrame(image, name, rect.size, false, false,
); | 71 Rect srcFrame = new Rect.fromLTWH(rect.left + frame.left, rect.top + frame.t
op, rect.size.width, rect.size.height); |
| 72 // } | 72 Rect dstFrame = new Rect.fromLTWH(0.0, 0.0, rect.size.width, rect.size.heigh
t); |
| 73 return new Texture._fromSpriteFrame(image, name, rect.size, false, false, sr
cFrame, dstFrame, new Point(0.5, 0.5)); |
| 74 } |
| 75 |
| 76 void drawTexture(PaintingCanvas canvas, Point position, Paint paint) { |
| 77 |
| 78 // Account for position |
| 79 double x = position.x; |
| 80 double y = position.y; |
| 81 bool translate = (x != 0 || y != 0); |
| 82 if (translate) { |
| 83 canvas.translate(x, y); |
| 84 } |
| 85 |
| 86 // Draw the texture |
| 87 if (rotated) { |
| 88 // Calculate the rotated frame and spriteSourceSize |
| 89 Size originalFrameSize = frame.size; |
| 90 Rect rotatedFrame = new Rect.fromPointAndSize(frame.upperLeft, new Size(or
iginalFrameSize.height, originalFrameSize.width)); |
| 91 Point rotatedSpriteSourcePoint = new Point( |
| 92 -spriteSourceSize.top - (spriteSourceSize.bottom - spriteSourceSize.to
p), |
| 93 spriteSourceSize.left); |
| 94 Rect rotatedSpriteSourceSize = new Rect.fromPointAndSize(rotatedSpriteSour
cePoint, new Size(originalFrameSize.height, originalFrameSize.width)); |
| 95 |
| 96 // Draw the rotated sprite |
| 97 canvas.rotate(-Math.PI/2.0); |
| 98 canvas.drawImageRect(image, rotatedFrame, rotatedSpriteSourceSize, paint); |
| 99 canvas.rotate(Math.PI/2.0); |
| 100 } else { |
| 101 // Draw the sprite |
| 102 canvas.drawImageRect(image, frame, spriteSourceSize, paint); |
| 103 } |
| 104 |
| 105 // Translate back |
| 106 if (translate) { |
| 107 canvas.translate(-x, -y); |
| 108 } |
| 109 } |
| 73 } | 110 } |
| OLD | NEW |