mirror of
https://github.com/motajs/template.git
synced 2026-11-21 12:14:31 +08:00
fix: 修复所有内置组件的问题
This commit is contained in:
1 parent
2744a76e07
commit
f47838e316
9 files changed
+241
-127
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { ElementLocator } from '@motajs/render';
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { computed, defineComponent } from 'vue';
|
||||
|
||||
export interface BackgroundProps extends DefaultProps {
|
||||
loc: ElementLocator;
|
||||
winskin?: ImageIds;
|
||||
color?: CanvasStyle;
|
||||
border?: CanvasStyle;
|
||||
}
|
||||
|
||||
const backgroundProps = {
|
||||
props: ['loc', 'winskin', 'color', 'border']
|
||||
} satisfies SetupComponentOptions<BackgroundProps>;
|
||||
|
||||
/**
|
||||
* 背景组件,与 Selection 类似,不过绘制的是背景,而不是选择光标,参数参考 {@link BackgroundProps},用例如下:
|
||||
* ```tsx
|
||||
* // 使用 winskin2.png 作为背景
|
||||
* <Background loc={[8, 8, 160, 160]} winskin="winskin2.png" />
|
||||
* // 使用指定填充和边框颜色作为背景
|
||||
* <Background loc={[8, 8, 160, 160]} color="#333" border="gold" />
|
||||
* ```
|
||||
*/
|
||||
export const Background = defineComponent<BackgroundProps>(props => {
|
||||
const isWinskin = computed(() => !!props.winskin);
|
||||
const fixedLoc = computed<ElementLocator>(() => {
|
||||
const [x = 0, y = 0, width = 200, height = 200] = props.loc;
|
||||
return [x + 2, y + 2, width - 4, height - 4];
|
||||
});
|
||||
|
||||
return () =>
|
||||
isWinskin.value ? (
|
||||
<winskin imageName={props.winskin!} loc={props.loc} noanti />
|
||||
) : (
|
||||
<g-rectr
|
||||
loc={fixedLoc.value}
|
||||
fillStyle={props.color}
|
||||
strokeStyle={props.border}
|
||||
fill
|
||||
stroke
|
||||
lineWidth={2}
|
||||
circle={[4]}
|
||||
/>
|
||||
);
|
||||
}, backgroundProps);
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { DefaultProps, transitionedColor } from '@motajs/render-vue';
|
||||
import { computed, defineComponent, onUnmounted, ref, watch } from 'vue';
|
||||
import { TextContent, TextContentProps } from './textbox';
|
||||
import {
|
||||
@@ -11,8 +11,6 @@ import {
|
||||
IRenderItem,
|
||||
IRenderTreeRoot
|
||||
} from '@motajs/render';
|
||||
// @ts-expect-error render/use.ts 按 D-54 留在实现层,本文件已移入系统层,按 D-68 暂以标注记录,不反向引用、不解耦
|
||||
import { transitionedColor } from '../use';
|
||||
import { linear } from '@motajs/animate';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
|
||||
@@ -71,7 +69,7 @@ const inputProps = {
|
||||
* <Input
|
||||
* placeholder="提示词"
|
||||
* // 双向数据绑定,当输入内容改变时,inputValue 会同时改变
|
||||
* v-model={inputValue.value}
|
||||
* v-model:value={inputValue.value}
|
||||
* // 设置为多行模式
|
||||
* multiline
|
||||
* // 边框颜色
|
||||
@@ -205,7 +203,9 @@ export const Input = defineComponent<InputProps, InputEmits, keyof InputEmits>(
|
||||
ele.style.height = `${inputHeight}px`;
|
||||
ele.style.font = font.string();
|
||||
ele.style.color = String(props.fillStyle ?? 'white');
|
||||
ele.style.backgroundColor = '#111';
|
||||
ele.style.zIndex = '100';
|
||||
ele.value = value.value;
|
||||
document.body.appendChild(ele);
|
||||
ele.focus();
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { computed, defineComponent, ref, SlotsType, VNode } from 'vue';
|
||||
import { computed, defineComponent, ref, SlotsType, VNode, watch } from 'vue';
|
||||
import { Selection } from './misc';
|
||||
import { ElementLocator, Font } from '@motajs/render';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
@@ -8,11 +8,11 @@ import { Scroll } from './scroll';
|
||||
export interface ListProps extends DefaultProps {
|
||||
/** 列表内容,第一项表示 id,第二项表示显示的内容 */
|
||||
list: [string, string][];
|
||||
/** 当前选中的项 */
|
||||
selected: string;
|
||||
/** 定位 */
|
||||
loc: ElementLocator;
|
||||
/** 每行的高度,默认 18 */
|
||||
/** 当前选中的项 */
|
||||
selected?: string;
|
||||
/** 每行的高度,默认 24 */
|
||||
lineHeight?: number;
|
||||
/** 字体 */
|
||||
font?: Font;
|
||||
@@ -53,8 +53,8 @@ const listProps = {
|
||||
|
||||
export const List = defineComponent<ListProps, ListEmits, keyof ListEmits>(
|
||||
(props, { emit }) => {
|
||||
const selected = ref(props.list[0][0]);
|
||||
const lineHeight = computed(() => props.lineHeight ?? 18);
|
||||
const selected = ref(props.selected ?? props.list[0][0]);
|
||||
const lineHeight = computed(() => props.lineHeight ?? 24);
|
||||
|
||||
const select = (value: string) => {
|
||||
selected.value = value;
|
||||
@@ -62,6 +62,13 @@ export const List = defineComponent<ListProps, ListEmits, keyof ListEmits>(
|
||||
emit('update:selected', value);
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.selected,
|
||||
value => {
|
||||
selected.value = value ?? props.list[0][0];
|
||||
}
|
||||
);
|
||||
|
||||
return () => (
|
||||
<Scroll loc={props.loc}>
|
||||
{props.list.map((v, i) => {
|
||||
@@ -87,7 +94,11 @@ export const List = defineComponent<ListProps, ListEmits, keyof ListEmits>(
|
||||
0.5
|
||||
];
|
||||
return (
|
||||
<container loc={loc} onClick={() => select(key)}>
|
||||
<container
|
||||
loc={loc}
|
||||
nocache
|
||||
onClick={() => select(key)}
|
||||
>
|
||||
{selected.value === key && (
|
||||
<Selection
|
||||
loc={selectionLoc}
|
||||
@@ -164,7 +175,7 @@ export const ListPage = defineComponent<
|
||||
keyof ListPageEmits,
|
||||
ListPageSlots
|
||||
>((props, { emit, slots }) => {
|
||||
const selected = ref(props.selected);
|
||||
const selected = ref(props.selected ?? props.list[0][0]);
|
||||
|
||||
const basis = computed(() => props.basis ?? 0.3);
|
||||
const width = computed(() => props.loc[2] ?? 200);
|
||||
@@ -187,10 +198,18 @@ export const ListPage = defineComponent<
|
||||
});
|
||||
|
||||
const update = (key: string) => {
|
||||
selected.value = key;
|
||||
emit('update', key);
|
||||
emit('update:selected', key);
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.selected,
|
||||
value => {
|
||||
selected.value = value ?? props.list[0][0];
|
||||
}
|
||||
);
|
||||
|
||||
const close = () => emit('close');
|
||||
|
||||
return () => (
|
||||
|
||||
@@ -3,16 +3,17 @@ import {
|
||||
CustomRenderItem,
|
||||
MotaOffscreenCanvas2D
|
||||
} from '@motajs/render';
|
||||
import { DefaultProps, PathProps } from '@motajs/render-vue';
|
||||
import {
|
||||
cycledTransition,
|
||||
DefaultProps,
|
||||
onExcitedFunc,
|
||||
PathProps
|
||||
} from '@motajs/render-vue';
|
||||
import { computed, defineComponent, ref, watch } from 'vue';
|
||||
import { TextContent, TextContentProps } from './textbox';
|
||||
import { Scroll, ScrollExpose, ScrollProps } from './scroll';
|
||||
// @ts-expect-error render/use.ts 按 D-54 留在实现层,本文件已移入系统层,按 D-68 暂以标注记录,不反向引用、不解耦
|
||||
import { transitioned } from '../use';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { clamp } from 'lodash-es';
|
||||
// @ts-expect-error render/renderer 按 D-54 留在实现层,本文件已移入系统层,按 D-59 暂以标注记录,不反向引用、不解耦
|
||||
import { using } from '../renderer';
|
||||
import { cosh, CurveMode } from '@motajs/animate';
|
||||
|
||||
interface ProgressProps extends DefaultProps {
|
||||
@@ -29,7 +30,7 @@ interface ProgressProps extends DefaultProps {
|
||||
}
|
||||
|
||||
const progressProps = {
|
||||
props: ['loc', 'progress', 'success', 'background']
|
||||
props: ['loc', 'progress', 'success', 'background', 'lineWidth']
|
||||
} satisfies SetupComponentOptions<ProgressProps>;
|
||||
|
||||
/**
|
||||
@@ -115,14 +116,15 @@ export const Arrow = defineComponent<ArrowProps>(props => {
|
||||
path.lineTo(x2 - x, y2 - y);
|
||||
const angle = Math.atan2(y2 - y1, x2 - x1);
|
||||
path.moveTo(
|
||||
x2 - head * Math.cos(angle - Math.PI / 6),
|
||||
y2 - head * Math.sin(angle - Math.PI / 6)
|
||||
x2 - head * Math.cos(angle - Math.PI / 6) - x,
|
||||
y2 - head * Math.sin(angle - Math.PI / 6) - y
|
||||
);
|
||||
path.lineTo(x2 - x, y2 - y);
|
||||
path.lineTo(
|
||||
x2 - head * Math.cos(angle + Math.PI / 6),
|
||||
y2 - head * Math.sin(angle + Math.PI / 6)
|
||||
x2 - head * Math.cos(angle + Math.PI / 6) - x,
|
||||
y2 - head * Math.sin(angle + Math.PI / 6) - y
|
||||
);
|
||||
|
||||
return path;
|
||||
});
|
||||
|
||||
@@ -218,22 +220,24 @@ export const ScrollText = defineComponent<
|
||||
const eleHeight = computed(() => props.loc[3] ?? props.width);
|
||||
const pad = computed(() => props.pad ?? 16);
|
||||
|
||||
let lastFixedTime = Date.now();
|
||||
let lastFixedTime = -1;
|
||||
let lastFixedPos = 0;
|
||||
let paused = false;
|
||||
let nowScroll = 0;
|
||||
|
||||
using.onExcitedFunc(() => {
|
||||
onExcitedFunc(time => {
|
||||
if (paused || !scroll.value) return;
|
||||
const now = Date.now();
|
||||
const dt = now - lastFixedTime;
|
||||
if (lastFixedTime === -1) {
|
||||
lastFixedTime = time;
|
||||
return;
|
||||
}
|
||||
const dt = time - lastFixedTime;
|
||||
nowScroll = (dt / 1000) * speed.value + lastFixedPos;
|
||||
scroll.value.scrollTo(nowScroll, 0);
|
||||
if (nowScroll >= scroll.value.getScrollLength()) {
|
||||
emit('scrollEnd');
|
||||
paused = true;
|
||||
}
|
||||
lastFixedTime = now;
|
||||
});
|
||||
|
||||
const pause = () => {
|
||||
@@ -243,18 +247,18 @@ export const ScrollText = defineComponent<
|
||||
const resume = () => {
|
||||
paused = false;
|
||||
lastFixedPos = nowScroll;
|
||||
lastFixedTime = Date.now();
|
||||
lastFixedTime = -1;
|
||||
};
|
||||
|
||||
const setSpeed = (value: number) => {
|
||||
lastFixedPos = nowScroll;
|
||||
lastFixedTime = Date.now();
|
||||
lastFixedTime = -1;
|
||||
speed.value = value;
|
||||
};
|
||||
|
||||
const rescroll = () => {
|
||||
nowScroll = 0;
|
||||
lastFixedTime = Date.now();
|
||||
lastFixedTime = -1;
|
||||
lastFixedPos = 0;
|
||||
};
|
||||
|
||||
@@ -302,8 +306,9 @@ const selectionProps = {
|
||||
export const Selection = defineComponent<SelectionProps>(props => {
|
||||
const minAlpha = computed(() => props.alphaRange?.[0] ?? 0.25);
|
||||
const maxAlpha = computed(() => props.alphaRange?.[1] ?? 0.55);
|
||||
const alpha = transitioned(
|
||||
const alpha = cycledTransition(
|
||||
minAlpha.value,
|
||||
maxAlpha.value,
|
||||
2000,
|
||||
cosh(2, CurveMode.EaseInOut)
|
||||
)!;
|
||||
@@ -336,15 +341,6 @@ export const Selection = defineComponent<SelectionProps>(props => {
|
||||
ctx.drawImage(image, 158, 66, 2, 28, width - 2, 2, 2, height - 4);
|
||||
};
|
||||
|
||||
using.onExcitedFunc(() => {
|
||||
if (alpha.value === maxAlpha.value) {
|
||||
alpha.set(minAlpha.value);
|
||||
}
|
||||
if (alpha.value === minAlpha.value) {
|
||||
alpha.set(maxAlpha.value);
|
||||
}
|
||||
});
|
||||
|
||||
return () =>
|
||||
isWinskin.value ? (
|
||||
<custom
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
onUnmounted,
|
||||
onUpdated,
|
||||
ref,
|
||||
shallowRef,
|
||||
SlotsType,
|
||||
VNode,
|
||||
watch
|
||||
@@ -24,14 +25,17 @@ import {
|
||||
ActionType,
|
||||
CustomContainerPropagateOrigin,
|
||||
IRenderItem,
|
||||
ICustomContainer
|
||||
ICustomContainer,
|
||||
IRenderItemBaseHooks
|
||||
} from '@motajs/render';
|
||||
import { cosh, linear, Transition } from '@motajs/animate';
|
||||
import { cosh, CurveMode, linear } from '@motajs/animate';
|
||||
import { clamp } from 'lodash-es';
|
||||
// @ts-expect-error render/use.ts 按 D-54 留在实现层,本文件已移入系统层,按 D-68 暂以标注记录,不反向引用、不解耦
|
||||
import { transitioned } from '../use';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import {
|
||||
DefaultProps,
|
||||
transitioned,
|
||||
useRendererRoot
|
||||
} from '@motajs/render-vue';
|
||||
|
||||
export const enum ScrollDirection {
|
||||
Horizontal,
|
||||
@@ -108,13 +112,16 @@ const SCROLL_COLOR = '255,255,255';
|
||||
*/
|
||||
export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
(props, { slots, expose }) => {
|
||||
/** 根元素 */
|
||||
const root = useRendererRoot()!;
|
||||
|
||||
/** 滚动条的定位 */
|
||||
const sp = ref<ElementLocator>([0, 0, 1, 1]);
|
||||
|
||||
const listenedChild: Set<IRenderItem> = new Set();
|
||||
const areaMap: Map<IRenderItem, [number, number]> = new Map();
|
||||
const content = ref<Container>();
|
||||
const scroll = ref<CustomRenderItem>();
|
||||
const content = shallowRef<Container>();
|
||||
const scroll = shallowRef<CustomRenderItem>();
|
||||
|
||||
const scrollAlpha = transitioned(0.5, 100, linear())!;
|
||||
|
||||
@@ -132,10 +139,6 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
scroll.value?.update();
|
||||
});
|
||||
|
||||
/** 滚动内容的当前位置 */
|
||||
let contentPos = 0;
|
||||
/** 滚动条的当前位置 */
|
||||
let scrollPos = 0;
|
||||
/** 滚动内容的目标位置 */
|
||||
let contentTarget = 0;
|
||||
/** 滚动条的目标位置 */
|
||||
@@ -145,25 +148,13 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
/** 滚动条的长度 */
|
||||
let scrollLength = SCROLL_MIN_LENGTH;
|
||||
|
||||
const transition = new Transition();
|
||||
transition.value.scroll = 0;
|
||||
transition.value.showScroll = 0;
|
||||
transition.mode(cosh('sin', 'out')).absolute();
|
||||
/** 滚动条的当前位置 */
|
||||
const scrollPos = transitioned(0, 300, cosh(2, CurveMode.EaseOut))!;
|
||||
/** 滚动内容的当前位置 */
|
||||
const contentPos = transitioned(0, 300, cosh(2, CurveMode.EaseOut))!;
|
||||
|
||||
//#region 滚动操作
|
||||
|
||||
transition.ticker.add(() => {
|
||||
if (scrollPos !== scrollTarget) {
|
||||
scrollPos = transition.value.scroll;
|
||||
content.value?.update();
|
||||
}
|
||||
if (contentPos !== contentTarget) {
|
||||
contentPos = transition.value.showScroll;
|
||||
checkAllItem();
|
||||
content.value?.update();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 滚动到目标值
|
||||
* @param time 动画时长
|
||||
@@ -181,10 +172,14 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
(height.value - scrollLength) * (contentTarget / max);
|
||||
}
|
||||
if (isNaN(scrollTarget)) scrollTarget = 0;
|
||||
transition.time(time).transition('scroll', scrollTarget);
|
||||
transition.time(time).transition('showScroll', target);
|
||||
scrollPos.set(scrollTarget, time);
|
||||
contentPos.set(target, time);
|
||||
};
|
||||
|
||||
watch(contentPos.ref, () => {
|
||||
checkAllItem();
|
||||
});
|
||||
|
||||
/**
|
||||
* 计算一个元素会在画面上显示的区域
|
||||
*/
|
||||
@@ -200,13 +195,14 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
* 检查一个元素是否需要显示,不需要则隐藏
|
||||
*/
|
||||
const checkItem = (item: IRenderItem) => {
|
||||
if (item.isComment) return;
|
||||
const area = areaMap.get(item);
|
||||
if (!area) {
|
||||
item.show();
|
||||
return;
|
||||
}
|
||||
const [min, max] = area;
|
||||
if (contentPos > min - 10 && contentPos < max + 10) {
|
||||
if (contentPos.value > min - 10 && contentPos.value < max + 10) {
|
||||
item.show();
|
||||
} else {
|
||||
item.hide();
|
||||
@@ -221,9 +217,10 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
};
|
||||
|
||||
/**
|
||||
* 当一个元素的矩阵发生变换时执行,检查其显示区域
|
||||
* 更新容器中的渲染元素状态
|
||||
* @param item 渲染元素
|
||||
*/
|
||||
const onTransform = (item: IRenderItem) => {
|
||||
const updateElementStatus = (item: IRenderItem) => {
|
||||
const rect = item.getBoundingRect();
|
||||
const pad = props.padEnd ?? 0;
|
||||
if (item.parent === content.value) {
|
||||
@@ -239,7 +236,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
item.off('transform', onTransform);
|
||||
item.removeHook(transformHook);
|
||||
listenedChild.delete(item);
|
||||
}
|
||||
getArea(item, rect);
|
||||
@@ -248,6 +245,18 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
content.value?.update();
|
||||
};
|
||||
|
||||
/**
|
||||
* 当一个元素的矩阵发生变换时执行,检查其显示区域
|
||||
*/
|
||||
const transformHook: IRenderItemBaseHooks = {
|
||||
onTransform(item: IRenderItem) {
|
||||
updateElementStatus(item);
|
||||
},
|
||||
onResize(item: IRenderItem) {
|
||||
updateElementStatus(item);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 更新滚动条位置
|
||||
*/
|
||||
@@ -276,6 +285,10 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
};
|
||||
|
||||
let updating = false;
|
||||
|
||||
/**
|
||||
* 更新滚动条内容
|
||||
*/
|
||||
const updateScroll = () => {
|
||||
if (!content.value || updating) return;
|
||||
updating = true;
|
||||
@@ -283,7 +296,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
updating = false;
|
||||
});
|
||||
let max = 0;
|
||||
listenedChild.forEach(v => v.off('transform', onTransform));
|
||||
listenedChild.forEach(v => v.removeHook(transformHook));
|
||||
listenedChild.clear();
|
||||
areaMap.clear();
|
||||
content.value.children.forEach(v => {
|
||||
@@ -300,7 +313,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
}
|
||||
getArea(v, rect);
|
||||
if (!listenedChild.has(v)) {
|
||||
v.on('transform', onTransform);
|
||||
v.addHook(transformHook);
|
||||
listenedChild.add(v);
|
||||
}
|
||||
checkItem(v);
|
||||
@@ -314,7 +327,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
onUpdated(updateScroll);
|
||||
onMounted(updateScroll);
|
||||
onUnmounted(() => {
|
||||
listenedChild.forEach(v => v.off('transform', onTransform));
|
||||
listenedChild.forEach(v => v.removeHook(transformHook));
|
||||
});
|
||||
|
||||
//#endregion
|
||||
@@ -328,7 +341,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
ctx.lineWidth = 3;
|
||||
ctx.strokeStyle = scrollColor.value;
|
||||
ctx.beginPath();
|
||||
const scroll = transition.value.scroll;
|
||||
const scroll = scrollPos.value;
|
||||
if (direction.value === ScrollDirection.Horizontal) {
|
||||
ctx.moveTo(scroll + 5, 5);
|
||||
ctx.lineTo(scroll + scrollLength - 5, 5);
|
||||
@@ -347,9 +360,9 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
const ctx = canvas.ctx;
|
||||
ctx.save();
|
||||
if (direction.value === ScrollDirection.Horizontal) {
|
||||
ctx.translate(-contentPos, 0);
|
||||
ctx.translate(-contentPos.value, 0);
|
||||
} else {
|
||||
ctx.translate(0, -contentPos);
|
||||
ctx.translate(0, -contentPos.value);
|
||||
}
|
||||
children.forEach(v => {
|
||||
if (v.hidden) return;
|
||||
@@ -373,9 +386,9 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
) => {
|
||||
if (progress === EventProgress.Capture) {
|
||||
if (direction.value === ScrollDirection.Horizontal) {
|
||||
event.offsetX += contentPos;
|
||||
event.offsetX += contentPos.value;
|
||||
} else {
|
||||
event.offsetY += contentPos;
|
||||
event.offsetY += contentPos.value;
|
||||
}
|
||||
}
|
||||
origin(type, progress, event);
|
||||
@@ -471,9 +484,9 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
const pos = getPos(ev, true);
|
||||
// 计算点击在了滚动条的哪个位置
|
||||
const offsetPos = getPos(ev);
|
||||
const sEnd = scrollPos + scrollLength;
|
||||
if (offsetPos >= scrollPos && offsetPos <= sEnd) {
|
||||
scrollDownPos = pos - scrollPos;
|
||||
const sEnd = scrollPos.value + scrollLength;
|
||||
if (offsetPos >= scrollPos.value && offsetPos <= sEnd) {
|
||||
scrollDownPos = pos - scrollPos.value;
|
||||
scrollMutate = false;
|
||||
scrollPin = getScrollPin(ev);
|
||||
} else {
|
||||
@@ -509,7 +522,7 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
scrollAlpha.set(0.7);
|
||||
if (!scrollMutate) return;
|
||||
const pos = getPos(ev);
|
||||
if (pos < scrollPos) {
|
||||
if (pos < scrollPos.value) {
|
||||
scrollTo(contentTarget - 50, 300);
|
||||
} else {
|
||||
scrollTo(contentTarget + 50, 300);
|
||||
@@ -526,15 +539,24 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
|
||||
//#endregion
|
||||
|
||||
const hook: IRenderItemBaseHooks = {
|
||||
onMove(ev) {
|
||||
move(ev);
|
||||
moveScroll(ev);
|
||||
}
|
||||
};
|
||||
|
||||
watch(root, (value, old) => {
|
||||
old?.removeHook(hook);
|
||||
value?.addHook(hook);
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
scroll.value?.root?.on('move', move);
|
||||
scroll.value?.root?.on('move', moveScroll);
|
||||
checkAllItem();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
scroll.value?.root?.off('move', move);
|
||||
scroll.value?.root?.off('move', moveScroll);
|
||||
transition.ticker.destroy();
|
||||
root.value?.removeHook(hook);
|
||||
});
|
||||
|
||||
//#region expose 函数
|
||||
@@ -552,14 +574,16 @@ export const Scroll = defineComponent<ScrollProps, {}, string, ScrollSlots>(
|
||||
|
||||
return () => {
|
||||
return (
|
||||
<container loc={props.loc} onWheel={wheel}>
|
||||
<container loc={props.loc} onWheel={wheel} nocache>
|
||||
<container-custom
|
||||
loc={[0, 0, props.loc[2], props.loc[3]]}
|
||||
loc={[0, 0, width.value, height.value]}
|
||||
ref={content}
|
||||
onDown={down}
|
||||
render={renderContent}
|
||||
propagate={customPropagate}
|
||||
zIndex={0}
|
||||
bindings={[scrollPos.ref.value, contentPos.ref.value]}
|
||||
onUpdateChildren={updateScroll}
|
||||
>
|
||||
{slots.default?.()}
|
||||
</container-custom>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { IHookController } from '@motajs/common';
|
||||
import { CanvasStyle, Font, IActionEvent, IRenderItem } from '@motajs/render';
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { clamp, isNil } from 'lodash-es';
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
@@ -85,10 +86,10 @@ export const Slider = defineComponent<
|
||||
|
||||
/** 当前值 */
|
||||
const value = ref(props.value ?? min.value);
|
||||
const container = ref<IRenderItem>();
|
||||
const rect = ref<IRenderItem>();
|
||||
|
||||
const width = computed(() => props.loc?.[2] ?? props.width ?? 200);
|
||||
const height = computed(() => props.loc?.[3] ?? props.width ?? 200);
|
||||
const height = computed(() => props.loc?.[3] ?? props.height ?? 200);
|
||||
|
||||
const cy = computed(() => height.value / 2);
|
||||
const sx = computed(() => props.padding ?? 4);
|
||||
@@ -104,11 +105,19 @@ export const Slider = defineComponent<
|
||||
() => props.value,
|
||||
newValue => {
|
||||
if (!isNil(newValue)) {
|
||||
value.value = newValue;
|
||||
value.value = clamp(newValue, min.value, max.value);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
watch(min, newValue => {
|
||||
value.value = clamp(value.value, newValue, max.value);
|
||||
});
|
||||
|
||||
watch(max, newValue => {
|
||||
value.value = clamp(value.value, min.value, newValue);
|
||||
});
|
||||
|
||||
/** 端点横坐标 */
|
||||
const fx = computed(() => {
|
||||
const ratio = (value.value - min.value) / (max.value - min.value);
|
||||
@@ -119,16 +128,22 @@ export const Slider = defineComponent<
|
||||
/** 数值显示值 */
|
||||
const valueStr = computed(() => {
|
||||
const v = Number(value.value.toFixed(8));
|
||||
if (v < 1000 && v > 0.01) return value.value.toString();
|
||||
else return v.toExponential(2);
|
||||
const abs = Math.abs(v);
|
||||
if ((abs < 1000 && abs > 0.01) || abs < 1e-8) {
|
||||
return value.value.toString();
|
||||
} else {
|
||||
return v.toExponential(2);
|
||||
}
|
||||
});
|
||||
|
||||
let dragging = false;
|
||||
let startX = -1;
|
||||
let startValue = 0;
|
||||
|
||||
const down = (ev: IActionEvent) => {
|
||||
dragging = true;
|
||||
startX = ev.absoluteX;
|
||||
startValue = value.value;
|
||||
};
|
||||
|
||||
const up = () => {
|
||||
@@ -139,7 +154,7 @@ export const Slider = defineComponent<
|
||||
const move = (ev: IActionEvent) => {
|
||||
if (!dragging || startX < 0) return;
|
||||
const dx = ev.absoluteX - startX;
|
||||
const ratio = (value.value - min.value) / (max.value - min.value);
|
||||
const ratio = (startValue - min.value) / (max.value - min.value);
|
||||
const now = ratio * (ex.value - sx.value);
|
||||
if (dx < -now) {
|
||||
value.value = min.value;
|
||||
@@ -152,27 +167,41 @@ export const Slider = defineComponent<
|
||||
const step = props.step ?? 1;
|
||||
const steps = Math.round((targetValue - min.value) / step);
|
||||
const aligned = steps * step + min.value;
|
||||
value.value = aligned;
|
||||
value.value = clamp(aligned, min.value, max.value);
|
||||
emit('update:value', aligned);
|
||||
}
|
||||
};
|
||||
|
||||
let controller: IHookController | null = null;
|
||||
onMounted(() => {
|
||||
if (!container.value) return;
|
||||
const root = container.value.root;
|
||||
if (!rect.value) return;
|
||||
const root = rect.value.root;
|
||||
if (!root) return;
|
||||
root.on('moveCapture', move);
|
||||
controller = root.addHook({
|
||||
onMoveCapture(ev) {
|
||||
move(ev);
|
||||
},
|
||||
onUpCapture() {
|
||||
up();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
if (!container.value) return;
|
||||
const root = container.value.root;
|
||||
if (!rect.value) return;
|
||||
const root = rect.value.root;
|
||||
if (!root) return;
|
||||
root.off('moveCapture', move);
|
||||
controller?.unload();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container ref={container}>
|
||||
<container
|
||||
ref={rect}
|
||||
loc={props.loc}
|
||||
width={props.width}
|
||||
height={props.height}
|
||||
nocache
|
||||
>
|
||||
<g-line
|
||||
line={[sx.value, cy.value, ex.value, cy.value]}
|
||||
lineCap="round"
|
||||
@@ -197,7 +226,6 @@ export const Slider = defineComponent<
|
||||
cursor="pointer"
|
||||
zIndex={2}
|
||||
onDown={down}
|
||||
onUp={up}
|
||||
/>
|
||||
<text
|
||||
text={valueStr.value}
|
||||
|
||||
@@ -31,7 +31,7 @@ import {
|
||||
} from './textboxTyper';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { texture } from '../elements';
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { DefaultProps, onExcited } from '@motajs/render-vue';
|
||||
|
||||
// todo: TextContent 应该改成渲染元素?
|
||||
|
||||
@@ -131,6 +131,8 @@ export const TextContent = defineComponent<
|
||||
let renderable: TyperRenderable[] = [];
|
||||
let needUpdate = false;
|
||||
|
||||
onExcited(typer);
|
||||
|
||||
const retype = () => {
|
||||
if (needUpdate || props.hidden) return;
|
||||
needUpdate = true;
|
||||
@@ -407,15 +409,15 @@ export const Textbox = defineComponent<
|
||||
});
|
||||
|
||||
const onSetText = () => {
|
||||
nextTick(() => {
|
||||
titleElement.value?.requestBeforeFrame(() => {
|
||||
if (titleElement.value) {
|
||||
const { width, height } = titleElement.value;
|
||||
tw.value = width + data.titlePadding! * 2;
|
||||
th.value = height + data.titlePadding! * 2;
|
||||
}
|
||||
});
|
||||
});
|
||||
// nextTick(() => {
|
||||
// titleElement.value?.requestBeforeFrame(() => {
|
||||
// if (titleElement.value) {
|
||||
// const { width, height } = titleElement.value;
|
||||
// tw.value = width + data.titlePadding! * 2;
|
||||
// th.value = height + data.titlePadding! * 2;
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
};
|
||||
|
||||
//#region store
|
||||
|
||||
@@ -3,8 +3,7 @@ import { Font, MotaOffscreenCanvas2D } from '@motajs/render';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { RenderableData, AutotileRenderable, texture } from '../elements';
|
||||
// @ts-expect-error render/renderer 按 D-54 留在实现层,本文件已移入系统层,按 D-59 暂以标注记录,不反向引用、不解耦
|
||||
import { using } from '../renderer';
|
||||
import { IExcitable } from '@motajs/animate';
|
||||
|
||||
/** 文字的安全填充,会填充在文字的上侧和下侧,防止削顶和削底 */
|
||||
const SAFE_PAD = 1;
|
||||
@@ -244,7 +243,10 @@ interface TextContentTyperEvent {
|
||||
|
||||
type TyperFunction = (data: TyperRenderable[], typing: boolean) => void;
|
||||
|
||||
export class TextContentTyper extends EventEmitter<TextContentTyperEvent> {
|
||||
export class TextContentTyper
|
||||
extends EventEmitter<TextContentTyperEvent>
|
||||
implements IExcitable<number>
|
||||
{
|
||||
/** 文字配置信息 */
|
||||
readonly config: Required<TyperConfig>;
|
||||
/** 文字解析器 */
|
||||
@@ -309,8 +311,6 @@ export class TextContentTyper extends EventEmitter<TextContentTyperEvent> {
|
||||
},
|
||||
this.config
|
||||
);
|
||||
|
||||
using.onExcitedFunc(() => this.tick());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -491,7 +491,7 @@ export class TextContentTyper extends EventEmitter<TextContentTyperEvent> {
|
||||
/**
|
||||
* 每帧执行的函数
|
||||
*/
|
||||
private tick() {
|
||||
excited() {
|
||||
if (!this.typing) return;
|
||||
const now = Date.now();
|
||||
const needType = Math.round(
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { ElementLocator, Font } from '@motajs/render';
|
||||
import { logger } from '@motajs/common';
|
||||
import { computed, defineComponent, onUnmounted, ref } from 'vue';
|
||||
// @ts-expect-error render/use.ts 按 D-54 留在实现层,本文件已移入系统层,按 D-68 暂以标注记录,不反向引用、不解耦
|
||||
import { transitioned } from '../use';
|
||||
import { cosh } from '@motajs/animate';
|
||||
import { debounce } from 'lodash-es';
|
||||
import { texture } from '../elements';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { DefaultProps } from '@motajs/render-vue';
|
||||
import { DefaultProps, transitioned } from '@motajs/render-vue';
|
||||
|
||||
export interface TipProps extends DefaultProps {
|
||||
/** 显示的位置 */
|
||||
|
||||
Reference in new issue
Block a user