Files
wwdpublic/Content.Client/Cargo/UI/CargoConsoleMenu.xaml.cs
SimpleStation14 a8242a38a3 Mirror: CargoConsoleMenu localization (#127)
## Mirror of PR #25931: [CargoConsoleMenu
localization](https://github.com/space-wizards/space-station-14/pull/25931)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `6e38b992cfc607e09178efc178b86f35152f0cde`

PR opened by <img
src="https://avatars.githubusercontent.com/u/87994977?v=4"
width="16"/><a href="https://github.com/modern-nm"> modern-nm</a> at
2024-03-08 14:46:29 UTC
PR merged by <img
src="https://avatars.githubusercontent.com/u/19864447?v=4"
width="16"/><a href="https://github.com/web-flow"> web-flow</a> at
2024-03-09 06:56:40 UTC

---

PR changed 20 files with 211 additions and 195 deletions.

The PR had the following labels:
- Changes: UI


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> This PR adds localization capability for CargoConsoleMenu (name of
entity: ComputerCargoOrders)
> 
> ## Why / Balance
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> 
> Please let me tell you a story of my vision. First i decided to make
these changes on one of russian community servers. But taking into
account future changes of .yml files by space-wizards community (they
are regularly added to our space-station build), we will have to
constantly edit xml files to remove duplicate categories — imagine
someone added cargoproduct with category "Fun", when we have tons of
items with category "Развлечения".
> 
> To summarize: my PR makes possible to localize categories of
"cargoproduct" and makes communities do less work when upstream comes
> 
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> 
> New code considers strings comparisons of prototype.Category
(Content.Client/Cargo/UI/CargoConsoleMenu.xaml.cs) using
Loc.GetString(...). And that's all.
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
> Off course there are no visible changes on eng-localization
> 
>
![Снимок_ножницы](https://github.com/space-wizards/space-station-14/assets/87994977/5cdbf5df-c4cc-42e6-861b-aa35a009e957)
> 
> That's how these changes look like on our community build, using other
.ftl file
> 
>
![Снимок_ножницы2](https://github.com/space-wizards/space-station-14/assets/87994977/20023fc8-5cdf-4069-981b-edbfa98fa31d)
> 
> 
> - [x] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> ## Breaking changes
> <!--
> List any breaking changes, including namespace, public
class/method/field changes, prototype renames; and provide instructions
for fixing them. This will be pasted in #codebase-changes.
> -->
> 
> 
> .yml
> - every category of type "cargoProduct" was edited to
"cargoproduct-category-name-{categoryName}"
> 
> .ftl
> - file "cargoproduct-categories.ftl" was added to english localization
to localize categories of cargoProduct
> 
> .cs
> - CargoConsoleMenu.xaml.cs got tweaks which prevent issues with
strings comparisons
> 
> **Changelog**
> <!--
> Make players aware of new features and changes that could affect how
they play the game by adding a Changelog entry. Please read the
Changelog guidelines located at:
https://docs.spacestation14.io/en/getting-started/pr-guideline#changelog
> -->
> 


</details>

---------

Signed-off-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: MODERN <87994977+modern-nm@users.noreply.github.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
2024-05-28 21:34:09 -04:00

199 lines
7.4 KiB
C#

using System.Linq;
using Content.Client.UserInterface.Controls;
using Content.Shared.Cargo;
using Content.Shared.Cargo.Components;
using Content.Shared.Cargo.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.Cargo.UI
{
[GenerateTypedNameReferences]
public sealed partial class CargoConsoleMenu : FancyWindow
{
private IEntityManager _entityManager;
private IPrototypeManager _protoManager;
private SpriteSystem _spriteSystem;
private EntityUid _owner;
public event Action<ButtonEventArgs>? OnItemSelected;
public event Action<ButtonEventArgs>? OnOrderApproved;
public event Action<ButtonEventArgs>? OnOrderCanceled;
private readonly List<string> _categoryStrings = new();
private string? _category;
public CargoConsoleMenu(EntityUid owner, IEntityManager entMan, IPrototypeManager protoManager, SpriteSystem spriteSystem)
{
RobustXamlLoader.Load(this);
_entityManager = entMan;
_protoManager = protoManager;
_spriteSystem = spriteSystem;
_owner = owner;
Title = Loc.GetString("cargo-console-menu-title");
SearchBar.OnTextChanged += OnSearchBarTextChanged;
Categories.OnItemSelected += OnCategoryItemSelected;
}
private void OnCategoryItemSelected(OptionButton.ItemSelectedEventArgs args)
{
SetCategoryText(args.Id);
PopulateProducts();
}
private void OnSearchBarTextChanged(LineEdit.LineEditEventArgs args)
{
PopulateProducts();
}
private void SetCategoryText(int id)
{
_category = id == 0 ? null : _categoryStrings[id];
Categories.SelectId(id);
}
public IEnumerable<CargoProductPrototype> ProductPrototypes
{
get
{
var allowedGroups = _entityManager.GetComponentOrNull<CargoOrderConsoleComponent>(_owner)?.AllowedGroups;
foreach (var cargoPrototype in _protoManager.EnumeratePrototypes<CargoProductPrototype>())
{
if (!allowedGroups?.Contains(cargoPrototype.Group) ?? false)
continue;
yield return cargoPrototype;
}
}
}
/// <summary>
/// Populates the list of products that will actually be shown, using the current filters.
/// </summary>
public void PopulateProducts()
{
Products.RemoveAllChildren();
var products = ProductPrototypes.ToList();
products.Sort((x, y) =>
string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase));
var search = SearchBar.Text.Trim().ToLowerInvariant();
foreach (var prototype in products)
{
// if no search or category
// else if search
// else if category and not search
if (search.Length == 0 && _category == null ||
search.Length != 0 && prototype.Name.ToLowerInvariant().Contains(search) ||
search.Length != 0 && prototype.Description.ToLowerInvariant().Contains(search) ||
search.Length == 0 && _category != null && Loc.GetString(prototype.Category).Equals(_category))
{
var button = new CargoProductRow
{
Product = prototype,
ProductName = { Text = prototype.Name },
MainButton = { ToolTip = prototype.Description },
PointCost = { Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", prototype.Cost.ToString())) },
Icon = { Texture = _spriteSystem.Frame0(prototype.Icon) },
};
button.MainButton.OnPressed += args =>
{
OnItemSelected?.Invoke(args);
};
Products.AddChild(button);
}
}
}
/// <summary>
/// Populates the list of products that will actually be shown, using the current filters.
/// </summary>
public void PopulateCategories()
{
_categoryStrings.Clear();
Categories.Clear();
foreach (var prototype in ProductPrototypes)
{
if (!_categoryStrings.Contains(Loc.GetString(prototype.Category)))
{
_categoryStrings.Add(Loc.GetString(prototype.Category));
}
}
_categoryStrings.Sort();
// Add "All" category at the top of the list
_categoryStrings.Insert(0, Loc.GetString("cargo-console-menu-populate-categories-all-text"));
foreach (var str in _categoryStrings)
{
Categories.AddItem(str);
}
}
/// <summary>
/// Populates the list of orders and requests.
/// </summary>
public void PopulateOrders(IEnumerable<CargoOrderData> orders)
{
Orders.DisposeAllChildren();
Requests.DisposeAllChildren();
foreach (var order in orders)
{
var product = _protoManager.Index<EntityPrototype>(order.ProductId);
var productName = product.Name;
var row = new CargoOrderRow
{
Order = order,
Icon = { Texture = _spriteSystem.Frame0(product) },
ProductName =
{
Text = Loc.GetString(
"cargo-console-menu-populate-orders-cargo-order-row-product-name-text",
("productName", productName),
("orderAmount", order.OrderQuantity),
("orderRequester", order.Requester))
},
Description = {Text = Loc.GetString("cargo-console-menu-order-reason-description",
("reason", order.Reason))}
};
row.Cancel.OnPressed += (args) => { OnOrderCanceled?.Invoke(args); };
if (order.Approved)
{
row.Approve.Visible = false;
row.Cancel.Visible = false;
Orders.AddChild(row);
}
else
{
// TODO: Disable based on access.
row.Approve.OnPressed += (args) => { OnOrderApproved?.Invoke(args); };
Requests.AddChild(row);
}
}
}
public void UpdateCargoCapacity(int count, int capacity)
{
// TODO: Rename + Loc.
ShuttleCapacityLabel.Text = $"{count}/{capacity}";
}
public void UpdateBankData(string name, int points)
{
AccountNameLabel.Text = name;
PointsLabel.Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", points.ToString()));
}
}
}