Files
wwdpublic/Content.Client/UserInterface/Systems/Actions/Controls/ActionButtonContainer.cs
SimpleStation14 6b236e4352 Mirror: Fix crash on load when no slot key assigned (#309)
## Mirror of PR #26354: [Fix crash on load when no slot key
assigned](https://github.com/space-wizards/space-station-14/pull/26354)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `e69723153dc571e1461c99ac5c7d82a1992e011d`

PR opened by <img
src="https://avatars.githubusercontent.com/u/81056464?v=4"
width="16"/><a href="https://github.com/wrexbe"> wrexbe</a> at
2024-03-23 02:21:12 UTC

---

PR changed 1 files with 5 additions and 4 deletions.

The PR had the following labels:


---

<details open="true"><summary><h1>Original Body</h1></summary>

> I fixed the game crashing if you don't have hotkeys assigned to the
action bar slots when the game loads


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-05 15:56:56 -04:00

128 lines
3.4 KiB
C#

using System.Linq;
using Content.Client.Actions;
using Content.Shared.Input;
using Robust.Client.Input;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface.Systems.Actions.Controls;
[Virtual]
public class ActionButtonContainer : GridContainer
{
[Dependency] private readonly IEntityManager _entity = default!;
[Dependency] private readonly IInputManager _input = default!;
public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionPressed;
public event Action<GUIBoundKeyEventArgs, ActionButton>? ActionUnpressed;
public event Action<ActionButton>? ActionFocusExited;
public ActionButtonContainer()
{
IoCManager.InjectDependencies(this);
}
public ActionButton this[int index]
{
get => (ActionButton) GetChild(index);
}
private void BuildActionButtons(int count)
{
var keys = ContentKeyFunctions.GetHotbarBoundKeys();
Children.Clear();
for (var index = 0; index < count; index++)
{
Children.Add(MakeButton(index));
}
ActionButton MakeButton(int index)
{
var button = new ActionButton(_entity);
if (!keys.TryGetValue(index, out var boundKey))
return button;
button.KeyBind = boundKey;
if (_input.TryGetKeyBinding(boundKey, out var binding))
{
button.Label.Text = binding.GetKeyString();
}
return button;
}
}
public void SetActionData(ActionsSystem system, params EntityUid?[] actionTypes)
{
var uniqueCount = Math.Min(system.GetClientActions().Count(), actionTypes.Length + 1);
if (ChildCount != uniqueCount)
BuildActionButtons(uniqueCount);
for (var i = 0; i < uniqueCount; i++)
{
if (!actionTypes.TryGetValue(i, out var action))
action = null;
((ActionButton) GetChild(i)).UpdateData(action, system);
}
}
public void ClearActionData()
{
foreach (var button in Children)
{
((ActionButton) button).ClearData();
}
}
protected override void ChildAdded(Control newChild)
{
base.ChildAdded(newChild);
if (newChild is not ActionButton button)
return;
button.ActionPressed += ActionPressed;
button.ActionUnpressed += ActionUnpressed;
button.ActionFocusExited += ActionFocusExited;
}
protected override void ChildRemoved(Control newChild)
{
if (newChild is not ActionButton button)
return;
button.ActionPressed -= ActionPressed;
button.ActionUnpressed -= ActionUnpressed;
button.ActionFocusExited -= ActionFocusExited;
}
public bool TryGetButtonIndex(ActionButton button, out int position)
{
if (button.Parent != this)
{
position = 0;
return false;
}
position = button.GetPositionInParent();
return true;
}
public IEnumerable<ActionButton> GetButtons()
{
foreach (var control in Children)
{
if (control is ActionButton button)
yield return button;
}
}
~ActionButtonContainer()
{
UserInterfaceManager.GetUIController<ActionUIController>().RemoveActionContainer();
}
}