2020-11-25 18:00:18 +01:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Interactable;
|
2022-02-17 15:40:03 +13:00
|
|
|
using Content.Shared.Interaction;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Client.Audio.Midi;
|
2021-02-01 16:49:32 +01:00
|
|
|
using Robust.Client.AutoGenerated;
|
2020-06-25 15:27:22 +02:00
|
|
|
using Robust.Client.Player;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Client.UserInterface;
|
|
|
|
|
using Robust.Client.UserInterface.CustomControls;
|
2021-02-01 16:49:32 +01:00
|
|
|
using Robust.Client.UserInterface.XAML;
|
2020-06-25 15:27:22 +02:00
|
|
|
using Robust.Shared.Containers;
|
2021-11-28 01:47:36 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-02-04 13:37:26 +01:00
|
|
|
using Robust.Shared.Input;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Shared.IoC;
|
2021-02-04 13:37:26 +01:00
|
|
|
using Robust.Shared.Timing;
|
2021-03-10 14:48:29 +01:00
|
|
|
using static Robust.Client.UserInterface.Controls.BaseButton;
|
2021-02-04 13:37:26 +01:00
|
|
|
using Range = Robust.Client.UserInterface.Controls.Range;
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Instruments.UI
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2021-02-01 16:49:32 +01:00
|
|
|
[GenerateTypedNameReferences]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed partial class InstrumentMenu : DefaultWindow
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IMidiManager _midiManager = default!;
|
|
|
|
|
[Dependency] private readonly IFileDialogManager _fileDialogManager = default!;
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2020-11-21 14:02:00 +01:00
|
|
|
private readonly InstrumentBoundUserInterface _owner;
|
2019-11-25 00:11:47 +01:00
|
|
|
|
|
|
|
|
public InstrumentMenu(InstrumentBoundUserInterface owner)
|
|
|
|
|
{
|
2021-02-01 16:49:32 +01:00
|
|
|
RobustXamlLoader.Load(this);
|
2019-11-25 00:11:47 +01:00
|
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
|
|
|
|
|
|
_owner = owner;
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (_owner.Instrument != null)
|
|
|
|
|
{
|
|
|
|
|
_owner.Instrument.OnMidiPlaybackEnded += InstrumentOnMidiPlaybackEnded;
|
2021-12-03 15:53:09 +01:00
|
|
|
Title = IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(_owner.Instrument.Owner).EntityName;
|
2021-03-10 14:48:29 +01:00
|
|
|
LoopButton.Disabled = !_owner.Instrument.IsMidiOpen;
|
|
|
|
|
LoopButton.Pressed = _owner.Instrument.LoopMidi;
|
|
|
|
|
StopButton.Disabled = !_owner.Instrument.IsMidiOpen;
|
|
|
|
|
PlaybackSlider.MouseFilter = _owner.Instrument.IsMidiOpen ? MouseFilterMode.Pass : MouseFilterMode.Ignore;
|
|
|
|
|
}
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2020-03-30 18:36:58 +02:00
|
|
|
if (!_midiManager.IsAvailable)
|
|
|
|
|
{
|
2021-02-21 12:38:56 +01:00
|
|
|
UnavailableOverlay.Visible = true;
|
2021-02-01 16:49:32 +01:00
|
|
|
// We return early as to not give the buttons behavior.
|
|
|
|
|
return;
|
2020-03-30 18:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-01 16:49:32 +01:00
|
|
|
InputButton.OnToggled += MidiInputButtonOnOnToggled;
|
|
|
|
|
FileButton.OnPressed += MidiFileButtonOnOnPressed;
|
|
|
|
|
LoopButton.OnToggled += MidiLoopButtonOnOnToggled;
|
|
|
|
|
StopButton.OnPressed += MidiStopButtonOnPressed;
|
2021-02-04 13:37:26 +01:00
|
|
|
PlaybackSlider.OnValueChanged += PlaybackSliderSeek;
|
|
|
|
|
PlaybackSlider.OnKeyBindUp += PlaybackSliderKeyUp;
|
2021-02-21 12:38:56 +01:00
|
|
|
|
|
|
|
|
MinSize = SetSize = (400, 150);
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstrumentOnMidiPlaybackEnded()
|
|
|
|
|
{
|
|
|
|
|
MidiPlaybackSetButtonsDisabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MidiPlaybackSetButtonsDisabled(bool disabled)
|
|
|
|
|
{
|
2021-02-01 16:49:32 +01:00
|
|
|
LoopButton.Disabled = disabled;
|
|
|
|
|
StopButton.Disabled = disabled;
|
2021-02-04 13:37:26 +01:00
|
|
|
|
|
|
|
|
// Whether to allow the slider to receive events..
|
|
|
|
|
PlaybackSlider.MouseFilter = !disabled ? MouseFilterMode.Pass : MouseFilterMode.Ignore;
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private async void MidiFileButtonOnOnPressed(ButtonEventArgs obj)
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2020-03-30 18:36:58 +02:00
|
|
|
var filters = new FileDialogFilters(new FileDialogFilters.Group("mid", "midi"));
|
2021-02-04 13:37:26 +01:00
|
|
|
await using var file = await _fileDialogManager.OpenFile(filters);
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2021-12-02 23:51:30 +13:00
|
|
|
// did the instrument menu get closed while waiting for the user to select a file?
|
|
|
|
|
if (Disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-25 15:27:22 +02:00
|
|
|
// The following checks are only in place to prevent players from playing MIDI songs locally.
|
|
|
|
|
// There are equivalents for these checks on the server.
|
|
|
|
|
|
2020-11-25 18:00:18 +01:00
|
|
|
if (file == null) return;
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2020-11-25 18:00:18 +01:00
|
|
|
/*if (!_midiManager.IsMidiFile(filename))
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
|
|
|
|
Logger.Warning($"Not a midi file! Chosen file: {filename}");
|
|
|
|
|
return;
|
2020-11-25 18:00:18 +01:00
|
|
|
}*/
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2020-10-29 12:22:56 +01:00
|
|
|
if (!PlayCheck())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-05-29 16:42:37 +02:00
|
|
|
MidiStopButtonOnPressed(null);
|
2021-02-04 13:37:26 +01:00
|
|
|
await using var memStream = new MemoryStream((int) file.Length);
|
2020-11-25 18:00:18 +01:00
|
|
|
// 100ms delay is due to a race condition or something idk.
|
|
|
|
|
// While we're waiting, load it into memory.
|
|
|
|
|
await Task.WhenAll(Timer.Delay(100), file.CopyToAsync(memStream));
|
|
|
|
|
|
2021-11-28 01:47:36 +01:00
|
|
|
if (_owner.Instrument is not {} instrument
|
2021-12-07 22:22:34 +11:00
|
|
|
|| !EntitySystem.Get<InstrumentSystem>().OpenMidi(instrument.Owner, memStream.GetBuffer().AsSpan(0, (int) memStream.Length), instrument))
|
2020-11-25 18:00:18 +01:00
|
|
|
return;
|
|
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
MidiPlaybackSetButtonsDisabled(false);
|
2021-02-01 16:49:32 +01:00
|
|
|
if (InputButton.Pressed)
|
|
|
|
|
InputButton.Pressed = false;
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void MidiInputButtonOnOnToggled(ButtonToggledEventArgs obj)
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2021-11-28 01:47:36 +01:00
|
|
|
var instrumentSystem = EntitySystem.Get<InstrumentSystem>();
|
|
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
if (obj.Pressed)
|
|
|
|
|
{
|
2020-10-29 12:22:56 +01:00
|
|
|
if (!PlayCheck())
|
|
|
|
|
return;
|
|
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
MidiStopButtonOnPressed(null);
|
2021-11-28 01:47:36 +01:00
|
|
|
if(_owner.Instrument is {} instrument)
|
2021-12-07 22:22:34 +11:00
|
|
|
instrumentSystem.OpenInput(instrument.Owner, instrument);
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
2021-11-28 01:47:36 +01:00
|
|
|
else if(_owner.Instrument is {} instrument)
|
2021-12-07 22:22:34 +11:00
|
|
|
instrumentSystem.CloseInput(instrument.Owner, false, instrument);
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-29 12:22:56 +01:00
|
|
|
private bool PlayCheck()
|
|
|
|
|
{
|
2021-03-13 02:46:32 +01:00
|
|
|
var instrumentEnt = _owner.Instrument?.Owner;
|
2020-10-29 12:22:56 +01:00
|
|
|
var instrument = _owner.Instrument;
|
|
|
|
|
|
2021-03-13 02:46:32 +01:00
|
|
|
// If either the entity or component are null, return.
|
2021-12-06 14:00:39 +01:00
|
|
|
if (instrumentEnt == null || instrument == null)
|
2021-03-10 14:48:29 +01:00
|
|
|
return false;
|
2021-03-13 02:46:32 +01:00
|
|
|
|
|
|
|
|
// If we're a handheld instrument, we might be in a container. Get it just in case.
|
2021-12-05 18:09:01 +01:00
|
|
|
instrumentEnt.Value.TryGetContainerMan(out var conMan);
|
2020-10-29 12:22:56 +01:00
|
|
|
|
|
|
|
|
var localPlayer = IoCManager.Resolve<IPlayerManager>().LocalPlayer;
|
|
|
|
|
|
|
|
|
|
// If we don't have a player or controlled entity, we return.
|
2020-11-25 18:00:18 +01:00
|
|
|
if (localPlayer?.ControlledEntity == null) return false;
|
2020-10-29 12:22:56 +01:00
|
|
|
|
|
|
|
|
// If the instrument is handheld and we're not holding it, we return.
|
2021-03-13 02:46:32 +01:00
|
|
|
if ((instrument.Handheld && (conMan == null
|
|
|
|
|
|| conMan.Owner != localPlayer.ControlledEntity))) return false;
|
2020-10-29 12:22:56 +01:00
|
|
|
|
|
|
|
|
// We check that we're in range unobstructed just in case.
|
2022-02-17 15:40:03 +13:00
|
|
|
return EntitySystem.Get<SharedInteractionSystem>().InRangeUnobstructed(localPlayer.ControlledEntity.Value, instrumentEnt.Value);
|
2020-10-29 12:22:56 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void MidiStopButtonOnPressed(ButtonEventArgs? obj)
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
|
|
|
|
MidiPlaybackSetButtonsDisabled(true);
|
2021-11-28 01:47:36 +01:00
|
|
|
|
|
|
|
|
if (_owner.Instrument is not { } instrument)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-12-07 22:22:34 +11:00
|
|
|
EntitySystem.Get<InstrumentSystem>().CloseMidi(instrument.Owner, false, instrument);
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void MidiLoopButtonOnOnToggled(ButtonToggledEventArgs obj)
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2021-11-28 01:47:36 +01:00
|
|
|
if (_owner.Instrument == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_owner.Instrument.LoopMidi = obj.Pressed;
|
|
|
|
|
_owner.Instrument.DirtyRenderer = true;
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
2021-02-04 13:37:26 +01:00
|
|
|
|
|
|
|
|
private void PlaybackSliderSeek(Range _)
|
|
|
|
|
{
|
|
|
|
|
// Do not seek while still grabbing.
|
2021-11-28 01:47:36 +01:00
|
|
|
if (PlaybackSlider.Grabbed || _owner.Instrument is not {} instrument) return;
|
2021-02-04 13:37:26 +01:00
|
|
|
|
2021-12-07 22:22:34 +11:00
|
|
|
EntitySystem.Get<InstrumentSystem>().SetPlayerTick(instrument.Owner, (int)Math.Ceiling(PlaybackSlider.Value), instrument);
|
2021-02-04 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaybackSliderKeyUp(GUIBoundKeyEventArgs args)
|
|
|
|
|
{
|
2021-11-28 01:47:36 +01:00
|
|
|
if (args.Function != EngineKeyFunctions.UIClick || _owner.Instrument is not {} instrument) return;
|
2021-03-10 14:48:29 +01:00
|
|
|
|
2021-12-07 22:22:34 +11:00
|
|
|
EntitySystem.Get<InstrumentSystem>().SetPlayerTick(instrument.Owner, (int)Math.Ceiling(PlaybackSlider.Value), instrument);
|
2021-02-04 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-26 17:10:31 -07:00
|
|
|
protected override void FrameUpdate(FrameEventArgs args)
|
2021-02-04 13:37:26 +01:00
|
|
|
{
|
2021-03-26 17:10:31 -07:00
|
|
|
base.FrameUpdate(args);
|
2021-02-04 13:37:26 +01:00
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (_owner.Instrument == null) return;
|
|
|
|
|
|
2021-02-04 13:37:26 +01:00
|
|
|
if (!_owner.Instrument.IsMidiOpen)
|
|
|
|
|
{
|
|
|
|
|
PlaybackSlider.MaxValue = 1;
|
|
|
|
|
PlaybackSlider.SetValueWithoutEvent(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PlaybackSlider.Grabbed) return;
|
|
|
|
|
|
|
|
|
|
PlaybackSlider.MaxValue = _owner.Instrument.PlayerTotalTick;
|
|
|
|
|
PlaybackSlider.SetValueWithoutEvent(_owner.Instrument.PlayerTick);
|
|
|
|
|
}
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
}
|