mirror of
https://github.com/motajs/template.git
synced 2026-11-22 21:14:31 +08:00
docs: complete advanced II custom UI documentation
This commit is contained in:
1 parent
e25ef39481
commit
5db2cefb39
127 files changed
+22831
-13
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[8, 8, 228, 148]}>
|
||||
<g-rect
|
||||
loc={[110, 70, 100, 60, 0, 0]}
|
||||
fillStyle="#2f6fed"
|
||||
alpha={0.6}
|
||||
/>
|
||||
<g-circle circle={[110, 70, 6]} fillStyle="#e8f0ff" />
|
||||
<text loc={[8, 122]} text="锚点 0, 0" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
|
||||
<container loc={[244, 8, 228, 148]}>
|
||||
<g-rect
|
||||
loc={[110, 70, 100, 60, 0.5, 0.5]}
|
||||
fillStyle="#f2b84b"
|
||||
alpha={0.6}
|
||||
/>
|
||||
<g-circle circle={[110, 70, 6]} fillStyle="#ffe9b0" />
|
||||
<text loc={[8, 122]} text="锚点 0.5, 0.5" fillStyle="#ffe9b0" />
|
||||
</container>
|
||||
|
||||
<container loc={[8, 164, 228, 148]}>
|
||||
<g-rect
|
||||
loc={[110, 70, 100, 60, 1, 1]}
|
||||
fillStyle="#58c4dd"
|
||||
alpha={0.6}
|
||||
/>
|
||||
<g-circle circle={[110, 70, 6]} fillStyle="#e8f0ff" />
|
||||
<text loc={[8, 122]} text="锚点 1, 1" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
|
||||
<container loc={[244, 164, 228, 148]}>
|
||||
<g-rect
|
||||
loc={[110, 70, 160, 100, 0.5, 0.5]}
|
||||
fillStyle="#c58af9"
|
||||
alpha={0.5}
|
||||
/>
|
||||
<g-circle circle={[110, 70, 6]} fillStyle="#f3e8ff" />
|
||||
<text
|
||||
loc={[8, 122]}
|
||||
text="中心锚点,尺寸更大"
|
||||
fillStyle="#f3e8ff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// UI 入门演示:一个 480×320 的容器,里面放一行文字和一个矩形
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
{/* 一行文字 */}
|
||||
<text loc={[24, 24]} text="你好,UI" fillStyle="#e8f0ff" />
|
||||
{/* 一个矩形 */}
|
||||
<g-rect loc={[24, 64, 160, 80]} fill fillStyle="#2f6fed" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { Arrow } from '../../../../../packages-user/client-base/src/components/misc';
|
||||
|
||||
// 箭头:三条箭头分别改 head 与 color
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<Arrow arrow={[48, 48, 300, 48]} head={8} color="#f2b84b" />
|
||||
<text loc={[48, 80]} text="head = 8" fillStyle="#cfe0ff" />
|
||||
|
||||
<Arrow arrow={[48, 136, 300, 136]} head={18} color="#7ee787" />
|
||||
<text loc={[48, 168]} text="head = 18" fillStyle="#cfe0ff" />
|
||||
|
||||
<Arrow arrow={[48, 224, 300, 224]} head={30} color="#7aa2ff" />
|
||||
<text loc={[48, 256]} text="head = 30" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { Background } from '../../../../../packages-user/client-base/src/components/background';
|
||||
|
||||
// 背景:同一个组件换两组 color 与 border
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[40, 48, 180, 150]}>
|
||||
<Background
|
||||
loc={[0, 0, 180, 150]}
|
||||
color="#1d2b45"
|
||||
border="#3a5a8c"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text="color + border"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[260, 48, 180, 150]}>
|
||||
<Background
|
||||
loc={[0, 0, 180, 150]}
|
||||
color="#2a2140"
|
||||
border="#c9a7ff"
|
||||
/>
|
||||
<text loc={[16, 16]} text="换一组颜色" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import {
|
||||
PlayIcon,
|
||||
PauseIcon,
|
||||
RollbackIcon,
|
||||
RetweetIcon,
|
||||
SoundVolume,
|
||||
Fullscreen,
|
||||
DoubleArrow,
|
||||
StepForward,
|
||||
ArrowLeftTailless
|
||||
} from '../../../../../packages-user/client-base/src/components/icons';
|
||||
|
||||
// 图标组件:每个图标都只接受 loc,按名字使用
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<PlayIcon loc={[56, 36, 32, 32]} />
|
||||
<text loc={[56, 80]} text="PlayIcon" fillStyle="#cfe0ff" />
|
||||
|
||||
<PauseIcon loc={[196, 36, 32, 32]} />
|
||||
<text loc={[196, 80]} text="PauseIcon" fillStyle="#cfe0ff" />
|
||||
|
||||
<RollbackIcon loc={[336, 36, 32, 32]} />
|
||||
<text loc={[336, 80]} text="RollbackIcon" fillStyle="#cfe0ff" />
|
||||
|
||||
<RetweetIcon loc={[56, 128, 32, 32]} />
|
||||
<text loc={[56, 172]} text="RetweetIcon" fillStyle="#cfe0ff" />
|
||||
|
||||
<SoundVolume loc={[196, 128, 32, 32]} />
|
||||
<text loc={[196, 172]} text="SoundVolume" fillStyle="#cfe0ff" />
|
||||
|
||||
<Fullscreen loc={[336, 128, 32, 32]} />
|
||||
<text loc={[336, 172]} text="Fullscreen" fillStyle="#cfe0ff" />
|
||||
|
||||
<DoubleArrow loc={[56, 220, 32, 32]} />
|
||||
<text loc={[56, 264]} text="DoubleArrow" fillStyle="#cfe0ff" />
|
||||
|
||||
<ArrowLeftTailless loc={[196, 220, 32, 32]} />
|
||||
<text
|
||||
loc={[196, 264]}
|
||||
text="ArrowLeftTailless"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<StepForward loc={[336, 220, 32, 32]} />
|
||||
<text loc={[336, 264]} text="StepForward" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Input } from '../../../../../packages-user/client-base/src/components/input';
|
||||
|
||||
// 输入框:单行占位、单行已填内容与多行输入
|
||||
export default defineComponent(() => {
|
||||
const name = ref('');
|
||||
const filled = ref('魔塔玩家');
|
||||
const note = ref('第一行备注\n第二行备注');
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[40, 24]} text="单行" fillStyle="#cfe0ff" />
|
||||
<Input
|
||||
loc={[40, 48, 280, 32]}
|
||||
v-model:value={name.value}
|
||||
placeholder="输入名字"
|
||||
/>
|
||||
|
||||
<text loc={[40, 96]} text="已填内容" fillStyle="#cfe0ff" />
|
||||
<Input loc={[40, 120, 280, 32]} v-model:value={filled.value} />
|
||||
|
||||
<text loc={[40, 168]} text="多行" fillStyle="#cfe0ff" />
|
||||
<Input
|
||||
loc={[40, 192, 320, 88]}
|
||||
v-model:value={note.value}
|
||||
multiline
|
||||
placeholder="多行备注"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { List } from '../../../../../packages-user/client-base/src/components/list';
|
||||
|
||||
const ITEMS: [string, string][] = [
|
||||
['basic', '基础设置'],
|
||||
['sound', '声音'],
|
||||
['key', '按键'],
|
||||
['about', '关于']
|
||||
];
|
||||
|
||||
// 列表:四项由 list 渲染,选中项显示闪烁光标
|
||||
export default defineComponent(() => {
|
||||
const selected = ref(ITEMS[0][0]);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<List
|
||||
loc={[40, 40, 300, 160]}
|
||||
list={ITEMS}
|
||||
lineHeight={32}
|
||||
color="#2b4a6f"
|
||||
border="#6fa8ff"
|
||||
alphaRange={[0.6, 0.9]}
|
||||
v-model:selected={selected.value}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { ListPage } from '../../../../../packages-user/client-base/src/components/list';
|
||||
|
||||
const ITEMS: [string, string][] = [
|
||||
['basic', '基础设置'],
|
||||
['sound', '声音'],
|
||||
['key', '按键']
|
||||
];
|
||||
const DETAIL: Record<string, string> = {
|
||||
basic: '这里放基础设置的说明。',
|
||||
sound: '这里放音量与音效的说明。',
|
||||
key: '这里放按键改绑的说明。'
|
||||
};
|
||||
|
||||
// 左列表、右详情:选中项高亮,右侧显示对应内容
|
||||
export default defineComponent(() => {
|
||||
const selected = ref(ITEMS[0][0]);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<ListPage
|
||||
loc={[40, 40, 400, 240]}
|
||||
list={ITEMS}
|
||||
v-model:selected={selected.value}
|
||||
>
|
||||
{(key: string) => (
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text={DETAIL[key] ?? '没有内容'}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
)}
|
||||
</ListPage>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Page } from '../../../../../packages-user/client-base/src/components/page';
|
||||
|
||||
const PAGES = ['第一页:基础设置', '第二页:声音', '第三页:按键'];
|
||||
|
||||
// 分页:三页内容共用一个 loc,页码条在底部
|
||||
export default defineComponent(() => {
|
||||
const page = ref(0);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<Page pages={3} v-model:page={page.value} loc={[90, 48, 300, 200]}>
|
||||
{(index: number) => (
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text={PAGES[index] ?? ''}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
)}
|
||||
</Page>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { Progress } from '../../../../../packages-user/client-base/src/components/misc';
|
||||
|
||||
// 进度条:progress 分别取 0.3、0.6 与 1
|
||||
const BARS = [
|
||||
{ label: 'progress = 0.3', y: 68, progress: 0.3 },
|
||||
{ label: 'progress = 0.6', y: 128, progress: 0.6 },
|
||||
{ label: 'progress = 1', y: 188, progress: 1 }
|
||||
];
|
||||
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
{BARS.map(bar => (
|
||||
<container key={bar.label} loc={[0, 0, 480, 320]}>
|
||||
<text
|
||||
loc={[48, bar.y - 24]}
|
||||
text={bar.label}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<Progress
|
||||
loc={[48, bar.y, 300, 10]}
|
||||
progress={bar.progress}
|
||||
success="#7ee787"
|
||||
background="#26364d"
|
||||
lineWidth={8}
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { Scroll } from '../../../../../packages-user/client-base/src/components/scroll';
|
||||
|
||||
const ROWS = Array.from({ length: 12 }, (_, index) => `第 ${index + 1} 行`);
|
||||
|
||||
// 滚动区域:直接子元素从上往下排,右侧是滚动条
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[40, 24]}
|
||||
text="直接子元素依次排列"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect loc={[40, 52, 300, 220]} fillStyle="#101820" />
|
||||
<Scroll loc={[40, 52, 300, 220]}>
|
||||
{ROWS.map((row, index) => (
|
||||
<container key={row} loc={[8, 6 + index * 30, 280, 28]}>
|
||||
<g-rect
|
||||
loc={[0, 0, 280, 28]}
|
||||
fillStyle={index % 2 === 0 ? '#26364d' : '#1d2b45'}
|
||||
/>
|
||||
<text loc={[10, 6]} text={row} fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
))}
|
||||
</Scroll>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { defineComponent, onUnmounted, ref } from 'vue';
|
||||
import {
|
||||
ScrollText,
|
||||
ScrollTextExpose
|
||||
} from '../../../../../packages-user/client-base/src/components/misc';
|
||||
|
||||
const NOTICE = [
|
||||
'公告:今晚八点维护',
|
||||
'提示:记得保存进度',
|
||||
'活动:新地图开放',
|
||||
'注意:离开前先存档'
|
||||
]
|
||||
.join('\n')
|
||||
.repeat(3);
|
||||
|
||||
// 滚动文字:内容在固定视口里向上滚动,超出视口的部分不显示
|
||||
export default defineComponent(() => {
|
||||
const scroll = ref<ScrollTextExpose>();
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const restart = () => {
|
||||
// 组件触发 scrollEnd 后会立刻暂停,等一小段可见停顿后再恢复
|
||||
timer = setTimeout(() => {
|
||||
timer = undefined;
|
||||
scroll.value?.rescroll();
|
||||
scroll.value?.resume();
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
if (timer !== undefined) clearTimeout(timer);
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[40, 32]} text="公告" fillStyle="#cfe0ff" />
|
||||
|
||||
<ScrollText
|
||||
ref={scroll}
|
||||
loc={[40, 60, 400, 160]}
|
||||
width={400}
|
||||
speed={80}
|
||||
text={NOTICE}
|
||||
fillStyle="#7ee787"
|
||||
onScrollEnd={restart}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { Selection } from '../../../../../packages-user/client-base/src/components/misc';
|
||||
|
||||
// 选中框:两组 color 与 border,第二组调大 alphaRange
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[40, 48, 240, 32]}>
|
||||
<Selection
|
||||
loc={[0, 0, 240, 32]}
|
||||
color="#2f6fed"
|
||||
border="#7aa2ff"
|
||||
/>
|
||||
<text loc={[12, 8]} text="被选中的项" fillStyle="#ffffff" />
|
||||
</container>
|
||||
|
||||
<container loc={[40, 96, 240, 32]}>
|
||||
<Selection
|
||||
loc={[0, 0, 240, 32]}
|
||||
color="#8a6a1f"
|
||||
border="#f2b84b"
|
||||
alphaRange={[0.4, 0.8]}
|
||||
/>
|
||||
<text
|
||||
loc={[12, 8]}
|
||||
text="alphaRange = [0.4, 0.8]"
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[40, 144, 240, 32]}>
|
||||
<text loc={[12, 8]} text="没有选中框的项" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { Slider } from '../../../../../packages-user/client-base/src/components/slider';
|
||||
|
||||
// 滑动条:右侧显示当前值,旁边配两个按钮增减数值
|
||||
export default defineComponent(() => {
|
||||
const volume = ref(40);
|
||||
|
||||
const decrease = () => {
|
||||
volume.value = Math.max(0, volume.value - 5);
|
||||
};
|
||||
const increase = () => {
|
||||
volume.value = Math.min(100, volume.value + 5);
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[44, 48]} text="音量" fillStyle="#cfe0ff" />
|
||||
|
||||
<container loc={[44, 76, 196, 24]}>
|
||||
<Slider
|
||||
loc={[0, 0, 196, 24]}
|
||||
v-model:value={volume.value}
|
||||
min={0}
|
||||
max={100}
|
||||
step={5}
|
||||
showValue
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[44, 124, 64, 28]} onClick={decrease}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 64, 28]}
|
||||
circle={[4]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text
|
||||
loc={[32, 14]}
|
||||
anc={[0.5, 0.5]}
|
||||
text="-"
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
<container loc={[120, 124, 64, 28]} onClick={increase}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 64, 28]}
|
||||
circle={[4]}
|
||||
fillStyle="#3a2f5f"
|
||||
/>
|
||||
<text
|
||||
loc={[32, 14]}
|
||||
anc={[0.5, 0.5]}
|
||||
text="+"
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { TextContent } from '../../../../../packages-user/client-base/src/components/textbox';
|
||||
|
||||
const TEXT =
|
||||
'[c=#f2b84b][s=20]标题字号[/s][/c]\n' +
|
||||
'[c=#7ee787]正文颜色,长句会按 width 折行,超出宽度的部分自动换到下一行[/c]\n' +
|
||||
'[c=#8aa0b8][s=13]小字注释:[[[[ 和 ]]]] 显示为方括号[/s][/c]';
|
||||
|
||||
// 文本标记:颜色、字号、手动换行与方括号字面量,长句按 width 折行
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[40, 24]} text="文本标记" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-rect loc={[40, 56, 400, 216]} fillStyle="#101820" />
|
||||
<TextContent
|
||||
loc={[60, 80]}
|
||||
width={360}
|
||||
autoHeight
|
||||
lineHeight={8}
|
||||
text={TEXT}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { computed, defineComponent } from 'vue';
|
||||
|
||||
interface ItemCardProps {
|
||||
position: [number, number];
|
||||
label: string;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
interface ItemCardOptions {
|
||||
props: (keyof ItemCardProps)[];
|
||||
}
|
||||
|
||||
const itemCardOptions: ItemCardOptions = {
|
||||
props: ['position', 'label', 'color']
|
||||
};
|
||||
|
||||
// 子组件:只按 props 决定卡片的位置、文字与颜色
|
||||
const ItemCard = defineComponent<ItemCardProps>(props => {
|
||||
// 没有传 color 时使用默认颜色
|
||||
const color = computed(() => props.color ?? '#f2b84b');
|
||||
|
||||
return () => (
|
||||
<container loc={props.position}>
|
||||
<g-rect loc={[0, 0, 220, 64]} fillStyle={color.value} />
|
||||
<text loc={[16, 22]} text={props.label} fillStyle="#ffffff" />
|
||||
</container>
|
||||
);
|
||||
}, itemCardOptions);
|
||||
|
||||
// 父组件:同一个 ItemCard 声明一次,用三次,只改传入的 props
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[40, 24]}
|
||||
text="同一个组件,不同的 props"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<ItemCard position={[40, 56]} label="卡片 A" color="#2f6fed" />
|
||||
<ItemCard position={[40, 144]} label="卡片 B" color="#7ee787" />
|
||||
<ItemCard position={[40, 232]} label="卡片 C" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// hidden 只切换绘制与命中,面板实例和内部计数都保留
|
||||
export default defineComponent(() => {
|
||||
const open = ref(true);
|
||||
const count = ref(0);
|
||||
|
||||
const togglePanel = () => {
|
||||
open.value = !open.value;
|
||||
};
|
||||
|
||||
const increaseCount = () => {
|
||||
count.value++;
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[40, 32, 180, 36]} onClick={togglePanel}>
|
||||
<g-rect loc={[0, 0, 180, 36]} fill fillStyle="#2f6fed" />
|
||||
<text
|
||||
loc={[12, 10]}
|
||||
text="隐藏 / 显示面板"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container
|
||||
loc={[40, 92, 240, 160]}
|
||||
hidden={!open.value}
|
||||
onClick={increaseCount}
|
||||
>
|
||||
<g-rect loc={[0, 0, 240, 160]} fill fillStyle="#26364d" />
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text="点击面板内部 +1"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={`面板内点击:${count.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 96]}
|
||||
text="隐藏后再显示,状态仍在"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
interface Row {
|
||||
id: number;
|
||||
label: string;
|
||||
}
|
||||
|
||||
interface StatefulRowProps {
|
||||
label: string;
|
||||
offsetY: number;
|
||||
}
|
||||
|
||||
interface StatefulRowOptions {
|
||||
props: (keyof StatefulRowProps)[];
|
||||
}
|
||||
|
||||
const statefulRowOptions: StatefulRowOptions = {
|
||||
props: ['label', 'offsetY']
|
||||
};
|
||||
|
||||
// 每一行有自己的计数;重排后它跟着组件实例走
|
||||
const StatefulRow = defineComponent<StatefulRowProps, {}, string>(props => {
|
||||
const count = ref(0);
|
||||
|
||||
const increaseCount = () => {
|
||||
count.value++;
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, props.offsetY, 200, 36]} onClick={increaseCount}>
|
||||
<g-rect loc={[0, 0, 200, 36]} fill fillStyle="#26364d" />
|
||||
<text loc={[10, 10]} text={props.label} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[104, 10]}
|
||||
text={`点击 ${count.value}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, statefulRowOptions);
|
||||
|
||||
// key=id 的状态跟着项,key=index 的状态留在原位置
|
||||
export default defineComponent(() => {
|
||||
const rows = ref<Row[]>([
|
||||
{ id: 1, label: '甲' },
|
||||
{ id: 2, label: '乙' },
|
||||
{ id: 3, label: '丙' }
|
||||
]);
|
||||
|
||||
const rotateRows = () => {
|
||||
const first = rows.value.shift();
|
||||
if (first) {
|
||||
rows.value.push(first);
|
||||
}
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="点行内 +1,再点「轮换顺序」"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<text loc={[24, 52]} text="key = id" fillStyle="#58c4dd" />
|
||||
<text loc={[256, 52]} text="key = index" fillStyle="#f2b84b" />
|
||||
|
||||
<container loc={[24, 84]}>
|
||||
{rows.value.map((row, index) => (
|
||||
<StatefulRow
|
||||
key={row.id}
|
||||
label={row.label}
|
||||
offsetY={index * 42}
|
||||
/>
|
||||
))}
|
||||
</container>
|
||||
|
||||
<container loc={[256, 84]}>
|
||||
{rows.value.map((row, index) => (
|
||||
<StatefulRow
|
||||
key={index}
|
||||
label={row.label}
|
||||
offsetY={index * 42}
|
||||
/>
|
||||
))}
|
||||
</container>
|
||||
|
||||
<container loc={[160, 260, 160, 36]} onClick={rotateRows}>
|
||||
<g-rect loc={[0, 0, 160, 36]} fill fillStyle="#2f6fed" />
|
||||
<text loc={[52, 10]} text="轮换顺序" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// 面板自己的计数:组件被卸载后实例消失,再挂载时从 0 开始
|
||||
const CounterPanel = defineComponent(() => {
|
||||
const count = ref(0);
|
||||
|
||||
const increaseCount = () => {
|
||||
count.value++;
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 240, 160]} onClick={increaseCount}>
|
||||
<g-rect loc={[0, 0, 240, 160]} fill fillStyle="#26364d" />
|
||||
<text loc={[16, 16]} text="点击面板内部 +1" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={`面板内点击:${count.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 96]}
|
||||
text="重新显示后,计数归零"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
|
||||
// 三元切换:面板被卸载与重建
|
||||
export default defineComponent(() => {
|
||||
const open = ref(true);
|
||||
|
||||
const togglePanel = () => {
|
||||
open.value = !open.value;
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[40, 32, 180, 36]} onClick={togglePanel}>
|
||||
<g-rect loc={[0, 0, 180, 36]} fill fillStyle="#2f6fed" />
|
||||
<text
|
||||
loc={[12, 10]}
|
||||
text="隐藏 / 显示面板"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
{open.value ? (
|
||||
<container loc={[40, 92]}>
|
||||
<CounterPanel />
|
||||
</container>
|
||||
) : null}
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { CustomContainerRenderFn } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 同一组 3 个子元素:左侧普通 container 原样绘制,右侧 container-custom 逐个设置递增模糊
|
||||
const rows = [
|
||||
{ fillStyle: '#2f6fed', textFillStyle: '#e8f0ff', label: '第一行' },
|
||||
{ fillStyle: '#7aa7ff', textFillStyle: '#141a22', label: '第二行' },
|
||||
{ fillStyle: '#f2b84b', textFillStyle: '#141a22', label: '第三行' }
|
||||
];
|
||||
|
||||
export default defineComponent(() => {
|
||||
// render 接管子元素绘制:按 index 给每个子元素设置递增的 blur,再逐个绘制
|
||||
const render: CustomContainerRenderFn = (canvas, children, transform) => {
|
||||
children.forEach((child, index) => {
|
||||
if (child.hidden) return;
|
||||
child.setFilter(`blur(${index * 2}px)`);
|
||||
child.renderContent(canvas, transform);
|
||||
});
|
||||
};
|
||||
|
||||
// 两侧使用相同结构的三行内容
|
||||
const renderRows = () =>
|
||||
rows.map((row, index) => (
|
||||
<container loc={[0, index * 52, 180, 44]}>
|
||||
<g-rect loc={[0, 0, 180, 44]} fillStyle={row.fillStyle} />
|
||||
<text
|
||||
loc={[14, 12]}
|
||||
text={row.label}
|
||||
fillStyle={row.textFillStyle}
|
||||
/>
|
||||
</container>
|
||||
));
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="<container-custom> 逐个设置递增 blur"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect loc={[40, 72, 180, 160]} fillStyle="#1d2430" />
|
||||
<g-rect loc={[260, 72, 180, 160]} fillStyle="#1d2430" />
|
||||
|
||||
<text
|
||||
loc={[40, 48]}
|
||||
text="container:原样绘制"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<container loc={[40, 72, 180, 160]}>{renderRows()}</container>
|
||||
|
||||
<text
|
||||
loc={[260, 48]}
|
||||
text="custom:递增 blur"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<container-custom loc={[260, 72, 180, 160]} render={render}>
|
||||
{renderRows()}
|
||||
</container-custom>
|
||||
|
||||
<text
|
||||
loc={[40, 272]}
|
||||
text="同一组子元素:右侧按顺序设置 blur(0 / 2 / 4px)"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<container loc={[60, 50, 360, 220]}>
|
||||
<g-rect
|
||||
loc={[0, 0, 360, 220]}
|
||||
fillStyle="#2f6fed"
|
||||
alpha={0.22}
|
||||
/>
|
||||
<g-circle circle={[0, 0, 6]} fillStyle="#e8f0ff" />
|
||||
<text loc={[16, 16]} text="父容器原点" fillStyle="#cfe0ff" />
|
||||
|
||||
<container loc={[80, 60, 200, 120]}>
|
||||
<g-rect
|
||||
loc={[0, 0, 200, 120]}
|
||||
fillStyle="#f2b84b"
|
||||
alpha={0.3}
|
||||
/>
|
||||
<g-circle circle={[0, 0, 6]} fillStyle="#ffe9b0" />
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text="子容器原点"
|
||||
fillStyle="#ffe9b0"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { computed, defineComponent, onUnmounted } from 'vue';
|
||||
import {
|
||||
GameUI,
|
||||
IUIDefaultPropsBase,
|
||||
IUIInstance,
|
||||
UIController
|
||||
} from '@motajs/system';
|
||||
|
||||
// 最小控制器:只注入 instance 与 controller,不依赖游戏状态
|
||||
const controller = new UIController<IUIDefaultPropsBase>();
|
||||
controller.setPropsBaseCreator((instance, ctrl) => ({
|
||||
instance,
|
||||
controller: ctrl
|
||||
}));
|
||||
|
||||
// 被打开的 UI:固定在画布右侧,避免和按钮重叠
|
||||
const DemoPanel = defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[220, 120, 220, 150]}>
|
||||
<g-rect loc={[0, 0, 220, 150]} fill fillStyle="#1d2b45" />
|
||||
<g-rect loc={[0, 0, 220, 150]} stroke strokeStyle="#7aa2ff" />
|
||||
<text loc={[16, 24]} text="这是一个最小 UI" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[16, 64]}
|
||||
text="由 UIController 打开"
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
|
||||
const demoUI = new GameUI('controller-demo-panel', DemoPanel);
|
||||
|
||||
// 把 controller.render() 挂进同一棵渲染树
|
||||
const UIRenderer = defineComponent(() => {
|
||||
return () => controller.render();
|
||||
});
|
||||
|
||||
export default defineComponent(() => {
|
||||
// 打开与关闭必须复用同一个实例
|
||||
let instance: IUIInstance | null = null;
|
||||
|
||||
const stackSize = computed(() => controller.stack.length);
|
||||
const activeText = computed(() =>
|
||||
controller.active.value ? '已打开' : '未打开'
|
||||
);
|
||||
|
||||
const openUI = () => {
|
||||
if (!instance) {
|
||||
instance = controller.open(demoUI, {});
|
||||
}
|
||||
};
|
||||
|
||||
const closeUI = () => {
|
||||
if (instance) {
|
||||
controller.close(instance);
|
||||
instance = null;
|
||||
}
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
controller.closeAll();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fill fillStyle="#141a22" />
|
||||
|
||||
<text loc={[20, 20]} text="打开 UI" fillStyle="#e8f0ff" />
|
||||
<container loc={[20, 44, 150, 44]} onClick={openUI}>
|
||||
<g-rect loc={[0, 0, 150, 44]} fill fillStyle="#2f6fed" />
|
||||
<text loc={[16, 12]} text="打开" fillStyle="#ffffff" />
|
||||
</container>
|
||||
|
||||
<text loc={[20, 116]} text="关闭 UI" fillStyle="#e8f0ff" />
|
||||
<container loc={[20, 140, 150, 44]} onClick={closeUI}>
|
||||
<g-rect loc={[0, 0, 150, 44]} fill fillStyle="#3a2f5f" />
|
||||
<text loc={[16, 12]} text="关闭" fillStyle="#ffffff" />
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[20, 220]}
|
||||
text={`栈长度:${stackSize.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[20, 252]}
|
||||
text={`状态:${activeText.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<UIRenderer />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import { computed, defineComponent, ref } from 'vue';
|
||||
|
||||
interface ItemCardProps {
|
||||
label: string;
|
||||
active?: boolean;
|
||||
}
|
||||
|
||||
type ItemCardEmits = {
|
||||
pick: (label: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCardOptions {
|
||||
props: (keyof ItemCardProps)[];
|
||||
emits: (keyof ItemCardEmits)[];
|
||||
}
|
||||
|
||||
const itemCardOptions: ItemCardOptions = {
|
||||
props: ['label', 'active'],
|
||||
emits: ['pick']
|
||||
};
|
||||
|
||||
// 子组件:按 props 决定颜色,点击时发出 pick 事件,底部内容由默认 slot 填入
|
||||
const ItemCard = defineComponent<
|
||||
ItemCardProps,
|
||||
ItemCardEmits,
|
||||
keyof ItemCardEmits
|
||||
>((props, { emit, slots }) => {
|
||||
const fill = computed(() => (props.active ? '#2f6fed' : '#26364d'));
|
||||
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, 200, 96]}
|
||||
onClick={() => emit('pick', props.label)}
|
||||
>
|
||||
<g-rect loc={[0, 0, 200, 96]} fillStyle={fill.value} />
|
||||
<text loc={[12, 12]} text={props.label} fillStyle="#ffffff" />
|
||||
{slots.default?.()}
|
||||
</container>
|
||||
);
|
||||
}, itemCardOptions);
|
||||
|
||||
// 父组件:用 ref 保存选中项,把 props、事件与默认 slot 传给两个卡片
|
||||
export default defineComponent(() => {
|
||||
const selected = ref('甲');
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text
|
||||
loc={[24, 28]}
|
||||
text={`当前选择:${selected.value}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<container loc={[24, 72]}>
|
||||
<ItemCard
|
||||
label="甲"
|
||||
active={selected.value === '甲'}
|
||||
onPick={label => (selected.value = label)}
|
||||
>
|
||||
<text loc={[12, 60]} text="点我选择" fillStyle="#cfe0ff" />
|
||||
</ItemCard>
|
||||
</container>
|
||||
<container loc={[256, 72]}>
|
||||
<ItemCard
|
||||
label="乙"
|
||||
active={selected.value === '乙'}
|
||||
onPick={label => (selected.value = label)}
|
||||
>
|
||||
<text loc={[12, 60]} text="点我选择" fillStyle="#cfe0ff" />
|
||||
</ItemCard>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
import type { CustomRenderFunction } from '@motajs/render';
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
const ringColors = ['#2f6fed', '#7aa7ff', '#f2b84b'];
|
||||
|
||||
// 演示 custom 的局部坐标绘制:同心圆环、径向渐变与旋转指针
|
||||
export default defineComponent(() => {
|
||||
// 把 angle.value 放进 bindings;点击按钮改变角度时会触发重绘
|
||||
const angle = ref(0);
|
||||
|
||||
// 在元素局部坐标里直接绘制:同心圆环、径向渐变与旋转指针
|
||||
const render: CustomRenderFunction = canvas => {
|
||||
const ctx = canvas.ctx;
|
||||
const cx = 100;
|
||||
const cy = 100;
|
||||
|
||||
// 径向渐变与同心圆环直接走 Canvas 2D
|
||||
const gradient = ctx.createRadialGradient(cx, cy, 12, cx, cy, 88);
|
||||
gradient.addColorStop(0, '#2f6fed');
|
||||
gradient.addColorStop(1, '#141a22');
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, 88, 0, Math.PI * 2);
|
||||
ctx.fillStyle = gradient;
|
||||
ctx.fill();
|
||||
|
||||
ringColors.forEach((color, index) => {
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, 70 - index * 16, 0, Math.PI * 2);
|
||||
ctx.strokeStyle = color;
|
||||
ctx.lineWidth = 3;
|
||||
ctx.stroke();
|
||||
});
|
||||
|
||||
// 指针从圆心出发,按 angle 旋转;坐标都相对元素左上角
|
||||
ctx.translate(cx, cy);
|
||||
ctx.rotate(angle.value);
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, 0);
|
||||
ctx.lineTo(0, -64);
|
||||
ctx.strokeStyle = '#e8f0ff';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.stroke();
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="<custom> 在局部坐标里直接绘制"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<custom
|
||||
loc={[140, 44, 200, 200]}
|
||||
bindings={[angle.value]}
|
||||
render={render}
|
||||
/>
|
||||
|
||||
<container
|
||||
loc={[138, 256, 92, 36]}
|
||||
onClick={() => {
|
||||
angle.value -= Math.PI / 12;
|
||||
}}
|
||||
>
|
||||
<g-rect loc={[0, 0, 92, 36]} fillStyle="#2f6fed" />
|
||||
<text loc={[18, 9]} text="−15°" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
<container
|
||||
loc={[250, 256, 92, 36]}
|
||||
onClick={() => {
|
||||
angle.value += Math.PI / 12;
|
||||
}}
|
||||
>
|
||||
<g-rect loc={[0, 0, 92, 36]} fillStyle="#2f6fed" />
|
||||
<text loc={[18, 9]} text="+15°" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[24, 300]}
|
||||
text="angle.value 放进 bindings,点击按钮会重绘"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import type { IActionEvent, IWheelEvent } from '@motajs/render';
|
||||
|
||||
// 内置事件:按下、抬起、点击,以及进入、离开、移动、滚轮
|
||||
export default defineComponent(() => {
|
||||
const log = ref<string[]>([]);
|
||||
const move = ref('—');
|
||||
|
||||
const push = (text: string) => {
|
||||
log.value = [...log.value, text].slice(-4);
|
||||
};
|
||||
|
||||
const handleDown = () => push('按下');
|
||||
const handleUp = () => push('抬起');
|
||||
const handleClick = () => push('点击');
|
||||
const handleEnter = () => push('进入');
|
||||
const handleLeave = () => push('离开');
|
||||
const handleMove = (ev: IActionEvent) => {
|
||||
move.value = `${Math.round(ev.offsetX)}, ${Math.round(ev.offsetY)}`;
|
||||
};
|
||||
const handleWheel = (ev: IWheelEvent) => {
|
||||
push(`滚轮 ${Math.round(ev.wheelY)}`);
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="内置事件" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-rect
|
||||
loc={[24, 56, 160, 48]}
|
||||
fillStyle="#2f6fed"
|
||||
onDown={handleDown}
|
||||
onUp={handleUp}
|
||||
onClick={handleClick}
|
||||
/>
|
||||
<text loc={[64, 73]} text="点我" fillStyle="#e8f0ff" noevent />
|
||||
|
||||
<g-rect
|
||||
loc={[212, 56, 244, 112]}
|
||||
fillStyle="#26364d"
|
||||
onEnter={handleEnter}
|
||||
onLeave={handleLeave}
|
||||
onMove={handleMove}
|
||||
onWheel={handleWheel}
|
||||
/>
|
||||
<text
|
||||
loc={[228, 72]}
|
||||
text="悬停、移动、滚轮"
|
||||
fillStyle="#cfe0ff"
|
||||
noevent
|
||||
/>
|
||||
<text
|
||||
loc={[228, 100]}
|
||||
text={`局部坐标:${move.value}`}
|
||||
fillStyle="#8b98a8"
|
||||
noevent
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[24, 196]}
|
||||
text={`事件:${log.value.join(' → ')}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 236]}
|
||||
text="移动只更新坐标,不写入事件日志"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,76 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import type { IActionEvent } from '@motajs/render';
|
||||
|
||||
// 同一个点击在内层、外层与画布坐标下的不同结果
|
||||
export default defineComponent(() => {
|
||||
const inner = ref('—');
|
||||
const outer = ref('—');
|
||||
const absolute = ref('—');
|
||||
|
||||
// 外层移动与点击都刷新同一组外层坐标
|
||||
const handleOuterPoint = (ev: IActionEvent) => {
|
||||
outer.value = `${Math.round(ev.offsetX)}, ${Math.round(ev.offsetY)}`;
|
||||
absolute.value = `${Math.round(ev.absoluteX)}, ${Math.round(
|
||||
ev.absoluteY
|
||||
)}`;
|
||||
};
|
||||
|
||||
// 内层移动与点击都刷新同一组内层坐标
|
||||
const handleInnerPoint = (ev: IActionEvent) => {
|
||||
inner.value = `${Math.round(ev.offsetX)}, ${Math.round(ev.offsetY)}`;
|
||||
};
|
||||
|
||||
const handleInnerLeave = () => {
|
||||
inner.value = '—';
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="事件坐标" fillStyle="#cfe0ff" />
|
||||
|
||||
<container
|
||||
loc={[24, 60, 216, 180]}
|
||||
onMove={handleOuterPoint}
|
||||
onClick={handleOuterPoint}
|
||||
>
|
||||
<g-rect loc={[0, 0, 216, 180]} fillStyle="#26364d" />
|
||||
<g-rect
|
||||
loc={[36, 40, 144, 88]}
|
||||
fillStyle="#2f6fed"
|
||||
onMove={handleInnerPoint}
|
||||
onClick={handleInnerPoint}
|
||||
onLeave={handleInnerLeave}
|
||||
/>
|
||||
<text
|
||||
loc={[72, 76]}
|
||||
text="点这里"
|
||||
fillStyle="#e8f0ff"
|
||||
noevent
|
||||
/>
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[264, 76]}
|
||||
text={`内层 offset:${inner.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[264, 108]}
|
||||
text={`外层 offset:${outer.value}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text
|
||||
loc={[264, 140]}
|
||||
text={`画布 absolute:${absolute.value}`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 264]}
|
||||
text="在内层与外层移动、点击,比较三组坐标"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import type { IWheelEvent } from '@motajs/render';
|
||||
|
||||
// enter / leave 由悬停集合做差集触发,滚轮与 cursor 跟随命中结果
|
||||
export default defineComponent(() => {
|
||||
const outerState = ref('未进入');
|
||||
const innerState = ref('未进入');
|
||||
const cursor = ref('pointer');
|
||||
const total = ref(0);
|
||||
|
||||
const handleOuterEnter = () => {
|
||||
outerState.value = '进入';
|
||||
cursor.value = 'pointer';
|
||||
};
|
||||
|
||||
const handleOuterLeave = () => {
|
||||
outerState.value = '离开';
|
||||
cursor.value = 'pointer';
|
||||
};
|
||||
|
||||
const handleOuterWheel = (ev: IWheelEvent) => {
|
||||
total.value += ev.wheelY;
|
||||
};
|
||||
|
||||
const handleInnerEnter = () => {
|
||||
innerState.value = '进入';
|
||||
cursor.value = 'text';
|
||||
};
|
||||
|
||||
const handleInnerLeave = () => {
|
||||
innerState.value = '离开';
|
||||
cursor.value = 'pointer';
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="悬停与滚轮" fillStyle="#cfe0ff" />
|
||||
|
||||
<container
|
||||
loc={[24, 64, 220, 176]}
|
||||
cursor="pointer"
|
||||
onEnter={handleOuterEnter}
|
||||
onLeave={handleOuterLeave}
|
||||
onWheel={handleOuterWheel}
|
||||
>
|
||||
<g-rect loc={[0, 0, 220, 176]} fillStyle="#26364d" />
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text="外层:cursor pointer"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<g-rect
|
||||
loc={[40, 64, 140, 80]}
|
||||
fillStyle="#2f6fed"
|
||||
cursor="text"
|
||||
onEnter={handleInnerEnter}
|
||||
onLeave={handleInnerLeave}
|
||||
/>
|
||||
<text
|
||||
loc={[40, 152]}
|
||||
text="内层:cursor text"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[264, 80]}
|
||||
text={`外层:${outerState.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[264, 112]}
|
||||
text={`内层:${innerState.value}`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
<text
|
||||
loc={[264, 144]}
|
||||
text={`cursor:${cursor.value}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text
|
||||
loc={[264, 176]}
|
||||
text={`滚轮累计:${Math.round(total.value)}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 264]}
|
||||
text="从内层移回外层时,外层不会重新 enter"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import type { IActionEvent } from '@motajs/render';
|
||||
|
||||
// 嵌套容器中的捕获与冒泡,以及可切换的 stopPropagation
|
||||
export default defineComponent(() => {
|
||||
const log = ref<string[]>([]);
|
||||
const stop = ref(false);
|
||||
|
||||
// 每次点击只保留本次交互的日志,不做累积
|
||||
const push = (text: string) => {
|
||||
log.value = [...log.value, text];
|
||||
};
|
||||
|
||||
const handleToggleStop = () => {
|
||||
stop.value = !stop.value;
|
||||
};
|
||||
|
||||
// 外层捕获是一次点击的起点:先清空日志,再记录本次触发的后续事件
|
||||
const handleOuterCapture = () => {
|
||||
log.value = ['外层捕获'];
|
||||
};
|
||||
|
||||
const handleOuterBubble = () => push('外层冒泡');
|
||||
const handleInnerCapture = () => push('内层捕获');
|
||||
const handleInnerBubble = () => push('内层冒泡');
|
||||
const handleButtonCapture = () => push('按钮捕获');
|
||||
|
||||
const handleButtonBubble = (ev: IActionEvent) => {
|
||||
// 先记录按钮冒泡,再按开关决定是否截断向外传播
|
||||
push('按钮冒泡');
|
||||
if (stop.value) ev.stopPropagation();
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="捕获与冒泡" fillStyle="#cfe0ff" />
|
||||
|
||||
{/* 切换 stop:只影响下方按钮冒泡是否继续向外层传递 */}
|
||||
<container loc={[24, 52, 156, 36]} onClick={handleToggleStop}>
|
||||
<g-rect
|
||||
loc={[0, 0, 156, 36]}
|
||||
fillStyle={stop.value ? '#f2b84b' : '#2f6fed'}
|
||||
/>
|
||||
<text
|
||||
loc={[18, 10]}
|
||||
text={stop.value ? 'stop:开启' : 'stop:关闭'}
|
||||
fillStyle="#141a22"
|
||||
/>
|
||||
</container>
|
||||
|
||||
{/* 外层捕获是一次点击的起点:先清空日志,再记录本次触发的后续事件 */}
|
||||
<container
|
||||
loc={[24, 108, 264, 172]}
|
||||
onClickCapture={handleOuterCapture}
|
||||
onClick={handleOuterBubble}
|
||||
>
|
||||
<g-rect loc={[0, 0, 264, 172]} fillStyle="#26364d" />
|
||||
<container
|
||||
loc={[32, 32, 200, 108]}
|
||||
onClickCapture={handleInnerCapture}
|
||||
onClick={handleInnerBubble}
|
||||
>
|
||||
<g-rect loc={[0, 0, 200, 108]} fillStyle="#1d2a3a" />
|
||||
<g-rect
|
||||
loc={[40, 28, 120, 52]}
|
||||
fillStyle="#2f6fed"
|
||||
onClickCapture={handleButtonCapture}
|
||||
onClick={handleButtonBubble}
|
||||
/>
|
||||
<text loc={[40, 90]} text="点按钮" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
|
||||
<text loc={[308, 80]} text="只显示本次点击" fillStyle="#8b98a8" />
|
||||
|
||||
{[0, 1, 2, 3, 4, 5].map(index => (
|
||||
<text
|
||||
key={index}
|
||||
loc={[308, 112 + index * 24]}
|
||||
text={log.value[index] ?? ''}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
))}
|
||||
|
||||
<text
|
||||
loc={[24, 292]}
|
||||
text="stop 开启后,按钮冒泡不再传给外层"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Texture } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 演示用纹理:只在 demo 内部生成,正文不展示生成方式
|
||||
function createDemoTexture() {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 64;
|
||||
canvas.height = 64;
|
||||
|
||||
const ctx = canvas.getContext('2d')!;
|
||||
ctx.fillStyle = '#1d2733';
|
||||
ctx.fillRect(0, 0, 64, 64);
|
||||
ctx.strokeStyle = '#7aa7ff';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.strokeRect(8, 8, 48, 48);
|
||||
ctx.fillStyle = '#f2b84b';
|
||||
ctx.beginPath();
|
||||
ctx.arc(32, 32, 14, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
|
||||
return new Texture(canvas);
|
||||
}
|
||||
|
||||
const demoTexture = createDemoTexture();
|
||||
|
||||
// 单张图片:loc 固定为 [40, 64, 128, 128]
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="<image> 按 loc 显示纹理的效果"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<image image={demoTexture} loc={[40, 64, 128, 128]} />
|
||||
|
||||
<text
|
||||
loc={[40, 208]}
|
||||
text="loc = [40, 64, 128, 128]"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Texture } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 演示用纹理:只在 demo 内部生成,正文不展示生成方式
|
||||
function createDemoTexture() {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 64;
|
||||
canvas.height = 64;
|
||||
|
||||
const ctx = canvas.getContext('2d')!;
|
||||
ctx.fillStyle = '#1d2733';
|
||||
ctx.fillRect(0, 0, 64, 64);
|
||||
ctx.strokeStyle = '#7aa7ff';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.strokeRect(8, 8, 48, 48);
|
||||
ctx.fillStyle = '#f2b84b';
|
||||
ctx.beginPath();
|
||||
ctx.arc(32, 32, 14, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
|
||||
return new Texture(canvas);
|
||||
}
|
||||
|
||||
const demoTexture = createDemoTexture();
|
||||
|
||||
// 同一张纹理放进三种尺寸的矩形
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="同一张图,三种显示尺寸"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<image image={demoTexture} loc={[48, 96, 64, 64]} />
|
||||
<text loc={[48, 172]} text="64×64" fillStyle="#8b98a8" />
|
||||
|
||||
<image image={demoTexture} loc={[192, 80, 96, 96]} />
|
||||
<text loc={[192, 188]} text="96×96" fillStyle="#8b98a8" />
|
||||
|
||||
<image image={demoTexture} loc={[336, 64, 128, 128]} />
|
||||
<text loc={[336, 204]} text="128×128" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// 示例(一)的 live:720×480 骨架、标题、关闭按钮与左右占位
|
||||
export default defineComponent(() => {
|
||||
const hint = ref('骨架阶段:先确认面板能显示、关闭按钮能用');
|
||||
|
||||
const close = () => {
|
||||
// 文档站没有真实 UI 栈,用一行提示代替 controller.close
|
||||
hint.value = '已请求关闭(真实面板会调用 controller.close)';
|
||||
};
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container loc={[626, 50, 56, 32]} onClick={close}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 56, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#7a3d3d"
|
||||
/>
|
||||
<text loc={[14, 8]} text="关闭" fillStyle="#ffe9e9" />
|
||||
</container>
|
||||
|
||||
<container loc={[54, 110, 400, 330]}>
|
||||
<g-rect loc={[0, 0, 400, 330]} fill fillStyle="#101820" />
|
||||
<text loc={[16, 16]} text="道具网格占位" fillStyle="#8fb6ff" />
|
||||
</container>
|
||||
|
||||
<container loc={[470, 110, 196, 330]}>
|
||||
<g-rect loc={[0, 0, 196, 330]} fill fillStyle="#101820" />
|
||||
<text loc={[16, 16]} text="详情占位" fillStyle="#8fb6ff" />
|
||||
</container>
|
||||
|
||||
<text loc={[54, 420]} text={hint.value} fillStyle="#8fb6ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import { computed, defineComponent, ref } from 'vue';
|
||||
|
||||
interface Item {
|
||||
id: string;
|
||||
name: string;
|
||||
count: number;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface ItemCellProps {
|
||||
item: Item;
|
||||
selected: boolean;
|
||||
size: number;
|
||||
}
|
||||
|
||||
type ItemCellEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCellOptions {
|
||||
props: (keyof ItemCellProps)[];
|
||||
emits: (keyof ItemCellEmits)[];
|
||||
}
|
||||
|
||||
const itemCellOptions: ItemCellOptions = {
|
||||
props: ['item', 'selected', 'size'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 单个格子:只负责画名称、数量与选中态,点击后把 id 交给父组件
|
||||
// 格子只接收结果,选中状态由父组件持有
|
||||
const ItemCell = defineComponent<
|
||||
ItemCellProps,
|
||||
ItemCellEmits,
|
||||
keyof ItemCellEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
onClick={() => emit('select', props.item.id)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
circle={[10]}
|
||||
fill
|
||||
fillStyle={props.selected ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[1, 1, props.size - 2, props.size - 2]}
|
||||
circle={[9]}
|
||||
stroke
|
||||
strokeStyle={props.selected ? '#f2b84b' : '#33465f'}
|
||||
/>
|
||||
<text loc={[8, 10]} text={props.item.name} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[8, props.size - 24]}
|
||||
text={`× ${props.item.count}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, itemCellOptions);
|
||||
|
||||
interface ItemGridProps {
|
||||
items: Item[];
|
||||
selectedId: string;
|
||||
columns: number;
|
||||
cellSize: number;
|
||||
gap: number;
|
||||
}
|
||||
|
||||
type ItemGridEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemGridOptions {
|
||||
props: (keyof ItemGridProps)[];
|
||||
emits: (keyof ItemGridEmits)[];
|
||||
}
|
||||
|
||||
const itemGridOptions: ItemGridOptions = {
|
||||
props: ['items', 'selectedId', 'columns', 'cellSize', 'gap'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 网格:用列数、格子尺寸与间距算出位置,不写死每格坐标
|
||||
const ItemGrid = defineComponent<
|
||||
ItemGridProps,
|
||||
ItemGridEmits,
|
||||
keyof ItemGridEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 0, 0]}>
|
||||
{props.items.map((item, index) => {
|
||||
const col = index % props.columns;
|
||||
const row = Math.floor(index / props.columns);
|
||||
const x = col * (props.cellSize + props.gap);
|
||||
const y = row * (props.cellSize + props.gap);
|
||||
return (
|
||||
<container loc={[x, y]}>
|
||||
<ItemCell
|
||||
item={item}
|
||||
selected={item.id === props.selectedId}
|
||||
size={props.cellSize}
|
||||
onSelect={id => emit('select', id)}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
);
|
||||
}, itemGridOptions);
|
||||
|
||||
const items: Item[] = [
|
||||
{ id: 'potion-red', name: '红药水', count: 3, desc: '恢复少量生命。' },
|
||||
{ id: 'potion-blue', name: '蓝药水', count: 1, desc: '恢复少量魔力。' },
|
||||
{ id: 'key-yellow', name: '黄钥匙', count: 5, desc: '打开黄色的门。' },
|
||||
{ id: 'key-blue', name: '蓝钥匙', count: 2, desc: '打开蓝色的门。' },
|
||||
{ id: 'sword-wood', name: '木剑', count: 1, desc: '最基础的武器。' },
|
||||
{ id: 'shield-wood', name: '木盾', count: 1, desc: '最基础的防具。' },
|
||||
{ id: 'gem-red', name: '红宝石', count: 4, desc: '镶嵌用的素材。' },
|
||||
{ id: 'gem-blue', name: '蓝宝石', count: 2, desc: '镶嵌用的素材。' },
|
||||
{ id: 'scroll', name: '回城卷轴', count: 1, desc: '回到起点。' },
|
||||
{ id: 'bread', name: '面包', count: 6, desc: '恢复少量生命。' }
|
||||
];
|
||||
|
||||
// 示例(二)的 live:骨架 + 网格 + 选中态
|
||||
export default defineComponent(() => {
|
||||
const selectedId = ref('potion-red');
|
||||
const selectedItem = computed(() =>
|
||||
items.find(item => item.id === selectedId.value)
|
||||
);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container loc={[626, 50, 56, 32]}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 56, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#7a3d3d"
|
||||
/>
|
||||
<text loc={[14, 8]} text="关闭" fillStyle="#ffe9e9" />
|
||||
</container>
|
||||
|
||||
<container loc={[54, 110, 400, 330]}>
|
||||
<g-rect loc={[0, 0, 400, 330]} fill fillStyle="#101820" />
|
||||
<ItemGrid
|
||||
items={items}
|
||||
selectedId={selectedId.value}
|
||||
columns={5}
|
||||
cellSize={66}
|
||||
gap={8}
|
||||
onSelect={id => (selectedId.value = id)}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[470, 110, 196, 330]}>
|
||||
<g-rect loc={[0, 0, 196, 330]} fill fillStyle="#101820" />
|
||||
<text
|
||||
loc={[16, 16]}
|
||||
text={
|
||||
selectedItem.value ? selectedItem.value.name : '未选择'
|
||||
}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 48]}
|
||||
text={
|
||||
selectedItem.value
|
||||
? `数量 × ${selectedItem.value.count}`
|
||||
: '点击左侧格子'
|
||||
}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,251 @@
|
||||
import { computed, defineComponent, ref } from 'vue';
|
||||
|
||||
interface Item {
|
||||
id: string;
|
||||
name: string;
|
||||
count: number;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface ItemCellProps {
|
||||
item: Item;
|
||||
selected: boolean;
|
||||
size: number;
|
||||
}
|
||||
|
||||
type ItemCellEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCellOptions {
|
||||
props: (keyof ItemCellProps)[];
|
||||
emits: (keyof ItemCellEmits)[];
|
||||
}
|
||||
|
||||
const itemCellOptions: ItemCellOptions = {
|
||||
props: ['item', 'selected', 'size'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 网格、详情与父组件共享同一份道具数据
|
||||
const ItemCell = defineComponent<
|
||||
ItemCellProps,
|
||||
ItemCellEmits,
|
||||
keyof ItemCellEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
onClick={() => emit('select', props.item.id)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
circle={[10]}
|
||||
fill
|
||||
fillStyle={props.selected ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[1, 1, props.size - 2, props.size - 2]}
|
||||
circle={[9]}
|
||||
stroke
|
||||
strokeStyle={props.selected ? '#f2b84b' : '#33465f'}
|
||||
/>
|
||||
<text loc={[8, 10]} text={props.item.name} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[8, props.size - 24]}
|
||||
text={`× ${props.item.count}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, itemCellOptions);
|
||||
|
||||
interface ItemGridProps {
|
||||
items: Item[];
|
||||
selectedId: string;
|
||||
columns: number;
|
||||
cellSize: number;
|
||||
gap: number;
|
||||
}
|
||||
|
||||
type ItemGridEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemGridOptions {
|
||||
props: (keyof ItemGridProps)[];
|
||||
emits: (keyof ItemGridEmits)[];
|
||||
}
|
||||
|
||||
const itemGridOptions: ItemGridOptions = {
|
||||
props: ['items', 'selectedId', 'columns', 'cellSize', 'gap'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
const ItemGrid = defineComponent<
|
||||
ItemGridProps,
|
||||
ItemGridEmits,
|
||||
keyof ItemGridEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 0, 0]}>
|
||||
{props.items.map((item, index) => {
|
||||
const col = index % props.columns;
|
||||
const row = Math.floor(index / props.columns);
|
||||
const x = col * (props.cellSize + props.gap);
|
||||
const y = row * (props.cellSize + props.gap);
|
||||
return (
|
||||
<container loc={[x, y]}>
|
||||
<ItemCell
|
||||
item={item}
|
||||
selected={item.id === props.selectedId}
|
||||
size={props.cellSize}
|
||||
onSelect={id => emit('select', id)}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
);
|
||||
}, itemGridOptions);
|
||||
|
||||
interface ItemDetailProps {
|
||||
item?: Item;
|
||||
}
|
||||
|
||||
type ItemDetailEmits = {
|
||||
clear: () => void;
|
||||
};
|
||||
|
||||
interface ItemDetailOptions {
|
||||
props: (keyof ItemDetailProps)[];
|
||||
emits: (keyof ItemDetailEmits)[];
|
||||
}
|
||||
|
||||
const itemDetailOptions: ItemDetailOptions = {
|
||||
props: ['item'],
|
||||
emits: ['clear']
|
||||
};
|
||||
|
||||
// 右侧详情:没有选中时显示提示,有选中时显示名称、数量与描述
|
||||
// 详情只接收选中的道具,没有选中时显示提示
|
||||
const ItemDetail = defineComponent<
|
||||
ItemDetailProps,
|
||||
ItemDetailEmits,
|
||||
keyof ItemDetailEmits
|
||||
>((props, { emit }) => {
|
||||
const clear = () => emit('clear');
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 196, 330]}>
|
||||
<g-rect loc={[0, 0, 196, 330]} fill fillStyle="#101820" />
|
||||
<text loc={[16, 16]} text="详情" fillStyle="#8fb6ff" />
|
||||
|
||||
{/* 两块都渲染,用 hidden 切换 */}
|
||||
<container hidden={!!props.item}>
|
||||
<text loc={[16, 52]} text="请选择道具" fillStyle="#4f6d94" />
|
||||
</container>
|
||||
|
||||
<container hidden={!props.item}>
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={props.item ? props.item.name : ''}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 84]}
|
||||
text={props.item ? `数量:${props.item.count}` : ''}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 120]}
|
||||
text={props.item ? props.item.desc : ''}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 156]}
|
||||
text="live 用 text 等价展示说明"
|
||||
fillStyle="#4f6d94"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[16, 286, 164, 28]} onClick={clear}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 164, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 6]} text="取消选择" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
}, itemDetailOptions);
|
||||
|
||||
// 假数据只用于演示,可替换成存档数据
|
||||
const items: Item[] = [
|
||||
{ id: 'potion-red', name: '红药水', count: 3, desc: '恢复少量生命。' },
|
||||
{ id: 'potion-blue', name: '蓝药水', count: 1, desc: '恢复少量魔力。' },
|
||||
{ id: 'key-yellow', name: '黄钥匙', count: 5, desc: '打开黄色的门。' },
|
||||
{ id: 'key-blue', name: '蓝钥匙', count: 2, desc: '打开蓝色的门。' },
|
||||
{ id: 'sword-wood', name: '木剑', count: 1, desc: '最基础的武器。' },
|
||||
{ id: 'shield-wood', name: '木盾', count: 1, desc: '最基础的防具。' },
|
||||
{ id: 'gem-red', name: '红宝石', count: 4, desc: '镶嵌用的素材。' },
|
||||
{ id: 'gem-blue', name: '蓝宝石', count: 2, desc: '镶嵌用的素材。' },
|
||||
{ id: 'scroll', name: '回城卷轴', count: 1, desc: '回到起点。' },
|
||||
{ id: 'bread', name: '面包', count: 6, desc: '恢复少量生命。' }
|
||||
];
|
||||
|
||||
// 示例(三)的 live:骨架 + 网格 + 选中态 + 详情区
|
||||
export default defineComponent(() => {
|
||||
const selectedId = ref('potion-red');
|
||||
const selectedItem = computed(() =>
|
||||
items.find(item => item.id === selectedId.value)
|
||||
);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container loc={[626, 50, 56, 32]}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 56, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#7a3d3d"
|
||||
/>
|
||||
<text loc={[14, 8]} text="关闭" fillStyle="#ffe9e9" />
|
||||
</container>
|
||||
|
||||
<container loc={[54, 110, 400, 330]}>
|
||||
<g-rect loc={[0, 0, 400, 330]} fill fillStyle="#101820" />
|
||||
<ItemGrid
|
||||
items={items}
|
||||
selectedId={selectedId.value}
|
||||
columns={5}
|
||||
cellSize={66}
|
||||
gap={8}
|
||||
onSelect={id => (selectedId.value = id)}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[470, 110, 196, 330]}>
|
||||
<ItemDetail
|
||||
item={selectedItem.value}
|
||||
onClear={() => (selectedId.value = '')}
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,301 @@
|
||||
import { computed, defineComponent, ref } from 'vue';
|
||||
|
||||
interface Item {
|
||||
id: string;
|
||||
name: string;
|
||||
count: number;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface ItemCellProps {
|
||||
item: Item;
|
||||
selected: boolean;
|
||||
size: number;
|
||||
}
|
||||
|
||||
type ItemCellEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCellOptions {
|
||||
props: (keyof ItemCellProps)[];
|
||||
emits: (keyof ItemCellEmits)[];
|
||||
}
|
||||
|
||||
const itemCellOptions: ItemCellOptions = {
|
||||
props: ['item', 'selected', 'size'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 滚动演示沿用网格与详情的组件结构
|
||||
const ItemCell = defineComponent<
|
||||
ItemCellProps,
|
||||
ItemCellEmits,
|
||||
keyof ItemCellEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
onClick={() => emit('select', props.item.id)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
circle={[10]}
|
||||
fill
|
||||
fillStyle={props.selected ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[1, 1, props.size - 2, props.size - 2]}
|
||||
circle={[9]}
|
||||
stroke
|
||||
strokeStyle={props.selected ? '#f2b84b' : '#33465f'}
|
||||
/>
|
||||
<text loc={[8, 10]} text={props.item.name} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[8, props.size - 24]}
|
||||
text={`× ${props.item.count}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, itemCellOptions);
|
||||
|
||||
interface ItemDetailProps {
|
||||
item?: Item;
|
||||
}
|
||||
|
||||
type ItemDetailEmits = {
|
||||
clear: () => void;
|
||||
};
|
||||
|
||||
interface ItemDetailOptions {
|
||||
props: (keyof ItemDetailProps)[];
|
||||
emits: (keyof ItemDetailEmits)[];
|
||||
}
|
||||
|
||||
const itemDetailOptions: ItemDetailOptions = {
|
||||
props: ['item'],
|
||||
emits: ['clear']
|
||||
};
|
||||
|
||||
const ItemDetail = defineComponent<
|
||||
ItemDetailProps,
|
||||
ItemDetailEmits,
|
||||
keyof ItemDetailEmits
|
||||
>((props, { emit }) => {
|
||||
const clear = () => emit('clear');
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 196, 330]}>
|
||||
<g-rect loc={[0, 0, 196, 330]} fill fillStyle="#101820" />
|
||||
<text loc={[16, 16]} text="详情" fillStyle="#8fb6ff" />
|
||||
|
||||
<container hidden={!!props.item}>
|
||||
<text loc={[16, 52]} text="请选择道具" fillStyle="#4f6d94" />
|
||||
</container>
|
||||
|
||||
<container hidden={!props.item}>
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={props.item ? props.item.name : ''}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 84]}
|
||||
text={props.item ? `数量:${props.item.count}` : ''}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 120]}
|
||||
text={props.item ? props.item.desc : ''}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[16, 286, 164, 28]} onClick={clear}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 164, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 6]} text="取消选择" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
}, itemDetailOptions);
|
||||
|
||||
const NAMES = [
|
||||
'红药水',
|
||||
'蓝药水',
|
||||
'黄钥匙',
|
||||
'蓝钥匙',
|
||||
'木剑',
|
||||
'木盾',
|
||||
'红宝石',
|
||||
'蓝宝石',
|
||||
'回城卷轴',
|
||||
'面包'
|
||||
];
|
||||
|
||||
// 40 个假道具用来撑出可滚动的内容
|
||||
const items: Item[] = Array.from({ length: 40 }, (_, index) => ({
|
||||
id: `item-${index + 1}`,
|
||||
name: NAMES[index % NAMES.length],
|
||||
count: (index % 6) + 1,
|
||||
desc: `第 ${index + 1} 个道具。`
|
||||
}));
|
||||
|
||||
const COLUMNS = 5;
|
||||
const CELL_SIZE = 66;
|
||||
const GAP = 8;
|
||||
const ROW_PITCH = CELL_SIZE + GAP;
|
||||
const VIEW_WIDTH = 372;
|
||||
const VIEW_HEIGHT = 294;
|
||||
const ROWS = Math.ceil(items.length / COLUMNS);
|
||||
const CONTENT_HEIGHT = ROWS * ROW_PITCH;
|
||||
const MAX_START_ROW = Math.max(0, ROWS - Math.floor(VIEW_HEIGHT / ROW_PITCH));
|
||||
const MAX_SCROLL = MAX_START_ROW * ROW_PITCH;
|
||||
|
||||
// 示例(四)的 live:用元素层等价实现 Scroll 的滚动窗口与滚动条
|
||||
// 滚动位置只保存在本地 ref,滚动条由计算出的位置绘制
|
||||
export default defineComponent(() => {
|
||||
const selectedId = ref('item-1');
|
||||
const scrollTop = ref(0);
|
||||
const selectedItem = computed(() =>
|
||||
items.find(item => item.id === selectedId.value)
|
||||
);
|
||||
const startRow = computed(() => Math.round(scrollTop.value / ROW_PITCH));
|
||||
|
||||
// 滚轮与按钮都只调整 scrollTop
|
||||
const scrollRows = (delta: number) => {
|
||||
const current = Math.round(scrollTop.value / ROW_PITCH);
|
||||
const next = Math.max(0, Math.min(MAX_START_ROW, current + delta));
|
||||
scrollTop.value = next * ROW_PITCH;
|
||||
};
|
||||
|
||||
const onWheel = (ev: { wheelY: number }) => {
|
||||
scrollRows(ev.wheelY > 0 ? 1 : -1);
|
||||
};
|
||||
|
||||
const backToTop = () => {
|
||||
scrollTop.value = 0;
|
||||
};
|
||||
|
||||
const toBottom = () => {
|
||||
scrollTop.value = MAX_SCROLL;
|
||||
};
|
||||
|
||||
const thumbHeight = Math.max(
|
||||
28,
|
||||
(VIEW_HEIGHT / CONTENT_HEIGHT) * VIEW_HEIGHT
|
||||
);
|
||||
const thumbY =
|
||||
MAX_SCROLL === 0
|
||||
? 0
|
||||
: (scrollTop.value / MAX_SCROLL) * (VIEW_HEIGHT - thumbHeight);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container loc={[626, 50, 56, 32]}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 56, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#7a3d3d"
|
||||
/>
|
||||
<text loc={[14, 8]} text="关闭" fillStyle="#ffe9e9" />
|
||||
</container>
|
||||
|
||||
<container loc={[54, 110, 400, 330]}>
|
||||
<g-rect loc={[0, 0, 400, 330]} fill fillStyle="#101820" />
|
||||
|
||||
<container
|
||||
loc={[0, 0, VIEW_WIDTH, VIEW_HEIGHT]}
|
||||
onWheel={onWheel}
|
||||
>
|
||||
{items.map((item, index) => {
|
||||
const row = Math.floor(index / COLUMNS);
|
||||
// 只渲染视口附近的行,等价于 Scroll 的虚拟滚动
|
||||
if (row < startRow.value || row >= startRow.value + 5) {
|
||||
return null;
|
||||
}
|
||||
const col = index % COLUMNS;
|
||||
return (
|
||||
<container
|
||||
key={item.id}
|
||||
loc={[
|
||||
col * ROW_PITCH,
|
||||
(row - startRow.value) * ROW_PITCH
|
||||
]}
|
||||
>
|
||||
<ItemCell
|
||||
item={item}
|
||||
selected={item.id === selectedId.value}
|
||||
size={CELL_SIZE}
|
||||
onSelect={id => (selectedId.value = id)}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
|
||||
<g-rect
|
||||
loc={[378, 0, 6, VIEW_HEIGHT]}
|
||||
fill
|
||||
fillStyle="#1b2634"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[378, thumbY, 6, thumbHeight]}
|
||||
circle={[3]}
|
||||
fill
|
||||
fillStyle="#3f6fa8"
|
||||
/>
|
||||
|
||||
<container loc={[0, 302, 90, 28]} onClick={backToTop}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text loc={[10, 6]} text="回到顶部" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[100, 302, 90, 28]} onClick={toBottom}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[10, 6]} text="滚到底部" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
<text
|
||||
loc={[204, 308]}
|
||||
text={`已滚动 ${scrollTop.value} / ${MAX_SCROLL}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[470, 110, 196, 330]}>
|
||||
<ItemDetail
|
||||
item={selectedItem.value}
|
||||
onClear={() => (selectedId.value = '')}
|
||||
/>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,402 @@
|
||||
import { computed, defineComponent, ref, watch } from 'vue';
|
||||
|
||||
interface Item {
|
||||
id: string;
|
||||
name: string;
|
||||
count: number;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface ItemCellProps {
|
||||
item: Item;
|
||||
selected: boolean;
|
||||
equipped: boolean;
|
||||
size: number;
|
||||
}
|
||||
|
||||
type ItemCellEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCellOptions {
|
||||
props: (keyof ItemCellProps)[];
|
||||
emits: (keyof ItemCellEmits)[];
|
||||
}
|
||||
|
||||
const itemCellOptions: ItemCellOptions = {
|
||||
props: ['item', 'selected', 'equipped', 'size'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 装备、使用与关闭的状态全部集中在根组件
|
||||
const ItemCell = defineComponent<
|
||||
ItemCellProps,
|
||||
ItemCellEmits,
|
||||
keyof ItemCellEmits
|
||||
>((props, { emit }) => {
|
||||
const strokeStyle = computed(() => {
|
||||
if (props.equipped) return '#7ee787';
|
||||
if (props.selected) return '#f2b84b';
|
||||
return '#33465f';
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
onClick={() => emit('select', props.item.id)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
circle={[10]}
|
||||
fill
|
||||
fillStyle={props.selected ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[1, 1, props.size - 2, props.size - 2]}
|
||||
circle={[9]}
|
||||
stroke
|
||||
strokeStyle={strokeStyle.value}
|
||||
/>
|
||||
<text loc={[8, 10]} text={props.item.name} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[8, props.size - 24]}
|
||||
text={`× ${props.item.count}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, itemCellOptions);
|
||||
|
||||
interface ItemDetailProps {
|
||||
item?: Item;
|
||||
}
|
||||
|
||||
type ItemDetailEmits = {
|
||||
clear: () => void;
|
||||
};
|
||||
|
||||
interface ItemDetailOptions {
|
||||
props: (keyof ItemDetailProps)[];
|
||||
emits: (keyof ItemDetailEmits)[];
|
||||
}
|
||||
|
||||
const itemDetailOptions: ItemDetailOptions = {
|
||||
props: ['item'],
|
||||
emits: ['clear']
|
||||
};
|
||||
|
||||
// 详情只负责显示父组件算好的结果
|
||||
const ItemDetail = defineComponent<
|
||||
ItemDetailProps,
|
||||
ItemDetailEmits,
|
||||
keyof ItemDetailEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 196, 280]}>
|
||||
<g-rect loc={[0, 0, 196, 280]} fill fillStyle="#101820" />
|
||||
<text loc={[16, 16]} text="详情" fillStyle="#8fb6ff" />
|
||||
|
||||
<container hidden={!!props.item}>
|
||||
<text loc={[16, 52]} text="请选择道具" fillStyle="#4f6d94" />
|
||||
</container>
|
||||
|
||||
<container hidden={!props.item}>
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={props.item ? props.item.name : ''}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 84]}
|
||||
text={props.item ? `数量:${props.item.count}` : ''}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 120]}
|
||||
text={props.item ? props.item.desc : ''}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[16, 236, 164, 28]} onClick={() => emit('clear')}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 164, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 6]} text="取消选择" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
}, itemDetailOptions);
|
||||
|
||||
interface ActionBarProps {
|
||||
canEquip: boolean;
|
||||
canUse: boolean;
|
||||
equipped: boolean;
|
||||
}
|
||||
|
||||
type ActionBarEmits = {
|
||||
equip: () => void;
|
||||
use: () => void;
|
||||
close: () => void;
|
||||
};
|
||||
|
||||
interface ActionBarOptions {
|
||||
props: (keyof ActionBarProps)[];
|
||||
emits: (keyof ActionBarEmits)[];
|
||||
}
|
||||
|
||||
const actionBarOptions: ActionBarOptions = {
|
||||
props: ['canEquip', 'canUse', 'equipped'],
|
||||
emits: ['equip', 'use', 'close']
|
||||
};
|
||||
|
||||
// 按钮的可用性由父组件传入,自己不做业务判断
|
||||
const ActionBar = defineComponent<
|
||||
ActionBarProps,
|
||||
ActionBarEmits,
|
||||
keyof ActionBarEmits
|
||||
>((props, { emit }) => {
|
||||
const makeButton = (
|
||||
x: number,
|
||||
label: string,
|
||||
enabled: boolean,
|
||||
onClick: () => void
|
||||
) => (
|
||||
<container
|
||||
loc={[x, 0, 96, 32]}
|
||||
onClick={() => {
|
||||
if (!enabled) return;
|
||||
onClick();
|
||||
}}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 96, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle={enabled ? '#2f6fed' : '#26364d'}
|
||||
alpha={enabled ? 1 : 0.5}
|
||||
/>
|
||||
<text loc={[12, 8]} text={label} fillStyle="#ffffff" />
|
||||
</container>
|
||||
);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 304, 32]}>
|
||||
{makeButton(
|
||||
0,
|
||||
props.equipped ? '已装备' : '装备',
|
||||
props.canEquip,
|
||||
() => emit('equip')
|
||||
)}
|
||||
{makeButton(104, '使用', props.canUse, () => emit('use'))}
|
||||
{makeButton(208, '关闭', true, () => emit('close'))}
|
||||
</container>
|
||||
);
|
||||
}, actionBarOptions);
|
||||
|
||||
const NAMES = [
|
||||
'红药水',
|
||||
'蓝药水',
|
||||
'黄钥匙',
|
||||
'蓝钥匙',
|
||||
'木剑',
|
||||
'木盾',
|
||||
'红宝石',
|
||||
'蓝宝石',
|
||||
'回城卷轴',
|
||||
'面包'
|
||||
];
|
||||
|
||||
// 40 个假道具同时用于测试滚动与使用次数
|
||||
const sourceItems: Item[] = Array.from({ length: 40 }, (_, index) => ({
|
||||
id: `item-${index + 1}`,
|
||||
name: NAMES[index % NAMES.length],
|
||||
count: (index % 6) + 1,
|
||||
desc: `第 ${index + 1} 个道具。`
|
||||
}));
|
||||
|
||||
const COLUMNS = 5;
|
||||
const CELL_SIZE = 66;
|
||||
const GAP = 8;
|
||||
const ROW_PITCH = CELL_SIZE + GAP;
|
||||
const VIEW_WIDTH = 372;
|
||||
const VIEW_HEIGHT = 244;
|
||||
const ROWS = Math.ceil(sourceItems.length / COLUMNS);
|
||||
const MAX_START_ROW = Math.max(0, ROWS - Math.floor(VIEW_HEIGHT / ROW_PITCH));
|
||||
const MAX_SCROLL = MAX_START_ROW * ROW_PITCH;
|
||||
|
||||
// 示例(五)的 live:滚动网格 + 详情 + 装备 / 使用 / 关闭
|
||||
// items 复制一份,使用道具时只改本地副本
|
||||
export default defineComponent(() => {
|
||||
const items = ref<Item[]>(sourceItems.map(item => ({ ...item })));
|
||||
const selectedId = ref('item-1');
|
||||
const equippedId = ref('');
|
||||
const hint = ref('选择道具后可以装备或使用');
|
||||
const scrollTop = ref(0);
|
||||
|
||||
const selectedItem = computed(() =>
|
||||
items.value.find(item => item.id === selectedId.value)
|
||||
);
|
||||
const canEquip = computed(() => !!selectedItem.value);
|
||||
const canUse = computed(
|
||||
() => !!selectedItem.value && selectedItem.value.count > 0
|
||||
);
|
||||
const isEquipped = computed(
|
||||
() => !!selectedId.value && equippedId.value === selectedId.value
|
||||
);
|
||||
const startRow = computed(() => Math.round(scrollTop.value / ROW_PITCH));
|
||||
|
||||
const equip = () => {
|
||||
if (!selectedItem.value) return;
|
||||
equippedId.value = selectedItem.value.id;
|
||||
hint.value = `已装备:${selectedItem.value.name}`;
|
||||
};
|
||||
|
||||
const use = () => {
|
||||
const item = selectedItem.value;
|
||||
if (!item || item.count <= 0) return;
|
||||
item.count -= 1;
|
||||
};
|
||||
|
||||
watch(
|
||||
() => selectedItem.value?.count ?? 0,
|
||||
(next, prev) => {
|
||||
hint.value = `数量:${prev} → ${next}`;
|
||||
}
|
||||
);
|
||||
|
||||
const scrollRows = (delta: number) => {
|
||||
const current = Math.round(scrollTop.value / ROW_PITCH);
|
||||
const next = Math.max(0, Math.min(MAX_START_ROW, current + delta));
|
||||
scrollTop.value = next * ROW_PITCH;
|
||||
};
|
||||
|
||||
const onWheel = (ev: { wheelY: number }) => {
|
||||
scrollRows(ev.wheelY > 0 ? 1 : -1);
|
||||
};
|
||||
|
||||
const thumbHeight = Math.max(
|
||||
28,
|
||||
(VIEW_HEIGHT / (ROWS * ROW_PITCH)) * VIEW_HEIGHT
|
||||
);
|
||||
const thumbY =
|
||||
MAX_SCROLL === 0
|
||||
? 0
|
||||
: (scrollTop.value / MAX_SCROLL) * (VIEW_HEIGHT - thumbHeight);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container loc={[54, 100, 400, 280]}>
|
||||
<g-rect loc={[0, 0, 400, 280]} fill fillStyle="#101820" />
|
||||
|
||||
<container
|
||||
loc={[0, 0, VIEW_WIDTH, VIEW_HEIGHT]}
|
||||
onWheel={onWheel}
|
||||
>
|
||||
{items.value.map((item, index) => {
|
||||
const row = Math.floor(index / COLUMNS);
|
||||
if (row < startRow.value || row >= startRow.value + 4) {
|
||||
return null;
|
||||
}
|
||||
const col = index % COLUMNS;
|
||||
return (
|
||||
<container
|
||||
key={item.id}
|
||||
loc={[
|
||||
col * ROW_PITCH,
|
||||
(row - startRow.value) * ROW_PITCH
|
||||
]}
|
||||
>
|
||||
<ItemCell
|
||||
item={item}
|
||||
selected={item.id === selectedId.value}
|
||||
equipped={item.id === equippedId.value}
|
||||
size={CELL_SIZE}
|
||||
onSelect={id => (selectedId.value = id)}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
|
||||
<g-rect
|
||||
loc={[378, 0, 6, VIEW_HEIGHT]}
|
||||
fill
|
||||
fillStyle="#1b2634"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[378, thumbY, 6, thumbHeight]}
|
||||
circle={[3]}
|
||||
fill
|
||||
fillStyle="#3f6fa8"
|
||||
/>
|
||||
|
||||
<container
|
||||
loc={[0, 252, 90, 28]}
|
||||
onClick={() => scrollRows(-1)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[10, 6]} text="上一行" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
<container
|
||||
loc={[100, 252, 90, 28]}
|
||||
onClick={() => scrollRows(1)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[10, 6]} text="下一行" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
<text
|
||||
loc={[204, 258]}
|
||||
text={`已滚动 ${scrollTop.value} / ${MAX_SCROLL}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[470, 100, 196, 280]}>
|
||||
<ItemDetail
|
||||
item={selectedItem.value}
|
||||
onClear={() => (selectedId.value = '')}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[54, 392, 304, 32]}>
|
||||
<ActionBar
|
||||
canEquip={canEquip.value}
|
||||
canUse={canUse.value}
|
||||
equipped={isEquipped.value}
|
||||
onEquip={equip}
|
||||
onUse={use}
|
||||
onClose={() => (hint.value = '已请求关闭')}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<text loc={[54, 432]} text={hint.value} fillStyle="#8fb6ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,482 @@
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
ref,
|
||||
watch
|
||||
} from 'vue';
|
||||
|
||||
interface Item {
|
||||
id: string;
|
||||
name: string;
|
||||
count: number;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface ItemCellProps {
|
||||
item: Item;
|
||||
selected: boolean;
|
||||
equipped: boolean;
|
||||
size: number;
|
||||
pulse: number;
|
||||
}
|
||||
|
||||
type ItemCellEmits = {
|
||||
select: (id: string) => void;
|
||||
};
|
||||
|
||||
interface ItemCellOptions {
|
||||
props: (keyof ItemCellProps)[];
|
||||
emits: (keyof ItemCellEmits)[];
|
||||
}
|
||||
|
||||
const itemCellOptions: ItemCellOptions = {
|
||||
props: ['item', 'selected', 'equipped', 'size', 'pulse'],
|
||||
emits: ['select']
|
||||
};
|
||||
|
||||
// 完整示例把网格、详情、滚动、装备与动画放在一个文件里
|
||||
const ItemCell = defineComponent<
|
||||
ItemCellProps,
|
||||
ItemCellEmits,
|
||||
keyof ItemCellEmits
|
||||
>((props, { emit }) => {
|
||||
const strokeStyle = computed(() => {
|
||||
if (props.equipped) return '#7ee787';
|
||||
if (props.selected) return '#f2b84b';
|
||||
return '#33465f';
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
onClick={() => emit('select', props.item.id)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, props.size, props.size]}
|
||||
circle={[10]}
|
||||
fill
|
||||
fillStyle={props.selected ? '#2f6fed' : '#26364d'}
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[1, 1, props.size - 2, props.size - 2]}
|
||||
circle={[9]}
|
||||
stroke
|
||||
strokeStyle={strokeStyle.value}
|
||||
alpha={props.selected ? props.pulse : 1}
|
||||
/>
|
||||
<text loc={[8, 10]} text={props.item.name} fillStyle="#ffffff" />
|
||||
<text
|
||||
loc={[8, props.size - 24]}
|
||||
text={`× ${props.item.count}`}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
}, itemCellOptions);
|
||||
|
||||
interface ItemDetailProps {
|
||||
item?: Item;
|
||||
pulse: number;
|
||||
}
|
||||
|
||||
type ItemDetailEmits = {
|
||||
clear: () => void;
|
||||
};
|
||||
|
||||
interface ItemDetailOptions {
|
||||
props: (keyof ItemDetailProps)[];
|
||||
emits: (keyof ItemDetailEmits)[];
|
||||
}
|
||||
|
||||
const itemDetailOptions: ItemDetailOptions = {
|
||||
props: ['item', 'pulse'],
|
||||
emits: ['clear']
|
||||
};
|
||||
|
||||
const ItemDetail = defineComponent<
|
||||
ItemDetailProps,
|
||||
ItemDetailEmits,
|
||||
keyof ItemDetailEmits
|
||||
>((props, { emit }) => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 196, 280]}>
|
||||
<g-rect loc={[0, 0, 196, 280]} fill fillStyle="#101820" />
|
||||
{/* 顶部亮条只改 alpha 与 scale,不重建组件 */}
|
||||
<g-rect
|
||||
loc={[0, 0, 196, 4]}
|
||||
fill
|
||||
fillStyle="#f2b84b"
|
||||
alpha={props.pulse}
|
||||
scale={[1, 0.5 + props.pulse]}
|
||||
/>
|
||||
<text loc={[16, 16]} text="详情" fillStyle="#8fb6ff" />
|
||||
|
||||
<container hidden={!!props.item}>
|
||||
<text loc={[16, 52]} text="请选择道具" fillStyle="#4f6d94" />
|
||||
</container>
|
||||
|
||||
<container hidden={!props.item}>
|
||||
<text
|
||||
loc={[16, 52]}
|
||||
text={props.item ? props.item.name : ''}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 84]}
|
||||
text={props.item ? `数量:${props.item.count}` : ''}
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[16, 120]}
|
||||
text={props.item ? props.item.desc : ''}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[16, 236, 164, 28]} onClick={() => emit('clear')}>
|
||||
<g-rectr
|
||||
loc={[0, 0, 164, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[12, 6]} text="取消选择" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
}, itemDetailOptions);
|
||||
|
||||
interface ActionBarProps {
|
||||
canEquip: boolean;
|
||||
canUse: boolean;
|
||||
equipped: boolean;
|
||||
}
|
||||
|
||||
type ActionBarEmits = {
|
||||
equip: () => void;
|
||||
use: () => void;
|
||||
close: () => void;
|
||||
};
|
||||
|
||||
interface ActionBarOptions {
|
||||
props: (keyof ActionBarProps)[];
|
||||
emits: (keyof ActionBarEmits)[];
|
||||
}
|
||||
|
||||
const actionBarOptions: ActionBarOptions = {
|
||||
props: ['canEquip', 'canUse', 'equipped'],
|
||||
emits: ['equip', 'use', 'close']
|
||||
};
|
||||
|
||||
const ActionBar = defineComponent<
|
||||
ActionBarProps,
|
||||
ActionBarEmits,
|
||||
keyof ActionBarEmits
|
||||
>((props, { emit }) => {
|
||||
const makeButton = (
|
||||
x: number,
|
||||
label: string,
|
||||
enabled: boolean,
|
||||
onClick: () => void
|
||||
) => (
|
||||
<container
|
||||
loc={[x, 0, 96, 32]}
|
||||
onClick={() => {
|
||||
if (!enabled) return;
|
||||
onClick();
|
||||
}}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 96, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle={enabled ? '#2f6fed' : '#26364d'}
|
||||
alpha={enabled ? 1 : 0.5}
|
||||
/>
|
||||
<text loc={[12, 8]} text={label} fillStyle="#ffffff" />
|
||||
</container>
|
||||
);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 304, 32]}>
|
||||
{makeButton(
|
||||
0,
|
||||
props.equipped ? '已装备' : '装备',
|
||||
props.canEquip,
|
||||
() => emit('equip')
|
||||
)}
|
||||
{makeButton(104, '使用', props.canUse, () => emit('use'))}
|
||||
{makeButton(208, '关闭', true, () => emit('close'))}
|
||||
</container>
|
||||
);
|
||||
}, actionBarOptions);
|
||||
|
||||
const NAMES = [
|
||||
'红药水',
|
||||
'蓝药水',
|
||||
'黄钥匙',
|
||||
'蓝钥匙',
|
||||
'木剑',
|
||||
'木盾',
|
||||
'红宝石',
|
||||
'蓝宝石',
|
||||
'回城卷轴',
|
||||
'面包'
|
||||
];
|
||||
|
||||
const sourceItems: Item[] = Array.from({ length: 40 }, (_, index) => ({
|
||||
id: `item-${index + 1}`,
|
||||
name: NAMES[index % NAMES.length],
|
||||
count: (index % 6) + 1,
|
||||
desc: `第 ${index + 1} 个道具。`
|
||||
}));
|
||||
|
||||
const COLUMNS = 5;
|
||||
const CELL_SIZE = 66;
|
||||
const GAP = 8;
|
||||
const ROW_PITCH = CELL_SIZE + GAP;
|
||||
const VIEW_WIDTH = 372;
|
||||
const VIEW_HEIGHT = 244;
|
||||
const ROWS = Math.ceil(sourceItems.length / COLUMNS);
|
||||
const MAX_START_ROW = Math.max(0, ROWS - Math.floor(VIEW_HEIGHT / ROW_PITCH));
|
||||
const MAX_SCROLL = MAX_START_ROW * ROW_PITCH;
|
||||
|
||||
// 示例(六)的 live:完整背包面板,含动画与关闭 / 重新打开
|
||||
// InventoryPanel 是唯一注册为 GameUI 的根组件
|
||||
const InventoryPanel = defineComponent(() => {
|
||||
const items = ref<Item[]>(sourceItems.map(item => ({ ...item })));
|
||||
const selectedId = ref('item-1');
|
||||
const equippedId = ref('');
|
||||
const hint = ref('选择道具后可以装备或使用');
|
||||
const scrollTop = ref(0);
|
||||
const closed = ref(false);
|
||||
const pulse = ref(0.4);
|
||||
let raf = 0;
|
||||
|
||||
const tick = (now: number) => {
|
||||
const phase = (now % 1600) / 1600;
|
||||
pulse.value = 0.35 + Math.sin(phase * Math.PI * 2) * 0.3;
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
raf = requestAnimationFrame(tick);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
// 卸载后必须停掉,否则回调会继续访问已销毁的组件
|
||||
cancelAnimationFrame(raf);
|
||||
});
|
||||
|
||||
const selectedItem = computed(() =>
|
||||
items.value.find(item => item.id === selectedId.value)
|
||||
);
|
||||
const canEquip = computed(() => !!selectedItem.value);
|
||||
const canUse = computed(
|
||||
() => !!selectedItem.value && selectedItem.value.count > 0
|
||||
);
|
||||
const isEquipped = computed(
|
||||
() => !!selectedId.value && equippedId.value === selectedId.value
|
||||
);
|
||||
const startRow = computed(() => Math.round(scrollTop.value / ROW_PITCH));
|
||||
|
||||
const equip = () => {
|
||||
if (!selectedItem.value) return;
|
||||
equippedId.value = selectedItem.value.id;
|
||||
hint.value = `已装备:${selectedItem.value.name}`;
|
||||
};
|
||||
|
||||
const use = () => {
|
||||
const item = selectedItem.value;
|
||||
if (!item || item.count <= 0) return;
|
||||
item.count -= 1;
|
||||
};
|
||||
|
||||
watch(
|
||||
() => selectedItem.value?.count ?? 0,
|
||||
(next, prev) => {
|
||||
hint.value = `数量:${prev} → ${next}`;
|
||||
}
|
||||
);
|
||||
|
||||
const scrollRows = (delta: number) => {
|
||||
const current = Math.round(scrollTop.value / ROW_PITCH);
|
||||
const next = Math.max(0, Math.min(MAX_START_ROW, current + delta));
|
||||
scrollTop.value = next * ROW_PITCH;
|
||||
};
|
||||
|
||||
const onWheel = (ev: { wheelY: number }) => {
|
||||
scrollRows(ev.wheelY > 0 ? 1 : -1);
|
||||
};
|
||||
|
||||
const thumbHeight = Math.max(
|
||||
28,
|
||||
(VIEW_HEIGHT / (ROWS * ROW_PITCH)) * VIEW_HEIGHT
|
||||
);
|
||||
const thumbY =
|
||||
MAX_SCROLL === 0
|
||||
? 0
|
||||
: (scrollTop.value / MAX_SCROLL) * (VIEW_HEIGHT - thumbHeight);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 720, 480]}>
|
||||
<g-rect loc={[0, 0, 720, 480]} fill fillStyle="#0e141b" />
|
||||
|
||||
<container loc={[0, 0, 720, 480]} hidden={closed.value}>
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
fill
|
||||
fillStyle="#1d2b45"
|
||||
stroke
|
||||
strokeStyle="#2f6fed"
|
||||
/>
|
||||
|
||||
{/* 呼吸边框只改 alpha,不每帧重建 */}
|
||||
<g-rectr
|
||||
loc={[30, 30, 660, 420]}
|
||||
circle={[18]}
|
||||
stroke
|
||||
strokeStyle="#f2b84b"
|
||||
alpha={pulse.value * 0.6}
|
||||
/>
|
||||
|
||||
<text loc={[54, 52]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
<container
|
||||
loc={[626, 50, 56, 32]}
|
||||
onClick={() => (closed.value = true)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 56, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#7a3d3d"
|
||||
/>
|
||||
<text loc={[14, 8]} text="关闭" fillStyle="#ffe9e9" />
|
||||
</container>
|
||||
|
||||
<container loc={[54, 100, 400, 280]}>
|
||||
<g-rect loc={[0, 0, 400, 280]} fill fillStyle="#101820" />
|
||||
|
||||
<container
|
||||
loc={[0, 0, VIEW_WIDTH, VIEW_HEIGHT]}
|
||||
onWheel={onWheel}
|
||||
>
|
||||
{items.value.map((item, index) => {
|
||||
const row = Math.floor(index / COLUMNS);
|
||||
if (
|
||||
row < startRow.value ||
|
||||
row >= startRow.value + 4
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const col = index % COLUMNS;
|
||||
return (
|
||||
<container
|
||||
key={item.id}
|
||||
loc={[
|
||||
col * ROW_PITCH,
|
||||
(row - startRow.value) * ROW_PITCH
|
||||
]}
|
||||
>
|
||||
<ItemCell
|
||||
item={item}
|
||||
selected={item.id === selectedId.value}
|
||||
equipped={item.id === equippedId.value}
|
||||
size={CELL_SIZE}
|
||||
pulse={pulse.value}
|
||||
onSelect={id => (selectedId.value = id)}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
})}
|
||||
</container>
|
||||
|
||||
<g-rect
|
||||
loc={[378, 0, 6, VIEW_HEIGHT]}
|
||||
fill
|
||||
fillStyle="#1b2634"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[378, thumbY, 6, thumbHeight]}
|
||||
circle={[3]}
|
||||
fill
|
||||
fillStyle="#3f6fa8"
|
||||
/>
|
||||
|
||||
<container
|
||||
loc={[0, 252, 90, 28]}
|
||||
onClick={() => scrollRows(-1)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[10, 6]} text="上一行" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
<container
|
||||
loc={[100, 252, 90, 28]}
|
||||
onClick={() => scrollRows(1)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 90, 28]}
|
||||
circle={[6]}
|
||||
fill
|
||||
fillStyle="#33465f"
|
||||
/>
|
||||
<text loc={[10, 6]} text="下一行" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
<text
|
||||
loc={[204, 258]}
|
||||
text={`已滚动 ${scrollTop.value} / ${MAX_SCROLL}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[470, 100, 196, 280]}>
|
||||
<ItemDetail
|
||||
item={selectedItem.value}
|
||||
pulse={pulse.value}
|
||||
onClear={() => (selectedId.value = '')}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[54, 392, 304, 32]}>
|
||||
<ActionBar
|
||||
canEquip={canEquip.value}
|
||||
canUse={canUse.value}
|
||||
equipped={isEquipped.value}
|
||||
onEquip={equip}
|
||||
onUse={use}
|
||||
onClose={() => (closed.value = true)}
|
||||
/>
|
||||
</container>
|
||||
|
||||
<text loc={[54, 432]} text={hint.value} fillStyle="#8fb6ff" />
|
||||
</container>
|
||||
|
||||
<container loc={[0, 0, 720, 480]} hidden={!closed.value}>
|
||||
<text loc={[300, 220]} text="面板已关闭" fillStyle="#e8f0ff" />
|
||||
<container
|
||||
loc={[300, 260, 120, 32]}
|
||||
onClick={() => (closed.value = false)}
|
||||
>
|
||||
<g-rectr
|
||||
loc={[0, 0, 120, 32]}
|
||||
circle={[8]}
|
||||
fill
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<text loc={[18, 8]} text="重新打开" fillStyle="#ffffff" />
|
||||
</container>
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
|
||||
export default InventoryPanel;
|
||||
@@ -0,0 +1,56 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 同一组重叠图形分别使用 source-over、multiply 与 screen
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="composite 对照" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[24, 48]}
|
||||
text="默认 source-over,可换成 multiply / screen"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<container loc={[25, 76, 140, 150]}>
|
||||
<g-rect loc={[0, 0, 140, 150]} fillStyle="#1b2430" />
|
||||
<g-circle circle={[50, 50, 50]} fillStyle="#2f6fed" />
|
||||
<g-rect
|
||||
loc={[40, 60, 100, 90]}
|
||||
fillStyle="#f2b84b"
|
||||
composite="source-over"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[170, 76, 140, 150]}>
|
||||
<g-rect loc={[0, 0, 140, 150]} fillStyle="#1b2430" />
|
||||
<g-circle circle={[50, 50, 50]} fillStyle="#2f6fed" />
|
||||
<g-rect
|
||||
loc={[40, 60, 100, 90]}
|
||||
fillStyle="#f2b84b"
|
||||
composite="multiply"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<container loc={[315, 76, 140, 150]}>
|
||||
<g-rect loc={[0, 0, 140, 150]} fillStyle="#1b2430" />
|
||||
<g-circle circle={[50, 50, 50]} fillStyle="#2f6fed" />
|
||||
<g-rect
|
||||
loc={[40, 60, 100, 90]}
|
||||
fillStyle="#f2b84b"
|
||||
composite="screen"
|
||||
/>
|
||||
</container>
|
||||
|
||||
<text loc={[25, 244]} text="source-over" fillStyle="#8b98a8" />
|
||||
<text loc={[170, 244]} text="multiply" fillStyle="#8b98a8" />
|
||||
<text loc={[315, 244]} text="screen" fillStyle="#8b98a8" />
|
||||
<text
|
||||
loc={[24, 286]}
|
||||
text="composite 只影响绘制结果,不改变布局与命中"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 父级缩放,子元素仍按局部坐标描述内容
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="变换沿树传递" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[24, 48]}
|
||||
text="父级 scale 后,子元素的 loc 不变"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
{/* 父级放大 1.5 倍,子元素仍写 [0, 0, 140, 100];加 nocache 避免缓存图被重采样 */}
|
||||
<container loc={[40, 88, 140, 100]} scale={[1.5, 1.5]} nocache>
|
||||
<g-rect loc={[0, 0, 140, 100]} fill fillStyle="#2f6fed" />
|
||||
<g-circle circle={[18, 18, 10]} fillStyle="#f2b84b" />
|
||||
<text loc={[12, 44]} text="子元素" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
{/* 同一棵子树未缩放的参考 */}
|
||||
<container loc={[290, 88, 140, 100]}>
|
||||
<g-rect loc={[0, 0, 140, 100]} fill fillStyle="#2f6fed" />
|
||||
<g-circle circle={[18, 18, 10]} fillStyle="#f2b84b" />
|
||||
<text loc={[12, 44]} text="子元素" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[40, 252]}
|
||||
text="父级 scale [1.5, 1.5]"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
<text loc={[290, 252]} text="未缩放参考" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 同一变换在左上角锚点与中心锚点下的对比;白点 = 锚点
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 14]}
|
||||
text="同一变换,不同锚点"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[24, 34]}
|
||||
text="白点 = 锚点:分别对比绕左上角与绕中心"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
{/* scale:左上角锚点 */}
|
||||
<container loc={[12, 52, 224, 124]}>
|
||||
<g-rect loc={[0, 0, 224, 124]} fillStyle="#1a2330" />
|
||||
<text
|
||||
loc={[10, 8]}
|
||||
text="scale · anc [0, 0]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<container
|
||||
loc={[84, 58, 76, 42]}
|
||||
anc={[0, 0]}
|
||||
scale={[1.35, 0.7]}
|
||||
nocache
|
||||
>
|
||||
<g-rect loc={[0, 0, 76, 42]} fillStyle="#2f6fed" />
|
||||
<g-circle circle={[14, 12, 7]} fillStyle="#f2b84b" />
|
||||
</container>
|
||||
<g-circle circle={[84, 58, 5]} fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
{/* scale:中心锚点 */}
|
||||
<container loc={[244, 52, 224, 124]}>
|
||||
<g-rect loc={[0, 0, 224, 124]} fillStyle="#1a2330" />
|
||||
<text
|
||||
loc={[10, 8]}
|
||||
text="scale · anc [0.5, 0.5]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<container
|
||||
loc={[84, 58, 76, 42]}
|
||||
anc={[0.5, 0.5]}
|
||||
scale={[1.35, 0.7]}
|
||||
nocache
|
||||
>
|
||||
<g-rect loc={[0, 0, 76, 42]} fillStyle="#2f6fed" />
|
||||
<g-circle circle={[14, 12, 7]} fillStyle="#f2b84b" />
|
||||
</container>
|
||||
<g-circle circle={[84, 58, 5]} fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
{/* rotate:左上角锚点 */}
|
||||
<container loc={[12, 180, 224, 124]}>
|
||||
<g-rect loc={[0, 0, 224, 124]} fillStyle="#1a2330" />
|
||||
<text
|
||||
loc={[10, 8]}
|
||||
text="rotate · anc [0, 0]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<container
|
||||
loc={[84, 58, 76, 42]}
|
||||
anc={[0, 0]}
|
||||
rotate={Math.PI / 12}
|
||||
nocache
|
||||
>
|
||||
<g-rect loc={[0, 0, 76, 42]} fillStyle="#c58af9" />
|
||||
<g-circle circle={[14, 12, 7]} fillStyle="#f2b84b" />
|
||||
</container>
|
||||
<g-circle circle={[84, 58, 5]} fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
|
||||
{/* rotate:中心锚点 */}
|
||||
<container loc={[244, 180, 224, 124]}>
|
||||
<g-rect loc={[0, 0, 224, 124]} fillStyle="#1a2330" />
|
||||
<text
|
||||
loc={[10, 8]}
|
||||
text="rotate · anc [0.5, 0.5]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<container
|
||||
loc={[84, 58, 76, 42]}
|
||||
anc={[0.5, 0.5]}
|
||||
rotate={Math.PI / 12}
|
||||
nocache
|
||||
>
|
||||
<g-rect loc={[0, 0, 76, 42]} fillStyle="#c58af9" />
|
||||
<g-circle circle={[14, 12, 7]} fillStyle="#f2b84b" />
|
||||
</container>
|
||||
<g-circle circle={[84, 58, 5]} fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
import { computed, defineComponent, onUnmounted, ref } from 'vue';
|
||||
import { GameUI, IUIDefaultPropsBase, UIController } from '@motajs/system';
|
||||
|
||||
// 切换 hidden / cache 的小面板,并显示 active / showBack 状态
|
||||
export default defineComponent(() => {
|
||||
// 控制器与状态 UI 都在 setup 里创建,卸载时释放
|
||||
const controller = new UIController<IUIDefaultPropsBase>();
|
||||
controller.setPropsBaseCreator((instance, ctrl) => ({
|
||||
instance,
|
||||
controller: ctrl
|
||||
}));
|
||||
|
||||
// 状态 UI 只负责显示控制器的两个引用值
|
||||
const StateUI = defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[330, 200, 130, 96]}>
|
||||
<g-rect loc={[0, 0, 130, 96]} fill fillStyle="#1d2b45" />
|
||||
<g-rect loc={[0, 0, 130, 96]} stroke strokeStyle="#7aa2ff" />
|
||||
<text loc={[10, 10]} text="控制器状态" fillStyle="#e8f0ff" />
|
||||
<text
|
||||
loc={[10, 40]}
|
||||
text={`active:${controller.active.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
<text
|
||||
loc={[10, 64]}
|
||||
text={`showBack:${controller.showBack.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
|
||||
const stateUI = new GameUI('perf-state', StateUI);
|
||||
controller.open(stateUI, {});
|
||||
|
||||
const Renderer = defineComponent(() => {
|
||||
return () => controller.render();
|
||||
});
|
||||
|
||||
const visible = ref(true);
|
||||
const cached = ref(false);
|
||||
const cacheText = computed(() => (cached.value ? '已开缓存' : '未开缓存'));
|
||||
|
||||
onUnmounted(() => {
|
||||
controller.closeAll();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fill fillStyle="#141a22" />
|
||||
|
||||
<container
|
||||
loc={[20, 20, 120, 32]}
|
||||
onClick={() => (visible.value = !visible.value)}
|
||||
>
|
||||
<g-rect loc={[0, 0, 120, 32]} fill fillStyle="#2f6fed" />
|
||||
<text loc={[12, 8]} text="显示 / 隐藏" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container
|
||||
loc={[150, 20, 120, 32]}
|
||||
onClick={() => (cached.value = !cached.value)}
|
||||
>
|
||||
<g-rect loc={[0, 0, 120, 32]} fill fillStyle="#3a2f5f" />
|
||||
<text loc={[12, 8]} text="切换缓存" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<text loc={[20, 64]} text={cacheText.value} fillStyle="#8fb6ff" />
|
||||
|
||||
{/* hidden 控制显示,cache / nocache 控制是否复用离屏画布 */}
|
||||
<container
|
||||
loc={[20, 96, 260, 200]}
|
||||
hidden={!visible.value}
|
||||
cache={cached.value}
|
||||
nocache={!cached.value}
|
||||
>
|
||||
<g-rect loc={[0, 0, 260, 200]} fill fillStyle="#101820" />
|
||||
{Array.from({ length: 8 }, (_, index) => (
|
||||
<container key={index} loc={[12, 12 + index * 22, 236, 18]}>
|
||||
<g-rect
|
||||
loc={[0, 0, 236, 18]}
|
||||
fill
|
||||
fillStyle={index % 2 === 0 ? '#26364d' : '#1d2b45'}
|
||||
/>
|
||||
<text
|
||||
loc={[8, 2]}
|
||||
text={`静态内容 ${index + 1}`}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
))}
|
||||
</container>
|
||||
|
||||
<text
|
||||
loc={[20, 300]}
|
||||
text="hidden 保留状态;cache 只在内容稳定时才有收益。"
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<Renderer />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 三个同尺寸矩形对比不同的 alpha
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="alpha 对照" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-rect loc={[40, 88, 120, 120]} fillStyle="#4f8cff" alpha={1} />
|
||||
<text loc={[40, 226]} text="alpha 1" fillStyle="#8b98a8" />
|
||||
|
||||
<g-rect loc={[180, 88, 120, 120]} fillStyle="#4f8cff" alpha={0.6} />
|
||||
<text loc={[180, 226]} text="alpha 0.6" fillStyle="#8b98a8" />
|
||||
|
||||
<g-rect
|
||||
loc={[320, 88, 120, 120]}
|
||||
fillStyle="#4f8cff"
|
||||
alpha={0.25}
|
||||
/>
|
||||
<text loc={[320, 226]} text="alpha 0.25" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Texture } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 演示用纹理:16×16 的棋盘格,放大后更容易看出抗锯齿差异
|
||||
function createPixelTexture() {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 16;
|
||||
canvas.height = 16;
|
||||
|
||||
const ctx = canvas.getContext('2d')!;
|
||||
for (let y = 0; y < 16; y++) {
|
||||
for (let x = 0; x < 16; x++) {
|
||||
ctx.fillStyle = (x + y) % 2 === 0 ? '#f2b84b' : '#2f6fed';
|
||||
ctx.fillRect(x, y, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return new Texture(canvas);
|
||||
}
|
||||
|
||||
const demoTexture = createPixelTexture();
|
||||
|
||||
// 同一张纹理放大显示,对照默认抗锯齿与 noanti
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="同一张纹理放大到 160×160"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<image image={demoTexture} loc={[56, 76, 160, 160]} />
|
||||
<text loc={[56, 250]} text="默认" fillStyle="#8b98a8" />
|
||||
|
||||
<image image={demoTexture} noanti loc={[264, 76, 160, 160]} />
|
||||
<text loc={[264, 250]} text="noanti" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 带 cursor 的区域:鼠标移入后光标变成手型
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="cursor 属性" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[24, 56]}
|
||||
text="把鼠标移到下面的区域上"
|
||||
fillStyle="#8b98a8"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[40, 100, 400, 140]}
|
||||
fillStyle="#2f6fed"
|
||||
cursor="pointer"
|
||||
/>
|
||||
<text loc={[40, 258]} text='cursor="pointer"' fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Texture } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 演示用纹理:带几种颜色,便于看出灰度与亮度变化
|
||||
function createDemoTexture() {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 64;
|
||||
canvas.height = 64;
|
||||
|
||||
const ctx = canvas.getContext('2d')!;
|
||||
ctx.fillStyle = '#2a3550';
|
||||
ctx.fillRect(0, 0, 64, 64);
|
||||
ctx.fillStyle = '#2f6fed';
|
||||
ctx.fillRect(0, 0, 64, 24);
|
||||
ctx.fillStyle = '#f2b84b';
|
||||
ctx.beginPath();
|
||||
ctx.arc(32, 42, 14, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.fillStyle = '#7ee787';
|
||||
ctx.fillRect(44, 44, 20, 20);
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.fillRect(6, 40, 10, 10);
|
||||
|
||||
return new Texture(canvas);
|
||||
}
|
||||
|
||||
const demoTexture = createDemoTexture();
|
||||
|
||||
// 同一张图分别使用正常、模糊与灰度滤镜
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="filter 对照" fillStyle="#cfe0ff" />
|
||||
|
||||
<image image={demoTexture} loc={[48, 84, 120, 120]} />
|
||||
<text loc={[48, 218]} text="正常" fillStyle="#8b98a8" />
|
||||
|
||||
<image
|
||||
image={demoTexture}
|
||||
loc={[180, 84, 120, 120]}
|
||||
filter="blur(3px)"
|
||||
/>
|
||||
<text loc={[180, 218]} text="blur(3px)" fillStyle="#8b98a8" />
|
||||
|
||||
<image
|
||||
image={demoTexture}
|
||||
loc={[312, 84, 120, 120]}
|
||||
filter="grayscale(1) brightness(0.7)"
|
||||
/>
|
||||
<text loc={[312, 218]} text="灰度 + 亮度" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 两个重叠矩形:先写的蓝色 zIndex 更高,盖在黄色上面
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="zIndex 对照" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-rect loc={[80, 88, 200, 140]} fillStyle="#2f6fed" zIndex={2} />
|
||||
<g-rect loc={[180, 148, 200, 140]} fillStyle="#f2b84b" />
|
||||
|
||||
<text loc={[24, 296]} text="蓝色 zIndex 2" fillStyle="#8b98a8" />
|
||||
<text loc={[300, 296]} text="黄色 zIndex 0" fillStyle="#8b98a8" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { computed, defineComponent, ref } from 'vue';
|
||||
|
||||
// computed 从 count 派生 doubled,不需要另外维护一份状态
|
||||
export default defineComponent(() => {
|
||||
const count = ref(0);
|
||||
const doubled = computed(() => count.value * 2);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[40, 36]}
|
||||
text="count 变化后 doubled 自动重算"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[40, 96, 180, 56]}
|
||||
fillStyle="#2f6fed"
|
||||
onClick={() => count.value++}
|
||||
/>
|
||||
<text loc={[64, 114]} text="点我 +1" fillStyle="#e8f0ff" noevent />
|
||||
|
||||
<text
|
||||
loc={[260, 104]}
|
||||
text={`count = ${count.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[260, 140]}
|
||||
text={`doubled = ${doubled.value}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { defineComponent, ref } from 'vue';
|
||||
|
||||
// 计数器:点击按钮只改 count,文字由响应式自动更新
|
||||
export default defineComponent(() => {
|
||||
const count = ref(0);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[40, 36]}
|
||||
text="点击按钮改变 count"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[40, 96, 180, 56]}
|
||||
fillStyle="#2f6fed"
|
||||
onClick={() => count.value++}
|
||||
/>
|
||||
<text loc={[64, 114]} text="点我 +1" fillStyle="#e8f0ff" noevent />
|
||||
|
||||
<text
|
||||
loc={[260, 112]}
|
||||
text={`count = ${count.value}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { defineComponent, reactive } from 'vue';
|
||||
|
||||
// reactive 代理对象:逐字段修改,标题与开关一起更新
|
||||
export default defineComponent(() => {
|
||||
const panel = reactive({
|
||||
open: true,
|
||||
title: '背包'
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[40, 36]}
|
||||
text="点击切换 open 与 title"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[40, 96, 180, 56]}
|
||||
fillStyle="#2f6fed"
|
||||
onClick={() => {
|
||||
panel.open = !panel.open;
|
||||
panel.title = panel.open ? '背包' : '道具';
|
||||
}}
|
||||
/>
|
||||
<text loc={[64, 114]} text="切换" fillStyle="#e8f0ff" noevent />
|
||||
|
||||
<text
|
||||
loc={[260, 104]}
|
||||
text={`open = ${panel.open}`}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[260, 140]}
|
||||
text={`title = ${panel.title}`}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { defineComponent, ref, watch } from 'vue';
|
||||
|
||||
// watch 监听 count,变化后把新旧值写进 log
|
||||
export default defineComponent(() => {
|
||||
const count = ref(0);
|
||||
const log = ref('还没有变化');
|
||||
|
||||
watch(count, (next, prev) => {
|
||||
log.value = `count:${prev} → ${next}`;
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[40, 36]}
|
||||
text="count 变化后 watch 写日志"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
|
||||
<g-rect
|
||||
loc={[40, 96, 180, 56]}
|
||||
fillStyle="#2f6fed"
|
||||
onClick={() => count.value++}
|
||||
/>
|
||||
<text loc={[64, 114]} text="点我 +1" fillStyle="#e8f0ff" noevent />
|
||||
|
||||
<text
|
||||
loc={[260, 112]}
|
||||
text={`log = ${log.value}`}
|
||||
fillStyle="#7ee787"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 八种图形放在一起:矩形、圆、椭圆、直线、三次贝塞尔、二次贝塞尔、路径、圆角矩形
|
||||
export default defineComponent(() => {
|
||||
const badge = new Path2D();
|
||||
badge.moveTo(300, 205);
|
||||
badge.lineTo(345, 230);
|
||||
badge.lineTo(333, 280);
|
||||
badge.lineTo(267, 280);
|
||||
badge.lineTo(255, 230);
|
||||
badge.closePath();
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
<text loc={[24, 14]} text="八种图形" fillStyle="#e8f0ff" />
|
||||
|
||||
<g-rect loc={[20, 72, 80, 62]} fillStyle="#2f6fed" />
|
||||
<g-circle circle={[180, 103, 34]} fillStyle="#58c4dd" />
|
||||
<g-ellipse ellipse={[300, 103, 50, 32]} fillStyle="#c58af9" />
|
||||
<g-line
|
||||
line={[370, 133, 460, 75]}
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={4}
|
||||
/>
|
||||
|
||||
<g-bezier
|
||||
curve={[20, 275, 45, 205, 80, 280, 105, 212]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-quad
|
||||
curve={[140, 270, 180, 205, 220, 270]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-path path={badge} fillStyle="#7ee787" />
|
||||
<g-rectr
|
||||
loc={[370, 215, 80, 62]}
|
||||
circle={[24]}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text loc={[44, 146]} text="矩形" fillStyle="#cfe0ff" />
|
||||
<text loc={[170, 146]} text="圆" fillStyle="#cfe0ff" />
|
||||
<text loc={[285, 146]} text="椭圆" fillStyle="#cfe0ff" />
|
||||
<text loc={[405, 146]} text="直线" fillStyle="#cfe0ff" />
|
||||
|
||||
<text loc={[20, 292]} text="三次贝塞尔" fillStyle="#cfe0ff" />
|
||||
<text loc={[140, 292]} text="二次贝塞尔" fillStyle="#cfe0ff" />
|
||||
<text loc={[290, 292]} text="路径" fillStyle="#cfe0ff" />
|
||||
<text loc={[380, 292]} text="圆角矩形" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 三次贝塞尔:curve 依次是起点、两个控制点、终点
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="三次贝塞尔:白点 = 控制点,虚线 = 控制多边形"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
{/* 控制多边形:起点 → cp1 → cp2 → 终点 */}
|
||||
<g-line
|
||||
line={[30, 110, 70, 85]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[70, 85, 135, 90]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[135, 90, 205, 120]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[255, 110, 300, 60]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[300, 60, 395, 65]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[395, 65, 450, 120]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[30, 235, 125, 215]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[125, 215, 110, 255]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[110, 255, 205, 235]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[255, 235, 330, 205]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[330, 205, 380, 265]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[380, 265, 450, 235]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-bezier
|
||||
curve={[30, 110, 70, 85, 135, 90, 205, 120]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-bezier
|
||||
curve={[255, 110, 300, 60, 395, 65, 450, 120]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-bezier
|
||||
curve={[30, 235, 125, 215, 110, 255, 205, 235]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-bezier
|
||||
curve={[255, 235, 330, 205, 380, 265, 450, 235]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
/>
|
||||
|
||||
{/* 每个控制点用白色小圆点标出 */}
|
||||
<g-circle circle={[70, 85, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[135, 90, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[300, 60, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[395, 65, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[125, 215, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[110, 255, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[330, 205, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[380, 265, 4]} fillStyle="#ffffff" />
|
||||
|
||||
<text loc={[30, 145]} text="控制点靠近" fillStyle="#cfe0ff" />
|
||||
<text loc={[270, 145]} text="控制点拉远" fillStyle="#cfe0ff" />
|
||||
<text loc={[30, 292]} text="控制点交叉" fillStyle="#cfe0ff" />
|
||||
<text loc={[270, 292]} text="S 形" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 圆:用 circle 简写给出圆心与半径
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="圆:半径、圆心与 start / end"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-circle circle={[80, 100, 30]} fillStyle="#58c4dd" />
|
||||
<g-circle circle={[210, 100, 55]} fillStyle="#58c4dd" />
|
||||
<g-circle circle={[350, 100, 80]} fillStyle="#58c4dd" />
|
||||
|
||||
<text loc={[65, 190]} text="r=30" fillStyle="#cfe0ff" />
|
||||
<text loc={[188, 190]} text="r=55" fillStyle="#cfe0ff" />
|
||||
<text loc={[323, 190]} text="r=80" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-circle circle={[90, 255, 35, 0, Math.PI]} fillStyle="#58c4dd" />
|
||||
<g-circle
|
||||
circle={[240, 255, 35, Math.PI / 2, Math.PI * 1.5]}
|
||||
stroke
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-circle
|
||||
circle={[390, 255, 35, 0, Math.PI * 1.5]}
|
||||
fill
|
||||
stroke
|
||||
fillStyle="#58c4dd"
|
||||
strokeStyle="#e8f0ff"
|
||||
lineWidth={3}
|
||||
/>
|
||||
|
||||
<text loc={[15, 298]} text="fill 0°→180°" fillStyle="#cfe0ff" />
|
||||
<text loc={[155, 298]} text="stroke 90°→270°" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[310, 298]}
|
||||
text="fill+stroke 0°→270°"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 椭圆:用 ellipse 简写给出圆心、横向半径与纵向半径
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="椭圆:rx / ry 与 start / end"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-ellipse ellipse={[100, 95, 70, 30]} fillStyle="#c58af9" />
|
||||
<g-ellipse ellipse={[250, 95, 90, 42]} fillStyle="#c58af9" />
|
||||
<g-ellipse ellipse={[400, 95, 55, 25]} fillStyle="#c58af9" />
|
||||
|
||||
<text loc={[55, 150]} text="rx=70 ry=30" fillStyle="#cfe0ff" />
|
||||
<text loc={[200, 150]} text="rx=90 ry=42" fillStyle="#cfe0ff" />
|
||||
<text loc={[355, 150]} text="rx=55 ry=25" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-ellipse
|
||||
ellipse={[90, 230, 60, 35, 0, Math.PI]}
|
||||
fillStyle="#c58af9"
|
||||
/>
|
||||
<g-ellipse
|
||||
ellipse={[240, 230, 60, 35, Math.PI, Math.PI * 2]}
|
||||
stroke
|
||||
strokeStyle="#c58af9"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-ellipse
|
||||
ellipse={[390, 230, 60, 35, 0, Math.PI]}
|
||||
fill
|
||||
stroke
|
||||
fillStyle="#c58af9"
|
||||
strokeStyle="#e8f0ff"
|
||||
lineWidth={3}
|
||||
/>
|
||||
|
||||
<text loc={[30, 298]} text="fill 0°→180°" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[172, 298]}
|
||||
text="stroke 180°→360°"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text
|
||||
loc={[320, 298]}
|
||||
text="fill+stroke 0°→180°"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 直线:用 line 简写给出两个端点,默认描边
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="直线:端点、线宽、虚线与颜色"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[40, 80, 210, 80]}
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={2}
|
||||
/>
|
||||
<g-line
|
||||
line={[260, 80, 440, 80]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={14}
|
||||
/>
|
||||
<g-line
|
||||
line={[40, 185, 210, 185]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={4}
|
||||
lineDash={[18, 12]}
|
||||
/>
|
||||
<g-line
|
||||
line={[260, 240, 440, 170]}
|
||||
strokeStyle="#c58af9"
|
||||
lineWidth={6}
|
||||
/>
|
||||
|
||||
<text loc={[40, 96]} text="细 lineWidth=2" fillStyle="#cfe0ff" />
|
||||
<text loc={[260, 110]} text="粗 lineWidth=14" fillStyle="#cfe0ff" />
|
||||
<text loc={[40, 203]} text="虚线 lineDash" fillStyle="#cfe0ff" />
|
||||
<text loc={[260, 254]} text="斜线,改端点" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 绘制模式:同一个矩形分别用 fill、stroke、fill + stroke 与 strokeAndFill
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 16]} text="四种绘制模式" fillStyle="#e8f0ff" />
|
||||
|
||||
<text loc={[40, 48]} text="fill" fillStyle="#cfe0ff" />
|
||||
<text loc={[260, 48]} text="stroke" fillStyle="#cfe0ff" />
|
||||
<text loc={[40, 180]} text="fill + stroke" fillStyle="#cfe0ff" />
|
||||
<text loc={[260, 180]} text="strokeAndFill" fillStyle="#cfe0ff" />
|
||||
|
||||
<g-rect loc={[40, 84, 180, 80]} fillStyle="#2f6fed" />
|
||||
<g-rect
|
||||
loc={[260, 84, 180, 80]}
|
||||
stroke
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={10}
|
||||
/>
|
||||
<g-rect
|
||||
loc={[40, 218, 180, 80]}
|
||||
fill
|
||||
stroke
|
||||
fillStyle="#2f6fed"
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={14}
|
||||
/>
|
||||
<g-rect
|
||||
loc={[260, 218, 180, 80]}
|
||||
strokeAndFill
|
||||
fillStyle="#2f6fed"
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={14}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 路径:path 接收一个 Path2D,默认填充
|
||||
export default defineComponent(() => {
|
||||
const polyline = new Path2D();
|
||||
polyline.moveTo(40, 230);
|
||||
polyline.lineTo(85, 140);
|
||||
polyline.lineTo(125, 215);
|
||||
polyline.lineTo(160, 150);
|
||||
|
||||
const polygon = new Path2D();
|
||||
polygon.moveTo(195, 230);
|
||||
polygon.lineTo(285, 230);
|
||||
polygon.lineTo(240, 135);
|
||||
polygon.closePath();
|
||||
|
||||
const arc = new Path2D();
|
||||
arc.moveTo(455, 180);
|
||||
arc.arc(390, 180, 65, 0, Math.PI * 1.5);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="路径:折线、闭合填充与圆弧"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-path
|
||||
path={polyline}
|
||||
stroke
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={3}
|
||||
/>
|
||||
<g-path path={polygon} fillStyle="#7ee787" />
|
||||
<g-path path={arc} stroke strokeStyle="#f2b84b" lineWidth={3} />
|
||||
|
||||
<text loc={[50, 285]} text="折线" fillStyle="#cfe0ff" />
|
||||
<text loc={[205, 285]} text="闭合填充" fillStyle="#cfe0ff" />
|
||||
<text loc={[374, 285]} text="圆弧" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 二次贝塞尔:curve 依次是起点、控制点、终点
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="二次贝塞尔:白点 = 控制点,虚线 = 控制多边形"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
{/* 控制多边形:起点 → 控制点 → 终点 */}
|
||||
<g-line
|
||||
line={[30, 110, 120, 65]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[120, 65, 210, 110]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[255, 110, 345, 155]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[345, 155, 450, 110]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[100, 270, 85, 250]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[85, 250, 210, 270]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-line
|
||||
line={[260, 270, 395, 250]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
<g-line
|
||||
line={[395, 250, 370, 270]}
|
||||
strokeStyle="#8a94a6"
|
||||
lineWidth={1}
|
||||
lineDash={[4, 4]}
|
||||
/>
|
||||
|
||||
<g-quad
|
||||
curve={[30, 110, 120, 65, 210, 110]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-quad
|
||||
curve={[255, 110, 345, 155, 450, 110]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-quad
|
||||
curve={[100, 270, 85, 250, 210, 270]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
<g-quad
|
||||
curve={[260, 270, 395, 250, 370, 270]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={4}
|
||||
/>
|
||||
|
||||
{/* 每个控制点用白色小圆点标出 */}
|
||||
<g-circle circle={[120, 65, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[345, 155, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[85, 250, 4]} fillStyle="#ffffff" />
|
||||
<g-circle circle={[395, 250, 4]} fillStyle="#ffffff" />
|
||||
|
||||
<text loc={[30, 145]} text="控制点在上" fillStyle="#cfe0ff" />
|
||||
<text loc={[270, 145]} text="控制点在下" fillStyle="#cfe0ff" />
|
||||
<text loc={[30, 292]} text="控制点在左" fillStyle="#cfe0ff" />
|
||||
<text loc={[270, 292]} text="控制点在右" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 矩形:用 loc 简写给出左上角与宽高
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="矩形:改 loc 对比位置与宽高"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-rect loc={[50, 70, 70, 55]} fillStyle="#2f6fed" />
|
||||
<g-rect loc={[250, 70, 170, 55]} fillStyle="#2f6fed" />
|
||||
<g-rect loc={[50, 170, 70, 95]} fillStyle="#2f6fed" />
|
||||
<g-rect loc={[250, 170, 170, 95]} fillStyle="#2f6fed" />
|
||||
|
||||
<text loc={[50, 135]} text="小 70×55" fillStyle="#cfe0ff" />
|
||||
<text loc={[250, 135]} text="宽 170×55" fillStyle="#cfe0ff" />
|
||||
<text loc={[50, 275]} text="高 70×95" fillStyle="#cfe0ff" />
|
||||
<text loc={[250, 275]} text="大 170×95" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 圆角矩形:circle 给圆形圆角,ellipse 给椭圆圆角
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 20]}
|
||||
text="圆角矩形:不同圆角写法对比"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
<g-rectr
|
||||
loc={[60, 60, 130, 90]}
|
||||
circle={[24]}
|
||||
fillStyle="#2f6fed"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[290, 60, 130, 90]}
|
||||
circle={[36, 0, 36, 0]}
|
||||
fillStyle="#58c4dd"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[60, 192, 130, 90]}
|
||||
ellipse={[50, 24]}
|
||||
fillStyle="#c58af9"
|
||||
/>
|
||||
<g-rectr
|
||||
loc={[290, 192, 130, 90]}
|
||||
circle={[90]}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
|
||||
<text loc={[60, 163]} text="1 项 circle=[24]" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[270, 163]}
|
||||
text="4 项 [36,0,36,0]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
<text loc={[60, 294]} text="ellipse=[50,24]" fillStyle="#cfe0ff" />
|
||||
<text
|
||||
loc={[290, 294]}
|
||||
text="大半径 circle=[90]"
|
||||
fillStyle="#cfe0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,127 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 描边:线宽、虚线、端点与拐角四组对比
|
||||
export default defineComponent(() => {
|
||||
const joinMiter = new Path2D();
|
||||
joinMiter.moveTo(275, 210);
|
||||
joinMiter.lineTo(300, 268);
|
||||
joinMiter.lineTo(325, 210);
|
||||
|
||||
const joinRound = new Path2D();
|
||||
joinRound.moveTo(345, 210);
|
||||
joinRound.lineTo(370, 268);
|
||||
joinRound.lineTo(395, 210);
|
||||
|
||||
const joinBevel = new Path2D();
|
||||
joinBevel.moveTo(415, 210);
|
||||
joinBevel.lineTo(440, 268);
|
||||
joinBevel.lineTo(465, 210);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[24, 16]}
|
||||
text="描边:线宽、虚线、端点与拐角"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
|
||||
{/* 线宽 */}
|
||||
<text loc={[30, 48]} text="线宽 lineWidth" fillStyle="#cfe0ff" />
|
||||
<g-line
|
||||
line={[30, 80, 200, 80]}
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={2}
|
||||
/>
|
||||
<g-line
|
||||
line={[30, 110, 200, 110]}
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={6}
|
||||
/>
|
||||
<g-line
|
||||
line={[30, 140, 200, 140]}
|
||||
strokeStyle="#7ee787"
|
||||
lineWidth={12}
|
||||
/>
|
||||
<text loc={[214, 72]} text="2" fillStyle="#cfe0ff" />
|
||||
<text loc={[214, 102]} text="6" fillStyle="#cfe0ff" />
|
||||
<text loc={[214, 132]} text="12" fillStyle="#cfe0ff" />
|
||||
|
||||
{/* 虚线 */}
|
||||
<text loc={[240, 48]} text="虚线 lineDash" fillStyle="#cfe0ff" />
|
||||
<g-line
|
||||
line={[340, 80, 456, 80]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={3}
|
||||
lineDash={[8, 6]}
|
||||
/>
|
||||
<g-line
|
||||
line={[340, 110, 456, 110]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={3}
|
||||
lineDash={[2, 6]}
|
||||
/>
|
||||
<g-line
|
||||
line={[340, 140, 456, 140]}
|
||||
strokeStyle="#58c4dd"
|
||||
lineWidth={3}
|
||||
lineDash={[16, 6, 2, 6]}
|
||||
/>
|
||||
<text loc={[240, 72]} text="8,6" fillStyle="#cfe0ff" />
|
||||
<text loc={[240, 102]} text="2,6" fillStyle="#cfe0ff" />
|
||||
<text loc={[240, 132]} text="16,6,2,6" fillStyle="#cfe0ff" />
|
||||
|
||||
{/* 端点 */}
|
||||
<text loc={[30, 168]} text="端点 lineCap" fillStyle="#cfe0ff" />
|
||||
<g-line
|
||||
line={[105, 210, 210, 210]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={16}
|
||||
lineCap="butt"
|
||||
/>
|
||||
<g-line
|
||||
line={[105, 245, 210, 245]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={16}
|
||||
lineCap="round"
|
||||
/>
|
||||
<g-line
|
||||
line={[105, 280, 210, 280]}
|
||||
strokeStyle="#f2b84b"
|
||||
lineWidth={16}
|
||||
lineCap="square"
|
||||
/>
|
||||
<text loc={[30, 202]} text="butt" fillStyle="#cfe0ff" />
|
||||
<text loc={[30, 237]} text="round" fillStyle="#cfe0ff" />
|
||||
<text loc={[30, 272]} text="square" fillStyle="#cfe0ff" />
|
||||
|
||||
{/* 拐角 */}
|
||||
<text loc={[260, 168]} text="拐角 lineJoin" fillStyle="#cfe0ff" />
|
||||
<g-path
|
||||
path={joinMiter}
|
||||
stroke
|
||||
strokeStyle="#c58af9"
|
||||
lineWidth={14}
|
||||
lineJoin="miter"
|
||||
/>
|
||||
<g-path
|
||||
path={joinRound}
|
||||
stroke
|
||||
strokeStyle="#c58af9"
|
||||
lineWidth={14}
|
||||
lineJoin="round"
|
||||
/>
|
||||
<g-path
|
||||
path={joinBevel}
|
||||
stroke
|
||||
strokeStyle="#c58af9"
|
||||
lineWidth={14}
|
||||
lineJoin="bevel"
|
||||
/>
|
||||
<text loc={[277, 296]} text="miter" fillStyle="#cfe0ff" />
|
||||
<text loc={[347, 296]} text="round" fillStyle="#cfe0ff" />
|
||||
<text loc={[417, 296]} text="bevel" fillStyle="#cfe0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 静态界面演示:容器、背景、标题、矩形、圆、分隔线与层级
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
{/* 深色底:用一块和容器等大的矩形铺出来 */}
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
{/* 标题 */}
|
||||
<text loc={[24, 24]} text="背包" fillStyle="#e8f0ff" />
|
||||
|
||||
{/* 矩形与圆 */}
|
||||
<g-rect loc={[24, 64, 160, 64]} fillStyle="#2f6fed" />
|
||||
<g-circle circle={[280, 96, 32]} fillStyle="#58c4dd" />
|
||||
|
||||
{/* 分隔线 */}
|
||||
<g-line line={[24, 168, 456, 168]} strokeStyle="#7aa7ff" />
|
||||
|
||||
{/* 层级:第一个 zIndex 更大,重叠处在上面 */}
|
||||
<g-rect loc={[320, 120, 120, 72]} fillStyle="#f2b84b" zIndex={2} />
|
||||
<g-rect loc={[368, 152, 120, 72]} fillStyle="#58c4dd" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,208 @@
|
||||
import { computed, defineComponent, onUnmounted } from 'vue';
|
||||
import {
|
||||
GameUI,
|
||||
IUIDefaultPropsBase,
|
||||
IUIInstance,
|
||||
UIController
|
||||
} from '@motajs/system';
|
||||
|
||||
// 策略演示:四个面板分别放在左右两个控制器里
|
||||
interface PanelLoc {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
color: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
// 生成一个固定位置的演示面板,两个策略各用两份
|
||||
const makePanel = (options: PanelLoc) => {
|
||||
const { x, y, width, height, color, label } = options;
|
||||
return defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[x, y, width, height]}>
|
||||
<g-rect loc={[0, 0, width, height]} fill fillStyle={color} />
|
||||
<g-rect
|
||||
loc={[0, 0, width, height]}
|
||||
stroke
|
||||
strokeStyle="#ffffff"
|
||||
/>
|
||||
<text loc={[12, 12]} text={label} fillStyle="#ffffff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// 每个面板都是一个独立 GameUI,打开时按策略压栈
|
||||
const leftBottomUI = new GameUI(
|
||||
'strategy-left-bottom',
|
||||
makePanel({
|
||||
x: 20,
|
||||
y: 110,
|
||||
width: 200,
|
||||
height: 130,
|
||||
color: '#2f6fed',
|
||||
label: '底层 A'
|
||||
})
|
||||
);
|
||||
const leftTopUI = new GameUI(
|
||||
'strategy-left-top',
|
||||
makePanel({
|
||||
x: 70,
|
||||
y: 160,
|
||||
width: 200,
|
||||
height: 130,
|
||||
color: '#3a2f5f',
|
||||
label: '上层 B'
|
||||
})
|
||||
);
|
||||
const rightBottomUI = new GameUI(
|
||||
'strategy-right-bottom',
|
||||
makePanel({
|
||||
x: 260,
|
||||
y: 110,
|
||||
width: 200,
|
||||
height: 130,
|
||||
color: '#2f6fed',
|
||||
label: '底层 A'
|
||||
})
|
||||
);
|
||||
const rightTopUI = new GameUI(
|
||||
'strategy-right-top',
|
||||
makePanel({
|
||||
x: 310,
|
||||
y: 160,
|
||||
width: 200,
|
||||
height: 130,
|
||||
color: '#3a2f5f',
|
||||
label: '上层 B'
|
||||
})
|
||||
);
|
||||
|
||||
const leftController = new UIController<IUIDefaultPropsBase>();
|
||||
leftController.setPropsBaseCreator((instance, ctrl) => ({
|
||||
instance,
|
||||
controller: ctrl
|
||||
}));
|
||||
// 左边:只显示最后一个
|
||||
leftController.lastOnly();
|
||||
|
||||
const rightController = new UIController<IUIDefaultPropsBase>();
|
||||
rightController.setPropsBaseCreator((instance, ctrl) => ({
|
||||
instance,
|
||||
controller: ctrl
|
||||
}));
|
||||
// 右边:显示全部
|
||||
rightController.showAll();
|
||||
|
||||
const LeftRenderer = defineComponent(() => {
|
||||
return () => leftController.render();
|
||||
});
|
||||
|
||||
const RightRenderer = defineComponent(() => {
|
||||
return () => rightController.render();
|
||||
});
|
||||
|
||||
export default defineComponent(() => {
|
||||
let leftBottom: IUIInstance | null = null;
|
||||
let leftTop: IUIInstance | null = null;
|
||||
let rightBottom: IUIInstance | null = null;
|
||||
let rightTop: IUIInstance | null = null;
|
||||
|
||||
const leftCount = computed(() => leftController.stack.length);
|
||||
const rightCount = computed(() => rightController.stack.length);
|
||||
|
||||
// 打开与关闭都直接调用对应控制器
|
||||
const openLeftBottom = () => {
|
||||
if (!leftBottom) {
|
||||
leftBottom = leftController.open(leftBottomUI, {});
|
||||
}
|
||||
};
|
||||
const openLeftTop = () => {
|
||||
if (!leftTop) {
|
||||
leftTop = leftController.open(leftTopUI, {});
|
||||
}
|
||||
};
|
||||
const openRightBottom = () => {
|
||||
if (!rightBottom) {
|
||||
rightBottom = rightController.open(rightBottomUI, {});
|
||||
}
|
||||
};
|
||||
const openRightTop = () => {
|
||||
if (!rightTop) {
|
||||
rightTop = rightController.open(rightTopUI, {});
|
||||
}
|
||||
};
|
||||
|
||||
const closeLeft = () => {
|
||||
leftController.closeAll();
|
||||
leftBottom = null;
|
||||
leftTop = null;
|
||||
};
|
||||
const closeRight = () => {
|
||||
rightController.closeAll();
|
||||
rightBottom = null;
|
||||
rightTop = null;
|
||||
};
|
||||
|
||||
onUnmounted(() => {
|
||||
leftController.closeAll();
|
||||
rightController.closeAll();
|
||||
});
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fill fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[20, 16]}
|
||||
text="只显示最后一个(lastOnly)"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<container loc={[20, 44, 80, 32]} onClick={openLeftBottom}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#2f6fed" />
|
||||
<text loc={[12, 8]} text="打开 A" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[110, 44, 80, 32]} onClick={openLeftTop}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#3a2f5f" />
|
||||
<text loc={[12, 8]} text="打开 B" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[200, 44, 80, 32]} onClick={closeLeft}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#7a3d3d" />
|
||||
<text loc={[12, 8]} text="清空" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<text
|
||||
loc={[20, 84]}
|
||||
text={`栈长度:${leftCount.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<text
|
||||
loc={[260, 16]}
|
||||
text="显示全部(showAll)"
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
<container loc={[260, 44, 80, 32]} onClick={openRightBottom}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#2f6fed" />
|
||||
<text loc={[12, 8]} text="打开 A" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[350, 44, 80, 32]} onClick={openRightTop}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#3a2f5f" />
|
||||
<text loc={[12, 8]} text="打开 B" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<container loc={[260, 76, 80, 32]} onClick={closeRight}>
|
||||
<g-rect loc={[0, 0, 80, 32]} fill fillStyle="#7a3d3d" />
|
||||
<text loc={[12, 8]} text="清空" fillStyle="#ffffff" />
|
||||
</container>
|
||||
<text
|
||||
loc={[360, 84]}
|
||||
text={`栈长度:${rightCount.value}`}
|
||||
fillStyle="#8fb6ff"
|
||||
/>
|
||||
|
||||
<LeftRenderer />
|
||||
<RightRenderer />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 最小 UI 模板的 live 版本:一个背景矩形加一行文字
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
{/* 背景矩形先铺满整块画布,避免文字落在透明区域 */}
|
||||
<g-rect loc={[0, 0, 480, 320]} fill fillStyle="#1d2b45" />
|
||||
<text loc={[24, 24]} text="我的第一个 UI" fillStyle="#e8f0ff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 三行文字分别用不同锚点,白点标出 loc 的位置
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[24, 20]} text="白点 = loc 的位置" fillStyle="#cfe0ff" />
|
||||
|
||||
{/* anc=[0, 0]:loc 是文字左上角 */}
|
||||
<text loc={[40, 56]} text="anc=[0, 0]" fillStyle="#8b98a8" />
|
||||
<text
|
||||
loc={[160, 60]}
|
||||
anc={[0, 0]}
|
||||
text="左对齐"
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<g-circle circle={[160, 60, 5]} fillStyle="#ffffff" />
|
||||
|
||||
{/* anc=[0.5, 0.5]:loc 是文字中心 */}
|
||||
<text loc={[40, 146]} text="anc=[0.5, 0.5]" fillStyle="#8b98a8" />
|
||||
<text
|
||||
loc={[280, 150]}
|
||||
anc={[0.5, 0.5]}
|
||||
text="居中"
|
||||
fillStyle="#58c4dd"
|
||||
/>
|
||||
<g-circle circle={[280, 150, 5]} fillStyle="#ffffff" />
|
||||
|
||||
{/* anc=[1, 0]:loc 是文字右边缘 */}
|
||||
<text loc={[40, 246]} text="anc=[1, 0]" fillStyle="#8b98a8" />
|
||||
<text
|
||||
loc={[420, 250]}
|
||||
anc={[1, 0]}
|
||||
text="右对齐"
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<g-circle circle={[420, 250, 5]} fillStyle="#ffffff" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 最基本的一行文字:内容写在 text,位置由 loc 决定
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[40, 60]} text="一行文字" fillStyle="#ffffff" />
|
||||
<text loc={[40, 110]} text="第二行文字" fillStyle="#58c4dd" />
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Font, FontWeight } from '@motajs/render';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 同内容不同字号、字重与斜体的对照
|
||||
export default defineComponent(() => {
|
||||
const small = new Font('Verdana', 14, 'px', FontWeight.Normal);
|
||||
const bold = new Font('Verdana', 20, 'px', FontWeight.Bold);
|
||||
const italic = new Font('Verdana', 24, 'px', FontWeight.Normal, true);
|
||||
const title = new Font('Verdana', 32, 'px', FontWeight.Bold);
|
||||
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text
|
||||
loc={[32, 40]}
|
||||
text="14px 常规文字"
|
||||
font={small}
|
||||
fillStyle="#ffffff"
|
||||
/>
|
||||
<text
|
||||
loc={[32, 80]}
|
||||
text="20px 粗体文字"
|
||||
font={bold}
|
||||
fillStyle="#58c4dd"
|
||||
/>
|
||||
<text
|
||||
loc={[32, 124]}
|
||||
text="24px 斜体文字"
|
||||
font={italic}
|
||||
fillStyle="#f2b84b"
|
||||
/>
|
||||
<text
|
||||
loc={[32, 176]}
|
||||
text="32px 粗体标题"
|
||||
font={title}
|
||||
fillStyle="#e8f0ff"
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
// 填充、描边颜色与描边宽度的对照
|
||||
export default defineComponent(() => {
|
||||
return () => (
|
||||
<container loc={[0, 0, 480, 320]}>
|
||||
<g-rect loc={[0, 0, 480, 320]} fillStyle="#141a22" />
|
||||
|
||||
<text loc={[32, 50]} text="白色填充" fillStyle="#ffffff" />
|
||||
<text loc={[32, 104]} text="蓝色填充" fillStyle="#58c4dd" />
|
||||
<text
|
||||
loc={[32, 158]}
|
||||
text="填充 + 描边 2"
|
||||
fillStyle="#ffffff"
|
||||
strokeStyle="#f2b84b"
|
||||
strokeWidth={2}
|
||||
/>
|
||||
<text
|
||||
loc={[32, 222]}
|
||||
text="填充 + 描边 6"
|
||||
fillStyle="#ffffff"
|
||||
strokeStyle="#c58af9"
|
||||
strokeWidth={6}
|
||||
/>
|
||||
</container>
|
||||
);
|
||||
});
|
||||
@@ -1,6 +1,16 @@
|
||||
<template>
|
||||
<div :style="{ background: props.background, overflow: 'hidden' }">
|
||||
<canvas ref="canvasRef"></canvas>
|
||||
<div
|
||||
:style="{
|
||||
background: props.background,
|
||||
overflow: 'hidden',
|
||||
display: 'flex',
|
||||
justifyContent: 'center'
|
||||
}"
|
||||
>
|
||||
<canvas
|
||||
ref="canvasRef"
|
||||
:style="{ display: 'block', flex: 'none' }"
|
||||
></canvas>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -82,3 +92,14 @@ onUnmounted(() => {
|
||||
dispose = null;
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.motajs-input-element {
|
||||
left: 0;
|
||||
top: 0;
|
||||
position: fixed;
|
||||
border: none;
|
||||
z-index: 1000;
|
||||
background-color: transparent;
|
||||
}
|
||||
</style>
|
||||
@@ -12,6 +12,7 @@ const custom = [
|
||||
'shader',
|
||||
'comment',
|
||||
'custom-container',
|
||||
'container-custom',
|
||||
'map-render',
|
||||
'animate',
|
||||
'damage',
|
||||
@@ -252,6 +253,162 @@ export default defineConfig({
|
||||
{
|
||||
text: '怪物上下文与战斗深入',
|
||||
link: '/advanced/ii/enemy-context-and-combat'
|
||||
},
|
||||
{
|
||||
text: '自定义 UI 与渲染',
|
||||
collapsed: true,
|
||||
items: [
|
||||
{
|
||||
text: '起步',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: 'UI 通用模板(TSX)',
|
||||
link: '/advanced/ii/ui/ui-template'
|
||||
},
|
||||
{
|
||||
text: 'UI 入门',
|
||||
link: '/advanced/ii/ui/ui-basics'
|
||||
},
|
||||
{
|
||||
text: '第一个静态界面',
|
||||
link: '/advanced/ii/ui/first-static-ui'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '元素与布局',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: '容器与坐标',
|
||||
link: '/advanced/ii/ui/container-and-coordinates'
|
||||
},
|
||||
{
|
||||
text: '基础图形',
|
||||
link: '/advanced/ii/ui/basic-shapes'
|
||||
},
|
||||
{
|
||||
text: '文字',
|
||||
link: '/advanced/ii/ui/text'
|
||||
},
|
||||
{
|
||||
text: '图片与纹理',
|
||||
link: '/advanced/ii/ui/image-and-texture'
|
||||
},
|
||||
{
|
||||
text: '自绘元素与自定义容器',
|
||||
link: '/advanced/ii/ui/custom-and-container-custom'
|
||||
},
|
||||
{
|
||||
text: '通用参数',
|
||||
link: '/advanced/ii/ui/common-props'
|
||||
},
|
||||
{
|
||||
text: '层级、变换与性能',
|
||||
link: '/advanced/ii/ui/layer-transform-performance'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '交互与数据',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: '响应式',
|
||||
link: '/advanced/ii/ui/reactivity'
|
||||
},
|
||||
{
|
||||
text: '事件与交互',
|
||||
link: '/advanced/ii/ui/events'
|
||||
},
|
||||
{
|
||||
text: '条件渲染与循环渲染',
|
||||
link: '/advanced/ii/ui/conditional-and-list-rendering'
|
||||
},
|
||||
{
|
||||
text: '按键与快捷键',
|
||||
link: '/advanced/ii/ui/hotkey-in-ui'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '组件化',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: '什么是组件',
|
||||
link: '/advanced/ii/ui/what-is-component'
|
||||
},
|
||||
{
|
||||
text: '内置组件:基础',
|
||||
link: '/advanced/ii/ui/builtin-basic'
|
||||
},
|
||||
{
|
||||
text: '内置组件:布局',
|
||||
link: '/advanced/ii/ui/builtin-layout'
|
||||
},
|
||||
{
|
||||
text: '内置组件:文本',
|
||||
link: '/advanced/ii/ui/builtin-text'
|
||||
},
|
||||
{
|
||||
text: '自定义组件',
|
||||
link: '/advanced/ii/ui/custom-components'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: 'UI 系统',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: 'UI 系统',
|
||||
link: '/advanced/ii/ui/ui-system'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '性能',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: 'UI 性能',
|
||||
link: '/advanced/ii/ui/ui-performance'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
text: '完整示例',
|
||||
collapsed: false,
|
||||
items: [
|
||||
{
|
||||
text: '示例(一)目标与骨架',
|
||||
link: '/advanced/ii/ui/inventory-example-1-skeleton'
|
||||
},
|
||||
{
|
||||
text: '示例(二)道具网格',
|
||||
link: '/advanced/ii/ui/inventory-example-2-grid'
|
||||
},
|
||||
{
|
||||
text: '示例(三)详情区',
|
||||
link: '/advanced/ii/ui/inventory-example-3-detail'
|
||||
},
|
||||
{
|
||||
text: '示例(四)滚动与列表',
|
||||
link: '/advanced/ii/ui/inventory-example-4-scroll'
|
||||
},
|
||||
{
|
||||
text: '示例(五)交互与状态',
|
||||
link: '/advanced/ii/ui/inventory-example-5-state'
|
||||
},
|
||||
{
|
||||
text: '示例(六)动画、拆分与注册',
|
||||
link: '/advanced/ii/ui/inventory-example-6-animation-and-register'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user