diff --git a/packages-user/client-base/src/components/background.tsx b/packages-user/client-base/src/components/background.tsx index ecbd45a..d012b93 100644 --- a/packages-user/client-base/src/components/background.tsx +++ b/packages-user/client-base/src/components/background.tsx @@ -17,12 +17,12 @@ export interface BackgroundProps extends DefaultProps { border?: CanvasStyle; /** 非 `winskin` 模式下,圆角矩形的参数,默认为 [4] */ circle?: RectRCircleParams; - /** 使用的贴图管理器 */ - manager?: ITextureManager; + /** 使用的贴图管理器,默认会使用主对象上的贴图管理器 */ + materials?: ITextureManager; } const backgroundProps = { - props: ['winskin', 'borderWidth', 'color', 'border', 'manager', 'circle'] + props: ['winskin', 'borderWidth', 'color', 'border', 'materials', 'circle'] } satisfies SetupComponentOptions; /** @@ -41,7 +41,7 @@ export const Background = defineComponent(props => { isWinskin.value ? ( diff --git a/packages-user/client-base/src/components/icon.tsx b/packages-user/client-base/src/components/icon.tsx index 557cd9a..c847aae 100644 --- a/packages-user/client-base/src/components/icon.tsx +++ b/packages-user/client-base/src/components/icon.tsx @@ -1,6 +1,6 @@ import { DefaultProps } from '@motajs/render-vue'; import { SetupComponentOptions } from '@motajs/system'; -import { computed, defineComponent, markRaw } from 'vue'; +import { computed, defineComponent, inject, markRaw } from 'vue'; import { IMaterialFramedData, ITextureManager } from '../material'; import { isNil } from 'lodash-es'; import { ITexture, TextureRowAnimater } from '@motajs/render'; @@ -8,8 +8,8 @@ import { ITexture, TextureRowAnimater } from '@motajs/render'; export interface IIconProps extends DefaultProps { /** 当前显示的图标 */ icon?: number | string; - /** 贴图管理器 */ - manager?: ITextureManager; + /** 贴图管理器,默认会使用主对象上的贴图管理器 */ + materials?: ITextureManager; /** 是否播放图块动画 */ animate?: boolean; /** 展示图块指定帧数的动画,传入时会忽略 `animate` 的动画 */ @@ -17,20 +17,23 @@ export interface IIconProps extends DefaultProps { } const iconProps = { - props: ['icon', 'manager', 'animate', 'frame'] + props: ['icon', 'materials', 'animate', 'frame'] } satisfies SetupComponentOptions; export const Icon = defineComponent(props => { const animater = markRaw(new TextureRowAnimater()); + const defaultMaterials = inject('@default/materials'); + /** 贴图管理器 */ + const materials = computed(() => props.materials ?? defaultMaterials); /** 图块的渲染数据 */ const data = computed(() => { - if (!props.manager) return null; + if (!materials.value) return null; const icon = props.icon ?? 0; if (typeof icon === 'number') { - return props.manager.getTile(icon); + return materials.value.getTile(icon); } else { - return props.manager.getTileByAlias(icon); + return materials.value.getTileByAlias(icon); } }); /** 图块的完整贴图 */ diff --git a/packages-user/client-base/src/components/misc.tsx b/packages-user/client-base/src/components/misc.tsx index b61117c..6f217fb 100644 --- a/packages-user/client-base/src/components/misc.tsx +++ b/packages-user/client-base/src/components/misc.tsx @@ -1,6 +1,5 @@ import { ElementLocator, - CustomRenderItem, MotaOffscreenCanvas2D, CanvasStyle, RectRCircleParams @@ -12,7 +11,7 @@ import { onExcitedFunc, PathProps } from '@motajs/render-vue'; -import { computed, defineComponent, ref, watch } from 'vue'; +import { computed, defineComponent, inject, ref, watch } from 'vue'; import { TextContent, TextContentProps } from './textbox'; import { Scroll, ScrollExpose, ScrollProps } from './scroll'; import { SetupComponentOptions } from '@motajs/system'; @@ -21,10 +20,8 @@ import { cosh, CurveMode } from '@motajs/animate'; import { ITextureManager } from '../material'; interface ProgressProps extends DefaultProps { - /** 进度条的位置 */ - loc: ElementLocator; /** 进度条的进度,1表示完成,0表示未完成 */ - progress: number; + progress?: number; /** 已完成部分的样式,默认为绿色(green) */ success?: CanvasStyle; /** 未完成部分的样式,默认为灰色(gray) */ @@ -48,36 +45,47 @@ const progressProps = { * ``` */ export const Progress = defineComponent(props => { - const element = ref(); + /** 当前的进度条值 */ + const progress = computed(() => props.progress ?? 0); + /** 进度条线宽 */ + const lineWidth = computed(() => props.lineWidth ?? 2); + /** 未完成部分的颜色 */ + const background = computed(() => props.background ?? 'gray'); + /** 已完成部分的颜色 */ + const success = computed(() => props.success ?? 'green'); const render = (canvas: MotaOffscreenCanvas2D) => { const { ctx, width, height } = canvas; + const lw = lineWidth.value; ctx.lineCap = 'round'; - const lineWidth = props.lineWidth ?? 2; - ctx.lineWidth = lineWidth; - ctx.strokeStyle = props.background ?? 'gray'; + ctx.lineWidth = lw; + ctx.strokeStyle = background.value; ctx.beginPath(); - ctx.moveTo(lineWidth, height / 2); - ctx.lineTo(width - lineWidth, height / 2); + ctx.moveTo(lw, height / 2); + ctx.lineTo(width - lw, height / 2); ctx.stroke(); - const progress = clamp(props.progress, 0, 1); - if (!isNaN(progress)) { - ctx.strokeStyle = props.success ?? 'green'; - const p = lineWidth + (width - lineWidth * 2) * progress; + const p = clamp(progress.value, 0, 1); + if (!isNaN(p)) { + ctx.strokeStyle = success.value; + const px = lw + (width - lw * 2) * p; ctx.beginPath(); - ctx.moveTo(lineWidth, height / 2); - ctx.lineTo(p, height / 2); + ctx.moveTo(lw, height / 2); + ctx.lineTo(px, height / 2); ctx.stroke(); } }; - watch(props, () => { - element.value?.update(); - }); - - return () => { - return ; - }; + return () => ( + + ); }, progressProps); export interface ArrowProps extends PathProps { @@ -264,6 +272,8 @@ export const ScrollText = defineComponent< lastFixedPos = 0; }; + watch(() => props.speed, setSpeed); + expose({ pause, resume, setSpeed, rescroll }); return () => ( @@ -294,8 +304,8 @@ export interface SelectionProps extends DefaultProps { winskin?: string; /** 边框宽度,默认为 2 */ borderSize?: number; - /** winskin 使用的贴图管理器 */ - manager?: ITextureManager; + /** winskin 使用的贴图管理器,默认会使用主对象上的贴图管理器 */ + materials?: ITextureManager; /** 选择图标的不透明度范围 */ alphaRange?: [number, number]; } @@ -307,7 +317,7 @@ const selectionProps = { 'circle', 'winskin', 'borderSize', - 'manager', + 'materials', 'alphaRange' ] } satisfies SetupComponentOptions; @@ -322,7 +332,9 @@ const selectionProps = { * ``` */ export const Selection = defineComponent(props => { + /** 最小不透明度值 */ const minAlpha = computed(() => props.alphaRange?.[0] ?? 0.25); + /** 最大不透明度值 */ const maxAlpha = computed(() => props.alphaRange?.[1] ?? 0.55); const alpha = cycledTransition( minAlpha.value, @@ -330,13 +342,18 @@ export const Selection = defineComponent(props => { 2000, cosh(2, CurveMode.EaseInOut) )!; + const defaultMaterials = inject('@default/materials'); + /** 使用的贴图管理器 */ + const materials = computed(() => props.materials ?? defaultMaterials); + /** 当前是否使用 winskin */ const isWinskin = computed(() => !isNil(props.winskin)); + /** 当前的 winskin 贴图 */ const winskin = computed(() => { - if (isNil(props.winskin) || !props.manager) { + if (isNil(props.winskin) || !materials.value) { return null; } else { - return props.manager.getImageByAlias(props.winskin); + return materials.value.getImageByAlias(props.winskin); } }); diff --git a/packages-user/client-base/src/components/textbox.tsx b/packages-user/client-base/src/components/textbox.tsx index 625c8ab..c91fb97 100644 --- a/packages-user/client-base/src/components/textbox.tsx +++ b/packages-user/client-base/src/components/textbox.tsx @@ -2,6 +2,7 @@ import { ElementLocator, Font, MotaOffscreenCanvas2D } from '@motajs/render'; import { computed, defineComponent, + inject, onMounted, ref, shallowRef, @@ -35,7 +36,7 @@ export interface TextContentProps extends DefaultProps { autoHeight?: boolean; /** 打字机对象,如果需要自定义配置,那么需要传入,如果不需要自定义配置,不传入也可以 */ typer?: TextContentTyper; - /** 素材管理器,用于显示图标 */ + /** 素材管理器,用于显示图标,默认使用主对象上的贴图管理器 */ materials?: ITextureManager; } @@ -99,6 +100,10 @@ export const TextContent = defineComponent< TextContentEmits, keyof TextContentEmits >((props, { emit, expose }) => { + const defaultMaterials = inject('@default/materials'); + /** 使用的贴图管理器 */ + const materials = computed(() => props.materials ?? defaultMaterials); + const [x, y, width, height] = xywh(props); /** 正文高度 */ const contentHeight = ref(height.value); @@ -112,7 +117,7 @@ export const TextContent = defineComponent< const root = useRendererRoot(); const typer = computed( () => - props.typer ?? new TextContentTyper({ materials: props.materials }) + props.typer ?? new TextContentTyper({ materials: materials.value }) ); const renderable = shallowRef([]); let needUpdate = false; @@ -142,7 +147,14 @@ export const TextContent = defineComponent< needUpdate = true; renderable.value = []; - typer.value.setConfig({ width: width.value }); + if (materials.value) { + typer.value.setConfig({ + width: width.value, + materials: materials.value + }); + } else { + typer.value.setConfig({ width: width.value }); + } typer.value.setText(props.text ?? ''); typer.value.type(); needUpdate = false; @@ -410,7 +422,7 @@ export const Textbox = defineComponent< color={props.backColor} border={props.strokeColor} circle={[0]} - manager={props.materials} + materials={props.materials} loc={[0, 0, tw.value, th.value]} /> @@ -437,7 +449,7 @@ export const Textbox = defineComponent< hidden={!!iconData.value} icon={props.icon} loc={iconLoc.value} - manager={props.materials} + materials={props.materials} animate /> ((props, { emit }) => { + const defaultRenderer = inject('@default/expandMapRenderer'); + /** 使用的地图渲染器 */ + const mapRenderer = computed(() => props.renderer ?? defaultRenderer); + const [x, y, width, height] = xywh(props); /** 渲染缩放尺寸 */ @@ -90,7 +94,7 @@ export const ThumbnailCell = defineComponent< /** 渲染区域大小 */ const chunkSize = computed<[number, number]>(() => { - const renderer = props.renderer; + const renderer = mapRenderer.value; if (!renderer || !props.origin || !map.value) return [0, 0]; const [ox, oy] = props.origin; const { chunkWidth, chunkHeight } = renderer.vertex.block; @@ -107,7 +111,7 @@ export const ThumbnailCell = defineComponent< /** 真实的渲染尺寸 */ const renderSize = computed<[number, number]>(() => { - const renderer = props.renderer; + const renderer = mapRenderer.value; if (!renderer) return [1, 1]; const [cw, ch] = chunkSize.value; if (cw === 0 || ch === 0) return [1, 1]; @@ -134,7 +138,7 @@ export const ThumbnailCell = defineComponent< const render = (canvas: MotaOffscreenCanvas2D) => { const [cw, ch] = chunkSize.value; const ctx = canvas.ctx; - if (!props.renderer || !map.value || cw === 0 || ch === 0) { + if (!mapRenderer.value || !map.value || cw === 0 || ch === 0) { ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'white'; @@ -145,7 +149,7 @@ export const ThumbnailCell = defineComponent< ctx.fillText('未绑定渲染器或楼层对象', hw, hh); return; } - const renderer = props.renderer; + const renderer = mapRenderer.value; // 重置激励时间戳,保证显示内容的一致性,不会因为动画导致显示内容变化 renderer.excited(0); renderer.setGameMap(map.value); @@ -307,6 +311,9 @@ const thumbnailProps = { } satisfies SetupComponentOptions; export const Thumbnail = defineComponent(props => { + const defaultRenderer = inject('@default/expandMapRenderer'); + /** 使用的地图渲染器 */ + const mapRenderer = computed(() => props.renderer ?? defaultRenderer); /** 空白区域填充样式 */ const padStyle = computed(() => props.padStyle ?? 'black'); /** 渲染尺寸比例 */ @@ -335,7 +342,7 @@ export const Thumbnail = defineComponent(props => { /** 渲染尺寸 */ const renderSize = computed(() => { - const renderer = props.renderer; + const renderer = mapRenderer.value; if (!renderer || !props.area) return [1, 1]; const [, , w, h] = props.area; const { cellWidth, cellHeight } = renderer; @@ -354,7 +361,7 @@ export const Thumbnail = defineComponent(props => { /** ThumbnailCell 的分块信息 */ const chunks = computed(() => { - const renderer = props.renderer; + const renderer = mapRenderer.value; if (!renderer || !props.area) { return { chunks: [], @@ -413,7 +420,7 @@ export const Thumbnail = defineComponent(props => { ; export const Tip = defineComponent((props, { expose }) => { + const defaultMaterials = inject('@default/materials'); + + /** 贴图管理器 */ + const materials = computed(() => props.materials ?? defaultMaterials); /** 图标 id 或数字 */ const icon = ref(0); /** 显示的文字 */ @@ -69,7 +82,7 @@ export const Tip = defineComponent((props, { expose }) => { const showIcon = computed(() => icon.value !== 0); /** 图标的原始尺寸 */ const iconSize = computed<[number, number]>(() => { - const manager = props.materials; + const manager = materials.value; if (!manager) return [0, 0]; const data = manager.tile(icon.value); if (!data) return [0, 0]; @@ -136,7 +149,7 @@ export const Tip = defineComponent((props, { expose }) => { />