Skip to content

Commit

Permalink
Merge pull request #444 from TGRCdev/spelf-mood-admin-verb
Browse files Browse the repository at this point in the history
Admins can now view and edit spelf moods
  • Loading branch information
formlessnameless authored Oct 11, 2024
2 parents f55b7fe + 4aee624 commit fd9a2db
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 18 deletions.
30 changes: 30 additions & 0 deletions Content.Client/Impstation/Spelfs/Eui/MoodContainer.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<BoxContainer
xmlns="https://spacestation14.io"
xmlns:graphics="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
HorizontalExpand="True"
Orientation="Vertical"
>
<customControls:HSeparator></customControls:HSeparator>
<BoxContainer Orientation="Vertical">
<LineEdit Name="SpelfMoodTitle" Access="Public" Margin="5 0 0 0" />
<PanelContainer
Margin="20 10 0 0"
MinHeight="128"
>
<PanelContainer.PanelOverride>
<graphics:StyleBoxFlat BackgroundColor="#1B1B1B"></graphics:StyleBoxFlat>
</PanelContainer.PanelOverride>
<BoxContainer Orientation="Horizontal" SeparationOverride="5">
<TextEdit Name="SpelfMoodContent" Access="Public" HorizontalExpand="True" Editable="True" MinWidth="500" MinHeight="80"></TextEdit>
</BoxContainer>
</PanelContainer>
</BoxContainer>
<BoxContainer Orientation="Horizontal" Margin="0 5 0 0" MaxHeight="64" Align="Begin">
<Button Name="MoveUp" Access="Public" Text="{Loc spelf-mood-admin-ui-move-up}" StyleClasses="OpenRight"></Button>
<Button Name="MoveDown" Access="Public" Text="{Loc spelf-mood-admin-ui-move-down}" StyleClasses="OpenLeft"></Button>
</BoxContainer>
<BoxContainer Orientation="Horizontal" Align="End" Margin="0 10 5 10">
<Button Name="Delete" Access="Public" Text="{Loc spelf-mood-admin-ui-delete}" ModulateSelfOverride="Red"></Button>
</BoxContainer>
</BoxContainer>
22 changes: 22 additions & 0 deletions Content.Client/Impstation/Spelfs/Eui/MoodContainer.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Content.Shared.Impstation.Spelfs;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Utility;

namespace Content.Client.Impstation.Spelfs.Eui;

[GenerateTypedNameReferences]
public sealed partial class MoodContainer : BoxContainer
{
public MoodContainer(SpelfMood? mood = null)
{
RobustXamlLoader.Load(this);

if (mood != null)
{
SpelfMoodTitle.Text = mood.GetLocName();
SpelfMoodContent.TextRope = new Rope.Leaf(mood.GetLocDesc());
}
}
}
22 changes: 22 additions & 0 deletions Content.Client/Impstation/Spelfs/Eui/SpelfMoodUi.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<controls:FancyWindow
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
Title="{Loc spelf-moods-admin-ui-title}"
MinSize="560 400"
>
<!-->
this shit does not layout properly unless I put the horizontal boxcontainer inside of a vertical one
????
<!-->
<BoxContainer Orientation="Vertical">
<BoxContainer Orientation="Horizontal" Align="End">
<Button Name="NewMoodButton" Text="{Loc spelf-moods-admin-ui-new-mood}" MaxSize="256 64" StyleClasses="OpenRight"></Button>
<Button Name="SaveButton" Text="{Loc spelf-moods-admin-ui-save}" MaxSize="256 64" Access="Public" StyleClasses="OpenLeft"></Button>
</BoxContainer>
</BoxContainer>
<BoxContainer Orientation="Vertical" Margin="4 60 0 0">
<ScrollContainer VerticalExpand="True" HorizontalExpand="True" HScrollEnabled="False">
<BoxContainer Orientation="Vertical" Name="MoodContainer" Access="Public" VerticalExpand="True" />
</ScrollContainer>
</BoxContainer>
</controls:FancyWindow>
94 changes: 94 additions & 0 deletions Content.Client/Impstation/Spelfs/Eui/SpelfMoodUi.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Content.Client.UserInterface.Controls;
using Content.Shared.Impstation.Spelfs;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Utility;

namespace Content.Client.Impstation.Spelfs.Eui;

