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

Atualização da Jukebox UI #54

Merged
merged 1 commit into from
Feb 24, 2025
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
15 changes: 15 additions & 0 deletions Content.Client/Audio/Jukebox/JukeboxBoundUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected override void Open()
_menu.OnSongSelected += SelectSong;

_menu.SetTime += SetTime;
_menu.SetVolume += SetVolume;
PopulateMusic();
Reload();
}
Expand All @@ -57,6 +58,7 @@ public void Reload()
return;

_menu.SetAudioStream(jukebox.AudioStream);
_menu.SetVolumeSlider(jukebox.Volume);

if (_protoManager.TryIndex(jukebox.SelectedSongId, out var songProto))
{
Expand Down Expand Up @@ -97,5 +99,18 @@ public void SetTime(float time)

SendMessage(new JukeboxSetTimeMessage(sentTime));
}
public void SetVolume(float volume)
{
var sentVolume = volume;

// Prediction
if (EntMan.TryGetComponent(Owner, out JukeboxComponent? jukebox) &&
EntMan.TryGetComponent(jukebox.AudioStream, out AudioComponent? audioComp))
{
audioComp.Volume = SharedJukeboxSystem.MapToRange(volume, jukebox.MinSlider, jukebox.MaxSlider, jukebox.MinVolume, jukebox.MaxVolume);
}

SendMessage(new JukeboxSetVolumeMessage(sentVolume));
}
}

27 changes: 20 additions & 7 deletions Content.Client/Audio/Jukebox/JukeboxMenu.xaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
<ui:FancyWindow xmlns="https://spacestation14.io" xmlns:ui="clr-namespace:Content.Client.UserInterface.Controls"
SetSize="400 500" Title="{Loc 'jukebox-menu-title'}">
SetSize="600 500" Title="{Loc 'jukebox-menu-title'}">
<BoxContainer Margin="4 0" Orientation="Vertical">
<ItemList Name="MusicList" SelectMode="Button" Margin="3 3 3 3"
HorizontalExpand="True" VerticalExpand="True" SizeFlagsStretchRatio="8"/>
<BoxContainer Orientation="Vertical">
<Label Name="SongSelected" Text="{Loc 'jukebox-menu-selectedsong'}" />
<Label Name="SongName" Text="---" />
<BoxContainer Orientation="Horizontal" HorizontalExpand="True"
VerticalExpand="False" SizeFlagsStretchRatio="1">
<Label Name="SongName" Text="---" />
<Label Name="DurationLabel" Text="00:00 / 00:00" HorizontalAlignment="Right" HorizontalExpand="True"/>
</BoxContainer>
<Slider Name="PlaybackSlider" HorizontalExpand="True" />
<Label Name="VolumeNumberLabel" Text="0" HorizontalAlignment="Right" HorizontalExpand="True"/>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True"
VerticalExpand="False" SizeFlagsStretchRatio="1">
<Label Name="VolumeLabel" Text="{Loc 'jukebox-menu-volume'} " />
<Slider Name="VolumeSlider" HorizontalExpand="True" />

</BoxContainer>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True"
VerticalExpand="False" SizeFlagsStretchRatio="1">
<Button Name="PlayButton" Text="{Loc 'jukebox-menu-buttonplay'}" />
<Button Name="StopButton" Text="{Loc 'jukebox-menu-buttonstop'}" />
<Label Name="DurationLabel" Text="00:00 / 00:00" HorizontalAlignment="Right" HorizontalExpand="True"/>
<BoxContainer Orientation="Vertical">
<PanelContainer StyleClasses="LowDivider" />
<BoxContainer Orientation="Horizontal" HorizontalExpand="True"
VerticalExpand="False" SizeFlagsStretchRatio="1" HorizontalAlignment="Center">
<Button Name="PlayButton" Text="{Loc 'jukebox-menu-buttonplay'}" />
<Button Name="StopButton" Text="{Loc 'jukebox-menu-buttonstop'}" />
</BoxContainer>
</BoxContainer>
</BoxContainer>
</ui:FancyWindow>
23 changes: 23 additions & 0 deletions Content.Client/Audio/Jukebox/JukeboxMenu.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed partial class JukeboxMenu : FancyWindow
public event Action? OnStopPressed;
public event Action<ProtoId<JukeboxPrototype>>? OnSongSelected;
public event Action<float>? SetTime;
public event Action<float>? SetVolume;

private EntityUid? _audio;

Expand Down Expand Up @@ -61,6 +62,9 @@ public JukeboxMenu()
OnStopPressed?.Invoke();
};
PlaybackSlider.OnReleased += PlaybackSliderKeyUp;
VolumeSlider.OnReleased += VolumeSliderKeyUp;

VolumeSlider.MaxValue = 100f;

SetPlayPauseButton(_audioSystem.IsPlaying(_audio), force: true);
}
Expand All @@ -81,6 +85,12 @@ private void PlaybackSliderKeyUp(Slider args)
_lockTimer = 0.5f;
}

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

