mirror of
https://github.com/motajs/template.git
synced 2026-11-21 04:04:30 +08:00
5.9 KiB
5.9 KiB
第一个静态界面
这一篇从头拼一个静态界面:深色底、标题、一个图标、一个矩形、一个圆、一条分隔线,最后用两块重叠的矩形看层级。整屏 UI 用引擎给出的 FULL_LOC。每一步只加一种东西,最后得到一份可以直接运行的静态界面。
前置知识
阅读本篇前,建议先读完:
容器
界面从容器开始。容器决定这块 UI 占多大,也决定子元素的坐标从哪里算:
<container loc={FULL_LOC}>
<g-rect loc={FULL_LOC} fillStyle="#141a22" />
</container>
FULL_LOC是引擎给出的整屏位置;- 子元素的坐标相对这个容器的左上角,而不是相对整块画布;
container本身不画颜色,深色底用一块和它一样大的g-rect铺出来;- 图形默认填充,
fillStyle是填充颜色。
现在得到一块铺满整屏的深色区域,后面的元素都放进这个 container。
文字
在容器里加标题:
<text loc={[24, 24]} text="背包" fillStyle="#e8f0ff" />
text是文字内容,写在属性里,不要写成标签之间的裸文字;loc只写两个数:文字左上角相对容器左上角的位置;fillStyle是文字颜色。
矩形与圆
矩形用 g-rect,位置和大小都由 loc 给出:
<g-rect loc={[24, 64, 160, 64]} fillStyle="#2f6fed" />
前两个数是左上角,后两个数是宽和高。
圆用 g-circle,写成 circle={[圆心x, 圆心y, 半径]}:
<g-circle circle={[280, 96, 32]} fillStyle="#58c4dd" />
圆默认填充,fillStyle 给颜色。
直线
分隔线用 g-line,给出两个端点即可:
<g-line line={[24, 168, 456, 168]} strokeStyle="#7aa7ff" />
line的四个数是起点x/y和终点x/y;- 线默认描边,
strokeStyle是颜色。
这条线从 (24, 168) 画到 (456, 168),横在面板中间。
素材管理器中的图片
图片从素材管理器按别名取:
const icon = props.state.materials.getImageByAlias('bg.jpg');
<image image={icon} loc={[24, 184, 64, 64]} />;
props.state.materials是 ClientCore 提供的素材管理器,素材和别名由它统一维护;getImageByAlias('bg.jpg')按别名取一张已经登记好的图,返回纹理或null;- 取到的纹理直接交给
image的image属性,loc决定画在哪里、画多大。
相对坐标与层级
前面所有元素的坐标都相对根容器。把一组元素放进子容器时,坐标改成相对这个子容器:
<container loc={[240, 200, 216, 88]}>
<g-rect loc={[0, 0, 216, 88]} fillStyle="#1d2733" />
<text loc={[12, 12]} text="状态" fillStyle="#9fb6d1" />
</container>
子容器的 loc 是它在根容器里的位置;里面 g-rect 的 [0, 0] 是子容器的左上角,不是面板的左上角。移动子容器时,里面的元素整体跟着走。
同一父级里的兄弟按 zIndex 决定谁在上面:值大的画在上面。第二个矩形后写,但给第一个写 zIndex={2},重叠处就是第一个在上面:
<g-rect loc={[320, 120, 120, 72]} fillStyle="#f2b84b" zIndex={2} />
<g-rect loc={[368, 152, 120, 72]} fillStyle="#58c4dd" />
zIndex 只在同一个父级的兄弟之间比较;不写时按书写顺序叠放。缓存与命中的细节不在本篇展开。
完整模板
把上面的内容按顺序放进标准模板:
import { defineComponent } from 'vue';
import { GameUI, SetupComponentOptions } from '@motajs/system';
import { DefaultProps } from '@motajs/render-vue';
import { IGameUIPropsBase } from '@user/client-base';
import { FULL_LOC } from '@/client';
// 这里填写自定义的 UI 参数
interface MyPanelProps extends IGameUIPropsBase, DefaultProps {}
const myPanelProps = {
props: ['controller', 'instance', 'state']
} satisfies SetupComponentOptions<MyPanelProps>;
const MyPanel = defineComponent<MyPanelProps>(props => {
// 图片从素材管理器按别名取
const icon = props.state.materials.getImageByAlias('bg.jpg');
return () => (
<container loc={FULL_LOC}>
<g-rect loc={FULL_LOC} 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" />
<image image={icon} loc={[24, 184, 64, 64]} />
<container loc={[240, 200, 216, 88]}>
<g-rect loc={[0, 0, 216, 88]} fillStyle="#1d2733" />
<text loc={[12, 12]} text="状态" fillStyle="#9fb6d1" />
</container>
<g-rect loc={[320, 120, 120, 72]} fillStyle="#f2b84b" zIndex={2} />
<g-rect loc={[368, 152, 120, 72]} fillStyle="#58c4dd" />
</container>
);
}, myPanelProps);
export const MyPanelUI = new GameUI('my-panel', MyPanel);
FULL_LOC 是整块画布的位置;在已有 UI 中使用 props.controller.open(MyPanelUI, {}) 打开后,看到的就是这块面板。