Car/Assets/Scripts/UI/GamePanel.cs

103 lines
3.2 KiB
C#
Raw Normal View History

2024-12-31 07:57:41 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
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;
private void Awake()
{
this.main.OnCarChanged += OnMainOnOnCarChanged;
this.main.OnCarComponentConfigChanged += OnCarComponentConfigChanged;
}
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 string ParseToSearchStringsFunc(object obj)
{
return "";
}
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);
}
}
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;
}
}