Merge pull request #3 from unanmed/dev-strawberry

feat(dev-strawberry): graphics & 图标 & winskin & 事件
This commit is contained in:
AncTe authored and GitHub committed 2025-01-12 15:45:28 +08:00
commit 6dde0334e1
12 files changed
+986 -611

No files matched your search

+3
View File
@@ -211,6 +211,7 @@ class TextureCache extends EventEmitter<TextureCacheEvent> {
// enemy48和npc48都应该视为大怪物
if (cls === 'enemy48' || cls === 'npc48') {
const img = core.material.images[cls];
if (!img) return null;
// @ts-ignore
const line = icons[cls][id];
const w = 32;
@@ -234,6 +235,7 @@ class TextureCache extends EventEmitter<TextureCacheEvent> {
}
// 自动元件
if (cls === 'autotile') {
if (!this.autotile) return null;
const auto = this.autotile[num as AllNumbersOf<'autotile'>];
const cell = 32;
const render: [number, number, number, number][] = [];
@@ -264,6 +266,7 @@ class TextureCache extends EventEmitter<TextureCacheEvent> {
core.material.images[
cls as Exclude<Cls, 'tileset' | 'autotile'>
];
if (!image) return null;
const frame = core.getAnimateFrames(cls);
const cell = 32;
// @ts-ignore
+1
View File
@@ -69,6 +69,7 @@ Mota.require('var', 'loading').once('coreInit', () => {
</layer-group>
<Textbox id="main-textbox" {...mainTextboxProps}></Textbox>
<FloorChange id="floor-change" zIndex={50}></FloorChange>
<icon icon={13} animate></icon>
</container>
);
});
+529 -37
View File
@@ -1,7 +1,8 @@
import { MotaOffscreenCanvas2D } from '@/core/fx/canvas2d';
import { ERenderItemEvent, RenderItem } from '../item';
import { ERenderItemEvent, RenderItem, RenderItemPosition } from '../item';
import { Transform } from '../transform';
import { ElementNamespace, ComponentInternalInstance } from 'vue';
import { isNil } from 'lodash-es';
/*
* Expected usage (this comment needs to be deleted after implementing correctly):
@@ -50,52 +51,124 @@ export const enum GraphicMode {
StrokeAndFill
}
const enum GraphicModeProp {
Fill,
Stroke,
StrokeAndFill
}
export interface EGraphicItemEvent extends ERenderItemEvent {}
export abstract class GraphicItemBase
extends RenderItem<EGraphicItemEvent>
implements IGraphicProperty
implements Required<ILineProperty>
{
mode: number = GraphicMode.Fill;
fill: CanvasStyle = '#fff';
stroke: CanvasStyle = '#fff';
lineWidth: number = 2;
lineDash?: number[] | undefined;
lineDashOffset?: number | undefined;
lineDash: number[] = [];
lineDashOffset: number = 0;
lineJoin: CanvasLineJoin = 'bevel';
lineCap: CanvasLineCap = 'butt';
miterLimit: number = 10;
fillRule: CanvasFillRule = 'nonzero';
private propFill: boolean = true;
private propStroke: boolean = false;
private strokeAndFill: boolean = false;
private propFillSet: boolean = false;
/**
* 设置描边绘制的信息
* @param options 线的信息
*/
setLineOption(options: Partial<ILineProperty>) {}
setLineOption(options: Partial<ILineProperty>) {
if (!isNil(options.lineWidth)) this.lineWidth = options.lineWidth;
if (!isNil(options.lineDash)) this.lineDash = options.lineDash;
if (!isNil(options.lineDashOffset))
this.lineDashOffset = options.lineDashOffset;
if (!isNil(options.lineJoin)) this.lineJoin = options.lineJoin;
if (!isNil(options.lineCap)) this.lineCap = options.lineCap;
if (!isNil(options.miterLimit)) this.miterLimit = options.miterLimit;
this.update();
}
/**
* 设置填充样式
* @param style 绘制样式
*/
setFillStyle(style: CanvasStyle) {}
setFillStyle(style: CanvasStyle) {
this.fill = style;
this.update();
}
/**
* 设置描边样式
* @param style 绘制样式
*/
setStrokeStyle(style: CanvasStyle) {}
setStrokeStyle(style: CanvasStyle) {
this.stroke = style;
this.update();
}
/**
* 设置填充原则
* @param rule 填充原则
*/
setFillRule(rule: CanvasFillRule) {}
setFillRule(rule: CanvasFillRule) {
this.fillRule = rule;
this.update();
}
/**
* 设置绘制模式,是描边还是填充
* @param mode 绘制模式
*/
setMode(mode: GraphicMode) {}
setMode(mode: GraphicMode) {
this.mode = mode;
this.update();
}
/**
* 检查渲染模式,参考 {@link GraphicPropsBase} 中的 fill stroke strokeAndFill 属性
*/
private checkMode(mode: GraphicModeProp, value: boolean) {
switch (mode) {
case GraphicModeProp.Fill:
this.propFill = value;
this.propFillSet = true;
break;
case GraphicModeProp.Stroke:
this.propStroke = value;
break;
case GraphicModeProp.StrokeAndFill:
this.strokeAndFill = value;
break;
}
if (this.strokeAndFill) {
this.mode = GraphicMode.StrokeAndFill;
} else {
if (!this.propFillSet) {
if (this.propStroke) {
this.mode = GraphicMode.Stroke;
} else {
this.mode = GraphicMode.Fill;
}
} else {
if (this.propFill && this.propStroke) {
this.mode = GraphicMode.FillAndStroke;
} else if (this.propFill) {
this.mode = GraphicMode.Fill;
} else if (this.propStroke) {
this.mode = GraphicMode.Stroke;
} else {
this.mode = GraphicMode.Fill;
}
}
}
this.update();
}
/**
* 设置画布的渲染状态,在实际渲染前调用
@@ -103,7 +176,14 @@ export abstract class GraphicItemBase
*/
protected setCanvasState(canvas: MotaOffscreenCanvas2D) {
const ctx = canvas.ctx;
ctx.fillStyle = this.fill; // 示例,后面的都按这个写,不需要save restore,写完把这个注释删了
ctx.fillStyle = this.fill;
ctx.strokeStyle = this.stroke;
ctx.lineWidth = this.lineWidth;
ctx.setLineDash(this.lineDash);
ctx.lineDashOffset = this.lineDashOffset;
ctx.lineJoin = this.lineJoin;
ctx.lineCap = this.lineCap;
ctx.miterLimit = this.miterLimit;
}
patchProp(
@@ -113,7 +193,60 @@ export abstract class GraphicItemBase
namespace?: ElementNamespace,
parentComponent?: ComponentInternalInstance | null
): void {
if (isNil(prevValue) && isNil(nextValue)) return;
switch (key) {
case 'fill':
if (!this.assertType(nextValue, 'boolean', key)) return;
this.checkMode(GraphicModeProp.Fill, nextValue);
break;
case 'stroke':
if (!this.assertType(nextValue, 'boolean', key)) return;
this.checkMode(GraphicModeProp.Stroke, nextValue);
break;
case 'strokeAndFill':
if (!this.assertType(nextValue, 'number', key)) return;
this.checkMode(GraphicModeProp.StrokeAndFill, nextValue);
break;
case 'fillRule':
if (!this.assertType(nextValue, 'string', key)) return;
this.setFillRule(nextValue);
break;
case 'fillStyle':
this.setFillStyle(nextValue);
break;
case 'strokeStyle':
this.setStrokeStyle(nextValue);
break;
case 'lineWidth':
if (!this.assertType(nextValue, 'number', key)) return;
this.lineWidth = nextValue;
this.update();
break;
case 'lineDash':
if (!this.assertType(nextValue, Array, key)) return;
this.lineDash = nextValue;
this.update();
break;
case 'lineDashOffset':
if (!this.assertType(nextValue, 'number', key)) return;
this.lineDashOffset = nextValue;
this.update();
break;
case 'lineJoin':
if (!this.assertType(nextValue, 'string', key)) return;
this.lineJoin = nextValue;
this.update();
break;
case 'lineCap':
if (!this.assertType(nextValue, 'string', key)) return;
this.lineCap = nextValue;
this.update();
break;
case 'miterLimit':
if (!this.assertType(nextValue, 'number', key)) return;
this.miterLimit = nextValue;
this.update();
break;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -122,7 +255,7 @@ export abstract class GraphicItemBase
export class Rect extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
@@ -155,21 +288,51 @@ export class Circle extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, this.start, this.end);
switch (this.mode) {
case GraphicMode.Fill:
ctx.fill(this.fillRule);
break;
case GraphicMode.Stroke:
ctx.stroke();
break;
case GraphicMode.FillAndStroke:
ctx.fill(this.fillRule);
ctx.stroke();
break;
case GraphicMode.StrokeAndFill:
ctx.stroke();
ctx.fill(this.fillRule);
break;
}
}
/**
* 设置圆的半径
* @param radius 半径
*/
setRadius(radius: number) {}
setRadius(radius: number) {
this.radius = radius;
this.size(radius * 2, radius * 2);
this.update();
}
/**
* 设置圆的起始与终止角度
* @param start 起始角度
* @param end 终止角度
*/
setAngle(start: number, end: number) {}
setAngle(start: number, end: number) {
this.start = start;
this.end = end;
this.update();
}
patchProp(
key: string,
@@ -179,6 +342,18 @@ export class Circle extends GraphicItemBase {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'radius':
if (!this.assertType(nextValue, 'number', key)) return;
this.setRadius(nextValue);
return;
case 'start':
if (!this.assertType(nextValue, 'number', key)) return;
this.setAngle(nextValue, this.end);
return;
case 'end':
if (!this.assertType(nextValue, 'number', key)) return;
this.setAngle(this.start, nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -192,22 +367,60 @@ export class Ellipse extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.ellipse(
this.x,
this.y,
this.radiusX,
this.radiusY,
0,
this.start,
this.end
);
switch (this.mode) {
case GraphicMode.Fill:
ctx.fill(this.fillRule);
break;
case GraphicMode.Stroke:
ctx.stroke();
break;
case GraphicMode.FillAndStroke:
ctx.fill(this.fillRule);
ctx.stroke();
break;
case GraphicMode.StrokeAndFill:
ctx.stroke();
ctx.fill(this.fillRule);
break;
}
}
/**
* 设置椭圆的横纵轴长度
* @param x 横轴长度
* @param y 纵轴长度
*/
setRadius(x: number, y: number) {}
setRadius(x: number, y: number) {
this.radiusX = x;
this.radiusY = y;
this.update();
}
/**
* 设置椭圆的起始与终止角度
* @param start 起始角度
* @param end 终止角度
*/
setAngle(start: number, end: number) {}
setAngle(start: number, end: number) {
this.start = start;
this.end = end;
this.update();
}
patchProp(
key: string,
@@ -217,6 +430,22 @@ export class Ellipse extends GraphicItemBase {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'radiusX':
if (!this.assertType(nextValue, 'number', key)) return;
this.setRadius(nextValue, this.radiusY);
return;
case 'radiusY':
if (!this.assertType(nextValue, 'number', key)) return;
this.setRadius(this.radiusY, nextValue);
return;
case 'start':
if (!this.assertType(nextValue, 'number', key)) return;
this.setAngle(nextValue, this.end);
return;
case 'end':
if (!this.assertType(nextValue, 'number', key)) return;
this.setAngle(this.start, nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -230,18 +459,33 @@ export class Line extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.moveTo(this.x1, this.y1);
ctx.lineTo(this.x2, this.y2);
ctx.stroke();
}
/**
* 设置第一个点的横纵坐标
*/
setPoint1(x: number, y: number) {}
setPoint1(x: number, y: number) {
this.x1 = x;
this.y1 = y;
this.update();
}
/**
* 设置第二个点的横纵坐标
*/
setPoint2(x: number, y: number) {}
setPoint2(x: number, y: number) {
this.x2 = x;
this.y2 = y;
this.update();
}
patchProp(
key: string,
@@ -251,6 +495,22 @@ export class Line extends GraphicItemBase {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'x1':
if (!this.assertType(nextValue, 'number', key)) return;
this.setPoint1(nextValue, this.y1);
return;
case 'y1':
if (!this.assertType(nextValue, 'number', key)) return;
this.setPoint1(this.x1, nextValue);
return;
case 'x2':
if (!this.assertType(nextValue, 'number', key)) return;
this.setPoint2(nextValue, this.y2);
return;
case 'y2':
if (!this.assertType(nextValue, 'number', key)) return;
this.setPoint2(this.x2, nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -268,28 +528,58 @@ export class BezierCurve extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.moveTo(this.sx, this.sy);
ctx.bezierCurveTo(
this.cp1x,
this.cp1y,
this.cp2x,
this.cp2y,
this.ex,
this.ey
);
ctx.stroke();
}
/**
* 设置起始点坐标
*/
setStart(x: number, y: number) {}
setStart(x: number, y: number) {
this.sx = x;
this.sy = y;
this.update();
}
/**
* 设置控制点1坐标
*/
setControl1(x: number, y: number) {}
setControl1(x: number, y: number) {
this.cp1x = x;
this.cp1y = y;
this.update();
}
/**
* 设置控制点2坐标
*/
setControl2(x: number, y: number) {}
setControl2(x: number, y: number) {
this.cp2x = x;
this.cp2y = y;
this.update();
}
/**
* 设置终点坐标
*/
setEnd(x: number, y: number) {}
setEnd(x: number, y: number) {
this.ex = x;
this.ey = y;
this.update();
}
patchProp(
key: string,
@@ -299,6 +589,38 @@ export class BezierCurve extends GraphicItemBase {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'sx':
if (!this.assertType(nextValue, 'number', key)) return;
this.setStart(nextValue, this.sy);
return;
case 'sy':
if (!this.assertType(nextValue, 'number', key)) return;
this.setStart(this.sx, nextValue);
return;
case 'cp1x':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl1(nextValue, this.cp1y);
return;
case 'cp1y':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl1(this.cp1x, nextValue);
return;
case 'cp2x':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl2(nextValue, this.cp2y);
return;
case 'cp2y':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl2(this.cp2x, nextValue);
return;
case 'ex':
if (!this.assertType(nextValue, 'number', key)) return;
this.setEnd(nextValue, this.ey);
return;
case 'ey':
if (!this.assertType(nextValue, 'number', key)) return;
this.setEnd(this.ex, nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -314,23 +636,42 @@ export class QuadraticCurve extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.moveTo(this.sx, this.sy);
ctx.quadraticCurveTo(this.cpx, this.cpy, this.ex, this.ey);
ctx.stroke();
}
/**
* 设置起始点坐标
*/
setStart(x: number, y: number) {}
setStart(x: number, y: number) {
this.sx = x;
this.sy = y;
this.update();
}
/**
* 设置控制点坐标
*/
setControl(x: number, y: number) {}
setControl(x: number, y: number) {
this.cpx = x;
this.cpy = y;
this.update();
}
/**
* 设置终点坐标
*/
setEnd(x: number, y: number) {}
setEnd(x: number, y: number) {
this.ex = x;
this.ey = y;
this.update();
}
patchProp(
key: string,
@@ -340,6 +681,30 @@ export class QuadraticCurve extends GraphicItemBase {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'sx':
if (!this.assertType(nextValue, 'number', key)) return;
this.setStart(nextValue, this.sy);
return;
case 'sy':
if (!this.assertType(nextValue, 'number', key)) return;
this.setStart(this.sx, nextValue);
return;
case 'cpx':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl(nextValue, this.cpy);
return;
case 'cpy':
if (!this.assertType(nextValue, 'number', key)) return;
this.setControl(this.cpx, nextValue);
return;
case 'ex':
if (!this.assertType(nextValue, 'number', key)) return;
this.setEnd(nextValue, this.ey);
return;
case 'ey':
if (!this.assertType(nextValue, 'number', key)) return;
this.setEnd(this.ex, nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
@@ -351,8 +716,27 @@ export class Path extends GraphicItemBase {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
switch (this.mode) {
case GraphicMode.Fill:
ctx.fill(this.path, this.fillRule);
break;
case GraphicMode.Stroke:
ctx.stroke(this.path);
break;
case GraphicMode.FillAndStroke:
ctx.fill(this.path, this.fillRule);
ctx.stroke(this.path);
break;
case GraphicMode.StrokeAndFill:
ctx.stroke(this.path);
ctx.fill(this.path, this.fillRule);
break;
}
}
/**
* 获取当前路径
@@ -365,7 +749,115 @@ export class Path extends GraphicItemBase {
* 为路径添加路径
* @param path 要添加的路径
*/
addPath(path: Path2D) {}
addPath(path: Path2D) {
this.path.addPath(path);
this.update();
}
patchProp(
key: string,
prevValue: any,
nextValue: any,
namespace?: ElementNamespace,
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'path':
if (!this.assertType(nextValue, Path2D, key)) return;
this.path = nextValue;
this.update();
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
}
const enum RectRType {
/** 圆角为椭圆 */
Ellipse,
/** 圆角为二次贝塞尔曲线 */
Quad,
/** 圆角为三次贝塞尔曲线。该模式下,包含两个控制点,一个控制点位于上下矩形边延长线,另一个控制点位于左右矩形边延长线 */
Cubic,
/** 圆角为直线连接 */
Line
}
export class RectR extends GraphicItemBase {
/** 矩形路径 */
private path: Path2D;
/** 圆角类型 */
roundType: RectRType = RectRType.Ellipse;
/** 横向圆角半径 */
radiusX: number = 0;
/** 纵向圆角半径 */
radiusY: number = 0;
/**
* 二次贝塞尔曲线下,表示控制点的横向比例,控制点在上下矩形边与圆角的交界处为0,在左右矩形边与圆角的交界处延长线为1
* 三次贝塞尔曲线下,表示在上下矩形边延长线上的控制点的比例
*/
cpx: number = 0;
/**
* 二次贝塞尔曲线下,表示控制点的纵向比例,控制点在左右矩形边与圆角的交界处为0,在上下矩形边与圆角的交界处延长线为1
* 三次贝塞尔曲线下,表示在左右矩形边延长线上的控制点的比例
*/
cpy: number = 0;
constructor(
type: RenderItemPosition,
cache: boolean = false,
fall: boolean = false
) {
super(type, cache, fall);
const path = new Path2D();
path.rect(this.x, this.y, this.width, this.height);
this.path = path;
}
/**
* 更新路径
*/
private updatePath() {}
/**
* 设置圆角半径
* @param x 横向半径
* @param y 纵向半径
*/
setRadius(x: number, y: number) {}
/**
* 设置贝塞尔曲线模式下的控制点
* @param x cpx
* @param y cpy
*/
setControl(x: number, y: number) {}
protected render(
canvas: MotaOffscreenCanvas2D,
_transform: Transform
): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
switch (this.mode) {
case GraphicMode.Fill:
ctx.fill(this.path, this.fillRule);
break;
case GraphicMode.Stroke:
ctx.stroke(this.path);
break;
case GraphicMode.FillAndStroke:
ctx.fill(this.path, this.fillRule);
ctx.stroke(this.path);
break;
case GraphicMode.StrokeAndFill:
ctx.stroke(this.path);
ctx.fill(this.path, this.fillRule);
break;
}
}
patchProp(
key: string,
+281 -16
View File
@@ -1,9 +1,17 @@
import { MotaOffscreenCanvas2D } from '@/core/fx/canvas2d';
import { Sprite } from '../sprite';
import { ERenderItemEvent, RenderItem, RenderItemPosition } from '../item';
import {
ERenderItemEvent,
IAnimateFrame,
renderEmits,
RenderItem,
RenderItemPosition
} from '../item';
import { Transform } from '../transform';
import { ElementNamespace, ComponentInternalInstance } from 'vue';
import { AutotileRenderable, RenderableData } from '../cache';
import { texture } from '../cache';
import { isNil } from 'lodash-es';
import { logger } from '@/core/common/logger';
type CanvasStyle = string | CanvasGradient | CanvasPattern;
@@ -33,7 +41,7 @@ export class Text extends RenderItem<ETextEvent> {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
_transform: Transform
): void {
const ctx = canvas.ctx;
ctx.textBaseline = 'bottom';
@@ -162,7 +170,7 @@ export class Image extends RenderItem<EImageEvent> {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
_transform: Transform
): void {
const ctx = canvas.ctx;
ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height);
@@ -200,33 +208,108 @@ export class Comment extends RenderItem {
}
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
_canvas: MotaOffscreenCanvas2D,
_transform: Transform
): void {}
}
export interface EIconEvent extends ERenderItemEvent {}
export class Icon extends RenderItem<EIconEvent> {
export class Icon extends RenderItem<EIconEvent> implements IAnimateFrame {
/** 图标id */
icon: AllNumbers = 0;
/** 帧数 */
frame: number = 0;
frame?: number = 0;
/** 是否启用动画 */
animate: boolean = false;
animate?: boolean = false;
/** 图标的渲染信息 */
private renderable?: RenderableData | AutotileRenderable;
private pendingIcon?: AllNumbers;
constructor(type: RenderItemPosition, cache?: boolean, fall?: boolean) {
super(type, cache, fall);
this.setAntiAliasing(false);
this.setHD(false);
}
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
const renderable = this.renderable;
if (!renderable) return;
const [x, y, w, h] = renderable.render[0];
const cw = canvas.width;
const ch = canvas.height;
const frame = this.animate
? RenderItem.animatedFrame % renderable.frame
: 0;
if (!this.animate) {
if (renderable.autotile) {
ctx.drawImage(renderable.image[0], x, y, w, h, 0, 0, cw, ch);
} else {
ctx.drawImage(renderable.image, x, y, w, h, 0, 0, cw, ch);
}
} else {
const [x1, y1, w1, h1] = renderable.render[frame];
if (renderable.autotile) {
const img = renderable.image[0];
ctx.drawImage(img, x1, y1, w1, h1, 0, 0, cw, ch);
} else {
ctx.drawImage(renderable.image, x1, y1, w1, h1, 0, 0, cw, ch);
}
}
}
/**
* 设置图标
* @param id 图标id
*/
setIcon(id: AllIds | AllNumbers) {}
setIcon(id: AllIds | AllNumbers) {
const num = typeof id === 'number' ? id : texture.idNumberMap[id];
const loading = Mota.require('var', 'loading');
if (loading.loaded) {
this.setIconRenderable(num);
} else {
if (isNil(this.pendingIcon)) {
loading.once('loaded', () => {
this.setIconRenderable(this.pendingIcon ?? 0);
delete this.pendingIcon;
});
}
this.pendingIcon = num;
}
}
private setIconRenderable(num: AllNumbers) {
const renderable = texture.getRenderable(num);
if (!renderable) {
logger.warn(43, num.toString());
return;
} else {
this.icon = num;
this.renderable = renderable;
this.frame = renderable.frame;
}
this.update();
}
/**
* 更新动画帧
*/
updateFrameAnimate(): void {
this.update(this);
}
destroy(): void {
renderEmits.removeFramer(this);
super.destroy();
}
patchProp(
key: string,
@@ -242,10 +325,14 @@ export class Icon extends RenderItem<EIconEvent> {
case 'animate':
if (!this.assertType(nextValue, 'boolean', key)) return;
this.animate = nextValue;
if (nextValue) renderEmits.addFramer(this);
else renderEmits.removeFramer(this);
this.update();
return;
case 'frame':
if (!this.assertType(nextValue, 'number', key)) return;
this.frame = nextValue;
this.update();
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
@@ -259,6 +346,8 @@ export class Winskin extends RenderItem<EWinskinEvent> {
/** 边框宽度 */
borderSize: number = 32;
private pendingImage?: ImageIds;
constructor(
image: SizedCanvasImageSource,
type: RenderItemPosition = 'static'
@@ -269,20 +358,188 @@ export class Winskin extends RenderItem<EWinskinEvent> {
protected render(
canvas: MotaOffscreenCanvas2D,
transform: Transform
): void {}
_transform: Transform
): void {
const ctx = canvas.ctx;
const img = this.image;
const x = 0;
const y = 0;
const w = canvas.width;
const h = canvas.height;
const sz = this.borderSize / 32;
ctx.drawImage(img, 128, 0, 16, 16, x, y, 16 * sz, 16 * sz);
let dx;
for (dx = 0; dx < w - 64 * sz; dx += 32 * sz) {
ctx.drawImage(
img,
144,
0,
32,
16,
x + dx + 16,
y,
32 * sz,
16 * sz
);
ctx.drawImage(
img,
144,
48,
32,
16,
x + dx + 16,
y + h - 16 * sz,
32 * sz,
16 * sz
);
}
ctx.drawImage(
img,
144,
0,
w - dx - 32,
16,
x + dx + 16 * sz,
y,
w - dx - 32 * sz,
16 * sz
);
ctx.drawImage(
img,
144,
48,
w - dx - 32,
16,
x + dx + 16 * sz,
y + h - 16 * sz,
w - dx - 32 * sz,
16 * sz
);
ctx.drawImage(
img,
176,
0,
16,
16,
x + w - 16 * sz,
y,
16 * sz,
16 * sz
);
// 左右
let dy;
for (dy = 0; dy < h - 64 * sz; dy += 32 * sz) {
ctx.drawImage(
img,
128,
16,
16,
32,
x,
y + dy + 16 * sz,
16 * sz,
32 * sz
);
ctx.drawImage(
img,
176,
16,
16,
32,
x + w - 16 * sz,
y + dy + 16 * sz,
16 * sz,
32 * sz
);
}
ctx.drawImage(
img,
128,
16,
16,
h - dy - 32,
x,
y + dy + 16 * sz,
16 * sz,
h - dy - 32 * sz
);
ctx.drawImage(
img,
176,
16,
16,
h - dy - 32,
x + w - 16 * sz,
y + dy + 16 * sz,
16 * sz,
h - dy - 32 * sz
);
// 下方
ctx.drawImage(
img,
128,
48,
16,
16,
x,
y + h - 16 * sz,
16 * sz,
16 * sz
);
ctx.drawImage(
img,
176,
48,
16,
16,
x + w - 16 * sz,
y + h - 16 * sz,
16 * sz,
16 * sz
);
this.update();
}
/**
* 设置winskin图片
* @param image winskin图片
*/
setImage(image: SizedCanvasImageSource) {}
setImage(image: SizedCanvasImageSource) {
this.image = image;
this.update();
}
/**
* 通过图片名称设置winskin
* @param name 图片名称
*/
setImageByName(name: ImageIds) {
const loading = Mota.require('var', 'loading');
if (loading.loaded) {
const image = core.material.images.images[name];
this.setImage(image);
} else {
if (isNil(this.pendingImage)) {
loading.once('loaded', () => {
const id = this.pendingImage;
if (!id) return;
const image = core.material.images.images[id];
this.setImage(image);
delete this.pendingImage;
});
}
this.pendingImage = name;
}
}
/**
* 设置边框大小
* @param size 边框大小
*/
setBorderSize(size: number) {}
setBorderSize(size: number) {
this.borderSize = size;
this.update();
}
patchProp(
key: string,
@@ -292,6 +549,14 @@ export class Winskin extends RenderItem<EWinskinEvent> {
parentComponent?: ComponentInternalInstance | null
): void {
switch (key) {
case 'image':
if (!this.assertType(nextValue, 'string', key)) return;
this.setImageByName(nextValue);
return;
case 'borderSize':
if (!this.assertType(nextValue, 'number', key)) return;
this.setBorderSize(nextValue);
return;
}
super.patchProp(key, prevValue, nextValue, namespace, parentComponent);
}
+2
View File
@@ -26,6 +26,7 @@ import {
PathProps,
QuadraticProps,
RectProps,
RectRProps,
ShaderProps,
SpriteProps,
TextProps,
@@ -101,6 +102,7 @@ declare module 'vue/jsx-runtime' {
'g-bezier': TagDefine<BezierProps, EGraphicItemEvent>;
'g-quad': TagDefine<QuadraticProps, EGraphicItemEvent>;
'g-path': TagDefine<PathProps, EGraphicItemEvent>;
'g-rectr': TagDefine<RectRProps, EGraphicItemEvent>;
icon: TagDefine<IconProps, EIconEvent>;
winskin: TagDefine<WinskinProps, EWinskinEvent>;
}
+5 -4
View File
@@ -21,9 +21,9 @@ import {
Line,
Path,
QuadraticCurve,
Rect
Rect,
RectR
} from '../preset/graphics';
import { BaseProps } from './props';
type OnItemCreate<
E extends ERenderItemEvent = ERenderItemEvent,
@@ -217,10 +217,10 @@ tagMap.register('layer-group', (_0, _1, props) => {
return l;
}
});
tagMap.register<EDamageEvent, Damage>('damage', (_0, _1, props) => {
tagMap.register<EDamageEvent, Damage>('damage', (_0, _1, _props) => {
return new Damage();
});
tagMap.register('animation', (_0, _1, props) => {
tagMap.register('animation', (_0, _1, _props) => {
return new Animate();
});
tagMap.register('g-rect', se(Rect, 'absolute', ElementState.None));
@@ -230,6 +230,7 @@ tagMap.register('g-line', se(Line, 'absolute', ElementState.None));
tagMap.register('g-bezier', se(BezierCurve, 'absolute', ElementState.None));
tagMap.register('g-quad', se(QuadraticCurve, 'absolute', ElementState.None));
tagMap.register('g-path', se(Path, 'absolute', ElementState.None));
tagMap.register('g-rectr', se(RectR, 'absolute', ElementState.None));
tagMap.register('icon', standardElementNoCache(Icon));
tagMap.register('winskin', (_0, _1, props) => {
if (!props) return new Winskin(core.material.images.images['winskin.png']);
+71 -8
View File
@@ -7,6 +7,7 @@ import {
} from '../preset/layer';
import type { EnemyCollection } from '@/game/enemy/damage';
import { ILineProperty } from '../preset/graphics';
import { SizedCanvasImageSource } from '../preset';
export interface CustomProps {
_item: (props: BaseProps) => RenderItem;
@@ -108,18 +109,80 @@ export interface GraphicPropsBase extends BaseProps, Partial<ILineProperty> {
export interface RectProps extends GraphicPropsBase {}
export interface CirclesProps extends GraphicPropsBase {}
export interface CirclesProps extends GraphicPropsBase {
radius?: number;
start?: number;
end?: number;
}
export interface EllipseProps extends GraphicPropsBase {}
export interface EllipseProps extends GraphicPropsBase {
radiusX?: number;
radiusY?: number;
start?: number;
end?: number;
}
export interface LineProps extends GraphicPropsBase {}
export interface LineProps extends GraphicPropsBase {
x1?: number;
y1?: number;
x2?: number;
y2?: number;
}
export interface BezierProps extends GraphicPropsBase {}
export interface BezierProps extends GraphicPropsBase {
sx?: number;
sy?: number;
cp1x?: number;
cp1y?: number;
cp2x?: number;
cp2y?: number;
ex?: number;
ey?: number;
}
export interface QuadraticProps extends GraphicPropsBase {}
export interface QuadraticProps extends GraphicPropsBase {
sx?: number;
sy?: number;
cpx?: number;
cpy?: number;
ex?: number;
ey?: number;
}
export interface PathProps extends GraphicPropsBase {}
export interface PathProps extends GraphicPropsBase {
path?: Path2D;
}
export interface IconProps extends BaseProps {}
export interface RectRProps extends GraphicPropsBase {
/** 圆角半径,此参数传入时,radiusX 和 radiusY 应保持一致 */
radius: number;
/** 圆角横向半径 */
radiusX?: number;
/** 圆角纵向半径 */
radiusY?: number;
/** 圆角为线模式 */
line?: boolean;
/** 圆角为椭圆模式,默认值 */
ellipse?: boolean;
/** 圆角为二次贝塞尔曲线模式 */
quad?: boolean;
/** 圆角为三次贝塞尔曲线模式 */
cubic?: boolean;
/** 控制点,此参数传入时,cpx 和 cpy 应保持一致 */
cp?: number;
/** 横向控制点 */
cpx?: number;
/** 纵向控制点 */
cpy?: number;
}
export interface WinskinProps extends BaseProps {}
export interface IconProps extends BaseProps {
icon: AllNumbers;
frame?: number;
animate?: boolean;
}
export interface WinskinProps extends BaseProps {
image: ImageIds;
borderSize?: number;
}
+1
View File
@@ -70,6 +70,7 @@
"40": "Cannot return canvas that is not provided by this pool.",
"41": "Width of text content components must be positive. receive: $1",
"42": "Repeated Textbox id: '$1'.",
"43": "Cannot set icon of '$1', since it does not exists. Please ensure you have delivered correct icon id or number.",
"1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.",
"1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance."
}
+5 -1
View File
@@ -13,12 +13,16 @@ class GameLoading extends EventEmitter<GameLoadEvent> {
private autotileLoaded: number = 0;
private autotileNum?: number;
private autotileListened: boolean = false;
loaded: boolean = false;
constructor() {
super();
this.on('coreInit', () => {
this.once('coreInit', () => {
this.autotileNum = Object.keys(core.material.icons.autotile).length;
});
this.once('loaded', () => {
this.loaded = true;
});
}
addAutotileLoaded() {