Files
tbd-station-14/Content.Client/UserInterface/LateJoinGui.cs
DrSmugleaf 435fb2630e Disable job buttons when they aren't available or late join is disallowed (#2251)
* Disable job buttons when they aren't available

In real time too, the technology is overwhelming

* Jobs are all unavailable when late join is disallowed.

* Better ToggleDisallowLateJoin command parsing

* Add togglelatejoin to groups.

Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es>
2020-10-16 11:22:58 +02:00

145 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.Interfaces;
using Content.Shared.Roles;
using Robust.Client.Console;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.Utility;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface
{
public sealed class LateJoinGui : SS14Window
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IClientConsole _console = default!;
[Dependency] private readonly IClientGameTicker _gameTicker = default!;
protected override Vector2? CustomSize => (360, 560);
public event Action<string> SelectedId;
private Dictionary<string, JobButton> JobButtons = new Dictionary<string, JobButton>();
public LateJoinGui()
{
IoCManager.InjectDependencies(this);
Title = Loc.GetString("Late Join");
var jobList = new VBoxContainer();
var vBox = new VBoxContainer
{
Children =
{
new ScrollContainer
{
SizeFlagsVertical = SizeFlags.FillExpand,
Children =
{
jobList
}
}
}
};
Contents.AddChild(vBox);
foreach (var job in _prototypeManager.EnumeratePrototypes<JobPrototype>().OrderBy(j => j.Name))
{
var jobButton = new JobButton
{
JobId = job.ID
};
var jobSelector = new HBoxContainer
{
SizeFlagsHorizontal = SizeFlags.FillExpand
};
var icon = new TextureRect
{
TextureScale = (2, 2),
Stretch = TextureRect.StretchMode.KeepCentered
};
if (job.Icon != null)
{
var specifier = new SpriteSpecifier.Rsi(new ResourcePath("/Textures/Interface/Misc/job_icons.rsi"), job.Icon);
icon.Texture = specifier.Frame0();
}
jobSelector.AddChild(icon);
var jobLabel = new Label
{
Text = job.Name
};
jobSelector.AddChild(jobLabel);
jobButton.AddChild(jobSelector);
jobList.AddChild(jobButton);
jobButton.OnPressed += args =>
{
SelectedId?.Invoke(jobButton.JobId);
};
if (!_gameTicker.JobsAvailable.Contains(job.ID))
{
jobButton.Disabled = true;
}
JobButtons[job.ID] = jobButton;
}
SelectedId += jobId =>
{
Logger.InfoS("latejoin", $"Late joining as ID: {jobId}");
_console.ProcessCommand($"joingame {CommandParsing.Escape(jobId)}");
Close();
};
_gameTicker.LobbyJobsAvailableUpdated += JobsAvailableUpdated;
}
public string ReturnId()
{
return SelectedId.ToString();
}
private void JobsAvailableUpdated(IReadOnlyList<string> jobs)
{
foreach (var (id, button) in JobButtons)
{
button.Disabled = !jobs.Contains(id);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_gameTicker.LobbyJobsAvailableUpdated -= JobsAvailableUpdated;
JobButtons.Clear();
}
}
}
class JobButton : ContainerButton
{
public string JobId { get; set; }
public JobButton()
{
AddStyleClass(StyleClassButton);
}
}
}