Files
tbd-station-14/Content.Client/Traitor/Uplink/UplinkMenu.xaml.cs

102 lines
3.5 KiB
C#

using System;
using Content.Shared.PDA;
using Content.Shared.Traitor.Uplink;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
namespace Content.Client.Traitor.Uplink
{
[GenerateTypedNameReferences]
public partial class UplinkMenu : SS14Window
{
private readonly IPrototypeManager _prototypeManager;
public event Action<BaseButton.ButtonEventArgs, UplinkListingData>? OnListingButtonPressed;
public event Action<BaseButton.ButtonEventArgs, UplinkCategory>? OnCategoryButtonPressed;
public UplinkMenu(IPrototypeManager prototypeManager)
{
RobustXamlLoader.Load(this);
_prototypeManager = prototypeManager;
PopulateUplinkCategoryButtons();
}
public UplinkCategory CurrentFilterCategory
{
get => _currentFilter;
set
{
if (value.GetType() != typeof(UplinkCategory))
{
return;
}
_currentFilter = value;
}
}
public UplinkAccountData? CurrentLoggedInAccount
{
get => _loggedInUplinkAccount;
set => _loggedInUplinkAccount = value;
}
private UplinkCategory _currentFilter;
private UplinkAccountData? _loggedInUplinkAccount;
public void AddListingGui(UplinkListingData listing)
{
if (!_prototypeManager.TryIndex(listing.ItemId, out EntityPrototype? prototype) || listing.Category != CurrentFilterCategory)
{
return;
}
var listingName = listing.ListingName == string.Empty ? prototype.Name : listing.ListingName;
var listingDesc = listing.Description == string.Empty ? prototype.Description : listing.Description;
var listingPrice = listing.Price;
var canBuy = _loggedInUplinkAccount?.DataBalance >= listing.Price;
var newListing = new UplinkListingControl(listingName, listingDesc, listingPrice, canBuy);
newListing.UplinkItemBuyButton.OnButtonDown += args
=> OnListingButtonPressed?.Invoke(args, listing);
UplinkListingsContainer.AddChild(newListing);
}
public void ClearListings()
{
UplinkListingsContainer.Children.Clear();
}
private void PopulateUplinkCategoryButtons()
{
foreach (UplinkCategory cat in Enum.GetValues(typeof(UplinkCategory)))
{
var catButton = new PDAUplinkCategoryButton
{
Text = Loc.GetString(cat.ToString()),
ButtonCategory = cat
};
//It'd be neat if it could play a cool tech ping sound when you switch categories,
//but right now there doesn't seem to be an easy way to do client-side audio without still having to round trip to the server and
//send to a specific client INetChannel.
catButton.OnPressed += args => OnCategoryButtonPressed?.Invoke(args, catButton.ButtonCategory);
CategoryListContainer.AddChild(catButton);
}
}
private sealed class PDAUplinkCategoryButton : Button
{
public UplinkCategory ButtonCategory;
}
}
}