ui控制系统

This commit is contained in:
AncTe committed 2022-11-14 17:11:23 +08:00
1 parent f3c96f7a07
commit da771dae91
23 files changed
+6559 -3014

No files matched your search

+9 -26
View File
@@ -1,39 +1,22 @@
<!-- 这里是vue的入口,在这里可以修改你的ui -->
<!-- 示例:显示一个响应式文字 -->
<template>
<div id="root">
<span @click="add" id="cnt"
>点我自增&nbsp;&nbsp;&nbsp;&nbsp;{{ cnt }}</span
>
<div id="ui">
<template v-for="com of uiStack">
<component :is="com"></component>
</template>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
const cnt = ref(0);
/**
* 点击后自增1
*/
function add() {
cnt.value++;
}
import { uiStack } from './plugin/uiController';
</script>
<style lang="less" scoped>
#root {
width: 100%;
#ui {
width: 90%;
height: 90%;
display: flex;
justify-content: center;
}
#cnt {
font-size: 3em;
text-align: center;
cursor: pointer;
color: white;
overflow: hidden;
}
</style>
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+154
View File
@@ -0,0 +1,154 @@
<template>
<div id="scroll">
<div>
<div :id="`content-${id}`" class="content">
<slot></slot>
</div>
</div>
<canvas :id="`scroll-${id}`" class="scroll"></canvas>
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUpdated } from 'vue';
import { useDrag, useWheel } from '../plugin/use';
let now = 0;
let total = 0;
const id = (1e8 * Math.random()).toFixed(0);
const scale = window.devicePixelRatio;
let ctx: CanvasRenderingContext2D;
let content: HTMLDivElement;
/** 绘制 */
function draw() {
if (total === 0) return;
if (now > total - ctx.canvas.height / scale) {
now = total - ctx.canvas.height / scale;
} else if (now < 0) {
now = 0;
}
const length =
Math.min(ctx.canvas.height / total / scale, 1) * ctx.canvas.height;
const py = (now / total) * ctx.canvas.height;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.beginPath();
ctx.moveTo(20, Math.max(py + 5, 5));
ctx.lineTo(20, Math.min(py + length - 5, ctx.canvas.height - 5));
ctx.lineCap = 'round';
ctx.lineWidth = 6;
ctx.strokeStyle = '#fff';
ctx.stroke();
}
/**
* 计算元素总长度
*/
function calHeight() {
const div = document.getElementById(`content-${id}`) as HTMLDivElement;
content = div;
const style = getComputedStyle(div);
total = parseFloat(style.height);
}
function scroll() {
draw();
content.style.top = `${-now}px`;
}
onUpdated(() => {
calHeight();
draw();
});
onMounted(() => {
calHeight();
const canvas = document.getElementById(`scroll-${id}`) as HTMLCanvasElement;
ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
const style = getComputedStyle(canvas);
canvas.width = 40 * scale;
canvas.height = parseFloat(style.height) * scale;
draw();
let lastY: number;
let contentLast: number;
// 绑定滚动条拖拽事件
useDrag(
canvas,
(x, y) => {
const dy = y - lastY;
lastY = y;
if (canvas.height < total * scale)
now += ((dy * total) / canvas.height) * scale;
content.style.transition = '';
scroll();
},
(x, y) => {
lastY = y;
},
true
);
// 绑定文本拖拽事件
useDrag(
content,
(x, y) => {
const dy = y - contentLast;
contentLast = y;
if (canvas.height < total * scale) now -= dy;
content.style.transition = '';
scroll();
},
(x, y) => {
contentLast = y;
},
true
);
useWheel(content, (x, y) => {
if (!core.domStyle.isVertical && Math.abs(y) > 50) {
content.style.transition = 'top 0.2s ease-out';
} else {
content.style.transition = '';
}
now += y;
scroll();
});
});
</script>
<style lang="less" scoped>
.scroll {
opacity: 0.2;
width: 40px;
height: 98%;
margin-top: 1%;
margin-bottom: 1%;
transition: opacity 0.2s linear;
}
.scroll:hover {
opacity: 0.4;
}
.scroll:active {
opacity: 0.6;
}
#scroll {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
flex-basis: content;
}
.content {
position: relative;
}
</style>
+3 -2
View File
@@ -1,9 +1,10 @@
// 需要引入所有的插件
import pop from './plugin/template';
import pop from './plugin/pop';
import ui from './plugin/uiController';
window.addEventListener('load', () => {
// 每个引入的插件都要在这里执行,否则不会被转发
const toForward = [pop()];
const toForward: any[] = [pop(), ui()];
// 初始化所有插件,并转发到core上
(async function () {
+1
View File
@@ -1,4 +1,5 @@
import { createApp } from 'vue';
import App from './App.vue';
import './styles.less';
createApp(App).mount('#root');
File renamed without changes.
+46
View File
@@ -0,0 +1,46 @@
import { Component, markRaw, ref, Ref, watch } from 'vue';
import Book from '../ui/book.vue';
import BookDetail from '../ui/bookDetail.vue';
export const bookOpened = ref(false);
export const bookDetail = ref(false);
let app: HTMLDivElement;
/** ui声明列表 */
const UI_LIST: [Ref<boolean>, Component][] = [
[bookOpened, Book],
[bookDetail, BookDetail]
];
/** ui栈 */
export const uiStack = ref<Component[]>([]);
export default function init() {
app = document.getElementById('root') as HTMLDivElement;
UI_LIST.forEach(([ref, com]) => {
watch(ref, n => {
if (n === true) {
uiStack.value.push(markRaw(com));
showApp();
} else {
const index = uiStack.value.findIndex(v => v === com);
uiStack.value.splice(index);
if (uiStack.value.length === 0) {
hideApp();
}
}
});
});
return { uiStack, bookDetail, bookOpened };
}
function showApp() {
app.style.display = 'flex';
core.lockControl();
}
function hideApp() {
app.style.display = 'none';
core.unlockControl();
}
+57
View File
@@ -0,0 +1,57 @@
export default function init() {
return { useDrag, useWheel };
}
/**
* 向一个元素添加拖拽事件
* @param ele 目标元素
* @param fn 推拽时触发的函数,传入x y和鼠标事件或点击事件
* @param ondown 鼠标按下时执行的函数
* @param global 是否全局拖拽,即拖拽后鼠标或手指离开元素后是否依然视为正在拖拽
*/
export function useDrag(
ele: HTMLElement,
fn: (x: number, y: number, e: MouseEvent | TouchEvent) => void,
ondown?: (x: number, y: number, e: MouseEvent | TouchEvent) => void,
global: boolean = false
) {
let down = false;
ele.addEventListener('mousedown', e => {
down = true;
if (ondown) ondown(e.clientX, e.clientY, e);
});
ele.addEventListener('touchstart', e => {
down = true;
if (ondown) ondown(e.touches[0].clientX, e.touches[0].clientY, e);
});
document.addEventListener('mouseup', () => (down = false));
document.addEventListener('touchend', () => (down = false));
const target = global ? document : ele;
target.addEventListener('mousemove', e => {
if (!down) return;
const ee = e as MouseEvent;
fn(ee.clientX, ee.clientY, ee);
});
target.addEventListener('touchmove', e => {
if (!down) return;
const ee = e as TouchEvent;
fn(ee.touches[0].clientX, ee.touches[0].clientY, ee);
});
}
/**
* 当触发滚轮时执行函数
* @param ele 目标元素
* @param fn 当滚轮触发时执行的函数
*/
export function useWheel(
ele: HTMLElement,
fn: (x: number, y: number, z: number, e: WheelEvent) => void
): void {
ele.addEventListener('wheel', e => {
fn(e.deltaX, e.deltaY, e.deltaZ, e);
});
}
+10
View File
@@ -0,0 +1,10 @@
#root {
position: absolute;
display: none;
width: 100%;
height: 100%;
z-index: 9999;
justify-content: center;
align-items: center;
overflow: hidden;
}
+7 -7
View File
@@ -1,4 +1,4 @@
type core = {
type Core = {
/** 地图的格子宽度 */
readonly _WIDTH_: number;
/** 地图的格子高度 */
@@ -186,10 +186,10 @@ type core = {
actions &
Forward<PluginDeclaration>;
type main = {
type Main = {
/** 是否在录像验证中 */
readonly replayChecking: boolean;
readonly core: core;
readonly core: Core;
readonly dom: { [key: string]: HTMLElement };
/** 游戏版本,发布后会被随机,请勿使用该属性 */
readonly version: string;
@@ -313,7 +313,7 @@ declare class Sprite {
removeEventListenr: HTMLCanvasElement['addEventListener'];
}
declare let main: main;
declare let core: core;
declare let flags: { [x: string]: any };
declare let hero: HeroStatus;
declare const main: main;
declare const core: Core;
declare const flags: { [x: string]: any };
declare const hero: HeroStatus;
+37
View File
@@ -1,5 +1,9 @@
// 这里包含所有插件导出的函数及变量声明,声明的函数会在类型标注中标注到core上
type Ref<T> = {
value: T;
};
interface PluginDeclaration {
/**
* 添加函数 例:添加弹出文字,像这个就可以使用core.addPop或core.plugin.addPop调用
@@ -11,6 +15,39 @@ interface PluginDeclaration {
/** 添加变量 例:所有的正在弹出的文字,像这个就可以使用core.plugin.pop获取 */
pop: any[];
/** 手册是否打开 */
readonly bookOpened: Ref<boolean>;
/** 手册详细信息 */
readonly bookDetail: Ref<boolean>;
/** ui栈 */
readonly uiStack: Ref<Component[]>;
/**
* 向一个元素添加拖拽事件
* @param ele 目标元素
* @param fn 推拽时触发的函数,传入x y和鼠标事件或点击事件
* @param ondown 鼠标按下时执行的函数
* @param global 是否全局拖拽,即拖拽后鼠标或手指离开元素后是否依然视为正在拖拽
*/
useDrag(
ele: HTMLElement,
fn: (x: number, y: number, e: MouseEvent | TouchEvent) => void,
ondown?: (x: number, y: number, e: MouseEvent | TouchEvent) => void,
global: boolean = false
): void;
/**
* 当触发滚轮时执行函数
* @param ele 目标元素
* @param fn 当滚轮触发时执行的函数
*/
useWheel(
ele: HTMLElement,
fn: (x: number, y: number, z: number, e: WheelEvent) => void
): void;
}
type Forward<T> = {
+20
View File
@@ -0,0 +1,20 @@
<!-- 怪物手册ui -->
<template>
<Scroll>
<div v-for="i of 100" class="test">test{{ i }}</div>
</Scroll>
</template>
<script setup lang="tsx">
import Scroll from '../components/scroll.vue';
const floorId = core.floorIds[core.status.event?.ui] ?? core.status.floorId;
const enemy = core.getCurrentEnemys(floorId);
</script>
<style lang="less" scoped>
.test {
color: white;
}
</style>
+7
View File
@@ -0,0 +1,7 @@
<!-- 怪物详细信息 -->
<template></template>
<script lang="tsx" setup></script>
<style lang="less" scoped></style>