Files
2026-04-07 14:52:34 +07:00

189 lines
6.4 KiB
C#

using BrewMonster.Assets.PerfectWorld.Scripts.UI.GamePlay;
using BrewMonster.UI;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster
{
/// <summary>Action/command palette for quickbar assign; same data layout as CDlgSkillSubAction, toggle selection only.</summary>
public class DlgAssignSubAction : AUIDialog
{
private static readonly int[] ObjCount = { 8, 2, 2, 27 };
private readonly List<ActionAssignRow> m_rows = new List<ActionAssignRow>();
[SerializeField] private Transform orderContain;
[SerializeField] private AUIToggleActionAssign orderTemplate;
[SerializeField] private AUIToggleActionAssign actionTemplate;
[SerializeField] private Transform actionContain;
[Serializable]
public struct ActionAssignRow
{
public AUIToggleActionAssign toggle;
public TextMeshProUGUI label;
}
public override void Awake()
{
base.Awake();
if (orderContain == null || actionContain == null || orderTemplate == null || actionTemplate == null)
{
BMLogger.LogError("DlgAssignSubAction: assign orderContain, actionContain, orderTemplate, actionTemplate.");
return;
}
orderTemplate.gameObject.SetActive(false);
actionTemplate.gameObject.SetActive(false);
InitRows();
}
private void InitRows()
{
m_rows.Clear();
for (int setIdx = 0; setIdx < 3; setIdx++)
{
for (int j = 0; j < ObjCount[setIdx]; j++)
{
var tp = Instantiate(orderTemplate, orderContain);
tp.gameObject.SetActive(true);
WireRow(tp, setIdx, j);
}
}
for (int j = 0; j < ObjCount[3]; j++)
{
var tp = Instantiate(actionTemplate, actionContain);
tp.gameObject.SetActive(true);
WireRow(tp, 3, j);
}
}
private void WireRow(AUIToggleActionAssign tp, int setIndex, int indexInSet)
{
tp.SetActionSlotIndices(setIndex, indexInSet);
var tgl = tp.GetUIToggle();
m_rows.Add(new ActionAssignRow
{
toggle = tp,
label = tp.GetComponentInChildren<TextMeshProUGUI>()
});
}
public override void OnEnable()
{
base.OnEnable();
UpdateView();
SubscribeAssignEvents();
HookActionToggles();
}
public override void OnDisable()
{
UnhookActionToggles();
UnsubscribeAssignEvents();
base.OnDisable();
}
private void SubscribeAssignEvents()
{
EventBus.Unsubscribe<OnAssignActionEvent>(OnAssignActionBusEvent);
EventBus.Subscribe<OnAssignActionEvent>(OnAssignActionBusEvent);
}
private void UnsubscribeAssignEvents()
{
EventBus.Unsubscribe<OnAssignActionEvent>(OnAssignActionBusEvent);
}
private void HookActionToggles()
{
UnhookActionToggles();
foreach (var row in m_rows)
{
if (row.toggle == null) continue;
row.toggle.OnActionAssignToggleChanged += OnActionAssignToggleChanged;
}
}
private void UnhookActionToggles()
{
foreach (var row in m_rows)
{
if (row.toggle == null) continue;
row.toggle.OnActionAssignToggleChanged -= OnActionAssignToggleChanged;
}
}
private void OnActionAssignToggleChanged(bool isOn, int actionSetIndex, int shortcutIndexInSet)
{
EventBus.Publish(new AssignActionSelectionChangedEvent(actionSetIndex, shortcutIndexInSet, isOn));
}
private void OnAssignActionBusEvent(OnAssignActionEvent e)
{
foreach (var row in m_rows)
{
if (row.toggle == null) continue;
if (row.toggle.GetActionSetIndexForAssign() != e.actionSetIndex) continue;
if (row.toggle.GetShortcutIndexInSetForAssign() != e.shortcutIndexInSet) continue;
row.toggle.UncheckAfterAssign();
return;
}
}
private void UpdateView()
{
var gameUIMan = CECUIManager.Instance?.GetInGameUIMan();
if (gameUIMan == null) return;
var pGameRun = CECGameRun.Instance;
if (pGameRun == null) return;
CECShortcutSet[] a_pSC =
{
pGameRun.GetGenCmdShortcuts(),
pGameRun.GetTeamCmdShortcuts(),
pGameRun.GetTradeCmdShortcuts(),
pGameRun.GetPoseCmdShortcuts()
};
int count = 0;
for (int i = 0; i < a_pSC.Length; i++)
{
for (int j = 0; j < ObjCount[i]; j++)
{
if (count >= m_rows.Count) return;
var row = m_rows[count];
var tp = row.toggle;
var pLabel = row.label;
count++;
if (tp == null) continue;
if (j < a_pSC[i].GetShortcutNum())
{
var pSCThis = a_pSC[i].GetShortcut(j);
tp.SetDataPtr(pSCThis, "ptr_CECShortcut");
string strFile = pSCThis != null ? pSCThis.GetIconFile() : "";
gameUIMan.SetCover(tp, strFile, EC_GAMEUI_ICONS.ICONS_ACTION);
if (pLabel != null)
pLabel.SetText(pSCThis != null ? pSCThis.GetDesc() : "");
tp.gameObject.SetActive(true);
}
else
{
tp.Clear();
if (pLabel != null)
pLabel.SetText("");
tp.gameObject.SetActive(false);
}
}
}
}
}
}