Skip to content

Commit

Permalink
aac tablet refactor (#2401)
Browse files Browse the repository at this point in the history
* another thing nobody asked for

* just.. why? who did this, and why?
  • Loading branch information
MilonPL authored Dec 9, 2024
1 parent 6ced034 commit 13c1e01
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 76 deletions.
9 changes: 4 additions & 5 deletions Content.Client/DeltaV/AACTablet/UI/AACBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Content.Shared.DeltaV.AACTablet;
using Content.Shared.DeltaV.QuickPhrase;
using Robust.Shared.Prototypes;

namespace Content.Client.DeltaV.AACTablet.UI;

public sealed class AACBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;

[ViewVariables]
private AACWindow? _window;

Expand All @@ -18,21 +17,21 @@ protected override void Open()
{
base.Open();
_window?.Close();
_window = new AACWindow(this, _prototypeManager);
_window = new AACWindow();
_window.OpenCentered();

_window.PhraseButtonPressed += OnPhraseButtonPressed;
_window.OnClose += Close;
}

private void OnPhraseButtonPressed(string phraseId)
private void OnPhraseButtonPressed(ProtoId<QuickPhrasePrototype> phraseId)
{
SendMessage(new AACTabletSendPhraseMessage(phraseId));
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_window?.Dispose();
_window?.Orphan();
}
}
2 changes: 1 addition & 1 deletion Content.Client/DeltaV/AACTablet/UI/AACWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
MinSize="540 300">
<ScrollContainer HScrollEnabled="False" Name="WindowBody">
</ScrollContainer>
</controls:FancyWindow>
</controls:FancyWindow>
54 changes: 22 additions & 32 deletions Content.Client/DeltaV/AACTablet/UI/AACWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ namespace Content.Client.DeltaV.AACTablet.UI;
[GenerateTypedNameReferences]
public sealed partial class AACWindow : FancyWindow
{
private IPrototypeManager _prototypeManager;
public event Action<string>? PhraseButtonPressed;
[Dependency] private readonly IPrototypeManager _prototype = default!;
public event Action<ProtoId<QuickPhrasePrototype>>? PhraseButtonPressed;

public AACWindow(AACBoundUserInterface ui, IPrototypeManager prototypeManager)
private const float SpaceWidth = 10f;
private const float ParentWidth = 540f;
private const int ColumnCount = 4;

private const int ButtonWidth =
(int)((ParentWidth - SpaceWidth * 2) / ColumnCount - SpaceWidth * ((ColumnCount - 1f) / ColumnCount));

public AACWindow()
{
RobustXamlLoader.Load(this);
_prototypeManager = prototypeManager;
PopulateGui(ui);
IoCManager.InjectDependencies(this);
PopulateGui();
}

private void PopulateGui(AACBoundUserInterface ui)
private void PopulateGui()
{
var loc = IoCManager.Resolve<ILocalizationManager>();
var phrases = _prototypeManager.EnumeratePrototypes<QuickPhrasePrototype>().ToList();
var phrases = _prototype.EnumeratePrototypes<QuickPhrasePrototype>().ToList();

// take ALL phrases and turn them into tabs and groups, so the buttons are sorted and tabbed
var sortedTabs = phrases
Expand All @@ -38,7 +44,7 @@ private void PopulateGui(AACBoundUserInterface ui)
.OrderBy(gg => gg.Key)
.ToDictionary(
gg => gg.Key,
gg => gg.OrderBy(p => loc.GetString(p.Text)).ToList()
gg => gg.OrderBy(p => Loc.GetString(p.Text)).ToList()
)
);

Expand All @@ -49,11 +55,10 @@ private void PopulateGui(AACBoundUserInterface ui)
private TabContainer CreateTabContainer(Dictionary<string, Dictionary<string, List<QuickPhrasePrototype>>> sortedTabs)
{
var tabContainer = new TabContainer();
var loc = IoCManager.Resolve<ILocalizationManager>();

foreach (var tab in sortedTabs)
{
var tabName = loc.GetString(tab.Key);
var tabName = Loc.GetString(tab.Key);
var boxContainer = CreateBoxContainerForTab(tab.Value);
tabContainer.AddChild(boxContainer);
tabContainer.SetTabTitle(tabContainer.ChildCount - 1, tabName);
Expand All @@ -64,7 +69,7 @@ private TabContainer CreateTabContainer(Dictionary<string, Dictionary<string, Li

private BoxContainer CreateBoxContainerForTab(Dictionary<string, List<QuickPhrasePrototype>> groups)
{
var boxContainer = new BoxContainer()
var boxContainer = new BoxContainer
{
HorizontalExpand = true,
Orientation = BoxContainer.LayoutOrientation.Vertical
Expand All @@ -81,7 +86,7 @@ private BoxContainer CreateBoxContainerForTab(Dictionary<string, List<QuickPhras
return boxContainer;
}

private Label CreateHeaderForGroup(string groupName)
private static Label CreateHeaderForGroup(string groupName)
{
var header = new Label
{
Expand All @@ -96,13 +101,12 @@ private Label CreateHeaderForGroup(string groupName)

private GridContainer CreateButtonContainerForGroup(List<QuickPhrasePrototype> phrases)
{
var loc = IoCManager.Resolve<ILocalizationManager>();
var buttonContainer = CreateButtonContainer();
foreach (var phrase in phrases)
{
var text = loc.GetString(phrase.Text);
var text = Loc.GetString(phrase.Text);
var button = CreatePhraseButton(text, phrase.StyleClass);
button.OnPressed += _ => OnPhraseButtonPressed(phrase.ID);
button.OnPressed += _ => OnPhraseButtonPressed(new ProtoId<QuickPhrasePrototype>(phrase.ID));
buttonContainer.AddChild(button);
}
return buttonContainer;
Expand All @@ -121,11 +125,10 @@ private static GridContainer CreateButtonContainer()

private static Button CreatePhraseButton(string text, string styleClass)
{
var buttonWidth = GetButtonWidth();
var phraseButton = new Button
{
Access = AccessLevel.Public,
MaxSize = new Vector2(buttonWidth, buttonWidth),
MaxSize = new Vector2(ButtonWidth, ButtonWidth),
ClipText = false,
HorizontalExpand = true,
StyleClasses = { styleClass }
Expand All @@ -142,20 +145,7 @@ private static Button CreatePhraseButton(string text, string styleClass)
return phraseButton;
}

private static int GetButtonWidth()
{
var spaceWidth = 10;
var parentWidth = 540;
var columnCount = 4;

var paddingSize = spaceWidth * 2;
var gutterScale = (columnCount - 1) / columnCount;
var columnWidth = (parentWidth - paddingSize) / columnCount;
var buttonWidth = columnWidth - spaceWidth * gutterScale;
return buttonWidth;
}

private void OnPhraseButtonPressed(string phraseId)
private void OnPhraseButtonPressed(ProtoId<QuickPhrasePrototype> phraseId)
{
PhraseButtonPressed?.Invoke(phraseId);
}
Expand Down
6 changes: 4 additions & 2 deletions Content.Server/DeltaV/AACTablet/AACTabletComponent.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;

namespace Content.Server.DeltaV.AACTablet;

[RegisterComponent]
[RegisterComponent, AutoGenerateComponentPause]
public sealed partial class AACTabletComponent : Component
{
// Minimum time between each phrase, to prevent spam
[DataField]
public TimeSpan Cooldown = TimeSpan.FromSeconds(1);

// Time that the next phrase can be sent.
[DataField]
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField]
public TimeSpan NextPhrase;
}
26 changes: 11 additions & 15 deletions Content.Server/DeltaV/AACTablet/AACTabletSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Content.Server.Chat.Systems;
using Content.Server.Speech.Components;
using Content.Shared.DeltaV.AACTablet;
using Content.Shared.DeltaV.QuickPhrase;
using Content.Shared.IdentityManagement;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
Expand All @@ -11,40 +10,37 @@ namespace Content.Server.DeltaV.AACTablet;
public sealed class AACTabletSystem : EntitySystem
{
[Dependency] private readonly ChatSystem _chat = default!;
[Dependency] private readonly ILocalizationManager _loc = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] protected readonly IGameTiming Timing = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AACTabletComponent, AACTabletSendPhraseMessage>(OnSendPhrase);
}

private void OnSendPhrase(EntityUid uid, AACTabletComponent component, AACTabletSendPhraseMessage message)
private void OnSendPhrase(Entity<AACTabletComponent> ent, ref AACTabletSendPhraseMessage message)
{
if (component.NextPhrase > Timing.CurTime)
if (ent.Comp.NextPhrase > _timing.CurTime)
return;

// the AAC tablet uses the name of the person who pressed the tablet button
// for quality of life
var senderName = Identity.Entity(message.Actor, EntityManager);
var speakerName = Loc.GetString("speech-name-relay",
("speaker", Name(uid)),
("speaker", Name(ent)),
("originalName", senderName));

if (!_prototypeManager.TryIndex<QuickPhrasePrototype>(message.PhraseID, out var phrase))
if (!_prototype.TryIndex(message.PhraseId, out var phrase))
return;

EnsureComp<VoiceOverrideComponent>(uid).NameOverride = speakerName;
EnsureComp<VoiceOverrideComponent>(ent).NameOverride = speakerName;

_chat.TrySendInGameICMessage(uid,
_loc.GetString(phrase.Text),
_chat.TrySendInGameICMessage(ent,
Loc.GetString(phrase.Text),
InGameICChatType.Speak,
hideChat: false,
nameOverride: speakerName);

var curTime = Timing.CurTime;
component.NextPhrase = curTime + component.Cooldown;
var curTime = _timing.CurTime;
ent.Comp.NextPhrase = curTime + ent.Comp.Cooldown;
}
}
17 changes: 17 additions & 0 deletions Content.Shared/DeltaV/AACTablet/AACTabletUiMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Shared.DeltaV.QuickPhrase;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;

namespace Content.Shared.DeltaV.AACTablet;

[Serializable, NetSerializable]
public enum AACTabletKey : byte
{
Key,
}

[Serializable, NetSerializable]
public sealed class AACTabletSendPhraseMessage(ProtoId<QuickPhrasePrototype> phraseId) : BoundUserInterfaceMessage
{
public ProtoId<QuickPhrasePrototype> PhraseId = phraseId;
}
19 changes: 0 additions & 19 deletions Content.Shared/DeltaV/AACTablet/SharedAACTabletSystem.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Content.Shared/DeltaV/QuickPhrase/QuickPhrasePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Content.Shared.DeltaV.QuickPhrase;

[Prototype("quickPhrase")]
[Prototype]
public sealed partial class QuickPhrasePrototype : IPrototype, IInheritingPrototype
{
/// <summary>
Expand Down Expand Up @@ -48,4 +48,4 @@ public sealed partial class QuickPhrasePrototype : IPrototype, IInheritingProtot
/// </summary>
[DataField]
public string StyleClass = string.Empty;
}
}

0 comments on commit 13c1e01

Please sign in to comment.