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

Loadouts Lazy Loading #1283

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
146 changes: 68 additions & 78 deletions Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ public void UpdateTraits(bool? showUnusable = null, bool reload = false)
// Reset the whole UI and delete caches
if (reload)
{
foreach (var tab in TraitsTabs.Tabs)
foreach (var tab in TraitsTabs.TabContainer.Children)
TraitsTabs.RemoveTab(tab);
_loadoutPreferences.Clear();
}
Expand Down Expand Up @@ -2004,14 +2004,13 @@ public void UpdateLoadouts(bool? showUnusable = null, bool reload = false)
// Reset the whole UI and delete caches
if (reload)
{
foreach (var tab in LoadoutsTabs.Tabs)
foreach (var tab in LoadoutsTabs.TabContainer.Children)
LoadoutsTabs.RemoveTab(tab);
foreach (var uid in _dummyLoadouts)
_entManager.QueueDeleteEntity(uid.Value);
_loadoutPreferences.Clear();
}


// Get the highest priority job to use for loadout filtering
var highJob = _controller.GetPreferredJob(Profile ?? HumanoidCharacterProfile.DefaultWithSpecies());

Expand All @@ -2031,13 +2030,6 @@ public void UpdateLoadouts(bool? showUnusable = null, bool reload = false)
out _
);
_loadouts.Add(loadout, usable);

var list = _loadoutPreferences.ToList();
if (list.FindIndex(lps => lps.Loadout.ID == loadout.ID) is not (not -1 and var i))
continue;

var selector = list[i];
UpdateSelector(selector, usable);
}

if (_loadouts.Count == 0)
Expand All @@ -2047,7 +2039,6 @@ out _
return;
}


var uncategorized = LoadoutsTabs.Contents.FirstOrDefault(c => c.Name == "Uncategorized");
if (uncategorized == null)
{
Expand Down Expand Up @@ -2091,76 +2082,18 @@ out _
categories.Add(key, value);

// Create the UI elements for the category tree
CreateCategoryUI(categories, LoadoutsTabs);

// Fill categories with loadouts
foreach (var (loadout, usable) in _loadouts
var sortedLoadouts = _loadouts
.OrderBy(l => l.Key.ID)
.ThenBy(l => Loc.GetString($"loadout-name-{l.Key.ID}"))
.ThenBy(l => l.Key.Cost))
{
if (_loadoutPreferences.Select(lps => lps.Loadout.ID).Contains(loadout.ID))
{
var first = _loadoutPreferences.First(lps => lps.Loadout.ID == loadout.ID);
var prof = Profile?.LoadoutPreferences.FirstOrDefault(lp => lp.LoadoutName == loadout.ID);
first.Preference = new(loadout.ID, prof?.CustomName, prof?.CustomDescription, prof?.CustomColorTint, prof?.CustomHeirloom);
UpdateSelector(first, usable);
continue;
}

var selector = new LoadoutPreferenceSelector(
loadout, highJob ?? new JobPrototype(),
Profile ?? HumanoidCharacterProfile.DefaultWithSpecies(), ref _dummyLoadouts,
_entManager, _prototypeManager, _cfgManager, _characterRequirementsSystem, _requirements)
{ Preference = new(loadout.ID) };
UpdateSelector(selector, usable);
AddSelector(selector);

// Look for an existing category tab
var match = FindCategory(loadout.Category, LoadoutsTabs);

// If there is no category put it in Uncategorized (this shouldn't happen)
(match ?? uncategorized).Children.First().Children.First().AddChild(selector);
}
.ThenBy(l => l.Key.Cost);
CreateCategoryUI(categories, LoadoutsTabs);

// Hide any empty tabs
HideEmptyTabs(_prototypeManager.EnumeratePrototypes<LoadoutCategoryPrototype>().ToList());
// Hide any empty tabs //TODO: This is broken
// HideEmptyTabs(_prototypeManager.EnumeratePrototypes<LoadoutCategoryPrototype>().ToList());

UpdateLoadoutPreferences();
return;


void UpdateSelector(LoadoutPreferenceSelector selector, bool usable)
{
selector.Valid = usable;
selector.ShowUnusable = showUnusable.Value;

foreach (var item in selector.Loadout.Items)
{
if (_dummyLoadouts.TryGetValue(selector.Loadout.ID + selector.Loadout.Items.IndexOf(item), out var entity)
&& _entManager.GetComponent<MetaDataComponent>(entity).EntityPrototype!.ID == item)
{
if (!_entManager.HasComponent<ClothingComponent>(entity))
{
selector.Wearable = true;
continue;
}
selector.Wearable = _characterRequirementsSystem.CanEntityWearItem(PreviewDummy, entity);
continue;
}

entity = _entManager.SpawnEntity(item, MapCoordinates.Nullspace);
_dummyLoadouts[selector.Loadout.ID + selector.Loadout.Items.IndexOf(item)] = entity;

if (!_entManager.HasComponent<ClothingComponent>(entity))
{
selector.Wearable = true;
continue;
}
selector.Wearable = _characterRequirementsSystem.CanEntityWearItem(PreviewDummy, entity);
}
}

void CreateCategoryUI(Dictionary<string, object> tree, NeoTabContainer parent)
{
foreach (var (key, value) in tree)
Expand Down Expand Up @@ -2198,7 +2131,8 @@ void CreateCategoryUI(Dictionary<string, object> tree, NeoTabContainer parent)
},
};

