Better notes and bans (#14228)
Co-authored-by: Chief-Engineer <119664036+Chief-Engineer@users.noreply.github.com>
This commit is contained in:
@@ -1,119 +1,167 @@
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Content.Shared.Administration.Notes;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Database;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using static Robust.Client.UserInterface.Controls.LineEdit;
|
||||
using Robust.Shared.Configuration;
|
||||
|
||||
namespace Content.Client.Administration.UI.Notes;
|
||||
|
||||
[GenerateTypedNameReferences]
|
||||
public sealed partial class AdminNotesControl : Control
|
||||
{
|
||||
public event Action<int, string>? OnNoteChanged;
|
||||
public event Action<string>? OnNewNoteEntered;
|
||||
public event Action<int>? OnNoteDeleted;
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
|
||||
public event Action<int, NoteType, string, NoteSeverity?, bool, DateTime?>? NoteChanged;
|
||||
public event Action<NoteType, string, NoteSeverity?, bool, DateTime?>? NewNoteEntered;
|
||||
public event Action<int, NoteType>? NoteDeleted;
|
||||
|
||||
private AdminNotesLinePopup? _popup;
|
||||
private readonly SpriteSystem _sprites;
|
||||
private readonly double _noteFreshDays;
|
||||
private readonly double _noteStaleDays;
|
||||
|
||||
public AdminNotesControl()
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
_sprites = _entitySystem.GetEntitySystem<SpriteSystem>();
|
||||
|
||||
NewNote.OnTextEntered += NewNoteEntered;
|
||||
// There should be a warning somewhere if fresh > stale
|
||||
// I thought about putting it here but then it would spam you every time you open notes
|
||||
_noteFreshDays = _cfg.GetCVar(CCVars.NoteFreshDays);
|
||||
_noteStaleDays = _cfg.GetCVar(CCVars.NoteStaleDays);
|
||||
|
||||
NewNoteButton.OnPressed += OnNewNoteButtonPressed;
|
||||
ShowMoreButton.OnPressed += OnShowMoreButtonPressed;
|
||||
}
|
||||
|
||||
private Dictionary<int, AdminNotesLine> Inputs { get; } = new();
|
||||
private Dictionary<(int noteId, NoteType noteType), AdminNotesLine> Inputs { get; } = new();
|
||||
private bool CanCreate { get; set; }
|
||||
private bool CanDelete { get; set; }
|
||||
private bool CanEdit { get; set; }
|
||||
private string PlayerName { get; set; } = "<Error>";
|
||||
|
||||
private void NewNoteEntered(LineEditEventArgs args)
|
||||
public void SetPlayerName(string playerName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(args.Text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NewNote.Clear();
|
||||
OnNewNoteEntered?.Invoke(args.Text);
|
||||
PlayerName = playerName;
|
||||
}
|
||||
|
||||
private void NoteSubmitted(AdminNotesLine input)
|
||||
private void OnNewNoteButtonPressed(BaseButton.ButtonEventArgs obj)
|
||||
{
|
||||
var text = input.EditText.Trim();
|
||||
if (input.OriginalMessage == text)
|
||||
var noteEdit = new NoteEdit(null, PlayerName, CanCreate, CanEdit);
|
||||
noteEdit.SubmitPressed += OnNoteSubmitted;
|
||||
noteEdit.OpenCentered();
|
||||
}
|
||||
|
||||
private void OnNoteSubmitted(int id, NoteType type, string message, NoteSeverity? severity, bool secret, DateTime? expiryTime)
|
||||
{
|
||||
if (id == 0)
|
||||
{
|
||||
NewNoteEntered?.Invoke(type, message, severity, secret, expiryTime);
|
||||
return;
|
||||
}
|
||||
|
||||
OnNoteChanged?.Invoke(input.Id, text);
|
||||
NoteChanged?.Invoke(id, type, message, severity, secret, expiryTime);
|
||||
}
|
||||
|
||||
private bool NoteClicked(AdminNotesLine line)
|
||||
{
|
||||
ClosePopup();
|
||||
|
||||
_popup = new AdminNotesLinePopup(line.Note, CanDelete, CanEdit);
|
||||
_popup.OnEditPressed += noteId =>
|
||||
_popup = new AdminNotesLinePopup(line.Note, PlayerName, CanDelete, CanEdit);
|
||||
_popup.OnEditPressed += (noteId, noteType) =>
|
||||
{
|
||||
if (!Inputs.TryGetValue(noteId, out var input))
|
||||
if (!Inputs.TryGetValue((noteId, noteType), out var input))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
input.SetEditable(true);
|
||||
var noteEdit = new NoteEdit(input.Note, PlayerName, CanCreate, CanEdit);
|
||||
noteEdit.SubmitPressed += OnNoteSubmitted;
|
||||
noteEdit.OpenCentered();
|
||||
};
|
||||
_popup.OnDeletePressed += noteId => OnNoteDeleted?.Invoke(noteId);
|
||||
|
||||
_popup.OnDeletePressed += (noteId, noteType) => NoteDeleted?.Invoke(noteId, noteType);
|
||||
var box = UIBox2.FromDimensions(UserInterfaceManager.MousePositionScaled.Position, Vector2.One);
|
||||
_popup.Open(box);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ClosePopup()
|
||||
public void SetNotes(Dictionary<(int, NoteType), SharedAdminNote> notes)
|
||||
{
|
||||
_popup?.Close();
|
||||
_popup = null;
|
||||
}
|
||||
|
||||
public void SetNotes(Dictionary<int, SharedAdminNote> notes)
|
||||
{
|
||||
foreach (var (id, input) in Inputs)
|
||||
foreach (var (key, input) in Inputs)
|
||||
{
|
||||
if (!notes.ContainsKey(id))
|
||||
if (!notes.ContainsKey(key))
|
||||
{
|
||||
Notes.RemoveChild(input);
|
||||
Inputs.Remove(id);
|
||||
// Yes this is slower than just updating, but new notes get added at the bottom. The user won't notice.
|
||||
Notes.RemoveAllChildren();
|
||||
Inputs.Clear();
|
||||
break;
|
||||
}
|
||||
Notes.RemoveChild(input);
|
||||
Inputs.Remove(key);
|
||||
}
|
||||
|
||||
foreach (var note in notes.Values.OrderBy(note => note.Id))
|
||||
var showMoreButtonVisible = false;
|
||||
foreach (var note in notes.Values.OrderByDescending(note => note.CreatedAt))
|
||||
{
|
||||
if (Inputs.TryGetValue(note.Id, out var input))
|
||||
if (Inputs.TryGetValue((note.Id, note.NoteType), out var input))
|
||||
{
|
||||
input.UpdateNote(note);
|
||||
continue;
|
||||
}
|
||||
|
||||
input = new AdminNotesLine(note);
|
||||
input.OnSubmitted += NoteSubmitted;
|
||||
input = new AdminNotesLine(_sprites, note);
|
||||
input.OnClicked += NoteClicked;
|
||||
|
||||
var timeDiff = DateTime.UtcNow - note.CreatedAt;
|
||||
float alpha;
|
||||
if (_noteFreshDays == 0 || timeDiff.TotalDays <= _noteFreshDays)
|
||||
{
|
||||
alpha = 1f;
|
||||
}
|
||||
else if (_noteStaleDays == 0 || timeDiff.TotalDays > _noteStaleDays)
|
||||
{
|
||||
alpha = 0f;
|
||||
input.Visible = false;
|
||||
showMoreButtonVisible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
alpha = (float) (1 - Math.Clamp((timeDiff.TotalDays - _noteFreshDays) / (_noteStaleDays - _noteFreshDays), 0, 1));
|
||||
}
|
||||
|
||||
input.Modulate = input.Modulate.WithAlpha(alpha);
|
||||
Notes.AddChild(input);
|
||||
Inputs[note.Id] = input;
|
||||
Inputs[(note.Id, note.NoteType)] = input;
|
||||
ShowMoreButton.Visible = showMoreButtonVisible;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnShowMoreButtonPressed(BaseButton.ButtonEventArgs obj)
|
||||
{
|
||||
foreach (var input in Inputs.Values)
|
||||
{
|
||||
input.Modulate = input.Modulate.WithAlpha(1f);
|
||||
input.Visible = true;
|
||||
}
|
||||
|
||||
ShowMoreButton.Visible = false;
|
||||
}
|
||||
|
||||
public void SetPermissions(bool create, bool delete, bool edit)
|
||||
{
|
||||
CanCreate = create;
|
||||
CanDelete = delete;
|
||||
CanEdit = edit;
|
||||
NewNoteLabel.Visible = create;
|
||||
NewNote.Visible = create;
|
||||
NewNoteButton.Visible = create;
|
||||
NewNoteButton.Disabled = !create;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
@@ -125,21 +173,14 @@ public sealed partial class AdminNotesControl : Control
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var input in Inputs.Values)
|
||||
{
|
||||
input.OnSubmitted -= NoteSubmitted;
|
||||
}
|
||||
|
||||
Inputs.Clear();
|
||||
NewNote.OnTextEntered -= NewNoteEntered;
|
||||
NewNoteButton.OnPressed -= OnNewNoteButtonPressed;
|
||||
|
||||
if (_popup != null)
|
||||
{
|
||||
UserInterfaceManager.PopupRoot.RemoveChild(_popup);
|
||||
}
|
||||
|
||||
OnNoteChanged = null;
|
||||
OnNewNoteEntered = null;
|
||||
OnNoteDeleted = null;
|
||||
NoteDeleted = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user