Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Late Join Menu Properly Retains Position On New Player Joins #26483

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 64 additions & 18 deletions Content.Client/LateJoin/LateJoinGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed class LateJoinGui : DefaultWindow
private readonly SpriteSystem _sprites;
private readonly CrewManifestSystem _crewManifest;

private readonly Dictionary<NetEntity, Dictionary<string, JobButton>> _jobButtons = new();
private readonly Dictionary<NetEntity, Dictionary<string, List<JobButton>>> _jobButtons = new();
private readonly Dictionary<NetEntity, Dictionary<string, BoxContainer>> _jobCategories = new();
private readonly List<ScrollContainer> _jobLists = new();

Expand Down Expand Up @@ -139,7 +139,7 @@ private void RebuildUI()
var jobListScroll = new ScrollContainer()
{
VerticalExpand = true,
Children = {jobList},
Children = { jobList },
Visible = false,
};

Expand All @@ -163,11 +163,12 @@ private void RebuildUI()
var departments = _prototypeManager.EnumeratePrototypes<DepartmentPrototype>().ToArray();
Array.Sort(departments, DepartmentUIComparer.Instance);

_jobButtons[id] = new Dictionary<string, List<JobButton>>();

foreach (var department in departments)
{
var departmentName = Loc.GetString($"department-{department.ID}");
_jobCategories[id] = new Dictionary<string, BoxContainer>();
_jobButtons[id] = new Dictionary<string, JobButton>();
var stationAvailable = _gameTicker.JobsAvailable[id];
var jobsAvailable = new List<JobPrototype>();

Expand Down Expand Up @@ -223,7 +224,13 @@ private void RebuildUI()
foreach (var prototype in jobsAvailable)
{
var value = stationAvailable[prototype.ID];
var jobButton = new JobButton(prototype.ID, value);

var jobLabel = new Label
{
Margin = new Thickness(5f, 0, 0, 0)
};

var jobButton = new JobButton(jobLabel, prototype.ID, prototype.LocalizedName, value);

var jobSelector = new BoxContainer
{
Expand All @@ -241,14 +248,6 @@ private void RebuildUI()
icon.Texture = _sprites.Frame0(jobIcon.Icon);
jobSelector.AddChild(icon);

var jobLabel = new Label
{
Margin = new Thickness(5f, 0, 0, 0),
Text = value != null ?
Loc.GetString("late-join-gui-job-slot-capped", ("jobName", prototype.LocalizedName), ("amount", value)) :
Loc.GetString("late-join-gui-job-slot-uncapped", ("jobName", prototype.LocalizedName)),
};

jobSelector.AddChild(jobLabel);
jobButton.AddChild(jobSelector);
category.AddChild(jobButton);
Expand Down Expand Up @@ -280,15 +279,43 @@ private void RebuildUI()
jobButton.Disabled = true;
}

_jobButtons[id][prototype.ID] = jobButton;
if (!_jobButtons[id].ContainsKey(prototype.ID))
{
_jobButtons[id][prototype.ID] = new List<JobButton>();
}

_jobButtons[id][prototype.ID].Add(jobButton);
}
}
}
}

private void JobsAvailableUpdated(IReadOnlyDictionary<NetEntity, Dictionary<string, uint?>> _)
private void JobsAvailableUpdated(IReadOnlyDictionary<NetEntity, Dictionary<string, uint?>> updatedJobs)
{
RebuildUI();
foreach (var stationEntries in updatedJobs)
{
if (_jobButtons.ContainsKey(stationEntries.Key))
{
var jobsAvailable = stationEntries.Value;

var existingJobEntries = _jobButtons[stationEntries.Key];
foreach (var existingJobEntry in existingJobEntries)
{
if (jobsAvailable.ContainsKey(existingJobEntry.Key))
{
var updatedJobValue = jobsAvailable[existingJobEntry.Key];
foreach (var matchingJobButton in existingJobEntry.Value)
{
if (matchingJobButton.Amount != updatedJobValue)
{
matchingJobButton.RefreshLabel(updatedJobValue);
matchingJobButton.Disabled = matchingJobButton.Amount == 0;
}
}
}
}
}
}
}

protected override void Dispose(bool disposing)
Expand All @@ -307,14 +334,33 @@ protected override void Dispose(bool disposing)

sealed class JobButton : ContainerButton
{
public Label JobLabel { get; }
public string JobId { get; }
public uint? Amount { get; }
public string JobLocalisedName { get; }
public uint? Amount { get; private set; }
private bool _initialised = false;

public JobButton(string jobId, uint? amount)
public JobButton(Label jobLabel, string jobId, string jobLocalisedName, uint? amount)
{
JobLabel = jobLabel;
JobId = jobId;
Amount = amount;
JobLocalisedName = jobLocalisedName;
RefreshLabel(amount);
AddStyleClass(StyleClassButton);
_initialised = true;
}

public void RefreshLabel(uint? amount)
{
if (Amount == amount && _initialised)
{
return;
}
Amount = amount;

JobLabel.Text = Amount != null ?
Loc.GetString("late-join-gui-job-slot-capped", ("jobName", JobLocalisedName), ("amount", Amount)) :
Loc.GetString("late-join-gui-job-slot-uncapped", ("jobName", JobLocalisedName));
}
}
}
Loading