Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image组件新增功能:支持上下对称,左右对称,上下左右对称的方式填充纹理 #739

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/layaAir/laya/display/Graphics.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ import { Texture } from "../resource/Texture"
import { Utils } from "../utils/Utils"
import { VectorGraphManager } from "../utils/VectorGraphManager"
import { ILaya } from "../../ILaya";
import { DrawSymmetricTexture } from "./cmd/DrawSymmetricTexture";


/**
* <code>Graphics</code> 类用于创建绘图显示对象。Graphics可以同时绘制多个位图或者矢量图,还可以结合save,restore,transform,scale,rotate,translate,alpha等指令对绘图效果进行变化。
@@ -735,4 +737,18 @@ export class Graphics {
draw9Grid(texture: Texture, x: number = 0, y: number = 0, width: number = 0, height: number = 0, sizeGrid: any[]): void {
this._saveToCmd(null, Draw9GridTexture.create(texture, x, y, width, height, sizeGrid));
}
}

/**
* @private
* 绘制对称填充纹理的图片
* @param texture
* @param x
* @param y
* @param width
* @param height
* @param type
*/
drawSymmetric(texture:Texture, x=0,y=0,width=0,height=0,type=0) {
this._saveToCmd(null, DrawSymmetricTexture.create(texture, x, y, width, height, type));
}
}
21 changes: 13 additions & 8 deletions src/layaAir/laya/display/GraphicsBounds.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ import { DrawTrianglesCmd } from "./cmd/DrawTrianglesCmd";
import { Draw9GridTexture } from "./cmd/Draw9GridTexture";
import { ClassUtils } from "../utils/ClassUtils";
import { SaveCmd } from "./cmd/SaveCmd"
import {DrawSymmetricTexture} from "./cmd/DrawSymmetricTexture";

/**
* @private
@@ -150,7 +151,7 @@ export class GraphicsBounds {

this._switchMatrix(tMatrix, tempMatrix);
break;
case RotateCmd.ID://case context._rotate:
case RotateCmd.ID://case context._rotate:
tempMatrix.identity();
tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
tempMatrix.rotate(cmd.angle);
@@ -172,11 +173,11 @@ export class GraphicsBounds {

this._switchMatrix(tMatrix, tempMatrix);
break;
case DrawImageCmd.ID://case context._drawTexture:
case DrawImageCmd.ID://case context._drawTexture:
case FillTextureCmd.ID://case context._fillTexture
GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
break;
case DrawTextureCmd.ID://case context._drawTextureTransform:
case DrawTextureCmd.ID://case context._drawTextureTransform:
tMatrix.copyTo(tempMatrix);
if (cmd.matrix)
tempMatrix.concat(cmd.matrix);
@@ -267,7 +268,7 @@ export class GraphicsBounds {

GraphicsBounds._addPointArrToRst(rst, GraphicsBounds._tempPoints, tMatrix);
break;
case DrawCurvesCmd.ID://context._drawCurves:
case DrawCurvesCmd.ID://context._drawCurves:
GraphicsBounds._addPointArrToRst(rst, Bezier.I.getBezierPoints(cmd.points), tMatrix, cmd.x, cmd.y);
break;
case DrawLinesCmd.ID://drawpoly
@@ -286,6 +287,10 @@ export class GraphicsBounds {
case Draw9GridTexture.ID:
GraphicsBounds._addPointArrToRst(rst, this._getDraw9GridBBXPoints(cmd as Draw9GridTexture), tMatrix);
break;
case DrawSymmetricTexture.ID:
//这两个的绘制原理差不多,因此获取包围盒的方式也用一样的
GraphicsBounds._addPointArrToRst(rst, this._getDraw9GridBBXPoints(cmd as Draw9GridTexture), tMatrix);
break;
}
}
if (rst.length > 200) {
@@ -337,7 +342,7 @@ export class GraphicsBounds {
rst.push(x - radius, y + radius);
return rst;
}
//
//
rst.push(x, y); // 中心

var delta: number = d1 % 360;
@@ -366,13 +371,13 @@ export class GraphicsBounds {
return rst;
/*
var segnum:int = 32;
var step:Number = delta / segnum;
var step:Number = delta / segnum;
var i:Number;
var angle:Number = startAngle;
for (i = 0; i <= segnum; i++) {
rst.push(x + radius * Math.cos(angle), y + radius * Math.sin(angle));
angle += step;
}
}
*/
}

@@ -420,4 +425,4 @@ export class GraphicsBounds {
}
return rst;
}
}
}
72 changes: 72 additions & 0 deletions src/layaAir/laya/display/cmd/DrawSymmetricTexture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/** created by WangCheng on 2021/9/12 17:56 */

import {Texture} from "../../resource/Texture";
import {Pool} from "../../utils/Pool";
import {Context} from "../../resource/Context";

/**
* 绘制对称填充纹理的图片
* @internal
*/
export class DrawSymmetricTexture {

static ID: string = "DrawSymmetricTexture";

/**
* 纹理。
*/
texture: Texture;
/**
* (可选)X轴偏移量。
*/
x: number;
/**
* (可选)Y轴偏移量。
*/
y: number;
/**
* (可选)宽度。
*/
width: number;
/**
* (可选)高度。
*/
height: number;
/**纹理填充类型, 默认为 0 */
type: number;



/**@private */
static create(texture: Texture, x: number, y: number, width: number, height: number, type:number): DrawSymmetricTexture {
var cmd: DrawSymmetricTexture = Pool.getItemByClass("DrawSymmetricTexture", DrawSymmetricTexture);
cmd.texture = texture;
texture._addReference();
cmd.x = x;
cmd.y = y;
cmd.width = width;
cmd.height = height;
cmd.type = type;

return cmd;
}

/**
* 回收到对象池
*/
recover(): void {
this.texture._removeReference();
Pool.recover("DrawSymmetricTexture", this);
}

/**@private */
run(context: Context, gx: number, gy: number): void {
context.drawSymmetricTexture(this.texture, this.x, this.y, this.width, this.height, this.type, gx, gy);
}

/**@private */
get cmdID(): string {
return DrawSymmetricTexture.ID;
}

}
Loading