Fix actions not showing the provider and not showing anything when dragging actions without icons (#11897)

This commit is contained in:
DrSmugleaf
2022-10-13 19:14:53 +02:00
committed by GitHub
parent 058260cf75
commit 69bee529c5
4 changed files with 210 additions and 62 deletions

View File

@@ -34,6 +34,7 @@ namespace Content.Client.Actions
public event Action<ActionType>? ActionAdded;
public event Action<ActionType>? ActionRemoved;
public event OnActionReplaced? ActionReplaced;
public event Action? ActionsUpdated;
public event Action<ActionsComponent>? LinkActions;
public event Action? UnlinkActions;
public event Action? ClearAssignments;
@@ -95,6 +96,8 @@ namespace Content.Client.Actions
{
ActionAdded?.Invoke(action);
}
ActionsUpdated?.Invoke();
}
protected override void AddActionInternal(ActionsComponent comp, ActionType action)

View File

@@ -12,6 +12,7 @@ using Content.Client.UserInterface.Systems.Actions.Windows;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Input;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
@@ -100,6 +101,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
_actionsBar.PageButtons.LeftArrow.OnPressed += OnLeftArrowPressed;
_actionsBar.PageButtons.RightArrow.OnPressed += OnRightArrowPressed;
_actionsSystem.ActionReplaced += OnActionReplaced;
_actionsSystem.ActionsUpdated += OnActionsUpdated;
UpdateFilterLabel();
SearchAndDisplay();
@@ -152,13 +154,16 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
public void OnStateExited(GameplayState state)
{
_actionsSystem.ActionReplaced -= OnActionReplaced;
_actionsSystem.ActionsUpdated -= OnActionsUpdated;
if (_window != null)
{
_window.OnOpen -= OnWindowOpened;
_window.OnClose -= OnWindowClosed;
_window.ClearButton.OnPressed -= OnClearPressed;
_window.SearchBar.OnTextChanged -= OnSearchChanged;
_window.FilterButton.OnItemSelected -= OnFilterSelected;
_window.OnOpen += OnWindowOpened;
_window.OnClose += OnWindowClosed;
_window.ClearButton.OnPressed += OnClearPressed;
_window.SearchBar.OnTextChanged += OnSearchChanged;
_window.FilterButton.OnItemSelected += OnFilterSelected;
_window.Dispose();
_window = null;
@@ -166,8 +171,8 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
if (_actionsBar != null)
{
_actionsBar.PageButtons.LeftArrow.OnPressed -= OnLeftArrowPressed;
_actionsBar.PageButtons.RightArrow.OnPressed -= OnRightArrowPressed;
_actionsBar.PageButtons.LeftArrow.OnPressed += OnLeftArrowPressed;
_actionsBar.PageButtons.RightArrow.OnPressed += OnRightArrowPressed;
}
if (_actionButton != null)
@@ -224,7 +229,18 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
foreach (var button in _container.GetButtons())
{
if (button.Action == existing)
button.UpdateData(_entities, action);
button.UpdateData(action);
}
}
private void OnActionsUpdated()
{
if (_container == null)
return;
foreach (var button in _container.GetButtons())
{
button.UpdateIcons();
}
}
@@ -293,7 +309,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
{
var button = new ActionButton {Locked = true};
button.UpdateData(_entities, action);
button.UpdateData(action);
button.ActionPressed += OnWindowActionPressed;
button.ActionUnpressed += OnWindowActionUnPressed;
button.ActionFocusExited += OnWindowActionFocusExisted;
@@ -355,7 +371,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
return;
}
if (button.TryReplaceWith(_entities, type) &&
if (button.TryReplaceWith(type) &&
_container != null &&
_container.TryGetButtonIndex(button, out position))
{
@@ -474,7 +490,23 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private bool OnMenuBeginDrag()
{
_dragShadow.Texture = _menuDragHelper.Dragged?.IconTexture;
if (_menuDragHelper.Dragged?.Action is { } action)
{
if (action.EntityIcon != null)
{
_dragShadow.Texture = _entities.GetComponent<SpriteComponent>(action.EntityIcon.Value).Icon?
.GetFrame(RSI.State.Direction.South, 0);
}
else if (action.Icon != null)
{
_dragShadow.Texture = action.Icon!.Frame0();
}
else
{
_dragShadow.Texture = null;
}
}
LayoutContainer.SetPosition(_dragShadow, UIManager.MousePositionScaled.Position - (32, 32));
return true;
}
@@ -488,6 +520,7 @@ public sealed class ActionUIController : UIController, IOnStateChanged<GameplayS
private void OnMenuEndDrag()
{
_dragShadow.Texture = null;
_dragShadow.Visible = false;
}

View File

@@ -1,6 +1,7 @@
using Content.Client.Actions.UI;
using Content.Client.Cooldown;
using Content.Client.Stylesheets;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
@@ -10,6 +11,8 @@ using Robust.Client.Utility;
using Robust.Shared.Input;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.Controls.BoxContainer;
using static Robust.Client.UserInterface.Controls.TextureRect;
namespace Content.Client.UserInterface.Systems.Actions.Controls;
@@ -19,6 +22,7 @@ public sealed class ActionButton : Control
private bool _beingHovered;
private bool _depressed;
private bool _toggled;
private bool _spriteViewDirty;
public BoundKeyFunction? KeyBind
{
@@ -36,16 +40,12 @@ public sealed class ActionButton : Control
public readonly TextureRect Button;
public readonly PanelContainer HighlightRect;
public readonly TextureRect Icon;
private readonly TextureRect _bigActionIcon;
private readonly TextureRect _smallActionIcon;
public readonly Label Label;
public readonly SpriteView Sprite;
public readonly CooldownGraphic Cooldown;
public Texture? IconTexture
{
get => Icon.Texture;
private set => Icon.Texture = value;
}
private readonly SpriteView _smallItemSpriteView;
private readonly SpriteView _bigItemSpriteView;
public ActionType? Action { get; private set; }
public bool Locked { get; set; }
@@ -68,12 +68,19 @@ public sealed class ActionButton : Control
MinSize = (32, 32),
Visible = false
};
Icon = new TextureRect
_bigActionIcon = new TextureRect
{
Name = "Icon",
TextureScale = new Vector2(2, 2),
MaxSize = (64, 64),
Stretch = TextureRect.StretchMode.Scale
HorizontalExpand = true,
VerticalExpand = true,
Stretch = StretchMode.Scale,
Visible = false
};
_smallActionIcon = new TextureRect
{
HorizontalAlignment = HAlignment.Right,
VerticalAlignment = VAlignment.Bottom,
Stretch = StretchMode.Scale,
Visible = false
};
Label = new Label
{
@@ -82,24 +89,55 @@ public sealed class ActionButton : Control
VerticalAlignment = VAlignment.Top,
Margin = new Thickness(5, 0, 0, 0)
};
Sprite = new SpriteView
_bigItemSpriteView = new SpriteView
{
Name = "Sprite",
OverrideDirection = Direction.South
Name = "Big Sprite",
HorizontalExpand = true,
VerticalExpand = true,
Scale = (2, 2),
Visible = false,
OverrideDirection = Direction.South,
};
_smallItemSpriteView = new SpriteView
{
Name = "Small Sprite",
HorizontalAlignment = HAlignment.Right,
VerticalAlignment = VAlignment.Bottom,
Visible = false,
OverrideDirection = Direction.South,
};
// padding to the left of the small icon
var paddingBoxItemIcon = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
VerticalExpand = true,
MinSize = (64, 64)
};
paddingBoxItemIcon.AddChild(new Control()
{
MinSize = (32, 32),
});
paddingBoxItemIcon.AddChild(new Control
{
Children =
{
_smallActionIcon,
_smallItemSpriteView
}
});
Cooldown = new CooldownGraphic {Visible = false};
AddChild(_bigActionIcon);
AddChild(_bigItemSpriteView);
AddChild(Button);
AddChild(HighlightRect);
AddChild(Icon);
AddChild(Label);
AddChild(Sprite);
AddChild(Cooldown);
AddChild(paddingBoxItemIcon);
Button.Modulate = new Color(255, 255, 255, 150);
Icon.Modulate = new Color(255, 255, 255, 150);
OnThemeUpdated();
OnThemeUpdated();
OnKeyBindDown += args =>
@@ -149,59 +187,134 @@ public sealed class ActionButton : Control
ActionFocusExited?.Invoke(this);
}
public bool TryReplaceWith(IEntityManager entityManager, ActionType action)
private void UpdateItemIcon()
{
var entityManager = IoCManager.Resolve<IEntityManager>();
if (Action?.EntityIcon != null && !entityManager.EntityExists(Action.EntityIcon))
{
// This is almost certainly because a player received/processed their own actions component state before
// being send the entity in their inventory that enabled this action.
// Defer updating icons to the next FrameUpdate().
_spriteViewDirty = true;
return;
}
if (Action?.EntityIcon == null ||
!entityManager.TryGetComponent(Action.EntityIcon.Value, out SpriteComponent? sprite))
{
_bigItemSpriteView.Visible = false;
_bigItemSpriteView.Sprite = null;
_smallItemSpriteView.Visible = false;
_smallItemSpriteView.Sprite = null;
}
else
{
switch (Action.ItemIconStyle)
{
case ItemActionIconStyle.BigItem:
_bigItemSpriteView.Visible = true;
_bigItemSpriteView.Sprite = sprite;
_smallItemSpriteView.Visible = false;
_smallItemSpriteView.Sprite = null;
break;
case ItemActionIconStyle.BigAction:
_bigItemSpriteView.Visible = false;
_bigItemSpriteView.Sprite = null;
_smallItemSpriteView.Visible = true;
_smallItemSpriteView.Sprite = sprite;
break;
case ItemActionIconStyle.NoItem:
_bigItemSpriteView.Visible = false;
_bigItemSpriteView.Sprite = null;
_smallItemSpriteView.Visible = false;
_smallItemSpriteView.Sprite = null;
break;
}
}
}
private void SetActionIcon(Texture? texture)
{
if (texture == null || Action == null)
{
_bigActionIcon.Texture = null;
_bigActionIcon.Visible = false;
_smallActionIcon.Texture = null;
_smallActionIcon.Visible = false;
}
else if (Action.EntityIcon != null && Action.ItemIconStyle == ItemActionIconStyle.BigItem)
{
_smallActionIcon.Texture = texture;
_smallActionIcon.Modulate = Action.IconColor;
_smallActionIcon.Visible = true;
_bigActionIcon.Texture = null;
_bigActionIcon.Visible = false;
}
else
{
_bigActionIcon.Texture = texture;
_bigActionIcon.Modulate = Action.IconColor;
_bigActionIcon.Visible = true;
_smallActionIcon.Texture = null;
_smallActionIcon.Visible = false;
}
}
public void UpdateIcons()
{
UpdateItemIcon();
if (Action == null)
{
SetActionIcon(null);
return;
}
if ((Controller.SelectingTargetFor?.Action == Action || Action.Toggled) && Action.IconOn != null)
SetActionIcon(Action.IconOn.Frame0());
else
SetActionIcon(Action.Icon?.Frame0());
}
public bool TryReplaceWith(ActionType action)
{
if (Locked)
{
return false;
}
UpdateData(entityManager, action);
UpdateData(action);
return true;
}
public void UpdateData(IEntityManager entityManager, ActionType action)
public void UpdateData(ActionType action)
{
Action = action;
if (action.Icon != null)
{
IconTexture = GetIcon();
Sprite.Sprite = null;
return;
}
if (action.Provider == null ||
!entityManager.TryGetComponent(action.Provider.Value, out SpriteComponent? sprite))
{
return;
}
IconTexture = null;
Sprite.Sprite = sprite;
UpdateIcons();
}
public void ClearData()
{
Action = null;
IconTexture = null;
Sprite.Sprite = null;
Cooldown.Visible = false;
Cooldown.Progress = 1;
}
private Texture? GetIcon()
{
if (Action == null)
return null;
return _toggled ? (Action.IconOn ?? Action.Icon)?.Frame0() : Action.Icon?.Frame0();
UpdateIcons();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (_spriteViewDirty)
{
_spriteViewDirty = false;
UpdateIcons();
}
if (Action?.Cooldown != null)
{
Cooldown.FromTime(Action.Cooldown.Value.Start, Action.Cooldown.Value.End);
@@ -210,7 +323,6 @@ public sealed class ActionButton : Control
if (Action != null && _toggled != Action.Toggled)
{
_toggled = Action.Toggled;
IconTexture = GetIcon();
}
}

View File

@@ -42,7 +42,7 @@ public class ActionButtonContainer : GridContainer
if (action == null)
continue;
((ActionButton) GetChild(i)).UpdateData(_entityManager, action);
((ActionButton) GetChild(i)).UpdateData(action);
}
}