CommandButton.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Client.Guidebook.Richtext;
  3. using Robust.Client.Console;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. namespace Content.Client.Administration.UI.CustomControls
  7. {
  8. [Virtual]
  9. public class CommandButton : Button, IDocumentTag
  10. {
  11. public string? Command { get; set; }
  12. public CommandButton()
  13. {
  14. OnPressed += Execute;
  15. }
  16. protected virtual bool CanPress()
  17. {
  18. return string.IsNullOrEmpty(Command) ||
  19. IoCManager.Resolve<IClientConGroupController>().CanCommand(Command.Split(' ')[0]);
  20. }
  21. protected override void EnteredTree()
  22. {
  23. if (!CanPress())
  24. {
  25. Visible = false;
  26. }
  27. }
  28. protected virtual void Execute(ButtonEventArgs obj)
  29. {
  30. // Default is to execute command
  31. if (!string.IsNullOrEmpty(Command))
  32. IoCManager.Resolve<IClientConsoleHost>().ExecuteCommand(Command);
  33. }
  34. public bool TryParseTag(Dictionary<string, string> args, [NotNullWhen(true)] out Control? control)
  35. {
  36. if (args.Count != 2 || !args.TryGetValue("Text", out var text) || !args.TryGetValue("Command", out var command))
  37. {
  38. Logger.Error($"Invalid arguments passed to {nameof(CommandButton)}");
  39. control = null;
  40. return false;
  41. }
  42. Command = command;
  43. Text = Loc.GetString(text);
  44. control = this;
  45. return true;
  46. }
  47. }
  48. }