Car/Assets/Scripts/UI/GamePanel.cs

146 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using YogiGameCore.ComponentEx;
using YogiGameCore.Utils;
public class GamePanel : MonoBehaviour
{
[SerializeField] private Main main;
[SerializeField] private UberButton selectComponentConfigBtnPrefab;
[SerializeField] private Transform selectComponentConfigParent;
[SerializeField] private UberButton selectComponentPrefab;
[SerializeField] private Transform selectComponentParent;
[SerializeField] private SearchView searchView;
[SerializeField] private TMPTypewriter typewriterPrefab;
[SerializeField] private Transform componentTextParent;
private Dictionary<string, TMPTypewriter> spawnedTypewriters;
[SerializeField] private Button uiTriggerBtn;
[SerializeField] private GameObject uiCanvas;
private void ClearAllComponentDisplayText()
{
Debug.Log("清空车辆所有文本信息");
componentTextParent.DestroyChildren();
spawnedTypewriters = new Dictionary<string, TMPTypewriter>();
}
private void UpdateComponentDisplayText()
{
var carConfig = main.CurrentCar;
for (var i = 0; i < carConfig..Count; i++)
{
CarComponentConfig carComponentConfig = carConfig.[i];
int selectComponentIndex = carComponentConfig.SelectComponentIndex;
ComponentConfig selectedComponent = carComponentConfig.[selectComponentIndex];
if (!spawnedTypewriters.TryGetValue(carComponentConfig., out TMPTypewriter typewriter))
{
typewriter = GameObject.Instantiate(typewriterPrefab, componentTextParent);
spawnedTypewriters.Add(carComponentConfig., typewriter);
}
if (!typewriter.GetDisplayText().Equals(selectedComponent.))
{
typewriter.StopAllCoroutines();
typewriter.PlayAnimation(selectedComponent.);
}
}
}
public void ResetCurrentComponent()
{
var btns = selectComponentParent.GetComponentsInChildren<UberButton>();
if (btns.Length <= 0) return;
btns[0].SetHold(true);
}
private void Awake()
{
ClearAllComponentDisplayText();
this.main.OnCarChanged += OnMainOnOnCarChanged;
this.main.OnCarComponentConfigChanged += OnCarComponentConfigChanged;
this.main.OnSwitchComponent += UpdateComponentDisplayText;
uiTriggerBtn.onClick.AddListener(() => { uiCanvas.SetActive(!uiCanvas.activeSelf); });
}
private void Start()
{
var cars = main.AllCars.Cast<object>().ToList();
searchView.SetUp(cars, (obj) => { return ((CarConfig)obj).; }, (result) =>
{
var carConfig = result.As<CarConfig>();
var index = cars.IndexOf(carConfig);
main.DisplayCar(index);
});
}
private void OnCarComponentConfigChanged(CarComponentConfig config)
{
selectComponentParent.DestroyChildren();
for (var index = 0; index < config..Count; index++)
{
var component = config.[index];
var componentName = component.;
var btn = SpawnSelectComponentBtn(componentName, index);
Debug.Log($"SelectComponentIndex:{config.SelectComponentIndex}");
if (config.SelectComponentIndex == index)
{
btn.SetHold(true);
}
}
}
private UberButton SpawnSelectComponentBtn(string componentName, int index)
{
var btn = GameObject.Instantiate(selectComponentPrefab, selectComponentParent);
btn.GetComponentInChildren<TextMeshProUGUI>().text = componentName;
btn.OnToggleValueChanged += v =>
{
if (!v)
return;
this.main.SwitchComponent(index);
};
return btn;
}
void OnMainOnOnCarChanged(CarConfig config)
{
selectComponentConfigParent.DestroyChildren();
for (var index = 0; index < config..Count; index++)
{
var carComponentConfig = config.[index];
var index1 = index;
var btn = SpawnSelectComponentConfigBtn(carComponentConfig., (v) =>
{
if (!v)
return;
main.SwitchComponentConfig(index1);
});
if (main.GetCurrentCarComponentConfigIndex == index1)
btn.SetHold(true);
}
ClearAllComponentDisplayText();
UpdateComponentDisplayText();
}
private UberButton SpawnSelectComponentConfigBtn(string displayText, Action<bool> clickAction)
{
var btn = GameObject.Instantiate(selectComponentConfigBtnPrefab, selectComponentConfigParent);
var textMeshProUGUI = btn.GetComponentInChildren<TextMeshProUGUI>();
textMeshProUGUI.text = displayText;
btn.OnToggleValueChanged += clickAction;
return btn;
}
}