refactor
This commit is contained in:
@@ -1,28 +1,39 @@
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System;
|
||||
using RebootKit.Engine.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace RebootKit.Engine.Services.Development {
|
||||
public class DebugOverlayView : MonoBehaviour, IView {
|
||||
[SerializeField] UIDocument m_Document;
|
||||
|
||||
void Start() {
|
||||
}
|
||||
|
||||
public async UniTask Show(CancellationToken cancellationToken) {
|
||||
gameObject.SetActive(true);
|
||||
await UniTask.Yield(cancellationToken);
|
||||
}
|
||||
|
||||
public async UniTask Hide(CancellationToken cancellationToken) {
|
||||
gameObject.SetActive(false);
|
||||
await UniTask.Yield(cancellationToken);
|
||||
}
|
||||
public class DebugOverlayView : UIDocumentView {
|
||||
const string k_DebugLabelClassName = "rr__debug-label";
|
||||
|
||||
void SetOverlayModeChanged(int mode) {
|
||||
VisualElement m_RootElement;
|
||||
|
||||
Label m_FPSLabel;
|
||||
|
||||
void Update() {
|
||||
if (m_RootElement == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_FPSLabel.text = $"fps: {Mathf.RoundToInt(1f / Time.deltaTime)} | dt: {Time.deltaTime:F4}ms | runtime: {Time.time:F4}s";
|
||||
}
|
||||
|
||||
public override VisualElement Build() {
|
||||
m_RootElement = new VisualElement();
|
||||
|
||||
CreateLabel($"Toggle Overlay [F3] | RebootKit | game: {Application.productName}, version: {Application.version}");
|
||||
m_FPSLabel = CreateLabel($"FPS: {Application.targetFrameRate}");
|
||||
|
||||
return m_RootElement;
|
||||
}
|
||||
|
||||
Label CreateLabel(string text) {
|
||||
Label label = (Label)LabelBuilder.New(text).Build();
|
||||
label.AddToClassList(k_DebugLabelClassName);
|
||||
|
||||
m_RootElement.Add(label);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,23 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Console;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace RebootKit.Engine.Services.Development {
|
||||
static class DebugCVars {
|
||||
public const string k_OverlayMode = "debug.mode";
|
||||
static class DebugConfig {
|
||||
[ConfigVar("debug.overlay", 1, "Controls overlay visibility. 0 - hidden, 1 - visible")] public static ConfigVar s_OverlayMode;
|
||||
}
|
||||
|
||||
public class DevToolsService : ServiceMonoBehaviour {
|
||||
[SerializeField] DebugOverlayView m_DebugOverlayView;
|
||||
|
||||
[CVar(DebugCVars.k_OverlayMode, 1)] CVar m_OverlayMode;
|
||||
|
||||
IDisposable m_CVarChangedListener;
|
||||
|
||||
void OnEnable() {
|
||||
m_CVarChangedListener = RR.CVarChanged.Listen(OnCVarChanged);
|
||||
OnOverlayModeChanged(m_OverlayMode.IndexValue);
|
||||
void Start() {
|
||||
ConfigVar.StateChanged += OnCVarChanged;
|
||||
// OnOverlayModeChanged(m_OverlayMode.IndexValue);
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
@@ -26,20 +25,26 @@ namespace RebootKit.Engine.Services.Development {
|
||||
}
|
||||
|
||||
public override void Dispose() {
|
||||
m_CVarChangedListener.Dispose();
|
||||
ConfigVar.StateChanged -= OnCVarChanged;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (InputSystem.GetDevice<Keyboard>().f3Key.wasReleasedThisFrame) {
|
||||
DebugConfig.s_OverlayMode.Set(DebugConfig.s_OverlayMode.IndexValue == 1 ? 0 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
void OnOverlayModeChanged(int mode) {
|
||||
if (mode == 1) {
|
||||
m_DebugOverlayView.Show(destroyCancellationToken).Forget();
|
||||
m_DebugOverlayView.gameObject.SetActive(true);
|
||||
} else {
|
||||
m_DebugOverlayView.Hide(destroyCancellationToken).Forget();
|
||||
m_DebugOverlayView.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
void OnCVarChanged(string cvarName, CVarValue value) {
|
||||
if (cvarName == DebugCVars.k_OverlayMode) {
|
||||
OnOverlayModeChanged((int)value.numberValue);
|
||||
void OnCVarChanged(ConfigVar cvar) {
|
||||
if (cvar == DebugConfig.s_OverlayMode) {
|
||||
OnOverlayModeChanged(cvar.IndexValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user