Skip to content

Commit

Permalink
Ползунок громкости для магнитофонов (булка наныл) (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
VigersRay authored Jun 18, 2024
1 parent 06b0959 commit d55622d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 16 deletions.
12 changes: 10 additions & 2 deletions Content.Client/_Sunrise/TapePlayer/TapePlayerBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Content.Shared._Sunrise.TapePlayer;
using Robust.Client.Audio;
using Robust.Shared.Audio.Components;
using Robust.Shared.Audio.Systems;

namespace Content.Client._Sunrise.TapePlayer;

Expand Down Expand Up @@ -42,6 +43,7 @@ protected override void Open()
};

_menu.SetTime += SetTime;
_menu.SetVolume += SetVolume;
Reload();
}

Expand All @@ -54,6 +56,7 @@ public void Reload()
return;

_menu.SetAudioStream(tapePlayer.AudioStream);
_menu.SetVolumeSlider(tapePlayer.Volume * 100f);

if (_entityManager.TryGetComponent<MusicTapeComponent>(tapePlayer.InsertedTape, out var musicTapeComponent))
{
Expand All @@ -77,15 +80,20 @@ public void SetTime(float time)
// so it will go BRRRRT
// Using ping gets us close enough that it SHOULD, MOST OF THE TIME, fall within the 0.1 second tolerance
// that's still on engine so our playback position never gets corrected.
if (EntMan.TryGetComponent(Owner, out TapePlayerComponent? jukebox) &&
EntMan.TryGetComponent(jukebox.AudioStream, out AudioComponent? audioComp))
if (EntMan.TryGetComponent(Owner, out TapePlayerComponent? tapePlayer) &&
EntMan.TryGetComponent(tapePlayer.AudioStream, out AudioComponent? audioComp))
{
audioComp.PlaybackPosition = time;
}

SendMessage(new TapePlayerSetTimeMessage(sentTime));
}

public void SetVolume(float volume)
{
SendMessage(new TapePlayerSetVolumeMessage(volume));
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Expand Down
12 changes: 9 additions & 3 deletions Content.Client/_Sunrise/TapePlayer/TapePlayerMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
SetSize="400 150" Title="{Loc 'tape-player-menu-title'}" Resizable="False">
<BoxContainer Margin="4 0" Orientation="Vertical">
<BoxContainer Orientation="Vertical">
<Label Name="InsertedTape" Text="{Loc 'tape-player-inserted-tape'}" />
<Label Name="SongName" Text="---" />
<Label Name="InsertedTape" Text="{Loc 'tape-player-menu-inserted-tape'}" />
<BoxContainer Orientation="Horizontal">
<Label Name="SongName" Text="---" MaxWidth="200" ClipText="True"/>
<Label Name="DurationLabel" Text="00:00 / 00:00" HorizontalAlignment="Right" HorizontalExpand="True"/>
</BoxContainer>
<Slider Name="PlaybackSlider" HorizontalExpand="True" />
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True"
VerticalExpand="False" SizeFlagsStretchRatio="1">
<Button Name="PlayButton" Text="{Loc 'tape-player-menu-buttonplay'}" />
<Button Name="StopButton" Text="{Loc 'tape-player-menu-buttonstop'}" />
<Label Name="DurationLabel" Text="00:00 / 00:00" HorizontalAlignment="Right" HorizontalExpand="True"/>
<BoxContainer Orientation="Horizontal" HorizontalAlignment="Right" HorizontalExpand="True" Align="End">
<Label Name="VolumeName" Text="{Loc 'tape-player-menu-volume'}" />
<Slider Name="VolumeSlider" SetWidth="100" HorizontalExpand="True" MinValue="0" MaxValue="100"/>
</BoxContainer>
</BoxContainer>
</BoxContainer>
</ui:FancyWindow>
14 changes: 14 additions & 0 deletions Content.Client/_Sunrise/TapePlayer/TapePlayerMenu.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed partial class TapePlayerMenu : FancyWindow
public event Action<bool>? OnPlayPressed;
public event Action? OnStopPressed;
public event Action<float>? SetTime;
public event Action<float>? SetVolume;

private EntityUid? _audio;

Expand All @@ -46,6 +47,7 @@ public TapePlayerMenu()
OnStopPressed?.Invoke();
};
PlaybackSlider.OnReleased += PlaybackSliderKeyUp;
VolumeSlider.OnReleased += VolumeSliderKeyUp;

SetPlayPauseButton(_audioSystem.IsPlaying(_audio), force: true);
}
Expand All @@ -60,12 +62,23 @@ public void SetAudioStream(EntityUid? audio)
_audio = audio;
}

public void SetVolumeSlider(float volume)
{
VolumeSlider.Value = volume;
}

private void PlaybackSliderKeyUp(Slider args)
{
SetTime?.Invoke(PlaybackSlider.Value);
_lockTimer = 0.5f;
}

private void VolumeSliderKeyUp(Slider args)
{
SetVolume?.Invoke(VolumeSlider.Value / 100f);
_lockTimer = 0.5f;
}

