Files
bansonic_beta_main/Assets/playerInfoDisplay/ghPrefab.cs
T

153 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class ghPrefab : MonoBehaviour
{
[Header("hahahaha")]
public Image songImg;
public Text playtime;
public Text songName;
public Text songDfct;
public Text songPerf;
public Text songScore;
public Image songRankImg;
[Header("rank config")]
public RankConfig rc;
private static readonly Color EarlySettlementColor = new Color32(204, 51, 51, 255);
private static readonly Color BrightScoreColor = new Color32(124, 255, 107, 255);
private const string UnreadableText = "\u65e0\u6cd5\u8bfb\u53d6";
public void Setup(RecentPlayRecord record, SongData songData)
{
if (record == null)
{
ApplyUnreadableFallback();
return;
}
if (songImg != null)
{
songImg.sprite = songData != null ? songData.illustration : null;
songImg.enabled = songImg.sprite != null;
}
if (playtime != null)
{
playtime.text = string.IsNullOrWhiteSpace(record.playedAt) ? "--" : record.playedAt;
}
if (songName != null)
{
string displayName = string.IsNullOrWhiteSpace(record.songName) ? "Unknown" : record.songName;
songName.text = FitTextWithEllipsis(songName, displayName);
}
if (songDfct != null)
{
songDfct.text = string.IsNullOrWhiteSpace(record.difficultyDisplay) ? "UN" : record.difficultyDisplay;
}
if (songPerf != null)
{
songPerf.text = "ACC: " + record.accuracy.ToString("F3") + "%\nSRKS: " + record.srks.ToString("F2");
}
if (songScore != null)
{
if (!record.scoreReadable)
{
songScore.text = UnreadableText;
songScore.color = Color.black;
}
else
{
songScore.text = record.totalScore.ToString();
if (record.wasEarlySettlement)
{
songScore.color = EarlySettlementColor;
}
else
{
songScore.color = BrightScoreColor;
}
}
}
if (songRankImg != null)
{
if (rc != null)
{
float rankScore = record.scoreReadable ? record.totalScore : 0f;
songRankImg.sprite = rc.GetRankSprite(rankScore);
}
else
{
songRankImg.sprite = null;
}
}
}
private void ApplyUnreadableFallback()
{
if (songImg != null)
{
songImg.sprite = null;
songImg.enabled = false;
}
if (playtime != null) playtime.text = "--";
if (songName != null) songName.text = "Unknown";
if (songDfct != null) songDfct.text = "UN";
if (songPerf != null) songPerf.text = "ACC: --\nSRKS: --";
if (songScore != null)
{
songScore.text = UnreadableText;
songScore.color = Color.black;
}
if (songRankImg != null)
{
songRankImg.sprite = rc != null ? rc.GetRankSprite(0f) : null;
}
}
private static string FitTextWithEllipsis(Text target, string value)
{
if (target == null || string.IsNullOrEmpty(value))
{
return value;
}
RectTransform rt = target.rectTransform;
float availableWidth = rt != null ? rt.rect.width : 0f;
if (availableWidth <= 0f || target.font == null)
{
return value;
}
TextGenerationSettings settings = target.GetGenerationSettings(new Vector2(availableWidth, rt.rect.height));
float preferred = target.cachedTextGeneratorForLayout.GetPreferredWidth(value, settings) / target.pixelsPerUnit;
if (preferred <= availableWidth)
{
return value;
}
const string ellipsis = "...";
string trimmed = value;
while (trimmed.Length > 0)
{
trimmed = trimmed.Substring(0, trimmed.Length - 1);
string candidate = trimmed + ellipsis;
preferred = target.cachedTextGeneratorForLayout.GetPreferredWidth(candidate, settings) / target.pixelsPerUnit;
if (preferred <= availableWidth)
{
return candidate;
}
}
return ellipsis;
}
}