riversong code showcase
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class DemoPanelUIController : UIControllerSystem<DemoPanelUIView>, IDisposable
|
||||
{
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
public DemoPanelUIController(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
protected override DemoPanelUIView View => UIRoot.GetView<DemoPanelUIView>();
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
await base.InitializeAsync();
|
||||
|
||||
View.FeedbackButtonClick += OnFeedbackButtonClick;
|
||||
_signalBus.Subscribe<DemoCompletedSignal>(OnDemoCompleted);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
View.FeedbackButtonClick -= OnFeedbackButtonClick;
|
||||
_signalBus.Unsubscribe<DemoCompletedSignal>(OnDemoCompleted);
|
||||
}
|
||||
|
||||
private void OnDemoCompleted(DemoCompletedSignal signal)
|
||||
{
|
||||
View.OnDemoCompleted(_world.PopulationState.Population, signal.GameTime);
|
||||
}
|
||||
|
||||
private void OnFeedbackButtonClick()
|
||||
{
|
||||
Application.OpenURL(AppLinks.DemoFeedbackUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
64
Source/Riversong/Game/UI/Panels/Demo/DemoPanelUIView.cs
Normal file
64
Source/Riversong/Game/UI/Panels/Demo/DemoPanelUIView.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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")}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user