public void SetPlayPauseButton(bool playing, bool force = false)
{
if (_playState == playing && !force)
Expand Down Expand Up @@ -99,6 +112,7 @@ protected override void FrameUpdate(FrameEventArgs args)
}

PlaybackSlider.Disabled = _lockTimer > 0f;
VolumeSlider.Disabled = _lockTimer > 0f;

if (_entManager.TryGetComponent(_audio, out AudioComponent? audio))
{
Expand Down
13 changes: 11 additions & 2 deletions Content.Server/_Sunrise/TapePlayer/TapePlayerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public override void Initialize()
SubscribeLocalEvent<TapePlayerComponent, TapePlayerPauseMessage>(OnTapePlayerPause);
SubscribeLocalEvent<TapePlayerComponent, TapePlayerStopMessage>(OnTapePlayerStop);
SubscribeLocalEvent<TapePlayerComponent, TapePlayerSetTimeMessage>(OnTapePlayerSetTime);
SubscribeLocalEvent<TapePlayerComponent, TapePlayerSetVolumeMessage>(OnTapePlayerSetVolume);


SubscribeLocalEvent<TapePlayerComponent, PowerChangedEvent>(OnPowerChanged);
}
Expand Down Expand Up @@ -73,10 +75,10 @@ private void OnTapePlayerPlay(EntityUid uid, TapePlayerComponent component, ref
}

var audioParams = AudioParams.Default
.WithVolume(component.Volume)
.WithVolume(SharedAudioSystem.GainToVolume(component.Volume))
.WithMaxDistance(component.MaxDistance)
.WithRolloffFactor(component.RolloffFactor)
.WithLoop(true);
.WithLoop(component.Loop);
component.AudioStream = Audio.PlayPvs(musicTapeComponent.Sound, uid, audioParams)?.Entity;
Dirty(uid, component);
}
Expand All @@ -97,6 +99,13 @@ private void OnTapePlayerSetTime(EntityUid uid, TapePlayerComponent component, T
}
}

private void OnTapePlayerSetVolume(EntityUid uid, TapePlayerComponent component, TapePlayerSetVolumeMessage args)
{
component.Volume = args.Volume;
Audio.SetVolume(component.AudioStream, SharedAudioSystem.GainToVolume(args.Volume));
Dirty(uid, component);
}

private void OnPowerChanged(Entity<TapePlayerComponent> entity, ref PowerChangedEvent args)
{
if (!entity.Comp.NeedPower)
Expand Down
14 changes: 11 additions & 3 deletions Content.Shared/_Sunrise/TapePlayer/TapePlayerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public sealed partial class TapePlayerComponent : Component
[DataField(required: true)]
public ItemSlot TapeSlot = new();

[DataField]
[ViewVariables(VVAccess.ReadWrite)]
public float Volume = 10f;
[DataField, AutoNetworkedField]
[ViewVariables]
public float Volume = 0.5f;

[DataField]
[ViewVariables(VVAccess.ReadWrite)]
Expand All @@ -41,6 +41,9 @@ public sealed partial class TapePlayerComponent : Component
[DataField]
public bool NeedPower;

[DataField]
public bool Loop = true;

[DataField]
public SoundSpecifier? ButtonSound;
}
Expand All @@ -59,6 +62,11 @@ public sealed class TapePlayerSetTimeMessage(float songTime) : BoundUserInterfac
{
public float SongTime { get; } = songTime;
}
[Serializable, NetSerializable]
public sealed class TapePlayerSetVolumeMessage(float volume) : BoundUserInterfaceMessage
{
public float Volume { get; } = volume;
}

[Serializable, NetSerializable]
public enum TapePlayerVisuals : byte
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tape-player-menu-title = Магнитофон
tape-player-inserted-tape = Установленая кассета:
tape-player-menu-inserted-tape = Установленая кассета:
tape-player-menu-volume = Громкость:
tape-player-menu-buttonplay = Играть
tape-player-menu-buttonpause = Пауза
tape-player-menu-buttonstop = Стоп
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
anchored: true
# Sunrise-Start
- type: TapePlayer
volume: 10
rolloffFactor: 1
maxDistance: 20
tapeSlot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
- state: "off"
map: ["enum.TapePlayerVisualLayers.Base"]
- type: TapePlayer
volume: -2
rolloffFactor: 1.2
maxDistance: 15
- type: MeleeWeapon
Expand All @@ -39,7 +38,6 @@
- state: "off"
map: ["enum.TapePlayerVisualLayers.Base"]
- type: TapePlayer
volume: 0
rolloffFactor: 1
maxDistance: 20
- type: MeleeWeapon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
- state: "off"
map: ["enum.TapePlayerVisualLayers.Base"]
- type: TapePlayer
volume: -4
rolloffFactor: 1.2
maxDistance: 10
tapeSlot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
- type: Sprite
state: tape1
- type: MusicTape
songName: Sunset by PigeonBeans
songName: Sunset by PigeonBeans Sunset by PigeonBeans
sound: /Audio/_Sunrise/TapePlayer/Tracks/sunset.ogg

0 comments on commit d55622d

Please sign in to comment.