Finished converting to new UI.

This commit is contained in:
Max 2025-02-27 15:52:40 +01:00
parent f70f89324b
commit 105055b307
37 changed files with 24044 additions and 1227 deletions

View file

@ -0,0 +1,46 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class ChestPopUpController : MonoBehaviour
{
public event Action BlackDieSelected;
public event Action KeySelected;
public event Action ArmorAndDiamondSelected;
private Button _blackDie;
private Button _key;
private Button _armorAndDiamond;
private void OnEnable()
{
VisualElement root = GetComponent<UIDocument>().rootVisualElement;
_blackDie = root.Q<Button>("BlackDie");
_blackDie.clicked += () => BlackDieSelected?.Invoke();
_key = root.Q<Button>("Key");
_key.clicked += () => KeySelected?.Invoke();
_armorAndDiamond = root.Q<Button>("ArmorAndDiamond");
_armorAndDiamond.clicked += () => ArmorAndDiamondSelected?.Invoke();
}
public void DisableBlackDie()
{
_blackDie.SetEnabled(false);
}
public void DisableKey()
{
_key.SetEnabled(false);
}
public void DisableArmorAndDiamond()
{
_armorAndDiamond.SetEnabled(false);
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f775e83b4c2599d468f1a1ce830ccff8

View file

@ -0,0 +1,265 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
enum DieState
{
Normal,
OnlyDieSelected,
SelectedOfPair,
Used
}
public class InGameHUDController : MonoBehaviour
{
private const string PASS_TEXT = "Pass";
private const string ROLL_TEXT = "Roll";
public event Action StartingBlackDieAbilityClicked;
public event Action BlackDieAbilityClicked;
public event Action KeyAbilityClicked;
public event Action RollClicked;
public event Action PassClicked;
public List<Button> WhiteDiceButtons = new List<Button>();
public Button BlackDieButton;
private Toggle _healthDiamondSelected;
private Toggle _torchSelected;
private Toggle _blackDieSelected;
private Button _rollOrPassButton;
private bool _rollMode = true;
private List<Toggle> _startingBlackDieAbilityUses = new List<Toggle>();
private int _startingBlackDieAbilityUsesUsed = 0;
private List<Toggle> _blackDieAbilityUses = new List<Toggle>();
private int _blackDieAbilityUsesUsed = 0;
private List<Toggle> _keyAbilityUses = new List<Toggle>();
private int _keyAbilityUsesUsed = 0;
private List<Toggle> _armorUses = new List<Toggle>();
private int _armorUsesUsed = 0;
public void OnEnable()
{
VisualElement root = GetComponent<UIDocument>().rootVisualElement;
GroupBox healthAndDiamondGroupBox = root.Q<GroupBox>("HeartAndDiamondUses");
GroupBox keyGroupBox = root.Q<GroupBox>("KeyUses");
GroupBox blackDieGroupBox = root.Q<GroupBox>("BlackDieUses");
GroupBox startingBlackDieGroupBox = root.Q<GroupBox>("StartingBlackDieUses");
startingBlackDieGroupBox.AddManipulator(new Clickable(evt => HandleStartingBlackDieAbilityClicked()));
GroupBox diceGroupBox = root.Q<GroupBox>("Dice");
foreach (Button diceButton in diceGroupBox.Children())
{
if (diceButton.name == "BlackDie")
{
BlackDieButton = diceButton;
}
else
{
WhiteDiceButtons.Add(diceButton);
}
}
foreach (Toggle use in startingBlackDieGroupBox.Children())
{
use.RegisterCallback<ClickEvent>(evt =>
{
use.value = !use.value; // User clicking check does not check or uncheck toggle.
HandleStartingBlackDieAbilityClicked();
});
_startingBlackDieAbilityUses.Add(use);
}
foreach (Toggle use in blackDieGroupBox.Children())
{
_blackDieAbilityUses.Add(use);
use.RegisterCallback<ClickEvent>(evt =>
{
use.value = !use.value; // User clicking check does not check or uncheck toggle.
BlackDieAbilityClicked?.Invoke();
});
}
foreach (Toggle use in keyGroupBox.Children())
{
_keyAbilityUses.Add(use);
use.RegisterCallback<ClickEvent>(evt =>
{
use.value = !use.value; // User clicking check does not check or uncheck toggle.
KeyAbilityClicked?.Invoke();
});
}
foreach (Toggle use in healthAndDiamondGroupBox.Children())
{
_armorUses.Add(use);
use.RegisterCallback<ClickEvent>(evt =>
{
use.value = !use.value; // User clicking check does not check or uncheck toggle.
});
}
_rollOrPassButton = root.Q<Button>("RollPass");
_rollOrPassButton.clicked += HandleRollOrPassClicked;
}
public void SetBlackDieAbilityEnabled()
{
foreach (Toggle use in _blackDieAbilityUses)
{
use.SetEnabled(true);
}
}
public void SetKeyAbilityEnabled()
{
foreach (Toggle use in _keyAbilityUses)
{
use.SetEnabled(true);
}
}
public void SetArmorAbilityEnabled()
{
foreach (Toggle use in _armorUses)
{
use.SetEnabled(true);
}
}
public void MarkKeyAbilityUsed()
{
_keyAbilityUses[_keyAbilityUsesUsed].value = true;
_keyAbilityUsesUsed++;
}
public void MarkBlackDieAbilityUsed()
{
_blackDieAbilityUses[_blackDieAbilityUsesUsed].value = true;
_blackDieAbilityUsesUsed++;
}
public void UnmarkBlackDieAbilityUsed()
{
_blackDieAbilityUses[_blackDieAbilityUsesUsed].value = false;
_blackDieAbilityUsesUsed--;
}
public void MarkStartingBlackDieAbilityUsed()
{
_startingBlackDieAbilityUses[_startingBlackDieAbilityUsesUsed].value = true;
_startingBlackDieAbilityUsesUsed++;
}
public void UnmarkStartingBlackDieAbilityUsed()
{
_startingBlackDieAbilityUses[_startingBlackDieAbilityUsesUsed].value = false;
_startingBlackDieAbilityUsesUsed--;
}
public void SwitchToDiceRoller()
{
_rollMode = true;
_rollOrPassButton.text = ROLL_TEXT;
}
public void SwitchToPass()
{
_rollMode = false;
_rollOrPassButton.text = PASS_TEXT;
}
public void SetDiceRollerEnabled(bool enabled)
{
if (enabled)
{
SwitchToDiceRoller();
}
else
{
SwitchToPass();
}
}
public void SetDieSelectedButNotComplete(Button button)
{
SetDiceButtonState(DieState.OnlyDieSelected, button);
}
public void SetDieSelectedAndPairComplete(Button button)
{
SetDiceButtonState(DieState.SelectedOfPair, button);
}
public void RestDie(Button button)
{
SetDiceButtonState(DieState.Normal, button);
}
public void SetHealthAndDiamondSelected(bool state)
{
_healthDiamondSelected.value = state;
}
public void SetTorchSelected(bool state)
{
_torchSelected.value = state;
}
public void SetBlackDieSelected(bool state)
{
_blackDieSelected.value = state;
}
private void HandleRollOrPassClicked()
{
if (_rollMode)
{
SwitchToPass();
RollClicked?.Invoke();
}
else
{
SwitchToDiceRoller();
PassClicked?.Invoke();
}
}
private void HandleStartingBlackDieAbilityClicked()
{
StartingBlackDieAbilityClicked?.Invoke();
}
private void SetDiceButtonState(DieState state, Button die)
{
die.RemoveFromClassList("selected-of-pair");
die.RemoveFromClassList("selected");
die.SetEnabled(true);
switch (state)
{
case DieState.Used:
die.SetEnabled(false);
break;
case DieState.OnlyDieSelected:
die.AddToClassList("selected");
break;
case DieState.SelectedOfPair:
die.AddToClassList("selected-of-pair");
break;
}
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6b8775a362b77bc4bb1bcae768aa15a3

View file

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using Abilities;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
namespace UI
{
public class UIManager : MonoBehaviour
{
[SerializeField] private GameObject canvas;
[FormerlySerializedAs("hud")] [SerializeField] private InGameHUDController hudController;
[SerializeField] private GameObject chestPopUpControllerGO;
[SerializeField] private ChestPopUpController chestPopUpController;
[SerializeField] private GameObject diceGO;
[SerializeField] private DiceRoller diceRoller;
[SerializeField] private PassManager passManager;
[SerializeField] private StartingBlackDieAbility startingBlackDieAbility;
[SerializeField] private BlackDieAbility blackDieAbility;
[SerializeField] private ChestRewardSelection chestRewardSelection;
[SerializeField] private TorchAbility torchAbility;
public static UIManager Instance { get; private set; }
private Dictionary<Die, Button> _dieToButtonMapping = new Dictionary<Die, Button>();
void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void OnEnable()
{
chestPopUpController = chestPopUpControllerGO.GetComponent<ChestPopUpController>();
RoomRewards.ChestRewarded += HandleChestRewarded;
chestPopUpController.BlackDieSelected += chestRewardSelection.HandleBlackDiceSelected;
chestPopUpController.KeySelected += chestRewardSelection.HandleTorchSelected;
chestPopUpController.ArmorAndDiamondSelected += chestRewardSelection.HandleDiamondAndLifeSelected;
hudController.RollClicked += diceRoller.RollDice;
hudController.PassClicked += passManager.OnPassClicked;
diceRoller.Enabled += () => hudController.SetDiceRollerEnabled(true);
diceRoller.Disabled += () => hudController.SetDiceRollerEnabled(false);
startingBlackDieAbility.MarkUseUsedEvent += hudController.MarkStartingBlackDieAbilityUsed;
startingBlackDieAbility.UnmarkUseUsedEvent += hudController.UnmarkStartingBlackDieAbilityUsed;
hudController.StartingBlackDieAbilityClicked += startingBlackDieAbility.UseClicked;
blackDieAbility.MarkUseUsedEvent += hudController.MarkBlackDieAbilityUsed;
blackDieAbility.UnmarkUseUsedEvent += hudController.UnmarkStartingBlackDieAbilityUsed;
blackDieAbility.AbilityGained += hudController.SetBlackDieAbilityEnabled;
hudController.BlackDieAbilityClicked += blackDieAbility.UseClicked;
chestRewardSelection.ArmorAndDiamondNoLongerAvailable += chestPopUpController.DisableArmorAndDiamond;
chestRewardSelection.TorchNoLongerAvailable += chestPopUpController.DisableKey;
chestRewardSelection.BlackDieNoLongerAvailable += chestPopUpController.DisableBlackDie;
torchAbility.AbilityGained += hudController.SetKeyAbilityEnabled;
TorchAbility.TorchAbilityUsed += hudController.MarkKeyAbilityUsed;
hudController.KeyAbilityClicked += torchAbility.UseClicked;
}
private void OnDisable()
{
RoomRewards.ChestRewarded -= HandleChestRewarded;
chestPopUpController.BlackDieSelected -= BlackDieAbilityGained;
chestPopUpController.KeySelected -= TorchAbilityGained;
chestPopUpController.ArmorAndDiamondSelected -= HealthAndDiamondGained;
}
private void Start()
{
int count = 0;
foreach (Transform dieTransform in diceGO.transform)
{
Die die = dieTransform.gameObject.GetComponent<Die>();
if (die.color == DiceColor.Black)
{
_dieToButtonMapping[die] = hudController.BlackDieButton;
}
else
{
_dieToButtonMapping[die] = hudController.WhiteDiceButtons[count];
count++;
}
}
foreach (var kvp in _dieToButtonMapping)
{
Button button = kvp.Value;
Die die = kvp.Key;
button.clicked += die.DiePressed;
die.DieRollResult += result => button.text = result.ToString();
die.DieSelectedButPairNotCompleteEvent += () => hudController.SetDieSelectedButNotComplete(button);
die.DieSelectedAndPairCompleteEvent += () => hudController.SetDieSelectedAndPairComplete(button);
die.DieReset += () => hudController.RestDie(button);
}
}
private void BlackDieAbilityGained()
{
hudController.SetBlackDieSelected(true);
}
private void TorchAbilityGained()
{
hudController.SetTorchSelected(true);
}
private void HealthAndDiamondGained()
{
hudController.SetHealthAndDiamondSelected(true);
}
private void HandleChestRewarded()
{
chestPopUpControllerGO.SetActive(true);
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a277ce4246f154447a52f88a6c9937e0