refactor: winskin 实现

This commit is contained in:
AncTe committed 2026-11-20 20:33:59 +08:00
1 parent 73acc4b50f
commit 99fff4a4ad
9 files changed
+89 -205

No files matched your search

@@ -17,8 +17,7 @@ export interface IIconProps extends DefaultProps {
}
const iconProps = {
props: ['icon', 'manager', 'animate', 'frame'],
emits: ['change', 'input', 'update:value']
props: ['icon', 'manager', 'animate', 'frame']
} satisfies SetupComponentOptions<IIconProps>;
export const Icon = defineComponent<IIconProps>(props => {
@@ -0,0 +1,74 @@
import { DefaultProps } from '@motajs/render-vue';
import { ITextureManager } from '../material';
import { SetupComponentOptions } from '@motajs/system';
import { ITexture, MotaOffscreenCanvas2D } from '@motajs/render';
import { computed, defineComponent } from 'vue';
import { isNil } from 'lodash-es';
export interface IWinskinProps extends DefaultProps {
/** winskin 图片的名称,带后缀 */
winskin?: string;
/** 使用的贴图素材管理器 */
manager?: ITextureManager;
/** 手动传入贴图对象,如果传入,会忽略 `winskin` 和 `manager` */
texture?: ITexture;
/** 边框的宽度,默认为 32 */
borderSize?: number;
}
const winskinProps = {
props: ['winskin', 'manager', 'texture']
} satisfies SetupComponentOptions<IWinskinProps>;
/** winskin 最小单元宽度 */
const CW = 16 / 192;
/** winskin 最小单元高度 */
const CH = 16 / 128;
export const Winskin = defineComponent<IWinskinProps>(props => {
/** 边框宽度 */
const border = computed(() => props.borderSize ?? 32);
/** 贴图对象 */
const texture = computed<ITexture | null>(() => {
if (props.texture) return props.texture;
if (isNil(props.winskin) || !props.manager) return null;
return props.manager.getImageByAlias(props.winskin);
});
const render = (canvas: MotaOffscreenCanvas2D) => {
if (!texture.value) return;
const ctx = canvas.ctx;
const w = canvas.width;
const h = canvas.height;
const pad = border.value / 2;
const { width, height, source: s } = texture.value;
const x1 = CW * width; // 16
const x2 = CW * 2 * width; // 32
const x3 = CW * 8 * width; // 128
const x4 = CW * 9 * width; // 144
const x5 = CW * 11 * width; // 176
const y1 = CH * height; // 16
const y2 = CH * 2 * height; // 32
const y3 = CH * 3 * height; // 48
const y4 = CH * 8 * height; // 128
// 背景
ctx.drawImage(s, 0, 0, x3, y4, 2, 2, w - 4, h - 4);
// 上下左右边框
ctx.drawImage(s, x4, 0, x2, y1, pad, 0, w - pad * 2, pad);
ctx.drawImage(s, x4, y3, x2, y1, pad, h - pad, w - pad * 2, pad);
ctx.drawImage(s, x3, y1, x1, y2, 0, pad, pad, h - pad * 2);
ctx.drawImage(s, x5, y1, x1, y2, w - pad, pad, pad, h - pad * 2);
// 四个角的边框
ctx.drawImage(s, x3, 0, x1, y1, 0, 0, pad, pad);
ctx.drawImage(s, x5, 0, x1, y1, w - pad, 0, pad, pad);
ctx.drawImage(s, x3, y3, x1, y1, 0, h - pad, pad, pad);
ctx.drawImage(s, x5, y3, x1, y1, w - pad, h - pad, pad, pad);
};
return () => (
<custom
render={render}
bindings={[border.value, texture.value]}
></custom>
);
}, winskinProps);
@@ -1,9 +0,0 @@
import { createCache } from './cache';
export function createElements() {
createCache();
}
export * from './cache';
export * from './misc';
export * from './props';
@@ -1,112 +0,0 @@
import {
RenderItem,
MotaOffscreenCanvas2D,
SizedCanvasImageSource
} from '@motajs/render';
import { isNil } from 'lodash-es';
import { IMotaWinskin } from './types';
export class Winskin extends RenderItem implements IMotaWinskin {
image: SizedCanvasImageSource | null = null;
/** 边框宽度,32表示原始宽度 */
borderSize: number = 32;
/** 图片名称 */
imageName: string = '';
private pendingImage?: ImageIds;
constructor() {
super();
this.setAntiAliasing(false);
}
protected getDefaultCache(): boolean {
return true;
}
protected render(canvas: MotaOffscreenCanvas2D): void {
const img = this.image;
if (!img) return;
const ctx = canvas.ctx;
const w = this.width;
const h = this.height;
const pad = this.borderSize / 2;
// 背景
ctx.drawImage(img, 0, 0, 128, 128, 2, 2, w - 4, h - 4);
// 上下左右边框
ctx.save();
ctx.drawImage(img, 144, 0, 32, 16, pad, 0, w - pad * 2, pad);
ctx.drawImage(img, 144, 48, 32, 16, pad, h - pad, w - pad * 2, pad);
ctx.drawImage(img, 128, 16, 16, 32, 0, pad, pad, h - pad * 2);
ctx.drawImage(img, 176, 16, 16, 32, w - pad, pad, pad, h - pad * 2);
// 四个角的边框
ctx.drawImage(img, 128, 0, 16, 16, 0, 0, pad, pad);
ctx.drawImage(img, 176, 0, 16, 16, w - pad, 0, pad, pad);
ctx.drawImage(img, 128, 48, 16, 16, 0, h - pad, pad, pad);
ctx.drawImage(img, 176, 48, 16, 16, w - pad, h - pad, pad, pad);
}
/**
* 设置winskin图片
* @param image winskin图片
*/
setImage(image: SizedCanvasImageSource) {
this.image = image;
this.imageName = '';
this.update();
}
/**
* 通过图片名称设置winskin
* @param name 图片名称
*/
setImageByName(name: ImageIds) {
const { loading } = Mota.require('@user/data-base');
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;
}
this.imageName = name;
}
/**
* 设置边框大小
* @param size 边框大小
*/
setBorderSize(size: number) {
this.borderSize = size;
this.update();
}
protected handleProps(
key: string,
_prevValue: any,
nextValue: any
): boolean {
switch (key) {
case 'image':
this.setImage(nextValue);
return true;
case 'imageName':
if (!this.assertType(nextValue, 'string', key)) return false;
this.setImageByName(nextValue);
return true;
case 'borderSize':
if (!this.assertType(nextValue, 'number', key)) return false;
this.setBorderSize(nextValue);
return true;
}
return false;
}
}
@@ -1,27 +0,0 @@
import { BaseProps, TagDefine } from '@motajs/render-vue';
import { IRenderItemBaseHooks, SizedCanvasImageSource } from '@motajs/render';
import { IGameMap } from '@user/data-base';
import { IMapRenderer } from '../map';
export interface WinskinProps extends BaseProps {
/** 直接设置 winskin 图片 */
image?: SizedCanvasImageSource;
/** 根据图片名称设置 winskin 图片 */
imageName?: string;
/** 边框大小 */
borderSize?: number;
}
export interface MapRenderProps extends BaseProps {
map: IGameMap;
renderer: IMapRenderer;
}
declare module 'vue/jsx-runtime' {
namespace JSX {
export interface IntrinsicElements {
winskin: TagDefine<WinskinProps, IRenderItemBaseHooks>;
'map-render': TagDefine<MapRenderProps, IRenderItemBaseHooks>;
}
}
}
@@ -1,28 +0,0 @@
import { IRenderItem, SizedCanvasImageSource } from '@motajs/render';
export interface IMotaWinskin extends IRenderItem {
/** winskin 图片源 */
readonly image: SizedCanvasImageSource | null;
/** 边框尺寸 */
readonly borderSize: number;
/** winskin 图片名称,如果不是使用名称设置的图片的话,此值为空字符串 */
readonly imageName: string;
/**
* 设置winskin图片
* @param image winskin图片
*/
setImage(image: SizedCanvasImageSource): void;
/**
* 通过图片名称设置winskin
* @param name 图片名称
*/
setImageByName(name: ImageIds): void;
/**
* 设置边框大小
* @param size 边框大小
*/
setBorderSize(size: number): void;
}
-1
View File
@@ -1,5 +1,4 @@
export * from './components';
export * from './elements';
export * from './hotkey';
export * from './layout';
export * from './map';
@@ -11,7 +11,6 @@ import {
} from '@motajs/render';
import { mat3 } from 'gl-matrix';
import { IMapRenderer, IMapRenderItem } from './types';
import { GameMap, IGameMap } from '@user/data-base';
import { MapRenderer } from './renderer';
export class MapRenderItem extends RenderItem implements IMapRenderItem {
@@ -20,8 +19,6 @@ export class MapRenderItem extends RenderItem implements IMapRenderItem {
/** 当前使用的地图渲染器 */
renderer: IMapRenderer | null = null;
/** 当前正在渲染的地图 */
map: IGameMap | null = null;
protected onConnected(root: IRenderTreeRoot): void {
this.delegation = root.delegateExcitable(time => {
@@ -115,28 +112,12 @@ export class MapRenderItem extends RenderItem implements IMapRenderItem {
setRenderer(renderer: IMapRenderer | null): void {
this.renderer = renderer;
this.renderer?.setGameMap(this.map);
this.update();
}
setRenderMap(map: IGameMap | null): void {
this.map = map;
this.renderer?.setGameMap(map);
this.update();
}
handleProps(key: string, prevValue: any, nextValue: any): boolean {
if (super.handleProps(key, prevValue, nextValue)) return true;
switch (key) {
case 'map': {
if (!nextValue) {
this.setRenderMap(null);
return true;
}
if (!this.assertType(nextValue, GameMap, key)) return false;
this.setRenderMap(nextValue);
return true;
}
case 'renderer': {
if (!nextValue) {
this.setRenderer(null);
+14 -7
View File
@@ -8,6 +8,7 @@ import {
import {
Font,
IRenderItem,
IRenderItemBaseHooks,
ITextureRenderable,
SizedCanvasImageSource,
Transform
@@ -25,6 +26,7 @@ import {
IExcitable,
IExcitation
} from '@motajs/animate';
import { BaseProps, TagDefine } from '@motajs/render-vue';
//#region 枚举值
@@ -1247,20 +1249,25 @@ export interface IMapViewportController {
export interface IMapRenderItem extends IRenderItem {
/** 当前使用的地图渲染器 */
readonly renderer: IMapRenderer | null;
/** 当前正在渲染的地图 */
readonly map: IGameMap | null;
/**
* 设置当前使用的地图渲染器
* @param renderer 地图渲染器
*/
setRenderer(renderer: IMapRenderer | null): void;
}
/**
* 设置当前要渲染的地图
* @param map 要渲染的地图
*/
setRenderMap(map: IGameMap | null): void;
export interface MapRenderProps extends BaseProps {
/** 当前使用的地图渲染器 */
renderer?: IMapRenderer;
}
declare module 'vue/jsx-runtime' {
namespace JSX {
export interface IntrinsicElements {
'map-render': TagDefine<MapRenderProps, IRenderItemBaseHooks>;
}
}
}
//#endregion