Skip to content

Commit

Permalink
Some quick UI-related code cleanups (YARC-Official#381)
Browse files Browse the repository at this point in the history
* Reformat ToastManager and put it inside a namespace

* Use display name instead of layout name when toasting device changes

* Move DevWatermark into a namespace and remove unused using directives

* Clean up FpsCounter
Move it into YARG.UI
Serialize color values and update rate instead of hard-coding them
Remove UpdateSettings, as it just calls SetVisible which is public anyways
Other general cleanups

* Remove DevWatermark disabled logging
Not really informative of anything
  • Loading branch information
TheNathannator authored May 27, 2023
1 parent a0ec127 commit 1a69913
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 277 deletions.
4 changes: 4 additions & 0 deletions Assets/Scenes/PersistantScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -4487,6 +4487,10 @@ MonoBehaviour:
m_EditorClassIdentifier:
fpsCircle: {fileID: 1604709612}
fpsText: {fileID: 647926893}
fpsCircleGreen: {r: 0.2745098, g: 0.9058823, b: 0.3098039, a: 1}
fpsCircleYellow: {r: 1, g: 0.8313725, b: 0.2274509, a: 1}
fpsCircleRed: {r: 1, g: 0, b: 0.2078431, a: 1}
fpsUpdateRate: 1
--- !u!1 &1953023866
GameObject:
m_ObjectHideFlags: 0
Expand Down
4 changes: 1 addition & 3 deletions Assets/Script/Settings/SettingsManager.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ private static void VSyncCallback(bool value) {
}

private static void FpsCouterCallback(bool value) {
// disable script
FpsCounter.Instance.enabled = value;
// UpdateSettings()
FpsCounter.Instance.UpdateSettings(value);
FpsCounter.Instance.SetVisible(value);

// enable script if in editor
#if UNITY_EDITOR
Expand Down
1 change: 1 addition & 0 deletions Assets/Script/Song/SongCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using UnityEngine;
using YARG.Data;
using YARG.UI;

namespace YARG.Song {
public class SongCache {
Expand Down
1 change: 1 addition & 0 deletions Assets/Script/ThirdParty/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json.Linq;
using UnityEngine;
using YARG.Data;
using YARG.UI;

namespace YARG {
public class UpdateChecker : MonoBehaviour {
Expand Down
33 changes: 14 additions & 19 deletions Assets/Script/UI/DevWatermark.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using YARG;
using Debug = UnityEngine.Debug;

public class DevWatermark : MonoBehaviour {
namespace YARG.UI {
public class DevWatermark : MonoBehaviour {
[SerializeField]
private TextMeshProUGUI watermarkText;

[SerializeField]
private TextMeshProUGUI watermarkText;
void Start() {
// check if Constants.VERSION_TAG ends with "b"
if (Constants.VERSION_TAG.beta) {
watermarkText.text = $"<b>YARG {Constants.VERSION_TAG}</b> Developer Build";
watermarkText.gameObject.SetActive(true);
} else {
this.gameObject.SetActive(false);
}

// Start is called before the first frame update
void Start() {
// check if Constants.VERSION_TAG ends with "b"
if (Constants.VERSION_TAG.beta) {
watermarkText.text = $"<b>YARG {Constants.VERSION_TAG}</b> Developer Build";
watermarkText.gameObject.SetActive(true);
} else {
this.gameObject.SetActive(false);
// disable script
this.enabled = false;
}

// disable script
this.enabled = false;
Debug.Log("DevWatermark script disabled");
}
}
92 changes: 39 additions & 53 deletions Assets/Script/UI/FpsCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,67 @@
using UnityEngine.Profiling;
using UnityEngine.UI;

public class FpsCounter : MonoBehaviour {
namespace YARG.UI {
public class FpsCounter : MonoBehaviour {
public static FpsCounter Instance { get; private set; } = null;

// Instance
public static FpsCounter Instance { get; private set; } = null;
[SerializeField]
private Image fpsCircle;
[SerializeField]
private TextMeshProUGUI fpsText;

// FPS Counter
public Image fpsCircle;
public TextMeshProUGUI fpsText;
private float updateTime = 1f;
[SerializeField]
private Color fpsCircleGreen;
[SerializeField]
private Color fpsCircleYellow;
[SerializeField]
private Color fpsCircleRed;

// Awake
private void Awake() {
Instance = this;
}
[SerializeField]
private float fpsUpdateRate;

// check if the settings have changed and update the fps counter accordingly
public void UpdateSettings(bool value) {
SetVisible(value);
}
private float nextUpdateTime;

public void SetVisible(bool value) {
fpsText.gameObject.SetActive(value);
fpsCircle.gameObject.SetActive(value);
}
private void Awake() {
Instance = this;
}

// OnDestroy - set the instance to null
private void OnDestroy() {
Instance = null;
}
public void SetVisible(bool value) {
fpsText.gameObject.SetActive(value);
fpsCircle.gameObject.SetActive(value);
}

// Update is called once per frame
void Update() {
// update fps per second
if (Time.unscaledTime > updateTime) {
private void OnDestroy() {
Instance = null;
}

// (1 / unscaledDeltaTime) = FPS
int fps = (int)(1f / Time.unscaledDeltaTime);
void Update() {
// Wait for next update period
if (Time.unscaledTime < nextUpdateTime) {
return;
}

// Clear the FPS text
fpsText.text = "";
int fps = (int)(1f / Time.unscaledDeltaTime);

// Color the FPS sprite based on the FPS
// Color the circle sprite based on the FPS
if (fps < 30) {
// RED
if (ColorUtility.TryParseHtmlString("#FF0035", out Color color)) {
fpsCircle.color = color;
} else {
fpsCircle.color = Color.red;
}
fpsCircle.color = fpsCircleRed;
} else if (fps < 60) {
// YELLOW
if (ColorUtility.TryParseHtmlString("#FFD43A", out Color color)) {
fpsCircle.color = color;
} else {
fpsCircle.color = Color.yellow;
}
fpsCircle.color = fpsCircleYellow;
} else {
// GREEN
if (ColorUtility.TryParseHtmlString("#46E74F", out Color color)) {
fpsCircle.color = color;
} else {
fpsCircle.color = Color.green;
}
fpsCircle.color = fpsCircleGreen;
}

// Display the FPS
fpsText.text += "FPS: " + fps.ToString();
fpsText.text = $"FPS: {fps}";

#if UNITY_EDITOR
// Display the memory usage
fpsText.text += "\nMemory: " + (Profiler.GetTotalAllocatedMemoryLong() / 1024 / 1024).ToString() + " MB";
fpsText.text += $"\nMemory: {Profiler.GetTotalAllocatedMemoryLong() / 1024 / 1024} MB";
#endif

// reset the update time
updateTime = Time.unscaledTime + 1f;
nextUpdateTime = Time.unscaledTime + fpsUpdateRate;
}
}
}
Loading

0 comments on commit 1a69913

Please sign in to comment.