Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Наконец то рабочий бумбокс. #320

Merged
merged 4 commits into from
Jun 17, 2024
Merged
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
8 changes: 7 additions & 1 deletion Content.Client/BoomBox/UI/BoomBoxBoundUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected override void Open()
_window.PlusVolButtonPressed += OnPlusVolButtonPressed;
_window.StartButtonPressed += OnStartButtonPressed;
_window.StopButtonPressed += OnStopButtonPressed;
_window.PlaybackSliderChanged += OnPlaybackSliderChanged;
}

private void OnPlaybackSliderChanged(float newPosition)
{
SendMessage(new BoomBoxSetTimeMessage(newPosition)); // Отправка сообщения при изменении ползунка
}

private void OnMinusVolButtonPressed()
Expand Down Expand Up @@ -64,4 +70,4 @@ protected override void Dispose(bool disposing)
if (disposing)
_window?.Dispose();
}
}
}
7 changes: 4 additions & 3 deletions Content.Client/BoomBox/UI/BoomBoxWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'boombox-ui-window'}"
MinSize="256 80"
SetSize="256 80">
MinSize="280 130"
SetSize="280 130">
<BoxContainer Orientation="Vertical" VerticalExpand="True">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Button Name="MinusVolButton"
Expand All @@ -21,5 +21,6 @@
HorizontalExpand="True"
Disabled="True" />
</BoxContainer>
<Slider Name="PlaybackSlider" HorizontalExpand="True"/>
</BoxContainer>
</DefaultWindow>
</DefaultWindow>
9 changes: 8 additions & 1 deletion Content.Client/BoomBox/UI/BoomBoxWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public sealed partial class BoomBoxWindow : DefaultWindow
public event Action? PlusVolButtonPressed;
public event Action? StartButtonPressed;
public event Action? StopButtonPressed;
public event Action<float>? PlaybackSliderChanged;

private bool _isSliderDragging = false;

public BoomBoxWindow()
{
Expand All @@ -22,6 +25,7 @@ public BoomBoxWindow()
PlusVolButton.OnPressed += _ => PlusVolButtonPressed?.Invoke();
StartButton.OnPressed += _ => StartButtonPressed?.Invoke();
StopButton.OnPressed += _ => StopButtonPressed?.Invoke();
PlaybackSlider.OnValueChanged += args => PlaybackSliderChanged?.Invoke(args.Value);
}

public void UpdateState(BoomBoxUiState state)
Expand All @@ -30,5 +34,8 @@ public void UpdateState(BoomBoxUiState state)
PlusVolButton.Disabled = !state.CanPlusVol;
StartButton.Disabled = !state.CanStart;
StopButton.Disabled = !state.CanStop;

PlaybackSlider.MaxValue = state.SoundDuration;
PlaybackSlider.SetValueWithoutEvent(state.PlaybackPosition);
}
}
}
4 changes: 3 additions & 1 deletion Content.Server/BoomBox/BoomBoxComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public sealed partial class BoomBoxComponent : Component
[DataField("slots")]
public Dictionary<string, ItemSlot> Slots = new();

}
public float PlaybackPosition = 0f;
public float SoundDuration = 0f;
}
48 changes: 38 additions & 10 deletions Content.Server/BoomBox/BoomBoxSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,44 @@ public override void Initialize()
SubscribeLocalEvent<BoomBoxComponent, BoomBoxMinusVolMessage>(OnMinusVolButtonPressed);
SubscribeLocalEvent<BoomBoxComponent, BoomBoxStartMessage>(OnStartButtonPressed);
SubscribeLocalEvent<BoomBoxComponent, BoomBoxStopMessage>(OnStopButtonPressed);
SubscribeLocalEvent<BoomBoxComponent, BoomBoxSetTimeMessage>(OnSetTimeMessage);
}

public override void Update(float frameTime)
{
base.Update(frameTime);

var query = EntityQueryEnumerator<BoomBoxComponent>();
while (query.MoveNext(out var uid, out var component))
{
if (component.Stream != null && _audioSystem.IsPlaying(component.Stream))
{
component.PlaybackPosition += frameTime;
UpdateUserInterface(uid, component);

if (component.PlaybackPosition >= component.SoundDuration)
{
StopPlay(uid, component);
component.PlaybackPosition = 0f;
}
}
}
}

private void OnSetTimeMessage(EntityUid uid, BoomBoxComponent component, BoomBoxSetTimeMessage args)
{
if (component.Stream != null)
{
_audioSystem.SetPlaybackPosition(component.Stream, args.PlaybackPosition);
component.PlaybackPosition = args.PlaybackPosition;
}

UpdateUserInterface(uid, component);
}

