Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024 12 25 customsplit suggestions #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Content.Client/Stack/StackCustomSplitBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using JetBrains.Annotations;
using Content.Shared.Stacks;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;

namespace Content.Client.Stack
{
[UsedImplicitly]
public sealed class StackCustomSplitBoundUserInterface : BoundUserInterface
{
private IEntityManager _entManager;
private EntityUid _owner;
[ViewVariables]
private StackCustomSplitWindow? _window;

public StackCustomSplitBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_owner = owner;
_entManager = IoCManager.Resolve<IEntityManager>();
}

protected override void Open()
{
base.Open();
_window = this.CreateWindow<StackCustomSplitWindow>();

if (_entManager.TryGetComponent<StackComponent>(_owner, out var comp))
_window.SetMax(comp.Count);

_window.ApplyButton.OnPressed += _ =>
{
if (int.TryParse(_window.AmountLineEdit.Text, out var i))
{
SendMessage(new StackCustomSplitAmountMessage(i));
_window.Close();
}
};
}
}
}
14 changes: 14 additions & 0 deletions Content.Client/Stack/StackCustomSplitWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'ui-custom-stack-split-title'}"
Resizable="False">

<BoxContainer Orientation="Vertical" SeparationOverride="4" MinSize="240 80">
<BoxContainer Orientation="Horizontal">
<LineEdit Name="AmountLineEdit" Access="Public" HorizontalExpand="True" PlaceHolder="{Loc 'ui-custom-stack-split-line-edit-placeholder'}"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<Label Name="MaximumAmount" Access="Public" />
</BoxContainer>
<Button Name="ApplyButton" Access="Public" Text="{Loc 'ui-custom-stack-split-apply'}"/>
</BoxContainer>
</DefaultWindow>
34 changes: 34 additions & 0 deletions Content.Client/Stack/StackCustomSplitWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;

namespace Content.Client.Stack
{
[GenerateTypedNameReferences]
public sealed partial class StackCustomSplitWindow : DefaultWindow
{
private int _max = Int32.MaxValue;
private int _min = 1;

public StackCustomSplitWindow()
{
RobustXamlLoader.Load(this);
AmountLineEdit.OnTextChanged += OnValueChanged;
}

public void SetMax(int max)
{
_max = max;
MaximumAmount.Text = Loc.GetString("comp-stack-split-size", ("size", _max));
}

private void OnValueChanged(LineEdit.LineEditEventArgs args)
{
if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount > _max || amount < _min)
ApplyButton.Disabled = true;
else
ApplyButton.Disabled = false;
}
}
}
28 changes: 28 additions & 0 deletions Content.Server/Stack/StackSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Content.Server.Stack
public sealed class StackSystem : SharedStackSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedUserInterfaceSystem _ui = default!;

public static readonly int[] DefaultSplitAmounts = { 1, 5, 10, 20, 30, 50 };

Expand Down Expand Up @@ -198,6 +199,33 @@ private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, Get

args.Verbs.Add(verb);
}

if (_ui.HasUi(uid, StackCustomSplitUiKey.Key))
{
AlternativeVerb custom = new()
{
Text = Loc.GetString("comp-stack-split-custom"),
Category = VerbCategory.Split,
Act = () =>
{
_ui.OpenUi(uid, StackCustomSplitUiKey.Key, args.User);
},
Priority = priority - 1
};
args.Verbs.Add(custom);
}
}

protected override void OnCustomSplitMessage(Entity<StackComponent> ent, ref StackCustomSplitAmountMessage message)
{
var (uid, comp) = ent;

// digital ghosts shouldn't be allowed to split stacks
if (!(message.Actor is { Valid: true } user))
return;

var amount = message.Amount;
UserSplit(uid, user, amount, comp);
}

