Moves guidebook UI logic to a UI Controller, some tweaks (#14601)

This commit is contained in:
AJCM-git
2023-03-22 23:41:43 -04:00
committed by GitHub
parent 6b2558456b
commit 7a6fddce4f
10 changed files with 268 additions and 127 deletions

View File

@@ -1,8 +1,7 @@
using System.Linq;
using Content.Client.Guidebook.Controls;
using Content.Client.Guidebook.Components;
using Content.Client.Light;
using Content.Client.Verbs;
using Content.Shared.Input;
using Content.Shared.Interaction;
using Content.Shared.Light.Component;
using Content.Shared.Speech;
@@ -10,10 +9,7 @@ using Content.Shared.Tag;
using Content.Shared.Verbs;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.Guidebook;
@@ -24,25 +20,21 @@ namespace Content.Client.Guidebook;
public sealed class GuidebookSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly VerbSystem _verbSystem = default!;
[Dependency] private readonly RgbLightControllerSystem _rgbLightControllerSystem = default!;
[Dependency] private readonly SharedPointLightSystem _pointLightSystem = default!;
[Dependency] private readonly TagSystem _tags = default!;
private GuidebookWindow _guideWindow = default!;
public event Action<List<string>, List<string>?, string?, bool, string?>? OnGuidebookOpen;
public const string GuideEmbedTag = "GuideEmbeded";
/// <inheritdoc/>
public override void Initialize()
{
CommandBinds.Builder
.Bind(ContentKeyFunctions.OpenGuidebook,
new PointerInputCmdHandler(HandleOpenGuidebook))
.Register<GuidebookSystem>();
_guideWindow = new GuidebookWindow();
SubscribeLocalEvent<GuideHelpComponent, GetVerbsEvent<ExamineVerb>>(OnGetVerbs);
SubscribeLocalEvent<GuideHelpComponent, ActivateInWorldEvent>(OnInteract);
SubscribeLocalEvent<GuidebookControlsTestComponent, InteractHandEvent>(OnGuidebookControlsTestInteractHand);
SubscribeLocalEvent<GuidebookControlsTestComponent, ActivateInWorldEvent>(OnGuidebookControlsTestActivateInWorld);
SubscribeLocalEvent<GuidebookControlsTestComponent, GetVerbsEvent<AlternativeVerb>>(
@@ -58,12 +50,21 @@ public sealed class GuidebookSystem : EntitySystem
{
Text = Loc.GetString("guide-help-verb"),
Icon = new SpriteSpecifier.Texture(new ResourcePath("/Textures/Interface/VerbIcons/information.svg.192dpi.png")),
Act = () => OpenGuidebook(component.Guides, includeChildren: component.IncludeChildren, selected: component.Guides[0]),
Act = () => OnGuidebookOpen?.Invoke(component.Guides, null, null, component.IncludeChildren, component.Guides[0]),
ClientExclusive = true,
CloseMenu = true
});
}
private void OnInteract(EntityUid uid, GuideHelpComponent component, ActivateInWorldEvent args)
{
if (!component.OpenOnActivation || component.Guides.Count == 0 || _tags.HasTag(uid, GuideEmbedTag))
return;
OnGuidebookOpen?.Invoke(component.Guides, null, null, component.IncludeChildren, component.Guides[0]);
args.Handled = true;
}
private void OnGuidebookControlsTestGetAlternateVerbs(EntityUid uid, GuidebookControlsTestComponent component, GetVerbsEvent<AlternativeVerb> args)
{
args.Verbs.Add(new AlternativeVerb()
@@ -81,8 +82,8 @@ public sealed class GuidebookSystem : EntitySystem
{
Act = () =>
{
var light = EnsureComp<PointLightComponent>(uid); // RGB demands this.
light.Enabled = false;
EnsureComp<PointLightComponent>(uid); // RGB demands this.
_pointLightSystem.SetEnabled(uid, false);
var rgb = EnsureComp<RgbLightControllerComponent>(uid);
var sprite = EnsureComp<SpriteComponent>(uid);
@@ -143,103 +144,4 @@ public sealed class GuidebookSystem : EntitySystem
var activateMsg = new InteractHandEvent(user, activated);
RaiseLocalEvent(activated, activateMsg, true);
}
private bool HandleOpenGuidebook(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (args.State != BoundKeyState.Down)
return false;
OpenGuidebook();
return true;
}
/// <summary>
/// Opens the guidebook.
/// </summary>
/// <param name="guides">What guides should be shown. If not specified, this will instead raise a <see
/// cref="GetGuidesEvent"/> and automatically include all guide prototypes.</param>
/// <param name="rootEntries">A list of guides that should form the base of the table of contents. If not specified,
/// this will automatically simply be a list of all guides that have no parent.</param>
/// <param name="forceRoot">This forces a singular guide to contain all other guides. This guide will
/// contain its own children, in addition to what would normally be the root guides if this were not
/// specified.</param>
/// <param name="includeChildren">Whether or not to automatically include child entries. If false, this will ONLY
/// show the specified entries</param>
/// <param name="selected">The guide whose contents should be displayed when the guidebook is opened</param>
public bool OpenGuidebook(
Dictionary<string, GuideEntry>? guides = null,
List<string>? rootEntries = null,
string? forceRoot = null,
bool includeChildren = true,
string? selected = null)
{
_guideWindow.OpenCenteredRight();
if (guides == null)
{
var ev = new GetGuidesEvent()
{
Guides = _prototypeManager.EnumeratePrototypes<GuideEntryPrototype>().ToDictionary(x => x.ID, x => (GuideEntry) x)
};
RaiseLocalEvent(ev);
guides = ev.Guides;
}
else if (includeChildren)
{
var oldGuides = guides;
guides = new(oldGuides);
foreach (var guide in oldGuides.Values)
{
RecursivelyAddChildren(guide, guides);
}
}
_guideWindow.UpdateGuides(guides, rootEntries, forceRoot, selected);
return true;
}
public bool OpenGuidebook(
List<string> guideList,
List<string>? rootEntries = null,
string? forceRoot = null,
bool includeChildren = true,
string? selected = null)
{
Dictionary<string, GuideEntry> guides = new();
foreach (var guideId in guideList)
{
if (!_prototypeManager.TryIndex<GuideEntryPrototype>(guideId, out var guide))
{
Logger.Error($"Encountered unknown guide prototype: {guideId}");
continue;
}
guides.Add(guideId, guide);
}
return OpenGuidebook(guides, rootEntries, forceRoot, includeChildren, selected);
}
private void RecursivelyAddChildren(GuideEntry guide, Dictionary<string, GuideEntry> guides)
{
foreach (var childId in guide.Children)
{
if (guides.ContainsKey(childId))
continue;
if (!_prototypeManager.TryIndex<GuideEntryPrototype>(childId, out var child))
{
Logger.Error($"Encountered unknown guide prototype: {childId} as a child of {guide.Id}. If the child is not a prototype, it must be directly provided.");
continue;
}
guides.Add(childId, child);
RecursivelyAddChildren(child, guides);
}
}
}
public sealed class GetGuidesEvent : EntityEventArgs
{
public Dictionary<string, GuideEntry> Guides { get; init; } = new();
}