// This method makes it possible to insert cassettes into the boombox
private void OnComponentInit(EntityUid uid, BoomBoxComponent component, ComponentInit args)
{

foreach (var slot in component.Slots)
{
_itemSlotsSystem.AddItemSlot(uid, slot.Key, slot.Value);
Expand Down Expand Up @@ -91,6 +122,8 @@ private void OnItemRemoved(EntityUid uid, BoomBoxComponent comp, EntRemovedFromC
// We change the value of this field to prevent the boombox from being turned on without a cassette.
comp.Inserted = false;
comp.Enabled = false;
comp.PlaybackPosition = 0f;
UpdateUserInterface(uid, comp);
}

// This method is an intermediate step where we embed additional checks.
Expand All @@ -106,14 +139,11 @@ private void UpdateSoundPath(EntityUid uid, BoomBoxComponent comp)
// This method updates the path to the music being played. That is why the initial value of the field is not particularly important
private void AddCurrentSoundPath(EntityUid uid, BoomBoxComponent comp, EntityUid added)
{

var tagComp = EnsureComp<TagComponent>(uid);

if (!TryComp<BoomBoxTapeComponent>(added, out var BoomBoxTapeComp) || BoomBoxTapeComp.SoundPath is null)
return;


comp.SoundPath = BoomBoxTapeComp.SoundPath;
comp.SoundDuration = (float) _audioSystem.GetAudioLength(comp.SoundPath).TotalSeconds;
}

private void OnMinusVolButtonPressed(EntityUid uid, BoomBoxComponent component, BoomBoxMinusVolMessage args)
Expand Down Expand Up @@ -141,8 +171,6 @@ private void OnToggleInterface(EntityUid uid, BoomBoxComponent component, AfterA
UpdateUserInterface(uid, component);
}

// ----------------------------------------------------------------------------------------------------------------

private void UpdateUserInterface(EntityUid uid, BoomBoxComponent? component = null)
{
if (!Resolve(uid, ref component))
Expand Down Expand Up @@ -178,8 +206,7 @@ private void UpdateUserInterface(EntityUid uid, BoomBoxComponent? component = nu
canStop = false;
}


var state = new BoomBoxUiState(canPlusVol, canMinusVol, canStop, canStart);
var state = new BoomBoxUiState(canPlusVol, canMinusVol, canStop, canStart, component.PlaybackPosition, component.SoundDuration);
_userInterface.SetUiState(uid, BoomBoxUiKey.Key, state);
}

Expand Down Expand Up @@ -222,6 +249,7 @@ private void StartPlay(EntityUid uid, BoomBoxComponent? component = null)

// We play music with these parameters. Be sure to set "WithLoop(true)" this will allow the music to play indefinitely.
component.Stream = _audioSystem.PlayPvs(component.SoundPath, uid, AudioParams.Default.WithVolume(component.Volume).WithLoop(true).WithMaxDistance(7f))?.Entity;
component.PlaybackPosition = 0f;
}

_signalSystem.InvokePort(uid, component.Port);
Expand All @@ -243,8 +271,8 @@ private void StopPlay(EntityUid uid, BoomBoxComponent? component = null)

// Turning off the looped audio stream
component.Stream = _audioSystem.Stop(component.Stream);
component.PlaybackPosition = 0f;
}

_signalSystem.InvokePort(uid, component.Port);

UpdateUserInterface(uid, component);
Expand Down
21 changes: 19 additions & 2 deletions Content.Shared/BoomBox/SharedBoomBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@ public sealed class BoomBoxUiState : BoundUserInterfaceState
public bool CanMinusVol { get; }
public bool CanStop { get; }
public bool CanStart { get; }
public float PlaybackPosition { get; }
public float SoundDuration { get; }

public BoomBoxUiState(
bool canPlusVol,
bool canMinusVol,
bool canStop,
bool canStart)
bool canStart,
float playbackPosition,
float soundDuration)
{
CanPlusVol = canPlusVol;
CanMinusVol = canMinusVol;
CanStop = canStop;
CanStart = canStart;
PlaybackPosition = playbackPosition;
SoundDuration = soundDuration;
}
}

[Serializable, NetSerializable]
public sealed class BoomBoxSetTimeMessage : BoundUserInterfaceMessage
{
public float PlaybackPosition { get; }

public BoomBoxSetTimeMessage(float playbackPosition)
{
PlaybackPosition = playbackPosition;
}
}

Expand All @@ -47,4 +64,4 @@ public sealed class BoomBoxStartMessage : BoundUserInterfaceMessage
[Serializable, NetSerializable]
public sealed class BoomBoxStopMessage : BoundUserInterfaceMessage
{
}
}
Loading