/// <summary>
/// Re-populates the list of jukebox prototypes available.
/// </summary>
Expand All @@ -92,6 +102,7 @@ public void Populate(IEnumerable<JukeboxPrototype> jukeboxProtos)
{
MusicList.AddItem(entry.Name, metadata: entry.ID);
}
MusicList.SortItemsByText();
}

public void SetPlayPauseButton(bool playing, bool force = false)
Expand All @@ -117,6 +128,12 @@ public void SetSelectedSong(string name, float length)
PlaybackSlider.SetValueWithoutEvent(0);
}

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


protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
Expand All @@ -127,6 +144,7 @@ protected override void FrameUpdate(FrameEventArgs args)
}

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

if (_entManager.TryGetComponent(_audio, out AudioComponent? audio))
{
Expand All @@ -137,9 +155,14 @@ protected override void FrameUpdate(FrameEventArgs args)
DurationLabel.Text = $"00:00 / 00:00";
}

VolumeNumberLabel.Text = $"{VolumeSlider.Value.ToString("0.##")} %";

if (PlaybackSlider.Grabbed)
return;

if (VolumeSlider.Grabbed)
return;

if (audio != null || _entManager.TryGetComponent(_audio, out audio))
{
PlaybackSlider.SetValueWithoutEvent(audio.PlaybackPosition);
Expand Down
17 changes: 16 additions & 1 deletion Content.Server/Audio/Jukebox/JukeboxSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public override void Initialize()
SubscribeLocalEvent<JukeboxComponent, JukeboxPauseMessage>(OnJukeboxPause);
SubscribeLocalEvent<JukeboxComponent, JukeboxStopMessage>(OnJukeboxStop);
SubscribeLocalEvent<JukeboxComponent, JukeboxSetTimeMessage>(OnJukeboxSetTime);
SubscribeLocalEvent<JukeboxComponent, JukeboxSetVolumeMessage>(OnJukeboxSetVolume);
SubscribeLocalEvent<JukeboxComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<JukeboxComponent, ComponentShutdown>(OnComponentShutdown);

Expand Down Expand Up @@ -56,7 +57,7 @@ private void OnJukeboxPlay(EntityUid uid, JukeboxComponent component, ref Jukebo
return;
}

component.AudioStream = Audio.PlayPvs(jukeboxProto.Path, uid, AudioParams.Default.WithMaxDistance(10f))?.Entity;
component.AudioStream = Audio.PlayPvs(jukeboxProto.Path, uid, AudioParams.Default.WithMaxDistance(10f).WithVolume(MapToRange(component.Volume, component.MinSlider, component.MaxSlider, component.MinVolume, component.MaxVolume)))?.Entity;
Dirty(uid, component);
}
}
Expand All @@ -74,6 +75,15 @@ private void OnJukeboxSetTime(EntityUid uid, JukeboxComponent component, Jukebox
Audio.SetPlaybackPosition(component.AudioStream, args.SongTime + offset);
}
}
private void OnJukeboxSetVolume(EntityUid uid, JukeboxComponent component, JukeboxSetVolumeMessage args)
{
SetJukeboxVolume(uid, component, args.Volume);

if (!TryComp<AudioComponent>(component.AudioStream, out var audioComponent))
return;

Audio.SetVolume(component.AudioStream, MapToRange(args.Volume, component.MinSlider, component.MaxSlider, component.MinVolume, component.MaxVolume));
}

private void OnPowerChanged(Entity<JukeboxComponent> entity, ref PowerChangedEvent args)
{
Expand Down Expand Up @@ -129,6 +139,11 @@ public override void Update(float frameTime)
}
}
}
private void SetJukeboxVolume(EntityUid uid, JukeboxComponent component, float volume)
{
component.Volume = volume;
Dirty(uid, component);
}

private void OnComponentShutdown(EntityUid uid, JukeboxComponent component, ComponentShutdown args)
{
Expand Down
14 changes: 14 additions & 0 deletions Content.Shared/Audio/Jukebox/JukeboxComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public sealed partial class JukeboxComponent : Component

[ViewVariables]
public float SelectAccumulator;

[ViewVariables, AutoNetworkedField]
public float Volume = 50f;

public float MinVolume = -30f;
public float MaxVolume = 0f;
public float MinSlider = 0f;
public float MaxSlider = 100f;
}

[Serializable, NetSerializable]
Expand All @@ -60,6 +68,12 @@ public sealed class JukeboxSetTimeMessage(float songTime) : BoundUserInterfaceMe
public float SongTime { get; } = songTime;
}

[Serializable, NetSerializable]
public sealed class JukeboxSetVolumeMessage(float volume) : BoundUserInterfaceMessage
{
public float Volume { get; } = volume;
}

[Serializable, NetSerializable]
public enum JukeboxVisuals : byte
{
Expand Down
4 changes: 4 additions & 0 deletions Content.Shared/Audio/Jukebox/SharedJukeboxSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ namespace Content.Shared.Audio.Jukebox;
public abstract class SharedJukeboxSystem : EntitySystem
{
[Dependency] protected readonly SharedAudioSystem Audio = default!;
public static float MapToRange(float value, float leftMin, float leftMax, float rightMin, float rightMax)
{
return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
}
}
Loading