JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/ConsoleConnection/CoachScreenController.cs

158 lines
5.7 KiB
C#

using System.Collections.Generic;
using Audio;
using LightJSON;
using Localization;
using Misc;
using Network;
using UI;
using UnityEngine;
using UnityEngine.UI;
namespace ConsoleConnection
{
public class CoachScreenController : MonoBehaviour
{
public GameObject ActivityPrefab;
public SimpleButton SwitchProfileButton;
public GameObject CoachLanePrefab;
public GameObject CoachItemPrefab;
public Button StartButtonPrefab;
public ImageLoader SelectedCoach;
private List<CoachItem> coachItems;
private Button startButton;
private RectTransform startButtonRect;
public JSON LobbySetupJsonObject
{
set { BuildUi(value); }
}
private JSON startCommand;
// Called after loading the JSON
private void BuildUi(JSON lobbySetupJsonObject)
{
var changeProfileDescriptor = lobbySetupJsonObject.GetField("changeProfile");
if ((changeProfileDescriptor != null) && changeProfileDescriptor.IsObject)
{
SwitchProfileButton.gameObject.SetActive(true);
var title = changeProfileDescriptor.GetFieldAsString("title");
SwitchProfileButton.Title = StringProcessor.removeMarkers(title);
var iconPath = changeProfileDescriptor.GetFieldAsString("icon");
SwitchProfileButton.IconUrl = string.IsNullOrEmpty(iconPath) ? "local://ConsoleConnection/right_arrow" : iconPath;
var actions = JSONParseHelper.GetActions(changeProfileDescriptor);
if (actions.Count > 0)
SwitchProfileButton.Command = actions[0].command;
}
var coachesList = JSONParseHelper.GetCoaches(lobbySetupJsonObject);
coachItems = new List<CoachItem>();
BuildUiLine(coachesList);
var startButtonJsonObject = JSONParseHelper.GetStartButton(lobbySetupJsonObject);
if (startButtonJsonObject != null)
{
startCommand = JSONParseHelper.GetDefaultActionCommand(startButtonJsonObject);
startButton = Instantiate(StartButtonPrefab);
startButton.transform.SetParent(gameObject.transform, true);
startButtonRect = startButton.GetComponent<RectTransform>();
var startButtonComponent = startButton.GetComponent<StartButton>();
startButtonComponent.danceButton.onClick.AddListener(StartButtonTapped);
startButton.interactable = startButtonJsonObject.GetFieldAsBool("isEnabled") || !startButtonJsonObject.HasField("isEnabled") /* legacy protocol didn't have the field at all */;
// workaround for 2016 & 2017: the locId was not set on the game side, so
// the game will always send "<MISSING:-1>"
var title = StringProcessor.removeMarkers(startButtonJsonObject.GetFieldAsString("title"));
if (title == "<MISSING:-1>")
title = LocalizationManager.Instance.GetLocalizedString(12204 /* Start */);
startButtonComponent.titleButton.text = title;
}
}
private void BuildUiLine(List<JSON> coachesList)
{
// Instantiate a global container that will contain the coach prefabs
var coachesContainer = Instantiate(CoachLanePrefab);
coachesContainer.transform.SetParent(ActivityPrefab.transform, false);
coachesList.ForEach(coachJsonObject =>
{
var coachItemGameObject = Instantiate(CoachItemPrefab);
var coachItem = coachItemGameObject.GetComponent<CoachItem>();
coachItemGameObject.transform.SetParent(coachesContainer.transform, false);
coachItems.Add(coachItem);
coachItem.SetModel(coachJsonObject, SelectedCoach, coachesList.Count == 1);
});
}
public void StartButtonTapped()
{
if (startCommand != null)
{
AudioManager.Instance.Play(AudioManager.SoundIds.ValidationDance);
ConsoleNetworkManager.Instance.Send(startCommand);
}
}
private void CoachItemTapped(CoachItem coachItem)
{
var itemIndex = coachItems.IndexOf(coachItem);
if (itemIndex == -1) return;
coachItems.ForEach(item =>
{
if (coachItem != item)
item.IsHighlighted = false;
});
coachItem.IsHighlighted = true;
AudioManager.Instance.Play(AudioManager.SoundIds.SmallValidation);
}
private void EnableStartButton(bool startButtonEnabled)
{
if (startButton != null)
startButton.interactable = startButtonEnabled;
}
private void OnEnable()
{
CoachItem.CoachItemTappedEvent += CoachItemTapped;
ConsoleNetworkManager.Instance.OnEnableLobbyStartButton += EnableStartButton;
}
private void OnDisable()
{
CoachItem.CoachItemTappedEvent -= CoachItemTapped;
ConsoleNetworkManager.Instance.OnEnableLobbyStartButton -= EnableStartButton;
}
// Quite odd to do so, but strangly I think they're an UI issue related to the Canvas redrawing at a bad moment
// Putting the "Dance" button in middle of the page
// Even when putting an other UI element in the middle, it does the same, so this is the only "quickfix" we have
private void Update()
{
if (startButtonRect == null)
return;
startButtonRect.anchoredPosition = Vector2.zero;
}
}
}