diff --git a/packages-user/client-base/src/components/floorSelect.tsx b/packages-user/client-base/src/components/floorSelect.tsx index 717e7e9..f6e621d 100644 --- a/packages-user/client-base/src/components/floorSelect.tsx +++ b/packages-user/client-base/src/components/floorSelect.tsx @@ -1,42 +1,37 @@ -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 { computed, defineComponent, onMounted, ref, watch } from 'vue'; import { Scroll, ScrollExpose } from './scroll'; import { MotaOffscreenCanvas2D, Font } from '@motajs/render'; -import { - HALF_STATUS_WIDTH, - STATUS_BAR_HEIGHT, - STATUS_BAR_WIDTH -} from '../../shared'; - -const SCROLL_HEIGHT = STATUS_BAR_HEIGHT - 280; -const HALF_HEIGHT = SCROLL_HEIGHT / 2; export interface FloorSelectorProps extends DefaultProps { - floors: FloorIds[]; + /** 楼层列表,顺序排列,键表示楼层 id,值表示这一楼层的显示名称 */ + floors?: [string, string][]; + /** 当前选中的楼层 */ now?: number; + /** 每行的行高,默认 24 */ + interval?: number; + /** 文字和竖直线是否靠右对齐 */ + alignRight?: boolean; + /** 每行的文字使用的字体,默认使用默认字体家族,字体尺寸为行高的一半 */ + font?: Font; } export type FloorSelectorEmits = { - /** - * 点击关闭按钮时触发 - */ - close: () => void; - /** * 当选中的楼层改变时触发 * @param floor 楼层索引 * @param floorId 楼层 id */ - update: (floor: number, floorId: FloorIds) => void; + update: (floor: number, floorId: string) => void; 'update:now': (value: number) => void; }; const floorSelectorProps = { - props: ['floors', 'now'], - emits: ['close', 'update', 'update:now'] + props: ['floors', 'now', 'loc', 'x', 'y', 'width', 'height'], + emits: ['update', 'update:now'] } satisfies SetupComponentOptions< FloorSelectorProps, FloorSelectorEmits, @@ -48,18 +43,37 @@ export const FloorSelector = defineComponent< FloorSelectorEmits, keyof FloorSelectorEmits >((props, { emit }) => { - const listFont = new Font(Font.defaultFamily, 12); - - /** 当前选中楼层,不反向 */ - const now = ref(props.now ?? 0); /** 当前鼠标选中楼层,反向 */ const selList = ref(0); const scrollRef = ref(); - const floors = computed(() => props.floors.toReversed()); - const floorId = computed(() => props.floors[now.value]); - const floorName = computed(() => core.floors[floorId.value].title); + const [loc, width, height] = lwh(props); + + /** 所有楼层的信息 */ + const floors = computed<[string, string][]>( + () => props.floors?.toReversed() ?? [['unknown', '未知']] + ); + /** 当前选中项的信息 */ + const current = computed( + () => floors.value[floors.value.length - now.value - 1] + ); + /** 每行的行高 */ + const lh = computed(() => props.interval ?? 24); + /** 竖线的横坐标 */ + const lx = computed(() => (props.alignRight ? width.value - 4 : 4)); + /** 竖线与文字边缘的距离 */ + const pad = computed(() => (props.alignRight ? -16 : 16)); + /** 每行选项使用的字体 */ + const listFont = computed( + () => props.font ?? Font.defaults({ size: lh.value / 2 }) + ); + + /** 当前选中楼层,不反向 */ + const now = ref(clamp(props.now ?? 0, 0, floors.value.length - 1)); + + /** 蒙版渐变对象 */ + let gradient: CanvasGradient | null = null; watch( () => props.now, @@ -70,11 +84,15 @@ export const FloorSelector = defineComponent< } ); - let gradient: CanvasGradient | null = null; + watch(loc, () => { + // 更新定位后,需要重新更新渐变对象 + gradient = null; + }); + /** 获取蒙版渐变对象 */ const getGradient = (ctx: CanvasRenderingContext2D) => { if (gradient) return gradient; - gradient = ctx.createLinearGradient(0, 0, 0, SCROLL_HEIGHT); + gradient = ctx.createLinearGradient(0, 0, 0, height.value); gradient.addColorStop(0, 'rgba(255,255,255,0)'); gradient.addColorStop(0.2, 'rgba(255,255,255,1)'); gradient.addColorStop(0.8, 'rgba(255,255,255,1)'); @@ -82,152 +100,94 @@ export const FloorSelector = defineComponent< return gradient; }; + /** 渲染蒙版渐变对象 */ const renderMask = (canvas: MotaOffscreenCanvas2D) => { const { ctx } = canvas; const gradient = getGradient(ctx); ctx.fillStyle = gradient; - ctx.fillRect(0, 0, 144, 200); + ctx.fillRect(0, 0, width.value, height.value); }; + /** 切换至指定楼层 */ const changeTo = (index: number, time: number = 500) => { // 取反是为了符合 2.x 的操作习惯 const res = clamp(index, 0, floors.value.length - 1); const reversed = floors.value.length - res - 1; now.value = res; selList.value = reversed; - const y = reversed * 24; + const y = reversed * lh.value; scrollRef.value?.scrollTo(y, time); - emit('update', now.value, floorId.value); + emit('update', now.value, current.value[0]); emit('update:now', now.value); }; - const changeFloor = (delta: number) => { - changeTo(now.value + delta); - }; - const enterList = (index: number) => { selList.value = index; }; - const close = () => { - emit('close'); - }; - onMounted(() => { changeTo(now.value, 0); }); return () => ( - - - + + + {floors.value.map(([, title], i, a) => { + const reversed = a.length - i - 1; + const nowFloor = a.length - now.value - 1; + const highlight = nowFloor === i || selList.value === i; + const color = highlight ? '#fff' : '#aaa'; + const fill = highlight ? '#fff' : '#000'; + const y = (i - 0.5) * lh.value + height.value / 2; + return ( + enterList(i)} + onLeave={() => enterList(nowFloor)} + onClick={() => changeTo(reversed)} + > + + + + ); + })} + - - changeFloor(10)} - /> - changeFloor(1)} - /> - changeFloor(-1)} - /> - changeFloor(-10)} - /> - - - {floors.value.map((v, i, a) => { - const floor = core.floors[v]; - const reversed = a.length - i - 1; - const nowFloor = a.length - now.value - 1; - const highlight = nowFloor === i || selList.value === i; - const color = highlight ? '#fff' : '#aaa'; - const fill = highlight ? '#fff' : '#000'; - return ( - - enterList(i)} - onLeave={() => enterList(nowFloor)} - onClick={() => changeTo(reversed)} - /> - - - ); - })} - - - - ); }, floorSelectorProps);