2020-07-23 11:00:46 +08:00
|
|
|
module es {
|
|
|
|
|
/** 各种辅助方法来辅助绘图 */
|
|
|
|
|
export class DrawUtils {
|
2020-07-28 16:25:20 +08:00
|
|
|
public static drawLine(shape: egret.Shape, start: Vector2, end: Vector2, color: number, thickness: number = 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
this.drawLineAngle(shape, start, MathHelper.angleBetweenVectors(start, end), Vector2.distance(start, end), color, thickness);
|
|
|
|
|
}
|
2020-07-09 16:16:04 +08:00
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
public static drawLineAngle(shape: egret.Shape, start: Vector2, radians: number, length: number, color: number, thickness = 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
shape.graphics.beginFill(color);
|
|
|
|
|
shape.graphics.drawRect(start.x, start.y, 1, 1);
|
|
|
|
|
shape.graphics.endFill();
|
2020-07-09 16:16:04 +08:00
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
shape.scaleX = length;
|
|
|
|
|
shape.scaleY = thickness;
|
|
|
|
|
shape.$anchorOffsetX = 0;
|
|
|
|
|
shape.$anchorOffsetY = 0;
|
|
|
|
|
shape.rotation = radians;
|
|
|
|
|
}
|
2020-07-09 16:16:04 +08:00
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
public static drawHollowRect(shape: egret.Shape, rect: Rectangle, color: number, thickness = 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
this.drawHollowRectR(shape, rect.x, rect.y, rect.width, rect.height, color, thickness);
|
|
|
|
|
}
|
2020-07-09 16:16:04 +08:00
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
public static drawHollowRectR(shape: egret.Shape, x: number, y: number, width: number, height: number, color: number, thickness = 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
let tl = new Vector2(x, y).round();
|
|
|
|
|
let tr = new Vector2(x + width, y).round();
|
|
|
|
|
let br = new Vector2(x + width, y + height).round();
|
|
|
|
|
let bl = new Vector2(x, y + height).round();
|
2020-07-09 16:16:04 +08:00
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
this.drawLine(shape, tl, tr, color, thickness);
|
|
|
|
|
this.drawLine(shape, tr, br, color, thickness);
|
|
|
|
|
this.drawLine(shape, br, bl, color, thickness);
|
|
|
|
|
this.drawLine(shape, bl, tl, color, thickness);
|
2020-07-09 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
public static drawPixel(shape: egret.Shape, position: Vector2, color: number, size: number = 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
let destRect = new Rectangle(position.x, position.y, size, size);
|
2020-07-28 16:25:20 +08:00
|
|
|
if (size != 1) {
|
2020-07-23 11:00:46 +08:00
|
|
|
destRect.x -= size * 0.5;
|
|
|
|
|
destRect.y -= size * 0.5;
|
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
shape.graphics.beginFill(color);
|
|
|
|
|
shape.graphics.drawRect(destRect.x, destRect.y, destRect.width, destRect.height);
|
|
|
|
|
shape.graphics.endFill();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
public static getColorMatrix(color: number): egret.ColorMatrixFilter {
|
2020-07-23 11:00:46 +08:00
|
|
|
let colorMatrix = [
|
|
|
|
|
1, 0, 0, 0, 0,
|
|
|
|
|
0, 1, 0, 0, 0,
|
|
|
|
|
0, 0, 1, 0, 0,
|
|
|
|
|
0, 0, 0, 1, 0
|
|
|
|
|
];
|
|
|
|
|
colorMatrix[0] = Math.floor(color / 256 / 256) / 255;
|
|
|
|
|
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
|
|
|
|
|
colorMatrix[12] = color % 256 / 255;
|
|
|
|
|
return new egret.ColorMatrixFilter(colorMatrix);
|
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
}
|