Implement traits system (#10693)

This commit is contained in:
Visne
2022-09-10 17:40:06 +02:00
committed by GitHub
parent e1782ec22b
commit 4cc5fa239e
17 changed files with 3006 additions and 29 deletions

View File

@@ -15,6 +15,7 @@ using Content.Shared.Markings;
using Content.Shared.Preferences;
using Content.Shared.Roles;
using Content.Shared.Species;
using Content.Shared.Traits;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
@@ -80,12 +81,14 @@ namespace Content.Client.Preferences.UI
private TabContainer _tabContainer => CTabContainer;
private BoxContainer _jobList => CJobList;
private BoxContainer _antagList => CAntagList;
private BoxContainer _traitsList => CTraitsList;
private readonly List<JobPrioritySelector> _jobPriorities;
private OptionButton _preferenceUnavailableButton => CPreferenceUnavailableButton;
private readonly Dictionary<string, BoxContainer> _jobCategories;
// Mildly hacky, as I don't trust prototype order to stay consistent and don't want the UI to break should a new one get added mid-edit. --moony
private readonly List<SpeciesPrototype> _speciesList;
private readonly List<AntagPreferenceSelector> _antagPreferences;
private readonly List<TraitPreferenceSelector> _traitPreferences;
private Control _previewSpriteControl => CSpriteViewFront;
private Control _previewSpriteSideControl => CSpriteViewSide;
@@ -461,14 +464,46 @@ namespace Content.Client.Preferences.UI
#endregion Antags
#region Traits
var traits = prototypeManager.EnumeratePrototypes<TraitPrototype>().OrderBy(t => t.Name).ToList();
_traitPreferences = new List<TraitPreferenceSelector>();
_tabContainer.SetTabTitle(3, Loc.GetString("humanoid-profile-editor-traits-tab"));
if (traits.Count > 0)
{
foreach (var trait in traits)
{
var selector = new TraitPreferenceSelector(trait);
_traitsList.AddChild(selector);
_traitPreferences.Add(selector);
selector.PreferenceChanged += preference =>
{
Profile = Profile?.WithTraitPreference(trait.ID, preference);
IsDirty = true;
};
}
}
else
{
_traitsList.AddChild(new Label
{
Text = "No traits available :(",
FontColorOverride = Color.Gray,
});
}
#endregion
#region Save
_saveButton.OnPressed += args => { Save(); };
_saveButton.OnPressed += _ => { Save(); };
#endregion Save
#region Markings
_tabContainer.SetTabTitle(3, Loc.GetString("humanoid-profile-editor-markings-tab"));
_tabContainer.SetTabTitle(4, Loc.GetString("humanoid-profile-editor-markings-tab"));
CMarkings.OnMarkingAdded += OnMarkingChange;
CMarkings.OnMarkingRemoved += OnMarkingChange;
@@ -483,7 +518,7 @@ namespace Content.Client.Preferences.UI
{
var flavorText = new FlavorText.FlavorText();
_tabContainer.AddChild(flavorText);
_tabContainer.SetTabTitle(_tabContainer.ChildCount-1, Loc.GetString("humanoid-profile-editor-flavortext-tab"));
_tabContainer.SetTabTitle(_tabContainer.ChildCount - 1, Loc.GetString("humanoid-profile-editor-flavortext-tab"));
_flavorTextEdit = flavorText.CFlavorTextInput;
flavorText.OnFlavorTextChanged += OnFlavorTextChange;
@@ -960,6 +995,7 @@ namespace Content.Client.Preferences.UI
UpdateSaveButton();
UpdateJobPriorities();
UpdateAntagPreferences();
UpdateTraitPreferences();
UpdateMarkings();
NeedsDummyRebuild = true;
@@ -1103,6 +1139,17 @@ namespace Content.Client.Preferences.UI
}
}
private void UpdateTraitPreferences()
{
foreach (var preferenceSelector in _traitPreferences)
{
var traitId = preferenceSelector.Trait.ID;
var preference = Profile?.TraitPreferences.Contains(traitId) ?? false;
preferenceSelector.Preference = preference;
}
}
private sealed class AntagPreferenceSelector : Control
{
public AntagPrototype Antag { get; }
@@ -1138,5 +1185,38 @@ namespace Content.Client.Preferences.UI
PreferenceChanged?.Invoke(Preference);
}
}
private sealed class TraitPreferenceSelector : Control
{
public TraitPrototype Trait { get; }
private readonly CheckBox _checkBox;
public bool Preference
{
get => _checkBox.Pressed;
set => _checkBox.Pressed = value;
}
public event Action<bool>? PreferenceChanged;
public TraitPreferenceSelector(TraitPrototype trait)
{
Trait = trait;
_checkBox = new CheckBox {Text = $"{trait.Name}"};
_checkBox.OnToggled += OnCheckBoxToggled;
AddChild(new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
Children = { _checkBox },
});
}
private void OnCheckBoxToggled(BaseButton.ButtonToggledEventArgs args)
{
PreferenceChanged?.Invoke(Preference);
}
}
}
}