Revert "Remove some BUI boilerplate" (#30214)

Revert "Remove some BUI boilerplate (#28399)"

This reverts commit cbf329a82d.
This commit is contained in:
Nemanja
2024-07-20 20:42:27 -04:00
committed by GitHub
parent 6d664c9157
commit cb0ba66be3
137 changed files with 1755 additions and 1096 deletions

View File

@@ -11,9 +11,7 @@ public sealed partial class BandMenu : DefaultWindow
{
private readonly InstrumentBoundUserInterface _owner;
public EntityUid? Master;
public BandMenu(InstrumentBoundUserInterface owner)
public BandMenu(InstrumentBoundUserInterface owner) : base()
{
RobustXamlLoader.Load(this);
@@ -42,7 +40,7 @@ public sealed partial class BandMenu : DefaultWindow
{
var uid = entManager.GetEntity(nent);
var item = BandList.AddItem(name, null, true, uid);
item.Selected = Master == uid;
item.Selected = _owner.Instrument?.Master == uid;
}
}
}

View File

@@ -51,7 +51,7 @@ public sealed partial class ChannelsMenu : DefaultWindow
}
}
public void Populate(InstrumentComponent? instrument)
public void Populate()
{
ChannelList.Clear();
@@ -60,8 +60,7 @@ public sealed partial class ChannelsMenu : DefaultWindow
var item = ChannelList.AddItem(_owner.Loc.GetString("instrument-component-channel-name",
("number", i)), null, true, i);
item.Selected = !instrument?.FilteredChannels[i] ?? false;
item.Selected = !_owner.Instrument?.FilteredChannels[i] ?? false;
}
}
}

View File

