2024-11-17 19:10:01 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
/// <summary>
|
2024-11-19 14:38:40 +08:00
|
|
|
/// class that handles the side view ui
|
2024-11-17 19:10:01 +08:00
|
|
|
/// </summary>
|
|
|
|
public class SideViewUI : MonoBehaviour
|
|
|
|
{
|
|
|
|
/// <summary>
|
2024-11-17 21:32:26 +08:00
|
|
|
/// settings button for showing the settings menu
|
2024-11-17 19:10:01 +08:00
|
|
|
/// </summary>
|
|
|
|
private Button _accountButton;
|
|
|
|
|
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// text label for showing the player's stable-ish chroma accuracy
|
2024-11-17 19:10:01 +08:00
|
|
|
/// </summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
private Label _chromaAccuracyText;
|
2024-11-17 19:10:01 +08:00
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// text label for showing the player's stable-ish hue accuracy
|
2024-11-18 00:16:53 +08:00
|
|
|
/// </summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
private Label _hueAccuracyText;
|
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// leaderboard button for showing the leaderboard
|
2024-11-18 00:16:53 +08:00
|
|
|
/// </summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
private Button _leaderboardButton;
|
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
/// <summary>
|
|
|
|
/// text label for showing the player's stable-ish lightness accuracy
|
|
|
|
/// </summary>
|
|
|
|
private Label _lightnessAccuracyText;
|
2024-11-18 00:50:04 +08:00
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// play button for starting the game
|
2024-11-18 00:16:53 +08:00
|
|
|
/// </summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
private Button _playButton;
|
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// text label for showing the player's known name
|
2024-11-18 00:16:53 +08:00
|
|
|
/// </summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
private Label _playerText;
|
2024-11-18 00:16:53 +08:00
|
|
|
|
2024-11-17 19:10:01 +08:00
|
|
|
/// <summary>
|
2024-11-18 00:50:04 +08:00
|
|
|
/// text label for showing the player's rating
|
|
|
|
/// </summary>
|
|
|
|
private Label _ratingText;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// initialise the ui elements and register the button click event functions
|
2024-11-17 19:10:01 +08:00
|
|
|
/// </summary>
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
var ui = GetComponent<UIDocument>().rootVisualElement;
|
|
|
|
|
|
|
|
_playButton = ui.Q<Button>("PlayButton");
|
|
|
|
_playButton.clicked += OnPlayButtonClicked;
|
|
|
|
|
|
|
|
_leaderboardButton = ui.Q<Button>("LeaderboardButton");
|
|
|
|
_leaderboardButton.clicked += OnLeaderboardButtonClicked;
|
|
|
|
|
|
|
|
_accountButton = ui.Q<Button>("AccountButton");
|
|
|
|
_accountButton.clicked += OnAccountButtonClicked;
|
2024-11-18 00:50:04 +08:00
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
_playerText = ui.Q<Label>("PlayerText");
|
|
|
|
_ratingText = ui.Q<Label>("RatingText");
|
2024-11-18 00:50:04 +08:00
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
_lightnessAccuracyText = ui.Q<Label>("LightnessAccuracyText");
|
|
|
|
_chromaAccuracyText = ui.Q<Label>("ChromaAccuracyText");
|
|
|
|
_hueAccuracyText = ui.Q<Label>("HueAccuracyText");
|
|
|
|
|
|
|
|
GameManager.Instance.Backend.RegisterOnSignOutCallback(() =>
|
|
|
|
{
|
|
|
|
RenderFromPlayerData(GameManager.Instance.Data);
|
|
|
|
});
|
|
|
|
GameManager.Instance.RegisterOnLocalPlayerDataChangeCallback(RenderFromPlayerData);
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// function to show the play view
|
|
|
|
/// </summary>
|
|
|
|
private static void OnPlayButtonClicked()
|
|
|
|
{
|
2024-11-18 03:19:37 +08:00
|
|
|
GameManager.Instance.ui.SetDisplayState(UIManager.DisplayState.GameView);
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// function to show the leaderboard view
|
|
|
|
/// </summary>
|
|
|
|
private static void OnLeaderboardButtonClicked()
|
|
|
|
{
|
2024-11-17 22:05:04 +08:00
|
|
|
GameManager.Instance.ui.SetDisplayState(UIManager.DisplayState.LeaderboardView);
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// function to show the account view
|
|
|
|
/// </summary>
|
|
|
|
private static void OnAccountButtonClicked()
|
|
|
|
{
|
2024-11-17 22:05:04 +08:00
|
|
|
GameManager.Instance.ui.SetDisplayState(UIManager.DisplayState.AccountView);
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
2024-11-18 00:16:53 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// function to update the ui with the latest data
|
|
|
|
/// </summary>
|
|
|
|
private void RenderFromPlayerData(LocalPlayerData data)
|
|
|
|
{
|
2024-11-19 01:36:19 +08:00
|
|
|
Debug.Log("updating SideView > AccountSection with new player data");
|
2024-11-19 08:30:45 +08:00
|
|
|
|
2024-11-18 00:16:53 +08:00
|
|
|
// calculate averages from both recent local scores and online scores
|
|
|
|
var totalLightnessAcc = 0f;
|
|
|
|
var totalChromaAcc = 0f;
|
|
|
|
var totalHueAcc = 0f;
|
2024-11-19 03:59:29 +08:00
|
|
|
var totalGames = 0;
|
2024-11-18 00:16:53 +08:00
|
|
|
|
|
|
|
// average out all the scores we have to get a stable-ish average
|
|
|
|
|
|
|
|
foreach (var localScore in data.RecentLocalScores)
|
|
|
|
{
|
|
|
|
totalLightnessAcc += localScore.AvgLightnessAccuracy;
|
|
|
|
totalChromaAcc += localScore.AvgChromaAccuracy;
|
|
|
|
totalHueAcc += localScore.AvgHueAccuracy;
|
2024-11-19 03:59:29 +08:00
|
|
|
totalGames++;
|
2024-11-18 00:16:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var onlineScore in data.RecentOnlineScores)
|
|
|
|
{
|
|
|
|
totalLightnessAcc += onlineScore.AvgLightnessAccuracy;
|
|
|
|
totalChromaAcc += onlineScore.AvgChromaAccuracy;
|
|
|
|
totalHueAcc += onlineScore.AvgHueAccuracy;
|
2024-11-19 03:59:29 +08:00
|
|
|
totalGames++;
|
2024-11-18 00:16:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var onlineScore in data.BestOnlineScores)
|
|
|
|
{
|
|
|
|
totalLightnessAcc += onlineScore.AvgLightnessAccuracy;
|
|
|
|
totalChromaAcc += onlineScore.AvgChromaAccuracy;
|
|
|
|
totalHueAcc += onlineScore.AvgHueAccuracy;
|
2024-11-19 03:59:29 +08:00
|
|
|
totalGames++;
|
2024-11-18 00:16:53 +08:00
|
|
|
}
|
|
|
|
|
2024-11-19 03:59:29 +08:00
|
|
|
Debug.Log($"tL={totalLightnessAcc} tC={totalChromaAcc} tH={totalHueAcc} tG={totalGames}");
|
|
|
|
if (totalGames == 0) totalGames = 1;
|
|
|
|
var lightnessAcc = totalLightnessAcc / totalGames;
|
|
|
|
var chromaAcc = totalChromaAcc / totalGames;
|
|
|
|
var hueAcc = totalHueAcc / totalGames;
|
2024-11-18 00:16:53 +08:00
|
|
|
var playerText = GameManager.Instance.Backend.IsSignedIn
|
|
|
|
? data.LastKnownUsername
|
|
|
|
: $"{data.LastKnownUsername} (Not Signed In)";
|
2024-11-18 18:02:41 +08:00
|
|
|
var rating = data.CalculateUserRating();
|
2024-11-18 00:16:53 +08:00
|
|
|
|
|
|
|
// finally, set the labels
|
|
|
|
_playerText.text = playerText;
|
2024-11-19 03:59:29 +08:00
|
|
|
_ratingText.text = $"{rating:F3}";
|
|
|
|
_lightnessAccuracyText.text = $"{lightnessAcc:F2}%";
|
|
|
|
_chromaAccuracyText.text = $"{chromaAcc:F2}%";
|
|
|
|
_hueAccuracyText.text = $"{hueAcc:F2}%";
|
2024-11-18 00:16:53 +08:00
|
|
|
}
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|