parent.AddTab(category, Loc.GetString($"loadout-category-{key}"));
var i = parent.AddTab(category, Loc.GetString($"loadout-category-{key}"));
parent.GetTabInfo(i).Initialize = () => LoadCategoryLoadouts(category, key);
}
// If the value is a dictionary, create a new tab for it and recursively call this function to fill it
else
Expand All @@ -2208,12 +2142,68 @@ void CreateCategoryUI(Dictionary<string, object> tree, NeoTabContainer parent)
Name = key,
HorizontalExpand = true,
VerticalExpand = true,
SeparatorMargin = new Thickness(0),
SeparatorMargin = new(0),
};

parent.AddTab(category, Loc.GetString($"loadout-category-{key}"));
CreateCategoryUI((Dictionary<string, object>) value, category);
var i = parent.AddTab(category, Loc.GetString($"loadout-category-{key}"));
parent.GetTabInfo(i).Initialize = () => CreateCategoryUI((Dictionary<string, object>) value, category);
}
}
}

void LoadCategoryLoadouts(BoxContainer category, string key)
{
foreach (var (loadout, usable) in sortedLoadouts.Where(l => l.Key.Category == key))
{
if (_loadoutPreferences.Select(lps => lps.Loadout.ID).Contains(loadout.ID))
{
var first = _loadoutPreferences.First(lps => lps.Loadout.ID == loadout.ID);
var prof = Profile?.LoadoutPreferences.FirstOrDefault(lp => lp.LoadoutName == loadout.ID);
first.Preference = new(loadout.ID, prof?.CustomName, prof?.CustomDescription, prof?.CustomColorTint, prof?.CustomHeirloom);
UpdateSelector(first, usable);
continue;
}

var selector = new LoadoutPreferenceSelector(
loadout, highJob ?? new JobPrototype(),
Profile ?? HumanoidCharacterProfile.DefaultWithSpecies(), ref _dummyLoadouts,
_entManager, _prototypeManager, _cfgManager, _characterRequirementsSystem, _requirements)
{ Preference = new(loadout.ID), };
UpdateSelector(selector, usable);
AddSelector(selector);

category.Children.First().Children.First().AddChild(selector);
}
}

void UpdateSelector(LoadoutPreferenceSelector selector, bool usable)
{
selector.Valid = usable;
selector.ShowUnusable = showUnusable.Value;

foreach (var item in selector.Loadout.Items)
{
if (_dummyLoadouts.TryGetValue(selector.Loadout.ID + selector.Loadout.Items.IndexOf(item), out var entity)
&& _entManager.GetComponent<MetaDataComponent>(entity).EntityPrototype!.ID == item)
{
if (!_entManager.HasComponent<ClothingComponent>(entity))
{
selector.Wearable = true;
continue;
}
selector.Wearable = _characterRequirementsSystem.CanEntityWearItem(PreviewDummy, entity);
continue;
}

entity = _entManager.SpawnEntity(item, MapCoordinates.Nullspace);
_dummyLoadouts[selector.Loadout.ID + selector.Loadout.Items.IndexOf(item)] = entity;

if (!_entManager.HasComponent<ClothingComponent>(entity))
{
selector.Wearable = true;
continue;
}
selector.Wearable = _characterRequirementsSystem.CanEntityWearItem(PreviewDummy, entity);
}
}

Expand Down
10 changes: 6 additions & 4 deletions Content.Client/Lobby/UI/LoadoutPreferenceSelector.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
Name="PreferenceButton"
Access="Public"
ToggleMode="True"
VerticalAlignment="Center" />
VerticalAlignment="Center"
StyleClasses="OpenRight" />

