124 lines
3.5 KiB
C#
124 lines
3.5 KiB
C#
using GameServer.Client;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class rkPrefab : MonoBehaviour
|
|
{
|
|
[SerializeField] private Image buttom_image;
|
|
[SerializeField] private Text listID;
|
|
[SerializeField] private Image userProfile;
|
|
[SerializeField] private Text this_username;
|
|
[SerializeField] private Text this_submitTime;
|
|
[SerializeField] private Text this_totalScore;
|
|
[SerializeField] private Text this_keepOnTime;
|
|
|
|
public void Bind(LeaderboardEntry entry, Sprite backgroundSprite, string formattedSubmitTime)
|
|
{
|
|
BindInternal(entry, backgroundSprite, formattedSubmitTime, false);
|
|
}
|
|
|
|
public void BindRoomRanking(LeaderboardEntry entry, Sprite backgroundSprite, string formattedSubmitTime)
|
|
{
|
|
BindInternal(entry, backgroundSprite, formattedSubmitTime, true);
|
|
}
|
|
|
|
private void BindInternal(LeaderboardEntry entry, Sprite backgroundSprite, string formattedSubmitTime, bool isRoomRanking)
|
|
{
|
|
if (entry == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (buttom_image != null)
|
|
{
|
|
buttom_image.sprite = backgroundSprite;
|
|
}
|
|
|
|
if (listID != null)
|
|
{
|
|
listID.text = entry.rank.ToString();
|
|
}
|
|
|
|
if (this_username != null)
|
|
{
|
|
string displayName = GameServer.Client.NetworkManager.ResolveDisplayName(
|
|
entry.display_name,
|
|
entry.steam_id,
|
|
false);
|
|
this_username.text = TruncateToFit(this_username, displayName);
|
|
}
|
|
|
|
if (this_submitTime != null)
|
|
{
|
|
this_submitTime.text = formattedSubmitTime;
|
|
}
|
|
|
|
if (this_totalScore != null)
|
|
{
|
|
this_totalScore.text = entry.total_score.ToString();
|
|
}
|
|
|
|
if (this_keepOnTime != null)
|
|
{
|
|
this_keepOnTime.text = isRoomRanking
|
|
? (!string.IsNullOrWhiteSpace(entry.room_status_text) ? entry.room_status_text : "-")
|
|
: entry.keep_on_time.ToString();
|
|
}
|
|
|
|
if (userProfile != null)
|
|
{
|
|
userProfile.enabled = false;
|
|
}
|
|
}
|
|
|
|
private static string TruncateToFit(Text targetText, string source)
|
|
{
|
|
if (targetText == null || string.IsNullOrEmpty(source))
|
|
{
|
|
return source ?? string.Empty;
|
|
}
|
|
|
|
RectTransform rect = targetText.rectTransform;
|
|
if (rect == null || rect.rect.width <= 0f)
|
|
{
|
|
return source;
|
|
}
|
|
|
|
float maxWidth = rect.rect.width;
|
|
if (GetPreferredWidth(targetText, source) <= maxWidth)
|
|
{
|
|
return source;
|
|
}
|
|
|
|
const string ellipsis = "...";
|
|
if (GetPreferredWidth(targetText, ellipsis) > maxWidth)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
int low = 0;
|
|
int high = source.Length;
|
|
while (low < high)
|
|
{
|
|
int mid = (low + high + 1) / 2;
|
|
string candidate = source.Substring(0, mid) + ellipsis;
|
|
if (GetPreferredWidth(targetText, candidate) <= maxWidth)
|
|
{
|
|
low = mid;
|
|
}
|
|
else
|
|
{
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
|
|
return low <= 0 ? ellipsis : source.Substring(0, low) + ellipsis;
|
|
}
|
|
|
|
private static float GetPreferredWidth(Text targetText, string content)
|
|
{
|
|
TextGenerationSettings settings = targetText.GetGenerationSettings(Vector2.zero);
|
|
return targetText.cachedTextGeneratorForLayout.GetPreferredWidth(content, settings) / targetText.pixelsPerUnit;
|
|
}
|
|
}
|