diff --git a/packages-user/client-base/src/components/icon.tsx b/packages-user/client-base/src/components/icon.tsx new file mode 100644 index 0000000..1a4553b --- /dev/null +++ b/packages-user/client-base/src/components/icon.tsx @@ -0,0 +1,65 @@ +import { DefaultProps } from '@motajs/render-vue'; +import { SetupComponentOptions } from '@motajs/system'; +import { computed, defineComponent, markRaw } from 'vue'; +import { IMaterialFramedData, ITextureManager } from '../material'; +import { isNil } from 'lodash-es'; +import { ITexture, TextureRowAnimater } from '@motajs/render'; + +export interface IIconProps extends DefaultProps { + /** 当前显示的图标 */ + icon?: number | string; + /** 贴图管理器 */ + manager?: ITextureManager; + /** 是否播放图块动画 */ + animate?: boolean; + /** 展示图块指定帧数的动画,传入时会忽略 `animate` 的动画 */ + frame?: number; +} + +const iconProps = { + props: ['icon', 'manager', 'animate', 'frame'], + emits: ['change', 'input', 'update:value'] +} satisfies SetupComponentOptions; + +export const Icon = defineComponent(props => { + const animater = markRaw(new TextureRowAnimater()); + + /** 图块的渲染数据 */ + const data = computed(() => { + if (!props.manager) return null; + const icon = props.icon ?? 0; + if (typeof icon === 'number') { + return props.manager.getTile(icon); + } else { + return props.manager.getTileByAlias(icon); + } + }); + /** 图块的完整贴图 */ + const texture = computed( + () => data.value?.texture ?? null + ); + /** 是否播放图块动画 */ + const animate = computed(() => { + const frames = data.value?.frames ?? 1; + return isNil(props.frame) && !!props.animate && frames > 1; + }); + /** 当不播放图块动画时,静态使用的贴图 */ + const staticImage = computed(() => { + if (!animate.value || !data.value) return null; + const frame = props.frame ?? data.value.defaultFrame; + const o = data.value.offset * frame; + const tex = data.value.texture; + return tex.clip({ x: o, y: 0, w: o, h: tex.height }); + }); + + return () => + animate.value ? ( + + ) : ( + + ); +}, iconProps); diff --git a/packages-user/client-base/src/components/index.ts b/packages-user/client-base/src/components/index.ts index 5970bd8..d9916fe 100644 --- a/packages-user/client-base/src/components/index.ts +++ b/packages-user/client-base/src/components/index.ts @@ -1,4 +1,6 @@ +export * from './background'; export * from './floorSelect'; +export * from './icon'; export * from './icons'; export * from './input'; export * from './list'; diff --git a/packages-user/client-base/src/components/textbox.tsx b/packages-user/client-base/src/components/textbox.tsx index d70c1e9..1705cc3 100644 --- a/packages-user/client-base/src/components/textbox.tsx +++ b/packages-user/client-base/src/components/textbox.tsx @@ -2,13 +2,11 @@ import { ElementLocator, Font, CustomRenderItem, - Text, MotaOffscreenCanvas2D } from '@motajs/render'; import { computed, defineComponent, - nextTick, onMounted, onUnmounted, ref, @@ -32,6 +30,7 @@ import { import { SetupComponentOptions } from '@motajs/system'; import { texture } from '../elements'; import { DefaultProps, onExcited } from '@motajs/render-vue'; +import { Icon } from './icon'; // todo: TextContent 应该改成渲染元素? @@ -526,7 +525,8 @@ export const Textbox = defineComponent< > )} {hasIcon.value && ( - + // TODO: manager + )} {hasIcon.value && ( ((props, { expose }) => { hide(); }; - const onSetText = (width: number) => { + const onSetText = (_: unknown, width: number) => { textWidth.value = width; }; @@ -128,9 +129,10 @@ export const Tip = defineComponent((props, { expose }) => { fill fillStyle="rgba(0,0,0,0.8)" /> -