From 3af7f71478c5f9fb1e10467cf9b7ea57f7380c05 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Mon, 11 Mar 2024 22:53:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B8=A7=E7=8E=87=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/main/setting.ts | 23 ++++++++++++++++++++- src/core/plugin.ts | 2 ++ src/plugin/frame.ts | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/plugin/frame.ts diff --git a/src/core/main/setting.ts b/src/core/main/setting.ts index 5d875fa..d19d0f9 100644 --- a/src/core/main/setting.ts +++ b/src/core/main/setting.ts @@ -8,6 +8,7 @@ import { SoundEffect } from '../audio/sound'; import settingsText from '@/data/settings.json'; import { isMobile } from '@/plugin/use'; import { fontSize } from '@/plugin/ui/statusBar'; +import { show as showFrame, hide as hideFrame } from '@/plugin/frame'; export interface SettingComponentProps { item: MotaSettingItem; @@ -338,6 +339,8 @@ mainSetting.on('valueChange', (key, n, o) => { handleActionSetting(setting, n, o); } else if (root === 'audio') { handleAudioSetting(setting, n, o); + } else if (root === 'debug') { + handleDebugSetting(setting, n, o) } }); @@ -414,6 +417,17 @@ function handleAudioSetting( } } +function handleDebugSetting( + key: string, + n: T, + o: T +) { + if (key === 'frame'){ + if (n) showFrame(); + else hideFrame() + } +} + // ----- 游戏的所有设置项 // todo: 虚拟键盘缩放,小地图楼传缩放 mainSetting @@ -467,6 +481,12 @@ mainSetting new MotaSetting() .register('mapScale', '小地图缩放', 100, COM.Number, [50, 1000, 50]) .setDisplayFunc('mapScale', value => `${value}%`) + ) + .register( + 'debug', + '调试设置', + new MotaSetting() + .register('frame', '帧率显示', false, COM.Boolean) ); const loading = Mota.require('var', 'loading'); @@ -490,7 +510,8 @@ loading.once('coreInit', () => { 'ui.mapScale': storage.getValue( 'ui.mapScale', isMobile ? 300 : Math.floor(window.innerWidth / 600) * 50 - ) + ), + 'debug.frame': !!storage.getValue('debug.frame', false), }); }); diff --git a/src/core/plugin.ts b/src/core/plugin.ts index 6aa1a6d..5ace8d6 100644 --- a/src/core/plugin.ts +++ b/src/core/plugin.ts @@ -26,6 +26,7 @@ import * as use from '@/plugin/use'; import * as gameCanvas from '@/plugin/fx/gameCanvas'; import * as smooth from '@/plugin/fx/smoothView'; import * as shader from './fx/shader'; +import * as frame from '@/plugin/frame'; Mota.Plugin.register('shadow_r', shadow, shadow.init); Mota.Plugin.register('gameShadow_r', gameShadow, gameShadow.init); @@ -36,3 +37,4 @@ Mota.Plugin.register('use_r', use); Mota.Plugin.register('gameCanvas_r', gameCanvas); Mota.Plugin.register('smooth_r', smooth, smooth.init); Mota.Plugin.register('shader_r', shader); +Mota.Plugin.register('frame_r', frame, frame.init); diff --git a/src/plugin/frame.ts b/src/plugin/frame.ts new file mode 100644 index 0000000..4f8e355 --- /dev/null +++ b/src/plugin/frame.ts @@ -0,0 +1,43 @@ +import { Ticker } from 'mutate-animate'; + +const ticker = new Ticker(); + +const span = document.createElement('span'); +span.style.fontSize = '16px'; +span.style.position = 'fixed'; +span.style.right = '0'; +span.style.top = '0'; +span.style.fontFamily = 'Arial'; +span.style.color = 'lightgreen'; +span.style.padding = '5px'; + +let showing = false; + +export function init() { + const settings = Mota.require('var', 'mainSetting'); + const setting = settings.getSetting('debug.frame'); + /** 记录前5帧的时间戳 */ + let lasttimes = [0, 0, 0, 0, 0]; + ticker.add(time => { + if (!setting?.value) return; + lasttimes.shift(); + lasttimes.push(time); + span.innerText = (1000 / ((lasttimes[4] - lasttimes[0]) / 4)).toFixed( + 1 + ); + }); +} + +export function show() { + showing = true; + document.body.appendChild(span); +} + +export function hide() { + showing = false; + span.remove(); +} + +export function isShowing() { + return showing; +}