feat: 弹幕编辑器

This commit is contained in:
AncTe committed 2024-04-23 21:43:26 +08:00
1 parent 36f0495b8e
commit ed3ef3c16a
13 files changed
+922 -39

No files matched your search

+53
View File
@@ -11,6 +11,17 @@ export const enum LogLevel {
ERROR
}
interface LoggerCatchInfo {
code?: number;
level: LogLevel;
message: string;
}
interface LoggerCatchReturns<T> {
ret: T;
info: LoggerCatchInfo[];
}
let logTip: HTMLSpanElement;
if (!main.replayChecking) {
const tip = document.createElement('span');
@@ -38,6 +49,9 @@ export class Logger {
level: LogLevel = LogLevel.LOG;
enabled: boolean = true;
private catching: boolean = false;
private catchedInfo: LoggerCatchInfo[] = [];
constructor(logLevel: LogLevel) {
this.level = logLevel;
}
@@ -56,6 +70,13 @@ export class Logger {
* @param text 错误信息
*/
error(code: number, text: string) {
if (this.catching) {
this.catchedInfo.push({
level: LogLevel.ERROR,
message: text,
code
});
}
if (this.level <= LogLevel.ERROR && this.enabled) {
console.error(`[ERROR Code ${code}] ${text}`);
if (!main.replayChecking) {
@@ -73,6 +94,13 @@ export class Logger {
* @param text 警告信息
*/
severe(code: number, text: string) {
if (this.catching) {
this.catchedInfo.push({
level: LogLevel.ERROR,
message: text,
code
});
}
if (this.level <= LogLevel.SEVERE_WARNING && this.enabled) {
console.warn(`[SEVERE WARNING Code ${code}] ${text}`);
if (!main.replayChecking) {
@@ -90,6 +118,13 @@ export class Logger {
* @param text 警告信息
*/
warn(code: number, text: string) {
if (this.catching) {
this.catchedInfo.push({
level: LogLevel.ERROR,
message: text,
code
});
}
if (this.level <= LogLevel.WARNING && this.enabled) {
console.warn(`[WARNING Code ${code}] ${text}`);
if (!main.replayChecking) {
@@ -106,11 +141,29 @@ export class Logger {
* @param text 日志信息
*/
log(text: string) {
if (this.catching) {
this.catchedInfo.push({
level: LogLevel.ERROR,
message: text
});
}
if (this.level <= LogLevel.LOG && this.enabled) {
console.log(`[LOG] ${text}`);
}
}
catch<T>(fn: () => T): LoggerCatchReturns<T> {
const before = this.enabled;
this.catchedInfo = [];
this.disable();
this.catching = true;
const ret = fn();
this.catching = false;
if (before) this.enable();
return { ret, info: this.catchedInfo };
}
disable() {
this.enabled = false;
}
+85 -14
View File
@@ -3,8 +3,8 @@ import { EmitableEvent, EventEmitter } from '@/core/common/eventEmitter';
import { logger } from '@/core/common/logger';
import { ResponseBase } from '@/core/interface';
import { deleteWith, ensureArray, parseCss, tip } from '@/plugin/utils';
import axios, { toFormData } from 'axios';
import { Component, VNode, h, ref, shallowReactive } from 'vue';
import axios, { AxiosResponse, toFormData } from 'axios';
import { Component, VNode, h, shallowReactive } from 'vue';
/* @__PURE__ */ import { id, password } from '../../../../user';
import { mainSetting } from '../setting';
@@ -49,17 +49,45 @@ interface DanmakuEvent extends EmitableEvent {
type SpecContentFn = (content: string, type: string) => VNode;
interface AllowedCSS {
property: string;
check: (value: string, prop: string) => true | string;
}
const allowedCSS: Partial<Record<CanParseCss, AllowedCSS>> = {
color: {
property: 'color',
check: () => true
},
backgroundColor: {
property: 'backgroundColor',
check: () => true
},
fontSize: {
property: 'fontSize',
check: value => {
if (!/^\d+%$/.test(value)) {
return '字体大小只能设置为百分格式';
}
if (parseInt(value) > 200) {
return '字体最大只能为200%';
}
return true;
}
}
};
export class Danmaku extends EventEmitter<DanmakuEvent> {
static num: number = 0;
static backend: string = `/backend/tower/barrage.php`;
static all: Set<Danmaku> = new Set();
static allInPos: Partial<Record<FloorIds, Record<LocString, Danmaku>>> = {};
static allInPos: Partial<Record<FloorIds, Record<LocString, Danmaku[]>>> =
{};
static showList: Danmaku[] = shallowReactive([]);
static showMap: Map<number, Danmaku> = new Map();
static specList: Record<string, SpecContentFn> = {};
num: number = Danmaku.num++;
static lastEditoredDanmaku?: Danmaku;
id: number = -1;
text: string = '';
@@ -82,10 +110,10 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
* 发送弹幕
* @returns 弹幕发送的 Axios Post 信息,为 Promise
*/
async post() {
async post(): Promise<AxiosResponse<PostDanmakuResponse>> {
if (this.posted || this.posting) {
logger.warn(5, `Repeat post danmaku.`);
return Promise.resolve();
return Promise.reject();
}
const data: DanmakuPostInfo = {
@@ -108,6 +136,14 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
this.id = res.data.id;
this.posting = false;
if (res.data.code === 0) {
this.posted = true;
tip('success', '发送成功');
this.addToList();
} else {
tip('error', res.data.message);
}
return res;
} catch (e) {
this.posted = false;
@@ -116,7 +152,7 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
3,
`Unexpected error when posting danmaku. Error info: ${e}`
);
return Promise.resolve();
return Promise.reject();
}
}
@@ -160,7 +196,7 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
if (!css.color) css.color = this.textColor;
if (!css.textShadow)
css.textShadow = `1px 1px 1px ${this.strokeColor}, 1px -1px 1px ${this.strokeColor}, -1px 1px 1px ${this.strokeColor}, -1px -1px 1px ${this.strokeColor}`;
return css;
return { ...css, ...this.style };
}
/**
@@ -195,9 +231,17 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
css(obj: CSSObj, overwrite?: boolean): void;
css(obj: string | CSSObj, overwrite: boolean = false) {
const res = typeof obj === 'string' ? parseCss(obj) : obj;
if (overwrite) this.style = res;
else {
this.style = { ...this.style, ...res };
const allow = Danmaku.checkCSSAllow(res);
if (allow.length === 0) {
if (overwrite) this.style = res;
else {
this.style = { ...this.style, ...res };
}
} else {
logger.error(
8,
`Post not allowed css danmaku. Allow info: ${allow.join(',')}`
);
}
}
@@ -208,7 +252,8 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
Danmaku.all.add(this);
if (!this.floor) return;
Danmaku.allInPos[this.floor] ??= {};
Danmaku.allInPos[this.floor]![`${this.x},${this.y}`] = this;
Danmaku.allInPos[this.floor]![`${this.x},${this.y}`] ??= [];
Danmaku.allInPos[this.floor]![`${this.x},${this.y}`].push(this);
}
/**
@@ -373,6 +418,27 @@ export class Danmaku extends EventEmitter<DanmakuEvent> {
return h('span');
}
/**
* 检查CSS内容是否符合发弹幕要求
* @param css 要检查的CSS内容
*/
static checkCSSAllow(css: CSSObj) {
const problem: string[] = [];
for (const [key, value] of Object.entries(css)) {
if (!allowedCSS[key as CanParseCss]) {
problem.push(`不允许的CSS:${key}`);
continue;
} else {
const res = allowedCSS[key as CanParseCss]!.check(value, key);
if (res !== true) {
problem.push(res);
}
}
}
return problem;
}
/**
* 拉取本塔所有弹幕
*/
@@ -431,6 +497,7 @@ Danmaku.registerSpecContent('i', content => {
return h(BoxAnimate as Component, {
id: content,
noborder: true,
noAnimate: true,
width: 32,
height: iconInfo.height
});
@@ -450,7 +517,11 @@ Mota.require('var', 'hook').on('moveOneStep', (x, y, floor) => {
if (f) {
const danmaku = f[`${x},${y}`];
if (danmaku) {
danmaku.show();
danmaku.forEach(v => {
setTimeout(() => {
v.show();
}, Math.random() * 1000);
});
}
}
});
+10 -1
View File
@@ -1,6 +1,6 @@
import { KeyCode } from '@/plugin/keyCodes';
import { Hotkey, HotkeyJSON } from '../custom/hotkey';
import { generateBinary, keycode } from '@/plugin/utils';
import { generateBinary, keycode, openDanmakuPoster } from '@/plugin/utils';
import { hovered } from './fixed';
import { hasMarkedEnemy, markEnemy, unmarkEnemy } from '@/plugin/mark';
import { mainUi } from './ui';
@@ -136,6 +136,12 @@ gameKey
name: '鼠标位置怪物临界',
defaults: KeyCode.KeyC
})
.register({
id: 'danmaku',
name: '发送弹幕',
defaults: KeyCode.KeyA,
ctrl: true
})
.register({
id: 'quickEquip_1',
name: '切换/保存套装_1',
@@ -486,6 +492,9 @@ gameKey
if (enemy) mainUi.open('fixedDetail', { panel: 'critical' });
}
})
.realize('danmaku', () => {
openDanmakuPoster();
})
.realize('restart', () => {
core.confirmRestart();
})
+2 -6
View File
@@ -34,13 +34,11 @@ fixedUi.register(
new GameUi('start', UI.Start),
new GameUi('toolbar', UI.Toolbar),
new GameUi('load', UI.Load),
new GameUi('danmaku', UI.Danmaku)
new GameUi('danmaku', UI.Danmaku),
new GameUi('danmakuEditor', UI.DanmakuEditor)
);
fixedUi.showAll();
let loaded = false;
let mounted = false;
const hook = Mota.require('var', 'hook');
hook.once('mounted', () => {
const ui = document.getElementById('ui-main')!;
@@ -73,6 +71,4 @@ hook.once('mounted', () => {
fixedUi.on('end', () => {
fixed.style.display = 'none';
});
mounted = true;
});
+1 -1
View File
@@ -523,7 +523,7 @@ loading.once('coreInit', () => {
'ui.danmaku': storage.getValue('ui.danmaku', true),
'ui.danmakuSpeed': storage.getValue(
'ui.danmakuSpeed',
Math.floor(window.innerWidth / 25) * 5
Math.floor(window.innerWidth / 30) * 5
),
});
});
+6
View File
@@ -27,6 +27,7 @@ import * as frag from '@/plugin/fx/frag';
import * as use from '@/plugin/use';
import * as gameCanvas from '@/plugin/fx/gameCanvas';
import * as smooth from '@/plugin/fx/smoothView';
import * as animateController from '@/plugin/animateController';
Mota.Plugin.register('shadow_r', shadow, shadow.init);
Mota.Plugin.register('gameShadow_r', gameShadow, gameShadow.init);
@@ -38,3 +39,8 @@ Mota.Plugin.register('frag_r', frag, frag.init);
Mota.Plugin.register('use_r', use);
Mota.Plugin.register('gameCanvas_r', gameCanvas);
Mota.Plugin.register('smooth_r', smooth, smooth.init);
Mota.Plugin.register(
'animateController_r',
animateController,
animateController.default
);