Improve on disposal routing code (#1795)

Co-authored-by: Julian Giebel <j.giebel@netrocks.info>
This commit is contained in:
Julian Giebel
2020-08-19 20:43:56 +02:00
committed by GitHub
parent 20ab566f8c
commit 7771f58460
8 changed files with 23 additions and 31 deletions

View File

@@ -23,15 +23,13 @@ namespace Content.Client.GameObjects.Components.Disposal
{ {
base.Open(); base.Open();
_window = new DisposalRouterWindow _window = new DisposalRouterWindow();
{
Title = Loc.GetString("Disposal Router"),
};
_window.OpenCentered(); _window.OpenCentered();
_window.OnClose += Close; _window.OnClose += Close;
_window.Confirm.OnPressed += _ => ButtonPressed(UiAction.Ok, _window.TagInput.Text); _window.Confirm.OnPressed += _ => ButtonPressed(UiAction.Ok, _window.TagInput.Text);
_window.TagInput.OnTextEntered += args => ButtonPressed(UiAction.Ok, args.Text);
} }

View File

@@ -1,7 +1,4 @@
using System.Runtime.CompilerServices; using Content.Shared.GameObjects.Components.Disposal;
using System.Text.RegularExpressions;
using Content.Shared.GameObjects.Components.Disposal;
using Robust.Client.Graphics.Drawing;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
@@ -21,11 +18,9 @@ namespace Content.Client.GameObjects.Components.Disposal
protected override Vector2? CustomSize => (400, 80); protected override Vector2? CustomSize => (400, 80);
private Regex _tagRegex;
public DisposalRouterWindow() public DisposalRouterWindow()
{ {
_tagRegex = new Regex("^[a-zA-Z0-9, ]*$", RegexOptions.Compiled); Title = Loc.GetString("Disposal Router");
Contents.AddChild(new VBoxContainer Contents.AddChild(new VBoxContainer
{ {
@@ -38,7 +33,7 @@ namespace Content.Client.GameObjects.Components.Disposal
Children = Children =
{ {
(TagInput = new LineEdit {SizeFlagsHorizontal = SizeFlags.Expand, CustomMinimumSize = (320, 0), (TagInput = new LineEdit {SizeFlagsHorizontal = SizeFlags.Expand, CustomMinimumSize = (320, 0),
ToolTip = Loc.GetString("A comma separated list of tags"), IsValid = tags => _tagRegex.IsMatch(tags)}), ToolTip = Loc.GetString("A comma separated list of tags"), IsValid = tags => TagRegex.IsMatch(tags)}),
new Control {CustomMinimumSize = (10, 0)}, new Control {CustomMinimumSize = (10, 0)},
(Confirm = new Button {Text = Loc.GetString("Confirm")}) (Confirm = new Button {Text = Loc.GetString("Confirm")})
} }

View File

@@ -23,15 +23,13 @@ namespace Content.Client.GameObjects.Components.Disposal
{ {
base.Open(); base.Open();
_window = new DisposalTaggerWindow _window = new DisposalTaggerWindow();
{
Title = Loc.GetString("Disposal Tagger"),
};
_window.OpenCentered(); _window.OpenCentered();
_window.OnClose += Close; _window.OnClose += Close;
_window.Confirm.OnPressed += _ => ButtonPressed(UiAction.Ok, _window.TagInput.Text); _window.Confirm.OnPressed += _ => ButtonPressed(UiAction.Ok, _window.TagInput.Text);
_window.TagInput.OnTextEntered += args => ButtonPressed(UiAction.Ok, args.Text);
} }

View File

@@ -1,7 +1,4 @@
using System.Runtime.CompilerServices; using Content.Shared.GameObjects.Components.Disposal;
using System.Text.RegularExpressions;
using Content.Shared.GameObjects.Components.Disposal;
using Robust.Client.Graphics.Drawing;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
@@ -21,11 +18,9 @@ namespace Content.Client.GameObjects.Components.Disposal
protected override Vector2? CustomSize => (400, 80); protected override Vector2? CustomSize => (400, 80);
private Regex _tagRegex;
public DisposalTaggerWindow() public DisposalTaggerWindow()
{ {
_tagRegex = new Regex("^[a-zA-Z0-9 ]*$", RegexOptions.Compiled); Title = Loc.GetString("Disposal Tagger");
Contents.AddChild(new VBoxContainer Contents.AddChild(new VBoxContainer
{ {
@@ -38,7 +33,7 @@ namespace Content.Client.GameObjects.Components.Disposal
Children = Children =
{ {
(TagInput = new LineEdit {SizeFlagsHorizontal = SizeFlags.Expand, CustomMinimumSize = (320, 0), (TagInput = new LineEdit {SizeFlagsHorizontal = SizeFlags.Expand, CustomMinimumSize = (320, 0),
IsValid = tag => _tagRegex.IsMatch(tag)}), IsValid = tag => TagRegex.IsMatch(tag)}),
new Control {CustomMinimumSize = (10, 0)}, new Control {CustomMinimumSize = (10, 0)},
(Confirm = new Button {Text = Loc.GetString("Confirm")}) (Confirm = new Button {Text = Loc.GetString("Confirm")})
} }

View File

@@ -78,16 +78,16 @@ namespace Content.Server.GameObjects.Components.Disposal
if (!PlayerCanUseDisposalTagger(obj.Session.AttachedEntity)) if (!PlayerCanUseDisposalTagger(obj.Session.AttachedEntity))
return; return;
if (msg.Action == UiAction.Ok) //Check for correct message and ignore maleformed strings
if (msg.Action == UiAction.Ok && TagRegex.IsMatch(msg.Tags))
{ {
_tags.Clear(); _tags.Clear();
foreach (var tag in msg.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries)) foreach (var tag in msg.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries))
{ {
_tags.Add(tag.Trim()); _tags.Add(tag.Trim());
ClickSound();
} }
} }
ClickSound();
} }
/// <summary> /// <summary>

View File

@@ -68,12 +68,12 @@ namespace Content.Server.GameObjects.Components.Disposal
if (!PlayerCanUseDisposalTagger(obj.Session.AttachedEntity)) if (!PlayerCanUseDisposalTagger(obj.Session.AttachedEntity))
return; return;
if (msg.Action == UiAction.Ok) //Check for correct message and ignore maleformed strings
if (msg.Action == UiAction.Ok && TagRegex.IsMatch(msg.Tag))
{ {
_tag = msg.Tag; _tag = msg.Tag;
ClickSound();
} }
ClickSound();
} }
/// <summary> /// <summary>

View File

@@ -2,6 +2,7 @@
using Robust.Shared.GameObjects.Components.UserInterface; using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using System; using System;
using System.Text.RegularExpressions;
namespace Content.Shared.GameObjects.Components.Disposal namespace Content.Shared.GameObjects.Components.Disposal
{ {
@@ -9,6 +10,8 @@ namespace Content.Shared.GameObjects.Components.Disposal
{ {
public override string Name => "DisposalRouter"; public override string Name => "DisposalRouter";
public static readonly Regex TagRegex = new Regex("^[a-zA-Z0-9, ]*$", RegexOptions.Compiled);
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class DisposalRouterUserInterfaceState : BoundUserInterfaceState public class DisposalRouterUserInterfaceState : BoundUserInterfaceState
{ {

View File

@@ -3,6 +3,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.UserInterface; using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using System; using System;
using System.Text.RegularExpressions;
namespace Content.Shared.GameObjects.Components.Disposal namespace Content.Shared.GameObjects.Components.Disposal
{ {
@@ -10,6 +11,8 @@ namespace Content.Shared.GameObjects.Components.Disposal
{ {
public override string Name => "DisposalTagger"; public override string Name => "DisposalTagger";
public static readonly Regex TagRegex = new Regex("^[a-zA-Z0-9 ]*$", RegexOptions.Compiled);
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class DisposalTaggerUserInterfaceState : BoundUserInterfaceState public class DisposalTaggerUserInterfaceState : BoundUserInterfaceState
{ {
@@ -33,7 +36,7 @@ namespace Content.Shared.GameObjects.Components.Disposal
if (Action == UiAction.Ok) if (Action == UiAction.Ok)
{ {
Tag = tag; Tag = tag.Substring(0, Math.Min(tag.Length, 30));
} }
} }
} }