[GenerateTypedNameReferences]
public sealed partial class SpelfMoodUi : FancyWindow
{
private List<SpelfMood> _moods = new();

public SpelfMoodUi()
{
RobustXamlLoader.Load(this);
NewMoodButton.OnPressed += _ => AddNewMood();
}

private void AddNewMood()
{
MoodContainer.AddChild(new MoodContainer());
}

public List<SpelfMood> GetMoods()
{
var newMoods = new List<SpelfMood>();

foreach (var control in MoodContainer.Children)
{
if (control is not MoodContainer moodControl)
continue;

if (string.IsNullOrWhiteSpace(moodControl.SpelfMoodTitle.Text))
continue;

var moodText = Rope.Collapse(moodControl.SpelfMoodContent.TextRope).Trim();

if (string.IsNullOrWhiteSpace(moodText))
continue;

var mood = new SpelfMood()
{
MoodName = moodControl.SpelfMoodTitle.Text,
MoodDesc = moodText,
};

newMoods.Add(mood);
}

return newMoods;
}

private void MoveUp(int index)
{
if (index <= 0)
return;

(_moods[index], _moods[index - 1]) = (_moods[index - 1], _moods[index]);
SetMoods(_moods);
}

private void MoveDown(int index)
{
if (index >= _moods.Count - 1)
return;

(_moods[index], _moods[index + 1]) = (_moods[index + 1], _moods[index]);
SetMoods(_moods);
}

private void Delete(int index)
{
_moods.RemoveAt(index);

SetMoods(_moods);
}

public void SetMoods(List<SpelfMood> moods)
{
_moods = moods;
MoodContainer.RemoveAllChildren();
for (var i = 0; i < moods.Count; i++)
{
var index = i; // Copy for the closure
var mood = moods[i];
var moodControl = new MoodContainer(mood);
moodControl.MoveUp.OnPressed += _ => MoveUp(index);
moodControl.MoveDown.OnPressed += _ => MoveDown(index);
moodControl.Delete.OnPressed += _ => Delete(index);
MoodContainer.AddChild(moodControl);
}
}
}
42 changes: 42 additions & 0 deletions Content.Client/Impstation/Spelfs/Eui/SpelfMoodsEui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Content.Client.Eui;
using Content.Shared.Eui;
using Content.Shared.Impstation.Spelfs;

namespace Content.Client.Impstation.Spelfs.Eui;

public sealed class SpelfMoodsEui : BaseEui
{
private readonly EntityManager _entityManager;

private SpelfMoodUi _spelfMoodUi;
private NetEntity _target;

public SpelfMoodsEui()
{
_entityManager = IoCManager.Resolve<EntityManager>();

_spelfMoodUi = new SpelfMoodUi();
_spelfMoodUi.SaveButton.OnPressed += _ => SaveMoods();
}

private void SaveMoods()
{
var newMoods = _spelfMoodUi.GetMoods();
SendMessage(new SpelfMoodsSaveMessage(newMoods, _target));
_spelfMoodUi.SetMoods(newMoods);
}

public override void Opened()
{
_spelfMoodUi.OpenCentered();
}

public override void HandleState(EuiStateBase state)
{
if (state is not SpelfMoodsEuiState s)
return;

_target = s.Target;
_spelfMoodUi.SetMoods(s.Moods);
}
}
13 changes: 3 additions & 10 deletions Content.Client/Impstation/Spelfs/MoodDisplay.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Linq;
using Content.Client.Chat.Managers;
using Content.Client.Message;
using Content.Shared.Chat;
using Content.Shared.Impstation.Spelfs;
using Content.Shared.Radio;
using Content.Shared.Silicons.Laws;
using Content.Shared.Speech;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
Expand All @@ -32,12 +27,10 @@ public MoodDisplay(SpelfMood mood, bool shared)
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

var args = mood.MoodVars.Select(var => (var.Key, (object)var.Value)).ToArray();

if (shared)
MoodNameLabel.SetMarkup($"{Loc.GetString(mood.MoodName, args)} {GetSharedString()}");
MoodNameLabel.SetMarkup($"{mood.GetLocName()} {GetSharedString()}");
else
MoodNameLabel.SetMarkup(Loc.GetString(mood.MoodName, args));
MoodDescLabel.SetMarkup(Loc.GetString(mood.MoodDesc, args));
MoodNameLabel.SetMarkup(mood.GetLocName());
MoodDescLabel.SetMarkup(mood.GetLocDesc());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public sealed partial class AdminVerbSystem
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
[Dependency] private readonly GunSystem _gun = default!;
[Dependency] private readonly RevenantAnimatedSystem _revenantAnimate = default!;
[Dependency] private readonly SpelfMoodsSystem _moods = default!;

