mirror of
https://github.com/motajs/template.git
synced 2026-11-24 14:54:30 +08:00
init: 2.B
This commit is contained in:
commit
6bf32174d8
610 files changed
+151444
No files matched your search
@@ -0,0 +1,2 @@
|
||||
export * from './misc';
|
||||
export * from './storage';
|
||||
@@ -0,0 +1,106 @@
|
||||
<template>
|
||||
<div class="keyboard-container">
|
||||
<div
|
||||
v-for="(key, i) of keyboard.keys"
|
||||
class="keyboard-item"
|
||||
@click="keyboard.emitKey(key, i)"
|
||||
:active="checkAssist(assist, key.key)"
|
||||
:style="{
|
||||
left: `${key.x}px`,
|
||||
top: `${key.y}px`,
|
||||
width: `${key.width}px`,
|
||||
height: `${key.height}px`
|
||||
}"
|
||||
>
|
||||
<span
|
||||
class="keyboard-key button-text"
|
||||
v-html="key.text ?? KeyCodeUtils.toString(key.key)"
|
||||
></span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { checkAssist, Keyboard, KeyboardEmits } from '@motajs/system-action';
|
||||
import { KeyCodeUtils } from '@motajs/client-base';
|
||||
import { nextTick, onUnmounted, ref } from 'vue';
|
||||
|
||||
const props = defineProps<{
|
||||
keyboard: Keyboard;
|
||||
}>();
|
||||
|
||||
const assist = ref(props.keyboard.assist);
|
||||
const fontSize = `${props.keyboard.fontSize}px`;
|
||||
|
||||
const [width, height] = (() => {
|
||||
const key = props.keyboard;
|
||||
let mw = 0;
|
||||
let mh = 0;
|
||||
for (const k of key.keys) {
|
||||
if (k.x + k.width > mw) mw = k.x + k.width;
|
||||
if (k.y + k.height > mh) mh = k.y + k.height;
|
||||
}
|
||||
|
||||
return [`${mw}px`, `${mh}px`];
|
||||
})();
|
||||
|
||||
function onAssist() {
|
||||
nextTick(() => {
|
||||
assist.value = props.keyboard.assist;
|
||||
});
|
||||
}
|
||||
props.keyboard.on('emit', onAssist);
|
||||
|
||||
const emits = defineEmits<{
|
||||
(e: 'keyup', data: KeyboardEmits): void;
|
||||
}>();
|
||||
|
||||
onUnmounted(() => {
|
||||
props.keyboard.off('emit', onAssist);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.keyboard-container {
|
||||
width: v-bind(width);
|
||||
height: v-bind(height);
|
||||
display: block;
|
||||
font-size: v-bind(fontSize);
|
||||
position: relative;
|
||||
font-family: Arial;
|
||||
}
|
||||
|
||||
.keyboard-item {
|
||||
position: absolute;
|
||||
background-color: #222;
|
||||
border: 1.5px solid #ddd8;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
transition: all 0.1s linear;
|
||||
cursor: pointer;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.keyboard-item:hover,
|
||||
.keyboard-item[active='true'] {
|
||||
background-color: #555;
|
||||
}
|
||||
|
||||
.keyboard-item[active='true']::v-deep(> *) {
|
||||
color: gold;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.keyboard-key {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
text-overflow: clip;
|
||||
text-wrap: nowrap;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Keyboard } from '@motajs/system-action';
|
||||
import KeyboardUI from './keyboard.vue';
|
||||
|
||||
interface VirtualKeyProps {
|
||||
keyboard: Keyboard;
|
||||
}
|
||||
|
||||
export function VirtualKey(props: VirtualKeyProps) {
|
||||
return (
|
||||
<KeyboardUI
|
||||
style="align-self: center"
|
||||
keyboard={props.keyboard}
|
||||
></KeyboardUI>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
|
||||
export class GameStorage<T extends object = any> {
|
||||
static list: GameStorage<any>[] = [];
|
||||
|
||||
key: string;
|
||||
data!: Partial<T>;
|
||||
|
||||
constructor(key: string) {
|
||||
this.key = key;
|
||||
this.read();
|
||||
GameStorage.list.push(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地存储读取
|
||||
*/
|
||||
read(): T {
|
||||
const data = localStorage.getItem(this.key) ?? '{}';
|
||||
return (this.data = JSON.parse(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入本地存储
|
||||
*/
|
||||
write() {
|
||||
localStorage.setItem(this.key, JSON.stringify(this.data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置存储的值
|
||||
* @param key 存储的名称
|
||||
* @param value 存储的值
|
||||
*/
|
||||
setValue<K extends keyof T>(key: K, value: T[K]): void;
|
||||
setValue(key: string, value: any): void;
|
||||
setValue<K extends keyof T>(key: K, value: T[K]) {
|
||||
this.data[key] = value;
|
||||
}
|
||||
|
||||
getValue<K extends keyof T>(key: K): T[K] | null;
|
||||
getValue<K extends keyof T>(key: K, defaults: T[K]): T[K];
|
||||
getValue<T>(key: string, defaults?: T): T;
|
||||
getValue<K extends keyof T>(key: K, defaults?: T[K]) {
|
||||
if (!isNil(this.data[key])) return this.data[key];
|
||||
else {
|
||||
if (defaults !== void 0) {
|
||||
this.data[key] = defaults;
|
||||
return defaults;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return JSON.stringify(this.data);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
keys() {
|
||||
return Object.keys(this.data);
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.values(this.data);
|
||||
}
|
||||
|
||||
entries() {
|
||||
return Object.entries(this.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本游戏的存储键
|
||||
* @param key 存储名称
|
||||
*/
|
||||
static fromGame(key: string) {
|
||||
return `HumanBreak_${key}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取与作者相关联的存储键
|
||||
* @param author 作者名称
|
||||
* @param key 存储名称
|
||||
*/
|
||||
static fromAuthor(author: string, key: string) {
|
||||
return `${author}@${key}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据存储键获取对应的存储实例
|
||||
* @param key 存储键
|
||||
* @example Storage.get(Storage.fromAuthor('AncTe', 'setting'));
|
||||
*/
|
||||
static get(key: string) {
|
||||
return this.list.find(v => v.key === key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 与Symbol.for类似
|
||||
*/
|
||||
static for(key: string) {
|
||||
const s = this.get(key);
|
||||
if (s) return s;
|
||||
else return new GameStorage(key);
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('beforeunload', () => {
|
||||
GameStorage.list.forEach(v => v.write());
|
||||
});
|
||||
window.addEventListener('blur', () => {
|
||||
GameStorage.list.forEach(v => v.write());
|
||||
});
|
||||
Reference in new issue
Block a user