private void UserSplit(EntityUid uid, EntityUid userUid, int amount,
Expand Down
20 changes: 20 additions & 0 deletions Content.Shared/Stacks/CustomStackSplitPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.GameObjects;
using Robust.Shared.Utility;

namespace Content.Shared.Stacks;

[Prototype]
public sealed partial class CustomStackSplitPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;

/// <summary>
/// UI parameters for custom stack splitting.
/// </summary>
[DataField(required: true)]
public InterfaceData Interface { get; private set; } = default!;
}
13 changes: 13 additions & 0 deletions Content.Shared/Stacks/SharedStackSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Content.Shared.Popups;
using Content.Shared.Storage.EntitySystems;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
Expand All @@ -27,21 +28,33 @@ public abstract class SharedStackSystem : EntitySystem
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!;
[Dependency] private readonly SharedStorageSystem _storage = default!;
[Dependency] private readonly SharedUserInterfaceSystem _ui = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<StackComponent, ComponentInit>(OnStackInit);
SubscribeLocalEvent<StackComponent, ComponentGetState>(OnStackGetState);
SubscribeLocalEvent<StackComponent, ComponentHandleState>(OnStackHandleState);
SubscribeLocalEvent<StackComponent, ComponentStartup>(OnStackStarted);
SubscribeLocalEvent<StackComponent, ExaminedEvent>(OnStackExamined);
SubscribeLocalEvent<StackComponent, InteractUsingEvent>(OnStackInteractUsing);
SubscribeLocalEvent<StackComponent, StackCustomSplitAmountMessage>(OnCustomSplitMessage);

_vvm.GetTypeHandler<StackComponent>()
.AddPath(nameof(StackComponent.Count), (_, comp) => comp.Count, SetCount);
}

private void OnStackInit(EntityUid uid, StackComponent stack, ref ComponentInit args)
{
if (!string.IsNullOrEmpty(stack.CustomSplit) && _prototype.TryIndex<CustomStackSplitPrototype>(stack.CustomSplit, out var customStackSplit))
_ui.SetUi(uid, StackCustomSplitUiKey.Key, customStackSplit.Interface);
}

// client shouldn't try to split stacks so do nothing on client
protected virtual void OnCustomSplitMessage(Entity<StackComponent> ent, ref StackCustomSplitAmountMessage message) {}

public override void Shutdown()
{
base.Shutdown();
Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/Stacks/StackComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public sealed partial class StackComponent : Component
[DataField("layerStates")]
[ViewVariables(VVAccess.ReadWrite)]
public List<string> LayerStates = new();

/// <summary>
/// The CustomStackSplit prototype to use for the stack interface
/// </summary>
[DataField("customSplit", customTypeSerializer: typeof(PrototypeIdSerializer<CustomStackSplitPrototype>))]
[ViewVariables(VVAccess.ReadWrite)]
public string? CustomSplit;
}

[Serializable, NetSerializable]
Expand Down
21 changes: 21 additions & 0 deletions Content.Shared/Stacks/StackCustomSplit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Robust.Shared.Serialization;

namespace Content.Shared.Stacks
{
[Serializable, NetSerializable]
public sealed class StackCustomSplitAmountMessage : BoundUserInterfaceMessage
{
public int Amount;

public StackCustomSplitAmountMessage(int amount)
{
Amount = amount;
}
}

[Serializable, NetSerializable]
public enum StackCustomSplitUiKey
{
Key,
}
}
7 changes: 7 additions & 0 deletions Resources/Locale/en-US/stack/stack-component.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ comp-stack-becomes-full = Stack is now full.
# Text related to splitting a stack
comp-stack-split = You split the stack.
comp-stack-split-halve = Halve
comp-stack-split-custom = Split amount...
comp-stack-split-too-small = Stack is too small to split.

comp-stack-split-size = Max: {$size}

ui-custom-stack-split-title = Split Amount
ui-custom-stack-split-line-edit-placeholder = Amount
ui-custom-stack-split-apply = Split
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,4 @@
- type: DamageOtherOnHit
damage:
types:
Blunt: 0 # so the damage stats icon doesn't immediately give away the syndie ones
Blunt: 0 # so the damage stats icon doesn't immediately give away the syndie ones
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Objects/Misc/space_cash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- cash_500
- cash_1000
- cash_1000000
customSplit: Default
- type: Sprite
sprite: Objects/Economy/cash.rsi
state: cash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
- Smokable
- type: Item
size: Tiny

- type: entity
name: rainbow cannabis leaves
parent: LeavesCannabis
Expand Down Expand Up @@ -250,4 +250,4 @@
tags:
- Smokable
- type: Item
size: Tiny
size: Tiny
4 changes: 4 additions & 0 deletions Resources/Prototypes/custom_stack_split.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- type: customStackSplit
id: Default
interface:
type: StackCustomSplitBoundUserInterface
Loading