private void AddTricksVerbs(GetVerbsEvent<Verb> args)
{
Expand Down
22 changes: 22 additions & 0 deletions Content.Server/Administration/Systems/AdminVerbSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
using Robust.Server.Player;
using Robust.Shared.Physics.Components;
using static Content.Shared.Configurable.ConfigurationComponent;
using Content.Shared.Impstation.Spelfs.Components;
using Content.Server.Impstation.Spelfs;

namespace Content.Server.Administration.Systems
{
Expand Down Expand Up @@ -71,6 +73,7 @@ public sealed partial class AdminVerbSystem : EntitySystem
[Dependency] private readonly AdminFrozenSystem _freeze = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly SiliconLawSystem _siliconLawSystem = default!;
[Dependency] private readonly SpelfMoodsSystem _moods = default!;

private readonly Dictionary<ICommonSession, List<EditSolutionsEui>> _openSolutionUis = new();

Expand Down Expand Up @@ -364,6 +367,25 @@ private void AddAdminVerbs(GetVerbsEvent<Verb> args)
Icon = new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Actions/actions_borg.rsi"), "state-laws"),
});
}

if (TryComp<SpelfMoodsComponent>(args.Target, out var moods))
{
args.Verbs.Add(new Verb()
{
Text = Loc.GetString("spelf-moods-ui-verb"),
Category = VerbCategory.Admin,
Act = () =>
{
var ui = new SpelfMoodsEui(_moods, EntityManager, _adminManager);
if (!_playerManager.TryGetSessionByEntity(args.User, out var session))
return;

_euiManager.OpenEui(ui, session);
ui.UpdateMoods(moods, args.Target);
},
Icon = new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Actions/actions_borg.rsi"), "state-laws"),
});
}
}
}

Expand Down
25 changes: 19 additions & 6 deletions Content.Server/Impstation/Spelfs/SpelfMoodSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public sealed partial class SpelfMoodsSystem : SharedSpelfMoodSystem
[Dependency] private readonly UserInterfaceSystem _bui = default!;
[Dependency] private readonly IChatManager _chatManager = default!;

public readonly List<SpelfMood> SharedMoods = new();
public IReadOnlyList<SpelfMood> SharedMoods => _sharedMoods.AsReadOnly();
private readonly List<SpelfMood> _sharedMoods = new();


[ValidatePrototypeId<DatasetPrototype>]
Expand Down Expand Up @@ -64,7 +65,7 @@ public override void Initialize()

private void NewSharedMoods()
{
SharedMoods.Clear();
_sharedMoods.Clear();
for (int i = 0; i < _config.GetCVar(CCVars.SpelfSharedMoodCount); i++)
TryAddSharedMood();
}
Expand All @@ -73,7 +74,7 @@ public bool TryAddSharedMood(SpelfMood? mood = null, bool checkConflicts = true)
{
if (mood == null)
{
if (TryPick(SharedDataset, out var moodProto, SharedMoods))
if (TryPick(SharedDataset, out var moodProto, _sharedMoods))
{
mood = RollMood(moodProto);
checkConflicts = false; // TryPick has cleared this mood already
Expand All @@ -84,10 +85,10 @@ public bool TryAddSharedMood(SpelfMood? mood = null, bool checkConflicts = true)
}
}

if (checkConflicts && (GetConflicts(SharedMoods).Contains(mood.ProtoId) || GetMoodProtoSet(SharedMoods).Overlaps(mood.Conflicts)))
if (checkConflicts && (GetConflicts(_sharedMoods).Contains(mood.ProtoId) || GetMoodProtoSet(_sharedMoods).Overlaps(mood.Conflicts)))
return false;

SharedMoods.Add(mood);
_sharedMoods.Add(mood);
var enumerator = EntityManager.EntityQueryEnumerator<SpelfMoodsComponent>();
while (enumerator.MoveNext(out var ent, out var comp))
{
Expand Down Expand Up @@ -159,7 +160,7 @@ public void UpdateBUIState(EntityUid uid, SpelfMoodsComponent? comp = null)
if (!Resolve(uid, ref comp))
return;

var state = new SpelfMoodsBuiState(comp.Moods, comp.FollowsSharedMoods ? SharedMoods : []);
var state = new SpelfMoodsBuiState(comp.Moods, comp.FollowsSharedMoods ? _sharedMoods : []);
_bui.SetUiState(uid, SpelfMoodsUiKey.Key, state);
}

Expand Down Expand Up @@ -266,6 +267,18 @@ public bool TryAddRandomMood(EntityUid uid, SpelfMoodsComponent? comp = null)
return TryAddRandomMood(uid, datasetProto, comp);
}

public void SetMoods(EntityUid uid, IEnumerable<SpelfMood> moods, SpelfMoodsComponent? comp = null, bool notify = true)
{
if (!Resolve(uid, ref comp))
return;

comp.Moods = moods.ToList();
if (notify)
NotifyMoodChange(uid);

UpdateBUIState(uid, comp);
}

public HashSet<string> GetConflicts(IEnumerable<SpelfMood> moods)
{
var conflicts = new HashSet<string>();
Expand Down
Loading

0 comments on commit fd9a2db

Please sign in to comment.