- added search box for vending machine ui. WARNING! needs refactoring. or not

This commit is contained in:
Іван Оніщенко
2024-12-14 02:06:20 +03:00
parent ef466ba89a
commit 7eea717fe5
3 changed files with 39 additions and 8 deletions

View File

@@ -15,10 +15,11 @@ namespace Content.Client.VendingMachines
[ViewVariables]
private List<int> _cachedFilteredIndex = new();
private VendingMachineComponent component = new();//WD edit
public VendingMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
@@ -26,7 +27,7 @@ namespace Content.Client.VendingMachines
var vendingMachineSys = EntMan.System<VendingMachineSystem>();
// WD EDIT START
var component = EntMan.GetComponent<VendingMachineComponent>(Owner);
component = EntMan.GetComponent<VendingMachineComponent>(Owner);
_cachedInventory = vendingMachineSys.GetAllInventory(Owner, component);
_menu = new VendingMenu { Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName };
@@ -34,6 +35,7 @@ namespace Content.Client.VendingMachines
_menu.OnClose += Close;
_menu.OnItemSelected += OnItemSelected;
_menu.OnWithdraw += SendMessage;
_menu.SearchBar.OnTextChanged += UpdateFilter;
// WD EDIT END
_menu.Populate(_cachedInventory, component.PriceMultiplier, component.Credits);
@@ -41,6 +43,24 @@ namespace Content.Client.VendingMachines
_menu.OpenCenteredLeft();
}
// WD EDIT START
private void UpdateFilter(Robust.Client.UserInterface.Controls.LineEdit.LineEditEventArgs obj)
{
if (_menu != null)
{
_menu.filter = obj.Text;
_menu.Populate(_cachedInventory, component.PriceMultiplier, component.Credits);
}
}
// WD EDIT END
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
@@ -84,7 +104,7 @@ namespace Content.Client.VendingMachines
private void OnSearchChanged(string? filter)
{
//_menu?.Populate(_cachedInventory, out _cachedFilteredIndex, filter);
//_menu?.filter = (filter ?? "");
}
}
}

View File

@@ -1,4 +1,7 @@
<DefaultWindow xmlns="https://spacestation14.io">
<DefaultWindow xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:style="clr-namespace:Content.Client.Stylesheets">
<BoxContainer Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<Label Name="CreditsLabel" HorizontalExpand="True"/>
@@ -7,6 +10,9 @@
Text="{Loc 'store-ui-default-withdraw-text'}"
HorizontalAlignment="Right"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<LineEdit Name="SearchBar" PlaceHolder="{Loc 'vending-machine-component-search-filter'}" HorizontalExpand="True" Margin ="4 4" Access="Public"/>
</BoxContainer>
<PanelContainer StyleClasses="HighDivider" />
<Label
Name="OutOfStockLabel"

View File

@@ -13,17 +13,20 @@ namespace Content.Client._White.Economy.Ui;
public sealed partial class VendingMenu : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public event Action<int>? OnItemSelected;
public Action<VendingMachineWithdrawMessage>? OnWithdraw;
public string filter = "";
public VendingMenu()
{
MinSize = SetSize = new Vector2(250, 150);
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
// SearchBar.OnTextChanged += UpdateFilter;
}
/// <summary>
/// Populates the list of available items on the vending machine interface
/// and sets icons based on their prototypes
@@ -55,7 +58,7 @@ public sealed partial class VendingMenu : DefaultWindow
for (var i = 0; i < inventory.Count; i++)
{
var entry = inventory[i];
var itemName = entry.ID;
Texture? icon = null;
if (_prototypeManager.TryIndex<EntityPrototype>(entry.ID, out var prototype))
@@ -73,6 +76,8 @@ public sealed partial class VendingMenu : DefaultWindow
var j = i;
vendingItem.VendingItemBuyButton.OnPressed += _ => { OnItemSelected?.Invoke(j); };
if(filter == "" || (prototype?.Name?.Contains(filter) == true)) //WD
VendingContents.AddChild(vendingItem);
}
@@ -84,4 +89,4 @@ public sealed partial class VendingMenu : DefaultWindow
SetSize = new Vector2(Math.Clamp((longestEntryLength + 10) * 12, 250, 700),
Math.Clamp(VendingContents.ChildCount * 50, 150, 400));
}
}
}