paint method
- @override
Paints this node to the canvas.
Subclasses, such as Sprite, override this method to do the actual painting of the node. To do custom
drawing override this method and make calls to the canvas
object. All drawing is done in the node's local
coordinate system, relative to the node's position. If you want to make the drawing relative to the node's
bounding box's origin, override NodeWithSize and call the applyTransformForPivot method before making calls for
drawing.
void paint(Canvas canvas) {
canvas.save();
applyTransformForPivot(canvas);
// Do painting here
canvas.restore();
}
Implementation
@override
void paint(Canvas canvas) {
applyTransformForPivot(canvas);
// Setup paint object for opacity and transfer mode.
_updatePaint(_cachedPaint);
if (_isDirty) {
// Calcuate vertices and indices.
_vertices = <Offset>[
Offset.zero,
];
// Texture width and height.
double tw = texture.frame.width;
double th = texture.frame.height;
_textureCoordinates = <Offset>[];
_vertices = <Offset>[];
_colors = <Color>[];
for (int y = 0; y < 4; y += 1) {
double vy;
double ty;
switch(y) {
case 0:
vy = 0.0;
ty = texture.frame.top;
break;
case 1:
vy = insets.top * th;
ty = texture.frame.top + insets.top * th;
break;
case 2:
vy = size.height - insets.bottom * th;
ty = texture.frame.bottom - insets.bottom * th;
break;
case 3:
vy = size.height;
ty = texture.frame.bottom;
break;
}
for (int x = 0; x < 4; x += 1) {
double vx;
double tx;
switch(x) {
case 0:
vx = 0.0;
tx = texture.frame.left;
break;
case 1:
vx = insets.left * tw;
tx = texture.frame.left + insets.left * tw;
break;
case 2:
vx = size.width - insets.right * tw;
tx = texture.frame.right - insets.right * tw;
break;
case 3:
vx = size.width;
tx = texture.frame.right;
break;
}
_vertices.add(new Offset(vx, vy));
_textureCoordinates.add(new Offset(tx, ty));
_colors.add(const Color(0xffffffff));
}
}
// Build indices.
_indices = <int>[];
for (int y = 0; y < 3; y += 1) {
for (int x = 0; x < 3; x += 1) {
// Check if we should skip the middle rectangle.
if (!drawCenterPart && x == 1 && y == 1)
continue;
// Add a rectangle (two triangles).
int index = y * 4 + x;
_indices.add(index);
_indices.add(index + 1);
_indices.add(index + 4);
_indices.add(index + 1);
_indices.add(index + 5);
_indices.add(index + 4);
}
}
}
// TODO: Fix
// canvas.drawVertices(
// VertexMode.triangles,
// _vertices,
// _textureCoordinates,
// _colors,
// BlendMode.modulate,
// _indices,
// _cachedPaint
// );
}