Files
EnglewoodLAB/Assets/Scripts/UI/FloorController/FloorControlPanel.cs
2026-03-09 17:00:30 +09:00

81 lines
2.8 KiB
C#

using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using UnityEngine;
using EnglewoodLAB.UI;
using EnglewoodLAB.Command;
using EnglewoodLAB.Constants;
using EnglewoodLAB.Management;
namespace EnglewoodLAB.UI
{
public class FloorControlPanel : UIPanel
{
private int[] floors = new int[] { 2, 6 };
public int strartFloorIndex;
public Dictionary<int, FloorControlItem> floorButtons = new Dictionary<int, FloorControlItem>();
public FloorControlItem itemPrefab;
public FloorControlItem currentFloorItem;
public Action<int> onValueChanged;
public override async UniTask Init()
{
Debug.Log("FloorControlPanel: Init called.");
string prefabPath = $"{ResourceURL.UIPrefabFolderPath}{nameof(FloorControlItem)}";
itemPrefab = Resources.Load<FloorControlItem>(prefabPath);
if (itemPrefab == null)
{
Debug.LogError($"[FloorControlPanel] Failed to load 'FloorControlItem' prefab from Resources. Path was: '{prefabPath}'. Please check the path and ensure the prefab exists.", this.gameObject);
gameObject.SetActive(false);
return;
}
SettingButtons(floors);
gameObject.SetActive(false);
await UniTask.CompletedTask;
}
public void SettingButtons(int[] usedFloors)
{
// Destroy existing buttons and clear the dictionary to prevent duplicates on re-initialization.
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
floorButtons.Clear();
for (int i = 0; i < usedFloors.Length; i++)
{
var floorButton = Instantiate(itemPrefab, transform);
floorButton.SettingButton(usedFloors[i]);
floorButton.onClickButton += OnClickButton;
floorButtons.Add(i, floorButton);
}
ChangedCurrentFloor(strartFloorIndex);
Debug.Log($"FloorControlPanel: Initial floor set to {usedFloors.Length} in SettingButtons.");
}
private void OnClickButton(FloorControlItem currentButton)
{
OnValueChanged(currentButton.value);
//SceneMain.Instance.uiManager.GetCanvas<StaticCanvas>().GetPanel<BottomLeftToolbar>().HideItem();
}
public void ChangedCurrentFloor(int value)
{
OnValueChanged(value);
}
private int curValue;
private void OnValueChanged(int value)
{
if (curValue == value)
return;
CommandManager.Instance.Execute(new ChangedFloorCommand(value));
curValue = value;
onValueChanged?.Invoke(value);
}
}
}