mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-09 10:21:18 +03:00
296 lines
11 KiB
C#
296 lines
11 KiB
C#
using System.Linq;
|
|
using System.Numerics;
|
|
using Content.Client.Stylesheets.Palette;
|
|
using Content.Client.UserInterface.Controls;
|
|
using Content.Shared.Construction.Prototypes;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.CustomControls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Client.Construction.UI
|
|
{
|
|
/// <summary>
|
|
/// This is the interface for a UI View of the construction window. The point of it is to abstract away the actual
|
|
/// UI controls and just provide higher level operations on the entire window. This View is completely passive and
|
|
/// just raises events to the outside world. This class is controlled by the <see cref="ConstructionMenuPresenter"/>.
|
|
/// </summary>
|
|
public interface IConstructionMenuView : IDisposable
|
|
{
|
|
// It isn't optimal to expose UI controls like this, but the UI control design is
|
|
// questionable so it can't be helped.
|
|
string[] Categories { get; set; }
|
|
OptionButton OptionCategories { get; }
|
|
|
|
bool EraseButtonPressed { get; set; }
|
|
bool GridViewButtonPressed { get; set; }
|
|
bool BuildButtonPressed { get; set; }
|
|
|
|
ListContainer ListViewRecipes { get; }
|
|
|
|
ScrollContainer GridViewRecipesScrollContainer { get; }
|
|
GridContainer GridViewRecipes { get; }
|
|
|
|
ItemList RecipeStepList { get; }
|
|
|
|
event EventHandler<(string search, string catagory)> PopulateRecipes;
|
|
event EventHandler<ConstructionMenu.ConstructionMenuListData?> RecipeSelected;
|
|
event EventHandler RecipeFavorited;
|
|
event EventHandler<bool> BuildButtonToggled;
|
|
event EventHandler<bool> EraseButtonToggled;
|
|
event EventHandler ClearAllGhosts;
|
|
event EventHandler PreviousRecipeInHistoryButtonPressed;
|
|
event EventHandler NextRecipeInHistoryButtonPressed;
|
|
|
|
void ClearRecipeInfo();
|
|
void SetRecipeInfo(string name, string description, EntityPrototype? targetPrototype, bool isItem, bool isFavorite);
|
|
void ResetPlacement();
|
|
void TrySelectCategory(int categoryId);
|
|
void TrySelectListViewButton(ProtoId<ConstructionPrototype> constructionProtoId);
|
|
void TrySelectGridViewButton(ContainerButton button, ContainerButton? previousButton);
|
|
void TogglePreviousRecipeButton(bool enabled);
|
|
void ToggleNextRecipeButton(bool enabled);
|
|
void UpdateGridViewButtonStyle(BaseButton button, bool selected);
|
|
|
|
#region Window Control
|
|
|
|
event Action? OnClose;
|
|
|
|
bool IsOpen { get; }
|
|
|
|
void OpenCentered();
|
|
void MoveToFront();
|
|
bool IsAtFront();
|
|
void Close();
|
|
|
|
#endregion
|
|
}
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class ConstructionMenu : DefaultWindow, IConstructionMenuView
|
|
{
|
|
public bool BuildButtonPressed
|
|
{
|
|
get => BuildButton.Pressed;
|
|
set => BuildButton.Pressed = value;
|
|
}
|
|
|
|
public string[] Categories { get; set; } = Array.Empty<string>();
|
|
|
|
public bool EraseButtonPressed
|
|
{
|
|
get => EraseButton.Pressed;
|
|
set => EraseButton.Pressed = value;
|
|
}
|
|
|
|
public bool GridViewButtonPressed
|
|
{
|
|
get => MenuGridViewButton.Pressed;
|
|
set => MenuGridViewButton.Pressed = value;
|
|
}
|
|
|
|
public ConstructionMenu()
|
|
{
|
|
SetSize = new Vector2(560, 450);
|
|
MinSize = new Vector2(560, 320);
|
|
|
|
IoCManager.InjectDependencies(this);
|
|
RobustXamlLoader.Load(this);
|
|
|
|
Title = Loc.GetString("construction-menu-title");
|
|
|
|
BuildButton.Text = Loc.GetString("construction-menu-place-ghost");
|
|
ListViewRecipes.ItemPressed += (_, data) => RecipeSelected?.Invoke(this, data as ConstructionMenuListData);
|
|
ListViewRecipes.NoItemSelected += () => RecipeSelected?.Invoke(this, null);
|
|
ListViewRecipes.GenerateItem += (data, button) =>
|
|
{
|
|
if (data is not ConstructionMenuListData (var prototype, var targetPrototype))
|
|
return;
|
|
|
|
var entProtoView = new EntityPrototypeView()
|
|
{
|
|
SetSize = new(32f),
|
|
Stretch = SpriteView.StretchMode.Fill,
|
|
Scale = new(2),
|
|
Margin = new(0, 2),
|
|
};
|
|
entProtoView.SetPrototype(targetPrototype);
|
|
|
|
var label = new Label()
|
|
{
|
|
Text = prototype.Name,
|
|
Margin = new(5, 0),
|
|
};
|
|
|
|
var box = new BoxContainer();
|
|
box.AddChild(entProtoView);
|
|
box.AddChild(label);
|
|
|
|
button.AddChild(box);
|
|
button.ToolTip = prototype.Description;
|
|
button.AddStyleClass(ListContainer.StyleClassListContainerButton);
|
|
};
|
|
|
|
SearchBar.OnTextChanged += _ =>
|
|
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
|
|
OptionCategories.OnItemSelected += obj => OnCategorySelected(obj.Id);
|
|
|
|
BuildButton.Text = Loc.GetString("construction-menu-place-ghost");
|
|
BuildButton.OnToggled += args => BuildButtonToggled?.Invoke(this, args.Pressed);
|
|
ClearButton.Text = Loc.GetString("construction-menu-clear-all");
|
|
ClearButton.OnPressed += _ => ClearAllGhosts?.Invoke(this, EventArgs.Empty);
|
|
EraseButton.Text = Loc.GetString("construction-menu-eraser-mode");
|
|
EraseButton.OnToggled += args => EraseButtonToggled?.Invoke(this, args.Pressed);
|
|
|
|
NextRecipeInHistoryButton.OnPressed += _ => NextRecipeInHistoryButtonPressed?.Invoke(this, EventArgs.Empty);
|
|
PreviousRecipeInHistoryButton.OnPressed += _ => PreviousRecipeInHistoryButtonPressed?.Invoke(this, EventArgs.Empty);
|
|
|
|
FavoriteButton.OnPressed += args => RecipeFavorited?.Invoke(this, EventArgs.Empty);
|
|
|
|
MenuGridViewButton.OnPressed += _ =>
|
|
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
|
|
}
|
|
|
|
public event EventHandler? ClearAllGhosts;
|
|
public event EventHandler<(string search, string catagory)>? PopulateRecipes;
|
|
public event EventHandler<ConstructionMenuListData?>? RecipeSelected;
|
|
public event EventHandler? RecipeFavorited;
|
|
public event EventHandler<bool>? BuildButtonToggled;
|
|
public event EventHandler<bool>? EraseButtonToggled;
|
|
public event EventHandler? PreviousRecipeInHistoryButtonPressed;
|
|
public event EventHandler? NextRecipeInHistoryButtonPressed;
|
|
|
|
public void ResetPlacement()
|
|
{
|
|
BuildButton.Pressed = false;
|
|
EraseButton.Pressed = false;
|
|
}
|
|
|
|
public void SetRecipeInfo(
|
|
string name,
|
|
string description,
|
|
EntityPrototype? targetPrototype,
|
|
bool isItem,
|
|
bool isFavorite)
|
|
{
|
|
BuildButton.Disabled = false;
|
|
BuildButton.Text = Loc.GetString(isItem ? "construction-menu-place-ghost" : "construction-menu-craft");
|
|
TargetName.SetMessage(name);
|
|
TargetDesc.SetMessage(description);
|
|
TargetTexture.SetPrototype(targetPrototype?.ID);
|
|
FavoriteButton.Visible = true;
|
|
FavoriteButton.Text = Loc.GetString(
|
|
isFavorite ? "construction-add-favorite-button" : "construction-remove-from-favorite-button");
|
|
}
|
|
|
|
public void ClearRecipeInfo()
|
|
{
|
|
BuildButton.Disabled = true;
|
|
TargetName.SetMessage(string.Empty);
|
|
TargetDesc.SetMessage(string.Empty);
|
|
TargetTexture.SetPrototype(null);
|
|
FavoriteButton.Visible = false;
|
|
RecipeStepList.Clear();
|
|
}
|
|
|
|
public sealed record ConstructionMenuListData(ConstructionPrototype ConstructionProto, EntityPrototype EntityProto) : ListData;
|
|
|
|
/// <summary>
|
|
/// Handle category selection.
|
|
/// </summary>
|
|
private void OnCategorySelected(int categoryId)
|
|
{
|
|
OptionCategories.SelectId(categoryId);
|
|
SearchBar.SetText(string.Empty);
|
|
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[categoryId]));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to select category by its ID.
|
|
/// </summary>
|
|
/// <returns>Whether it was successful.</returns>
|
|
public void TrySelectCategory(int categoryId)
|
|
{
|
|
// do nothing if category is already selected
|
|
if (OptionCategories.SelectedId == categoryId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
OnCategorySelected(categoryId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to select specified list view button using recipe ID. Only works when in list view.
|
|
/// </summary>
|
|
/// <returns>Whether it was successful.</returns>
|
|
public void TrySelectListViewButton(ProtoId<ConstructionPrototype> constructionProtoId)
|
|
{
|
|
var isGridView = GridViewButtonPressed;
|
|
if (isGridView)
|
|
return;
|
|
|
|
if (!ListViewRecipes.Data.TryFirstOrDefault(
|
|
data => ((ConstructionMenuListData)data).ConstructionProto.ID == constructionProtoId,
|
|
out var matchingData))
|
|
{
|
|
return;
|
|
}
|
|
|
|
ListViewRecipes.Select(matchingData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to select specified grid view button using recipe ID. Only works when in grid view.
|
|
/// </summary>
|
|
/// <returns>Whether it was successful.</returns>
|
|
public void TrySelectGridViewButton(ContainerButton button, ContainerButton? previousButton)
|
|
{
|
|
var isGridView = GridViewButtonPressed;
|
|
if (!isGridView)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (previousButton is not null && previousButton != button)
|
|
{
|
|
previousButton.Pressed = false;
|
|
UpdateGridViewButtonStyle(previousButton, false);
|
|
}
|
|
|
|
button.Pressed = true;
|
|
UpdateGridViewButtonStyle(button, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Toggles previous recipe button on/off.
|
|
/// </summary>
|
|
/// <param name="enabled">Desired toggle state.</param>
|
|
public void TogglePreviousRecipeButton(bool enabled)
|
|
{
|
|
PreviousRecipeInHistoryButton.Disabled = !enabled;
|
|
}
|
|
|
|
/// <summary>d
|
|
/// </summary>
|
|
/// <param name="enabled">Desired toggle state.</param>
|
|
public void ToggleNextRecipeButton(bool enabled)
|
|
{
|
|
NextRecipeInHistoryButton.Disabled = !enabled;
|
|
}
|
|
|
|
|
|
|
|
public void UpdateGridViewButtonStyle(BaseButton button, bool selected)
|
|
{
|
|
if (button.Parent is not PanelContainer buttonPanel)
|
|
return;
|
|
|
|
button.Children.Single().Modulate = selected ? Color.Green : Color.White;
|
|
buttonPanel.PanelOverride = new StyleBoxFlat(selected ? Palettes.Navy.Element : Color.Transparent);
|
|
}
|
|
}
|
|
}
|