@@ -24,6 +24,8 @@ namespace Content.Client.Instruments.UI
[ViewVariables] private BandMenu? _bandMenu;
[ViewVariables] private ChannelsMenu? _channelsMenu;
[ViewVariables] public InstrumentComponent? Instrument { get; private set; }
public InstrumentBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
IoCManager.InjectDependencies(this);
@@ -41,20 +43,14 @@ namespace Content.Client.Instruments.UI
protected override void Open()
{
_instrumentMenu = this.CreateWindow<InstrumentMenu>();
_instrumentMenu.Title = EntMan.GetComponent<MetaDataComponent>(Owner).EntityName;
if (!EntMan.TryGetComponent(Owner, out InstrumentComponent? instrument))
return;
_instrumentMenu.OnOpenBand += OpenBandMenu;
_instrumentMenu.OnOpenChannels += OpenChannelsMenu;
_instrumentMenu.OnCloseChannels += CloseChannelsMenu;
_instrumentMenu.OnCloseBands += CloseBandMenu;
Instrument = instrument;
_instrumentMenu = new InstrumentMenu(this);
_instrumentMenu.OnClose += Close;
_instrumentMenu.SetMIDI(MidiManager.IsAvailable);
if (EntMan.TryGetComponent(Owner, out InstrumentComponent? instrument))
{
_instrumentMenu.SetInstrument((Owner, instrument));
}
_instrumentMenu.OpenCentered();
}
protected override void Dispose(bool disposing)
@@ -62,12 +58,7 @@ namespace Content.Client.Instruments.UI
base.Dispose(disposing);
if (!disposing)
return;
if (EntMan.TryGetComponent(Owner, out InstrumentComponent? instrument))
{
_instrumentMenu?.RemoveInstrument(instrument);
}
_instrumentMenu?.Dispose();
_bandMenu?.Dispose();
_channelsMenu?.Dispose();
}
@@ -81,11 +72,6 @@ namespace Content.Client.Instruments.UI
{
_bandMenu ??= new BandMenu(this);
if (EntMan.TryGetComponent(Owner, out InstrumentComponent? instrument))
{
_bandMenu.Master = instrument.Master;
}
// Refresh cache...
RefreshBands();
@@ -101,9 +87,7 @@ namespace Content.Client.Instruments.UI
public void OpenChannelsMenu()
{
_channelsMenu ??= new ChannelsMenu(this);
EntMan.TryGetComponent(Owner, out InstrumentComponent? instrument);
_channelsMenu.Populate(instrument);
_channelsMenu.Populate();
_channelsMenu.OpenCenteredRight();
}

View File

@@ -1,10 +1,7 @@
using System.IO;
using System.Numerics;
using System.Threading.Tasks;
using Content.Client.Interactable;
using Content.Shared.ActionBlocker;
using Robust.Client.AutoGenerated;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
@@ -19,23 +16,33 @@ namespace Content.Client.Instruments.UI
[GenerateTypedNameReferences]
public sealed partial class InstrumentMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly IFileDialogManager _dialogs = default!;
[Dependency] private readonly IPlayerManager _player = default!;
private readonly InstrumentBoundUserInterface _owner;
private bool _isMidiFileDialogueWindowOpen;
public event Action? OnOpenBand;
public event Action? OnOpenChannels;
public event Action? OnCloseBands;
public event Action? OnCloseChannels;
public EntityUid Entity;
public InstrumentMenu()
public InstrumentMenu(InstrumentBoundUserInterface owner)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_owner = owner;
if (_owner.Instrument != null)
{
_owner.Instrument.OnMidiPlaybackEnded += InstrumentOnMidiPlaybackEnded;
Title = _owner.Entities.GetComponent<MetaDataComponent>(_owner.Owner).EntityName;
LoopButton.Disabled = !_owner.Instrument.IsMidiOpen;
LoopButton.Pressed = _owner.Instrument.LoopMidi;
ChannelsButton.Disabled = !_owner.Instrument.IsRendererAlive;
StopButton.Disabled = !_owner.Instrument.IsMidiOpen;
PlaybackSlider.MouseFilter = _owner.Instrument.IsMidiOpen ? MouseFilterMode.Pass : MouseFilterMode.Ignore;
}
if (!_owner.MidiManager.IsAvailable)
{
UnavailableOverlay.Visible = true;
// We return early as to not give the buttons behavior.
return;
}
InputButton.OnToggled += MidiInputButtonOnOnToggled;
BandButton.OnPressed += BandButtonOnPressed;
@@ -50,34 +57,12 @@ namespace Content.Client.Instruments.UI
MinSize = SetSize = new Vector2(400, 150);
}
public void SetInstrument(Entity<InstrumentComponent> entity)
{
Entity = entity;
var component = entity.Comp;
component.OnMidiPlaybackEnded += InstrumentOnMidiPlaybackEnded;
LoopButton.Disabled = !component.IsMidiOpen;
LoopButton.Pressed = component.LoopMidi;
ChannelsButton.Disabled = !component.IsRendererAlive;
StopButton.Disabled = !component.IsMidiOpen;
PlaybackSlider.MouseFilter = component.IsMidiOpen ? MouseFilterMode.Pass : MouseFilterMode.Ignore;
}
public void RemoveInstrument(InstrumentComponent component)
{
component.OnMidiPlaybackEnded -= InstrumentOnMidiPlaybackEnded;
}
public void SetMIDI(bool available)
{
UnavailableOverlay.Visible = !available;
}
private void BandButtonOnPressed(ButtonEventArgs obj)
{
if (!PlayCheck())
return;
OnOpenBand?.Invoke();
_owner.OpenBandMenu();
}
private void BandButtonOnToggled(ButtonToggledEventArgs obj)
@@ -85,15 +70,12 @@ namespace Content.Client.Instruments.UI
if (obj.Pressed)
return;
if (_entManager.TryGetComponent(Entity, out InstrumentComponent? instrument))
{
_entManager.System<InstrumentSystem>().SetMaster(Entity, instrument.Master);
}
_owner.Instruments.SetMaster(_owner.Owner, null);
}
private void ChannelsButtonOnPressed(ButtonEventArgs obj)
{
OnOpenChannels?.Invoke();
_owner.OpenChannelsMenu();
}
private void InstrumentOnMidiPlaybackEnded()
@@ -103,10 +85,8 @@ namespace Content.Client.Instruments.UI
public void MidiPlaybackSetButtonsDisabled(bool disabled)
{
if (disabled)
{
OnCloseChannels?.Invoke();
}
if(disabled)
_owner.CloseChannelsMenu();
LoopButton.Disabled = disabled;
StopButton.Disabled = disabled;
@@ -120,7 +100,7 @@ namespace Content.Client.Instruments.UI
if (_isMidiFileDialogueWindowOpen)
return;
OnCloseBands?.Invoke();
_owner.CloseBandMenu();
var filters = new FileDialogFilters(new FileDialogFilters.Group("mid", "midi"));
@@ -128,7 +108,7 @@ namespace Content.Client.Instruments.UI
// or focus the previously-opened window.
_isMidiFileDialogueWindowOpen = true;
await using var file = await _dialogs.OpenFile(filters);
await using var file = await _owner.FileDialogManager.OpenFile(filters);
_isMidiFileDialogueWindowOpen = false;
@@ -149,18 +129,9 @@ namespace Content.Client.Instruments.UI
await file.CopyToAsync(memStream);
if (!_entManager.TryGetComponent<InstrumentComponent>(Entity, out var instrument))
{
if (_owner.Instrument is not {} instrument
|| !_owner.Instruments.OpenMidi(_owner.Owner, memStream.GetBuffer().AsSpan(0, (int) memStream.Length), instrument))
return;
}
if (!_entManager.System<InstrumentSystem>()
.OpenMidi(Entity,
memStream.GetBuffer().AsSpan(0, (int) memStream.Length),
instrument))
{
return;
}
MidiPlaybackSetButtonsDisabled(false);
if (InputButton.Pressed)
@@ -169,7 +140,7 @@ namespace Content.Client.Instruments.UI
private void MidiInputButtonOnOnToggled(ButtonToggledEventArgs obj)
{
OnCloseBands?.Invoke();
_owner.CloseBandMenu();
if (obj.Pressed)
{
@@ -177,99 +148,109 @@ namespace Content.Client.Instruments.UI
return;
MidiStopButtonOnPressed(null);
if (_entManager.TryGetComponent(Entity, out InstrumentComponent? instrument))
_entManager.System<InstrumentSystem>().OpenInput(Entity, instrument);
if(_owner.Instrument is {} instrument)
_owner.Instruments.OpenInput(_owner.Owner, instrument);
}
else
else if (_owner.Instrument is { } instrument)
{
_entManager.System<InstrumentSystem>().CloseInput(Entity, false);
OnCloseChannels?.Invoke();
_owner.Instruments.CloseInput(_owner.Owner, false, instrument);
_owner.CloseChannelsMenu();
}
}
private bool PlayCheck()
{
// TODO all of these checks should also be done server-side.
if (!_entManager.TryGetComponent(Entity, out InstrumentComponent? instrument))
var instrumentEnt = _owner.Owner;
var instrument = _owner.Instrument;
if (instrument == null)
return false;
var localEntity = _player.LocalEntity;
var localEntity = _owner.PlayerManager.LocalEntity;
// If we don't have a player or controlled entity, we return.
if (localEntity == null)
return false;
// By default, allow an instrument to play itself and skip all other checks
if (localEntity == Entity)
if (localEntity == instrumentEnt)
return true;
var container = _entManager.System<SharedContainerSystem>();
var container = _owner.Entities.System<SharedContainerSystem>();
// If we're a handheld instrument, we might be in a container. Get it just in case.
container.TryGetContainingContainer(Entity, out var conMan);
container.TryGetContainingContainer(instrumentEnt, out var conMan);
// If the instrument is handheld and we're not holding it, we return.
if (instrument.Handheld && (conMan == null || conMan.Owner != localEntity))
if ((instrument.Handheld && (conMan == null || conMan.Owner != localEntity)))
return false;
if (!_entManager.System<ActionBlockerSystem>().CanInteract(localEntity.Value, Entity))
if (!_owner.ActionBlocker.CanInteract(localEntity.Value, instrumentEnt))
return false;
// We check that we're in range unobstructed just in case.
return _entManager.System<InteractionSystem>().InRangeUnobstructed(localEntity.Value, Entity);
return _owner.Interactions.InRangeUnobstructed(localEntity.Value, instrumentEnt);
}
private void MidiStopButtonOnPressed(ButtonEventArgs? obj)
{
MidiPlaybackSetButtonsDisabled(true);
_entManager.System<InstrumentSystem>().CloseMidi(Entity, false);
OnCloseChannels?.Invoke();
if (_owner.Instrument is not {} instrument)
return;
_owner.Instruments.CloseMidi(_owner.Owner, false, instrument);
_owner.CloseChannelsMenu();
}
private void MidiLoopButtonOnOnToggled(ButtonToggledEventArgs obj)
{
var instrument = _entManager.System<InstrumentSystem>();
if (_owner.Instrument == null)
return;
if (_entManager.TryGetComponent(Entity, out InstrumentComponent? instrumentComp))
{
instrumentComp.LoopMidi = obj.Pressed;
}
instrument.UpdateRenderer(Entity);
_owner.Instrument.LoopMidi = obj.Pressed;
_owner.Instruments.UpdateRenderer(_owner.Owner, _owner.Instrument);
}
private void PlaybackSliderSeek(Range _)
{
// Do not seek while still grabbing.
if (PlaybackSlider.Grabbed)
if (PlaybackSlider.Grabbed || _owner.Instrument is not {} instrument)
return;
_entManager.System<InstrumentSystem>().SetPlayerTick(Entity, (int)Math.Ceiling(PlaybackSlider.Value));
_owner.Instruments.SetPlayerTick(_owner.Owner, (int)Math.Ceiling(PlaybackSlider.Value), instrument);
}
private void PlaybackSliderKeyUp(GUIBoundKeyEventArgs args)
{
if (args.Function != EngineKeyFunctions.UIClick)
if (args.Function != EngineKeyFunctions.UIClick || _owner.Instrument is not {} instrument)
return;
_entManager.System<InstrumentSystem>().SetPlayerTick(Entity, (int)Math.Ceiling(PlaybackSlider.Value));
_owner.Instruments.SetPlayerTick(_owner.Owner, (int)Math.Ceiling(PlaybackSlider.Value), instrument);
}
public override void Close()
{
base.Close();
_owner.CloseBandMenu();
_owner.CloseChannelsMenu();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (!_entManager.TryGetComponent(Entity, out InstrumentComponent? instrument))
if (_owner.Instrument == null)
return;
var hasMaster = instrument.Master != null;
var hasMaster = _owner.Instrument.Master != null;
BandButton.ToggleMode = hasMaster;
BandButton.Pressed = hasMaster;
BandButton.Disabled = instrument.IsMidiOpen || instrument.IsInputOpen;
ChannelsButton.Disabled = !instrument.IsRendererAlive;
BandButton.Disabled = _owner.Instrument.IsMidiOpen || _owner.Instrument.IsInputOpen;
ChannelsButton.Disabled = !_owner.Instrument.IsRendererAlive;
if (!instrument.IsMidiOpen)
if (!_owner.Instrument.IsMidiOpen)
{
PlaybackSlider.MaxValue = 1;
PlaybackSlider.SetValueWithoutEvent(0);
@@ -279,8 +260,8 @@ namespace Content.Client.Instruments.UI
if (PlaybackSlider.Grabbed)
return;
PlaybackSlider.MaxValue = instrument.PlayerTotalTick;
PlaybackSlider.SetValueWithoutEvent(instrument.PlayerTick);
PlaybackSlider.MaxValue = _owner.Instrument.PlayerTotalTick;
PlaybackSlider.SetValueWithoutEvent(_owner.Instrument.PlayerTick);
}
}
}