64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
[UIView("demo-panel")]
|
|
public class DemoPanelUIView : UIView
|
|
{
|
|
private Button _closeButton;
|
|
|
|
private Button _feedbackButton;
|
|
|
|
private Label _populationLabel;
|
|
|
|
private Label _gameTimeLabel;
|
|
|
|
public event Action FeedbackButtonClick;
|
|
|
|
public override UniTask InitializeAsync(UIService uiService, VisualElement rootElement)
|
|
{
|
|
base.InitializeAsync(uiService, rootElement);
|
|
|
|
_populationLabel = rootElement.Q<Label>(className: "demo-panel__population");
|
|
_gameTimeLabel = rootElement.Q<Label>(className: "demo-panel__game-time");
|
|
|
|
_feedbackButton = rootElement.Q<Button>(className: "demo-panel__feedback-button");
|
|
_feedbackButton.RegisterCallbackOnce<ClickEvent>(_ => FeedbackButtonClick?.Invoke());
|
|
|
|
_closeButton = rootElement.Q<Button>(className: "demo-panel__close-button");
|
|
_closeButton.RegisterCallbackOnce<ClickEvent>(_ => Show(false, true));
|
|
|
|
Show(false);
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public void OnDemoCompleted(int population, float gameTime)
|
|
{
|
|
FormatPanel(population, gameTime);
|
|
Show(true, true);
|
|
}
|
|
|
|
private void FormatPanel(int population, float gameTime)
|
|
{
|
|
_populationLabel.text = string.Format(_populationLabel.text, population);
|
|
_gameTimeLabel.text = string.Format(_gameTimeLabel.text, MakeGameTimeString(gameTime));
|
|
}
|
|
|
|
private string MakeGameTimeString(float gameTime)
|
|
{
|
|
var formatHelper = UIService.TextFormatHelper;
|
|
|
|
var totalSeconds = (int)gameTime;
|
|
var totalMinutes = totalSeconds / 60;
|
|
var hours = totalMinutes / 60;
|
|
var minutes = totalMinutes % 60;
|
|
|
|
return hours > 0
|
|
? $"{hours} {formatHelper.Pluralize(hours, "hour", "hours")} {minutes} {formatHelper.Pluralize(minutes, "minute", "minutes")}"
|
|
: $"{minutes} {formatHelper.Pluralize(minutes, "minute", "minutes")}";
|
|
}
|
|
}
|
|
} |