<Button
Name="HeirloomButton"
Access="Public"
Text="{Loc 'humanoid-profile-editor-loadouts-heirloom'}"
ToolTip="{Loc 'humanoid-profile-editor-loadouts-heirloom-tooltip'}"
ToggleMode="True"
VerticalAlignment="Center" />
VerticalAlignment="Center"
StyleClasses="OpenBoth" />

<!-- Yes I know I can use a TextureButton, but I'm doing this for style -->
<!-- Yes I know I can use a TextureButton, but that doesn't have styling -->
<Button
Name="GuidebookButton"
ToolTip="{Loc 'humanoid-profile-editor-loadouts-guidebook-button-tooltip'}"
Expand All @@ -35,7 +37,7 @@


<Collapsible Name="SpecialMenu" HorizontalExpand="True">
<Button Name="HeadingButton" Text="{Loc 'humanoid-profile-editor-loadouts-customize'}" ToggleMode="True" />
<Button Name="HeadingButton" Text="{Loc 'humanoid-profile-editor-loadouts-customize'}" ToggleMode="True" StyleClasses="OpenLeft" />

<CollapsibleBody HorizontalExpand="True" Margin="0 0 0 5">
<PanelContainer HorizontalExpand="True">
Expand Down
21 changes: 10 additions & 11 deletions Content.Client/Lobby/UI/LoadoutPreferenceSelector.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Numerics;
using System.Text;
using Content.Client.Guidebook;
using Content.Client.Paint;
using Content.Client.Players.PlayTimeTracking;
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Systems.Guidebook;
Expand Down Expand Up @@ -110,9 +109,9 @@ public LoadoutPreferenceSelector(LoadoutPrototype loadout, JobPrototype highJob,
entities.Add(loadout.ID + 0, dummyLoadoutItem);

// Create a sprite preview of the loadout item
previewLoadout = new SpriteView
previewLoadout = new()
{
Scale = new Vector2(1, 1),
Scale = new(1, 1),
OverrideDirection = Direction.South,
VerticalAlignment = VAlignment.Center,
SizeFlagsStretchRatio = 1,
Expand All @@ -122,9 +121,9 @@ public LoadoutPreferenceSelector(LoadoutPrototype loadout, JobPrototype highJob,
else
{
// Create a sprite preview of the loadout item
previewLoadout = new SpriteView
previewLoadout = new()
{
Scale = new Vector2(1, 1),
Scale = new(1, 1),
OverrideDirection = Direction.South,
VerticalAlignment = VAlignment.Center,
SizeFlagsStretchRatio = 1,
Expand Down Expand Up @@ -177,7 +176,7 @@ void UpdateGuidebook() => GuidebookButton.Visible =
MinWidth = 32,
MaxWidth = 32,
ClipText = true,
Margin = new Thickness(0, 0, 8, 0),
Margin = new(0, 0, 8, 0),
},
new PanelContainer
{
Expand All @@ -190,7 +189,7 @@ void UpdateGuidebook() => GuidebookButton.Visible =
new Label
{
Text = loadoutName,
Margin = new Thickness(8, 0, 0, 0),
Margin = new(8, 0, 0, 0),
},
},
});
Expand Down Expand Up @@ -221,7 +220,7 @@ void UpdateGuidebook() => GuidebookButton.Visible =
ColorEdit.OnColorChanged += _ =>
{
_preference.CustomColorTint = SpecialColorTintToggle.Pressed ? ColorEdit.Color.ToHex() : null;
UpdatePaint(new Entity<PaintedComponent>(dummyLoadoutItem, paint), entityManager);
UpdatePaint(new(dummyLoadoutItem, paint), entityManager);
};

NameEdit.PlaceHolder = loadoutName;
Expand All @@ -235,7 +234,7 @@ void UpdateGuidebook() => GuidebookButton.Visible =

// Get requirement reasons
characterRequirementsSystem.CheckRequirementsValid(
loadout.Requirements, highJob, profile, new Dictionary<string, TimeSpan>(),
loadout.Requirements, highJob, profile, new(),
jobRequirementsManager.IsWhitelisted(), loadout,
entityManager, prototypeManager, configManager,
out var reasons);
Expand All @@ -262,9 +261,9 @@ protected override void FrameUpdate(FrameEventArgs args)
// Move the special editor
var heading = SpecialMenu.Heading;
heading.Orphan();
ButtonGroup.AddChild(heading);
ButtonGroup.AddButton(heading, false);
GuidebookButton.Orphan();
ButtonGroup.AddChild(GuidebookButton);
ButtonGroup.AddButton(GuidebookButton);

// These guys are here too for reasons
HeadingButton.SetHeight = HeirloomButton.SetHeight = GuidebookButton.SetHeight = PreferenceButton.Size.Y;
Expand Down
Loading