From 853e3c4af48afaa81fe5afb3fd7b4a2d76c1e621 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 20 Nov 2026 21:17:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=86=85=E7=BD=AE=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=85=B3=E4=BA=8E=E5=AE=9A=E4=BD=8D=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E4=B8=80=E9=83=A8=E5=88=86=20props=20=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client-base/src/components/background.tsx | 39 ++- .../client-base/src/components/icons.tsx | 15 +- .../client-base/src/components/input.tsx | 11 +- .../client-base/src/components/list.tsx | 77 +++-- .../client-base/src/components/misc.tsx | 106 ++++--- .../client-base/src/components/page.tsx | 274 ++++++++++-------- .../client-base/src/components/scroll.tsx | 64 ++-- .../client-base/src/components/slider.tsx | 15 +- .../client-base/src/components/textbox.tsx | 4 +- .../client-base/src/components/winskin.tsx | 2 +- 10 files changed, 327 insertions(+), 280 deletions(-) diff --git a/packages-user/client-base/src/components/background.tsx b/packages-user/client-base/src/components/background.tsx index f265b4a..ecbd45a 100644 --- a/packages-user/client-base/src/components/background.tsx +++ b/packages-user/client-base/src/components/background.tsx @@ -1,47 +1,58 @@ import { SetupComponentOptions } from '@motajs/system'; -import { ElementLocator } from '@motajs/render'; import { DefaultProps } from '@motajs/render-vue'; import { computed, defineComponent } from 'vue'; +import { Winskin } from './winskin'; +import { ITextureManager } from '../material'; +import { isNil } from 'lodash-es'; +import { RectRCircleParams } from '@motajs/render'; export interface BackgroundProps extends DefaultProps { - loc: ElementLocator; - winskin?: ImageIds; + /** 使用 winskin 作为背景,此时 `color` 和 `border` 无效 */ + winskin?: string; + /** 边框宽度,当使用 `winskin` 作为背景时,默认为 32,否则默认为 2 */ + borderWidth?: number; + /** 填充颜色 */ color?: CanvasStyle; + /** 边框颜色 */ border?: CanvasStyle; + /** 非 `winskin` 模式下,圆角矩形的参数,默认为 [4] */ + circle?: RectRCircleParams; + /** 使用的贴图管理器 */ + manager?: ITextureManager; } const backgroundProps = { - props: ['loc', 'winskin', 'color', 'border'] + props: ['winskin', 'borderWidth', 'color', 'border', 'manager', 'circle'] } satisfies SetupComponentOptions; /** * 背景组件,与 Selection 类似,不过绘制的是背景,而不是选择光标,参数参考 {@link BackgroundProps},用例如下: * ```tsx * // 使用 winskin2.png 作为背景 - * + * * // 使用指定填充和边框颜色作为背景 * * ``` */ export const Background = defineComponent(props => { - const isWinskin = computed(() => !!props.winskin); - const fixedLoc = computed(() => { - const [x = 0, y = 0, width = 200, height = 200] = props.loc; - return [x + 2, y + 2, width - 4, height - 4]; - }); + const isWinskin = computed(() => !isNil(props.winskin)); return () => isWinskin.value ? ( - + ) : ( ); }, backgroundProps); diff --git a/packages-user/client-base/src/components/icons.tsx b/packages-user/client-base/src/components/icons.tsx index 7445311..efd998f 100644 --- a/packages-user/client-base/src/components/icons.tsx +++ b/packages-user/client-base/src/components/icons.tsx @@ -1,8 +1,6 @@ -import { ElementLocator } from '@motajs/render'; -import { DefaultProps, GraphicPropsBase } from '@motajs/render-vue'; +import { DefaultProps, GraphicPropsBase, lwh } from '@motajs/render-vue'; import { SetupComponentOptions } from '@motajs/system'; import { - computed, defineComponent, DefineSetupFnComponent, onMounted, @@ -11,12 +9,10 @@ import { watch } from 'vue'; -export interface IconsProps extends DefaultProps { - loc: ElementLocator; -} +export interface IconsProps extends DefaultProps {} const iconsProps = { - props: ['loc'] + props: ['loc', 'x', 'y', 'width', 'height'] } satisfies SetupComponentOptions; type IconLoc = [ @@ -120,8 +116,7 @@ export function defineIcon( return defineComponent(props => { const path = shallowRef(); - const width = computed(() => props.loc[2] ?? 200); - const height = computed(() => props.loc[3] ?? 200); + const [loc, width, height] = lwh(props); const generatePath = adjustPath(aspect, path, pathDef); watch(props, () => { @@ -134,7 +129,7 @@ export function defineIcon( return () => ( ( const value = ref(props.value ?? ''); const root = ref(); - const width = computed(() => props.loc?.[2] ?? 200); - const height = computed(() => props.loc?.[3] ?? 200); + const [loc, width, height] = lwh(props); const showText = computed(() => value.value || props.placeholder || ''); const padding = computed(() => props.pad ?? 4); const textLoc = computed(() => [ @@ -241,7 +244,7 @@ export const Input = defineComponent( return () => ( ( (props, { emit }) => { + /** 当前选中项 */ const selected = ref(props.selected ?? props.list[0][0]); - const lineHeight = computed(() => props.lineHeight ?? 24); + /** 行高 */ + const lh = computed(() => props.lineHeight ?? 24); + /** 当前选中项的索引 */ + const index = computed(() => + props.list.findIndex(([key]) => key === selected.value) + ); + + const [loc, w] = lwh(props); const select = (value: string) => { selected.value = value; @@ -70,52 +87,34 @@ export const List = defineComponent( ); return () => ( - - {props.list.map((v, i) => { - const [key, value] = v; - const loc: ElementLocator = [ - 0, - lineHeight.value * i, - props.loc[2] ?? 200, - lineHeight.value - ]; - const selectionLoc: ElementLocator = [ - 0, - 0, - (props.loc[2] ?? 200) - 10, - lineHeight.value - ]; - const textLoc: ElementLocator = [ - 10, - lineHeight.value / 2, - void 0, - void 0, - 0, - 0.5 - ]; + + {props.list.map(([key, value], i) => { return ( - select(key)} + zIndex={10} > - {selected.value === key && ( - - )} ); })} + ); }, diff --git a/packages-user/client-base/src/components/misc.tsx b/packages-user/client-base/src/components/misc.tsx index 158ab0f..b61117c 100644 --- a/packages-user/client-base/src/components/misc.tsx +++ b/packages-user/client-base/src/components/misc.tsx @@ -1,11 +1,14 @@ import { ElementLocator, CustomRenderItem, - MotaOffscreenCanvas2D + MotaOffscreenCanvas2D, + CanvasStyle, + RectRCircleParams } from '@motajs/render'; import { cycledTransition, DefaultProps, + lwh, onExcitedFunc, PathProps } from '@motajs/render-vue'; @@ -13,8 +16,9 @@ import { computed, defineComponent, ref, watch } from 'vue'; import { TextContent, TextContentProps } from './textbox'; import { Scroll, ScrollExpose, ScrollProps } from './scroll'; import { SetupComponentOptions } from '@motajs/system'; -import { clamp } from 'lodash-es'; +import { clamp, isNil } from 'lodash-es'; import { cosh, CurveMode } from '@motajs/animate'; +import { ITextureManager } from '../material'; interface ProgressProps extends DefaultProps { /** 进度条的位置 */ @@ -30,7 +34,7 @@ interface ProgressProps extends DefaultProps { } const progressProps = { - props: ['loc', 'progress', 'success', 'background', 'lineWidth'] + props: ['progress', 'success', 'background', 'lineWidth'] } satisfies SetupComponentOptions; /** @@ -47,9 +51,7 @@ export const Progress = defineComponent(props => { const element = ref(); const render = (canvas: MotaOffscreenCanvas2D) => { - const { ctx } = canvas; - const width = props.loc[2] ?? 200; - const height = props.loc[3] ?? 200; + const { ctx, width, height } = canvas; ctx.lineCap = 'round'; const lineWidth = props.lineWidth ?? 2; ctx.lineWidth = lineWidth; @@ -74,7 +76,7 @@ export const Progress = defineComponent(props => { }); return () => { - return ; + return ; }; }, progressProps); @@ -181,7 +183,7 @@ export interface ScrollTextExpose { } const scrollProps = { - props: ['speed', 'loc', 'pad', 'width'], + props: ['speed', 'loc', 'x', 'y', 'width', 'height', 'pad'], emits: ['scrollEnd'] } satisfies SetupComponentOptions< ScrollTextProps, @@ -217,7 +219,7 @@ export const ScrollText = defineComponent< const scroll = ref(); const speed = ref(props.speed); - const eleHeight = computed(() => props.loc[3] ?? props.width); + const [loc, width, height] = lwh(props); const pad = computed(() => props.pad ?? 16); let lastFixedTime = -1; @@ -267,14 +269,14 @@ export const ScrollText = defineComponent< return () => ( @@ -282,16 +284,32 @@ export const ScrollText = defineComponent< }, scrollProps); export interface SelectionProps extends DefaultProps { - loc: ElementLocator; + /** 非 `winskin` 模式下,填充颜色 */ color?: CanvasStyle; + /** 非 `winskin` 模式下,描边颜色 */ border?: CanvasStyle; - winskin?: ImageIds; + /** 非 `winskin` 模式下,圆角矩形的圆角参数,默认为 [4] */ + circle?: RectRCircleParams; + /** 使用 `winskin` 背景图,会忽略 `color` `border` `circle` 参数 */ + winskin?: string; + /** 边框宽度,默认为 2 */ + borderSize?: number; + /** winskin 使用的贴图管理器 */ + manager?: ITextureManager; /** 选择图标的不透明度范围 */ alphaRange?: [number, number]; } const selectionProps = { - props: ['loc', 'color', 'border', 'winskin', 'alphaRange'] + props: [ + 'color', + 'border', + 'circle', + 'winskin', + 'borderSize', + 'manager', + 'alphaRange' + ] } satisfies SetupComponentOptions; /** @@ -313,52 +331,60 @@ export const Selection = defineComponent(props => { cosh(2, CurveMode.EaseInOut) )!; - const isWinskin = computed(() => !!props.winskin); - const winskinImage = computed(() => - isWinskin.value ? core.material.images.images[props.winskin!] : null - ); - const fixedLoc = computed(() => { - const [x = 0, y = 0, width = 200, height = 200, ax, ay] = props.loc; - return [x + 1, y + 1, width - 2, height - 2, ax, ay]; + const isWinskin = computed(() => !isNil(props.winskin)); + const winskin = computed(() => { + if (isNil(props.winskin) || !props.manager) { + return null; + } else { + return props.manager.getImageByAlias(props.winskin); + } }); const renderWinskin = (canvas: MotaOffscreenCanvas2D) => { - const ctx = canvas.ctx; - const image = winskinImage.value; - if (!image) return; - const [, , width = 200, height = 200] = props.loc; + const { ctx, width: w, height: h } = canvas; + if (!winskin.value) return; + const { width, height, source: i } = winskin.value; // 背景 - ctx.drawImage(image, 130, 66, 28, 28, 2, 2, width - 4, height - 4); + const x1 = (2 / 192) * width; + const x2 = (28 / 192) * width; + const x3 = (128 / 192) * width; + const x4 = (130 / 192) * width; + const x5 = (158 / 192) * width; + const y1 = (2 / 128) * height; + const y2 = (28 / 128) * height; + const y3 = (64 / 128) * height; + const y4 = (66 / 128) * height; + const y5 = (94 / 128) * height; + ctx.drawImage(i, x4, y4, x2, y2, 2, 2, w - 4, h - 4); // 四个角 - ctx.drawImage(image, 128, 64, 2, 2, 0, 0, 2, 2); - ctx.drawImage(image, 158, 64, 2, 2, width - 2, 0, 2, 2); - ctx.drawImage(image, 128, 94, 2, 2, 0, height - 2, 2, 2); - ctx.drawImage(image, 158, 94, 2, 2, width - 2, height - 2, 2, 2); + ctx.drawImage(i, x3, y3, x1, y1, 0, 0, 2, 2); + ctx.drawImage(i, x5, y3, x1, y1, w - 2, 0, 2, 2); + ctx.drawImage(i, x3, y5, x1, y1, 0, h - 2, 2, 2); + ctx.drawImage(i, x5, y5, x1, y1, w - 2, h - 2, 2, 2); // 四条边 - ctx.drawImage(image, 130, 64, 28, 2, 2, 0, width - 4, 2); - ctx.drawImage(image, 130, 94, 28, 2, 2, height - 2, width - 4, 2); - ctx.drawImage(image, 128, 66, 2, 28, 0, 2, 2, height - 4); - ctx.drawImage(image, 158, 66, 2, 28, width - 2, 2, 2, height - 4); + ctx.drawImage(i, x4, y3, x2, y1, 2, 0, w - 4, 2); + ctx.drawImage(i, x4, y5, x2, y1, 2, h - 2, w - 4, 2); + ctx.drawImage(i, x3, y4, x1, y2, 0, 2, 2, h - 4); + ctx.drawImage(i, x5, y4, x1, y2, w - 2, 2, 2, h - 4); }; return () => isWinskin.value ? ( ) : ( ); }, selectionProps); diff --git a/packages-user/client-base/src/components/page.tsx b/packages-user/client-base/src/components/page.tsx index b185204..23ee63b 100644 --- a/packages-user/client-base/src/components/page.tsx +++ b/packages-user/client-base/src/components/page.tsx @@ -11,16 +11,13 @@ import { import { clamp, isNil } from 'lodash-es'; import { ElementLocator, Font } from '@motajs/render'; import { SetupComponentOptions } from '@motajs/system'; -import { DefaultProps } from '@motajs/render-vue'; - -/** 圆角矩形页码距离容器的边框大小,与 pageSize 相乘 */ -const RECT_PAD = 0.1; +import { DefaultProps, lwh } from '@motajs/render-vue'; export interface PageProps extends DefaultProps { /** 共有多少页 */ pages: number; /** 页码组件的定位 */ - loc: ElementLocator; + loc?: ElementLocator; /** 当前页码 */ page?: number; /** 页码的字体 */ @@ -59,7 +56,17 @@ type PageSlots = SlotsType<{ }>; const pageProps = { - props: ['pages', 'loc', 'page', 'font', 'hideIfSingle'], + props: [ + 'pages', + 'loc', + 'x', + 'y', + 'width', + 'height', + 'page', + 'font', + 'hideIfSingle' + ], emits: ['pageChange', 'update:page'] } satisfies SetupComponentOptions< PageProps, @@ -115,8 +122,7 @@ export const Page = defineComponent< const font = computed(() => props.font ?? new Font()); const isFirst = computed(() => nowPage.value === 0); const isLast = computed(() => nowPage.value === props.pages - 1); - const width = computed(() => props.loc[2] ?? 200); - const height = computed(() => props.loc[3] ?? 200); + const [loc, width, height] = lwh(props); const round = computed(() => font.value.size / 4); const nowPageFont = computed(() => Font.clone(font.value, { weight: 700 })); /** 页码的横向间距 */ @@ -135,6 +141,7 @@ export const Page = defineComponent< const rightColor = computed(() => (isLast.value ? '#666' : '#ddd')); let updating = false; + /** 更新页码矩形的位置 */ const updatePagePos = () => { if (updating) return; updating = true; @@ -147,13 +154,14 @@ export const Page = defineComponent< const center = width.value / 2; const size = font.value.size * 1.5; const int = size + interval.value; - nowPageLoc.value = [center, 0, size, size, 0.5, 0]; - leftPageLoc.value = [center - int, 0, size, size, 0.5, 0]; - leftLoc.value = [center - int * 2, 0, size, size, 0.5, 0]; - rightPageLoc.value = [center + int, 0, size, size, 0.5, 0]; - rightLoc.value = [center + int * 2, 0, size, size, 0.5, 0]; + nowPageLoc.value = [center, 0, size, size]; + leftPageLoc.value = [center - int, 0, size, size]; + leftLoc.value = [center - int * 2, 0, size, size]; + rightPageLoc.value = [center + int, 0, size, size]; + rightLoc.value = [center + int * 2, 0, size, size]; }; + /** 更新箭头的尺寸 */ const updateArrowPath = () => { const rectSize = font.value.size * 1.5; const size = font.value.size; @@ -170,11 +178,11 @@ export const Page = defineComponent< rightArrow.value = right; }; + /** 更新页码矩形和文字的位置 */ const updateRectAndText = () => { const size = font.value.size * 1.5; - const pad = RECT_PAD * size; - rectLoc.value = [pad, pad, size - pad * 2, size - pad * 2]; - textLoc.value = [size / 2, size / 2, void 0, void 0, 0.5, 0.5]; + rectLoc.value = [0, 0, size, size]; + textLoc.value = [size / 2, size / 2]; }; watch(font, () => { @@ -236,118 +244,130 @@ export const Page = defineComponent< expose({ changePage, movePage, now }); - return () => { - return ( - - - {slots.default?.(nowPage.value)} + return () => ( + + + {slots.default?.(nowPage.value)} + + + ); }, pageProps); diff --git a/packages-user/client-base/src/components/scroll.tsx b/packages-user/client-base/src/components/scroll.tsx index 16aefe0..f158433 100644 --- a/packages-user/client-base/src/components/scroll.tsx +++ b/packages-user/client-base/src/components/scroll.tsx @@ -32,6 +32,7 @@ import { clamp } from 'lodash-es'; import { SetupComponentOptions } from '@motajs/system'; import { DefaultProps, + lwh, transitioned, useRendererRoot } from '@motajs/render-vue'; @@ -72,7 +73,7 @@ export type ScrollSlots = SlotsType<{ }>; const scrollProps = { - props: ['hor', 'noscroll', 'loc', 'padEnd'] + props: ['hor', 'noscroll', 'loc', 'x', 'y', 'width', 'height', 'padEnd'] } satisfies SetupComponentOptions; /** 滚动条图示的最短长度 */ @@ -124,8 +125,7 @@ export const Scroll = defineComponent( const scrollAlpha = transitioned(0.5, 100, linear())!; - const width = computed(() => props.loc[2] ?? 200); - const height = computed(() => props.loc[3] ?? 200); + const [loc, width, height] = lwh(props); const direction = computed(() => props.hor ? ScrollDirection.Horizontal : ScrollDirection.Vertical ); @@ -570,36 +570,34 @@ export const Scroll = defineComponent( //#endregion - return () => { - return ( - - - {slots.default?.()} - - - - ); - }; + return () => ( + + + {slots.default?.()} + + + + ); }, scrollProps ); diff --git a/packages-user/client-base/src/components/slider.tsx b/packages-user/client-base/src/components/slider.tsx index 9ef982a..232c8bb 100644 --- a/packages-user/client-base/src/components/slider.tsx +++ b/packages-user/client-base/src/components/slider.tsx @@ -1,6 +1,6 @@ import { IHookController } from '@motajs/common'; import { CanvasStyle, Font, IActionEvent, IRenderItem } from '@motajs/render'; -import { DefaultProps } from '@motajs/render-vue'; +import { DefaultProps, lwh } from '@motajs/render-vue'; import { SetupComponentOptions } from '@motajs/system'; import { clamp, isNil } from 'lodash-es'; import { @@ -52,6 +52,8 @@ export type SliderEmits = { const sliderProps = { props: [ 'loc', + 'x', + 'y', 'width', 'height', 'value', @@ -88,8 +90,7 @@ export const Slider = defineComponent< const value = ref(props.value ?? min.value); const rect = ref(); - const width = computed(() => props.loc?.[2] ?? props.width ?? 200); - const height = computed(() => props.loc?.[3] ?? props.height ?? 200); + const [loc, width, height] = lwh(props); const cy = computed(() => height.value / 2); const sx = computed(() => props.padding ?? 4); @@ -196,13 +197,7 @@ export const Slider = defineComponent< }); return () => ( - + { const height = getHeight(); - const [x = 0, y = 0, , h = 0, ax = 0, ay = 0] = props.loc ?? loc.value; - loc.value = [x, y, props.width, props.autoHeight ? height : h, ax, ay]; + const [x = 0, y = 0, , h = 0] = props.loc ?? loc.value; + loc.value = [x, y, props.width, props.autoHeight ? height : h]; emit('updateHeight', height); }; diff --git a/packages-user/client-base/src/components/winskin.tsx b/packages-user/client-base/src/components/winskin.tsx index 613fe25..7b447ea 100644 --- a/packages-user/client-base/src/components/winskin.tsx +++ b/packages-user/client-base/src/components/winskin.tsx @@ -17,7 +17,7 @@ export interface IWinskinProps extends DefaultProps { } const winskinProps = { - props: ['winskin', 'manager', 'texture'] + props: ['winskin', 'manager', 'texture', 'borderSize'] } satisfies SetupComponentOptions; /** winskin 最小单元宽度 */