Skip to content

Commit

Permalink
Tons of stuff.
Browse files Browse the repository at this point in the history
Apart from adding a whack ton of songs, just loads of general clean up and additions before release.
  • Loading branch information
Pspritechologist committed Oct 30, 2023
1 parent c16e74a commit 29fa453
Show file tree
Hide file tree
Showing 68 changed files with 426 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace Content.Client.SimpleStation14.Jukebox.Ui;
public sealed class JukeboxBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IEntityManager _entity = default!;
private readonly ISawmill _log = default!;

private JukeboxWindow? _window;

public JukeboxBoundUserInterface(EntityUid owner, Enum uiKey) : base (owner, uiKey)
public JukeboxBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_log = EntMan.System<SharedJukeBoxSystem>().Log;
}

protected override void Open()
Expand All @@ -24,11 +26,11 @@ protected override void Open()

if (!_entity.TryGetComponent<JukeboxComponent>(Owner, out var jukeboxComp))
{
Logger.Error($"No Jukebox component found for {_entity.ToPrettyString(Owner)}!");
_log.Error("No Jukebox component found for {0}!", _entity.ToPrettyString(Owner));
return;
}

_window = new JukeboxWindow(this, jukeboxComp)
_window = new JukeboxWindow(jukeboxComp, _log)
{
Title = _entity.GetComponent<MetaDataComponent>(Owner).EntityName
};
Expand Down
60 changes: 29 additions & 31 deletions Content.Client/SimpleStation14/Jukebox/Ui/JukeboxWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed partial class JukeboxWindow : FancyWindow
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;

private readonly JukeboxBoundUserInterface _bui;
private readonly ISawmill _log = default!;

private readonly JukeboxComponent _jukeboxComp;

Expand All @@ -38,29 +38,30 @@ public sealed partial class JukeboxWindow : FancyWindow
public Action<string>? OnSongSelected;


public JukeboxWindow(JukeboxBoundUserInterface bui, JukeboxComponent jukeboxComp)
public JukeboxWindow(JukeboxComponent jukeboxComp, ISawmill log)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

_bui = bui;
_log = log;
_jukeboxComp = jukeboxComp;

PlayButton.OnPressed += _ => OnPlayButtonPressed?.Invoke();
SkipButton.OnPressed += _ => OnSkipButtonPressed?.Invoke();

SerialTitle.SetMessage(Loc.GetString("jukebox-ui-serial-title"));
SerialNumber.Text = jukeboxComp.SerialNumber;
SkipButton.TexturePath = jukeboxComp.UiButtonSkip;

// Sets up the custom colours of the ui.
BG_1.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorBG) };
Panel_1.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorPanel) };
Panel_2.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorPanel) };
Panel_3.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorPanel) };
BG_1.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorBG) };
Panel_1.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorPanel) };
Panel_2.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorPanel) };
Panel_3.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorPanel) };
// Accent_1.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorAccent) };
// Accent_2.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorAccent) };
Accent_3.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorAccent) };
Accent_4.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.JukeboxUiColorAccent) };
Accent_3.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorAccent) };
Accent_4.PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex(jukeboxComp.UiColorAccent) };

// Sets up all the fonts.
SongName.FontOverride = _resourceCache.GetFont("/Fonts/NotoSans/NotoSans-Regular.ttf", 18);
Expand Down Expand Up @@ -92,7 +93,7 @@ public JukeboxWindow(JukeboxBoundUserInterface bui, JukeboxComponent jukeboxComp
UpdateState(true);
}

// Updates the progress bar of the song every frame, matching to the duration and if one is actually playing.
// Updates the progress bar of the song every frame, matching to the duration, if a song is actually playing.
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
Expand All @@ -114,7 +115,7 @@ protected override void FrameUpdate(FrameEventArgs args)

if (_jukeboxComp.StoppedTime == null)
{
Logger.ErrorS("jukebox", $"Jukebox {_jukeboxComp} is stopped but has no StoppedTime!");
_log.Error(Loc.GetString("jukebox-error-no-stopped-time"), _jukeboxComp);

return;
}
Expand All @@ -127,23 +128,19 @@ private void PopulateSongs()
{
SongPickerBox.RemoveAllChildren();

var songsToAdd = _jukeboxComp.Songs;

songsToAdd.Sort();

foreach (var trackId in songsToAdd)
GenerateSongButton(trackId, false);

if (_jukeboxComp.Emagged)
if (_jukeboxComp.Emagged) // We want the emagged songs on top.
foreach (var trackId in _jukeboxComp.EmaggedSongs.OrderBy(song => song).ToList())
GenerateSongButton(trackId, true);

foreach (var trackId in _jukeboxComp.Songs.OrderBy(song => song).ToList())
GenerateSongButton(trackId, false);
}

private void GenerateSongButton(string trackId, bool illegal)
{
if (!_prototype.TryIndex<JukeboxTrackPrototype>(trackId, out var track))
{
Logger.Error($"No JukeboxTrackPrototype found for {trackId}!");
_log.Error(Loc.GetString("jukebox-error-no-prototype"), trackId);
return;
}

Expand Down Expand Up @@ -183,7 +180,7 @@ private void PopulateQueue()
{
if (!_prototype.TryIndex<JukeboxTrackPrototype>(trackId, out var track))
{
Logger.Error($"No JukeboxTrackPrototype found for {trackId}!");
_log.Error(Loc.GetString("jukebox-error-no-prototype"), trackId);

continue;
}
Expand All @@ -207,7 +204,7 @@ public void UpdateState(bool repopulateSongs = false)

PopulateQueue();

if (_jukeboxComp.CurrentlyPlayingTrack == null)
if (_jukeboxComp.CurrentlyPlayingTrack == null || !_prototype.TryIndex<JukeboxTrackPrototype>(_jukeboxComp.CurrentlyPlayingTrack, out var track))
{
SongName.Text = Loc.GetString("jukebox-ui-current-empty");
SongIcon.TexturePath = _jukeboxComp.DefaultSongArtPath;
Expand All @@ -218,7 +215,10 @@ public void UpdateState(bool repopulateSongs = false)
_timeWillFinish = TimeSpan.Zero;
_timeStopped = TimeSpan.Zero;

PlayButton.TexturePath = "/Textures/SimpleStation14/Interface/MediaControls/play.png";
PlayButton.TexturePath = _jukeboxComp.UiButtonPlay;

PlayButton.Disabled = true;
SkipButton.Disabled = true;

return;
}
Expand All @@ -227,19 +227,17 @@ public void UpdateState(bool repopulateSongs = false)

_timeStopped = _jukeboxComp.StoppedTime ?? TimeSpan.Zero;

_songDuration = _jukeboxComp.CurrentlyPlayingTrack.Duration;

var track = _jukeboxComp.CurrentlyPlayingTrack;
_songDuration = track.Duration;

SongName.Text = track.Name;
SongIcon.TexturePath = track.ArtPath;
SongIcon.TexturePath = track.ArtPath ?? _jukeboxComp.DefaultSongArtPath;

SkipButton.Disabled = false;
PlayButton.Disabled = false;

if (_jukeboxComp.Paused)
PlayButton.TexturePath = "/Textures/SimpleStation14/Interface/MediaControls/play.png";
else
PlayButton.TexturePath = "/Textures/SimpleStation14/Interface/MediaControls/pause.png";
PlayButton.TexturePath = _jukeboxComp.Paused ? _jukeboxComp.UiButtonPlay : _jukeboxComp.UiButtonPause;

PlayButton.Disabled = false;
SkipButton.Disabled = false;
}
}
40 changes: 9 additions & 31 deletions Content.Server/SimpleStation14/Jukebox/JukeboxSystem.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using Content.Server.Power.Components;
using Content.Shared.SimpleStation14.Jukebox;
using Content.Shared.SimpleStation14.Prototypes;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Server.GameObjects;
using Content.Shared.Emag.Systems;
using Content.Server.Construction;
using Content.Shared.Damage;
using Content.Server.DeviceLinking.Events;
using Content.Server.DeviceLinking.Systems;
using Robust.Shared.Random;
using Content.Shared.Power;
using System.Linq;
using Content.Server.Power.EntitySystems;

namespace Content.Server.SimpleStation14.Jukebox;

Expand All @@ -35,27 +31,6 @@ public sealed partial class JukeboxSystem : EntitySystem
public const string PortUnPause = "Unpause";
public const string PortTogglePuase = "TogglePause";

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<JukeboxComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<JukeboxComponent, ComponentShutdown>(OnComponentShutdown);

SubscribeLocalEvent<JukeboxComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<JukeboxComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<JukeboxComponent, EntityPausedEvent>(OnPaused);
SubscribeLocalEvent<JukeboxComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<JukeboxComponent, RefreshPartsEvent>(OnRefreshParts);
SubscribeLocalEvent<JukeboxComponent, UpgradeExamineEvent>(OnExamineParts);
SubscribeLocalEvent<JukeboxComponent, DamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<JukeboxComponent, SignalReceivedEvent>(OnSignalReceived);

SubscribeLocalEvent<JukeboxComponent, JukeboxPlayButtonPressedMessage>(OnPlayButtonPressed);
SubscribeLocalEvent<JukeboxComponent, JukeboxSkipButtonPressedMessage>(OnSkipButtonPressed);
SubscribeLocalEvent<JukeboxComponent, JukeboxSongSelectedMessage>(OnSongSelected);
}

#region Public functions

/// <summary>
Expand Down Expand Up @@ -220,7 +195,7 @@ public void TryPlayRandomSong(EntityUid jukebox, JukeboxComponent? jukeboxComp =

jukeboxComp.Playing = true; // Set the Jukebox to playing.

jukeboxComp.CurrentlyPlayingTrack = song; // Set the currently playing song.
jukeboxComp.CurrentlyPlayingTrack = song.ID; // Set the currently playing song.

jukeboxComp.FinishPlayingTime = _timing.CurTime + song.Duration - offset; // Set the time when the song should finish.

Expand Down Expand Up @@ -259,7 +234,7 @@ private void CheckCanPlay(EntityUid uid, JukeboxComponent component)
{
var canPlay = true;

if (EntityManager.TryGetComponent<ApcPowerReceiverComponent>(uid, out var powerComp) && !powerComp.Powered)
if (!this.IsPowered(uid, EntityManager))
canPlay = false;

component.CanPlay = canPlay;
Expand Down Expand Up @@ -337,14 +312,17 @@ private void TryRestart(EntityUid jukeBox, JukeboxComponent jukeboxComp)
if (jukeboxComp.Paused || !jukeboxComp.CanPlay)
return;

if (jukeboxComp.CurrentlyPlayingTrack == null || jukeboxComp.FinishPlayingTime == null || jukeboxComp.StoppedTime == null)
if (jukeboxComp.CurrentlyPlayingTrack == null ||
jukeboxComp.FinishPlayingTime == null ||
jukeboxComp.StoppedTime == null ||
!_prototype.TryIndex<JukeboxTrackPrototype>(jukeboxComp.CurrentlyPlayingTrack, out var trackProto))
return;

var timeLeftBeforeFinished = (TimeSpan) (jukeboxComp.CurrentlyPlayingTrack.Duration - (jukeboxComp.FinishPlayingTime - jukeboxComp.StoppedTime));
var timeLeftBeforeFinished = (TimeSpan) (trackProto.Duration - (jukeboxComp.FinishPlayingTime - jukeboxComp.StoppedTime));

jukeboxComp.StoppedTime = null;

TryPlaySong(jukeBox, jukeboxComp.CurrentlyPlayingTrack, jukeboxComp, timeLeftBeforeFinished);
TryPlaySong(jukeBox, trackProto, jukeboxComp, timeLeftBeforeFinished);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,31 @@ public sealed partial class JukeboxSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<JukeboxComponent, MapInitEvent>(OnComponentInit);
SubscribeLocalEvent<JukeboxComponent, ComponentShutdown>((ent, comp, _) => Clean(ent, comp));

SubscribeLocalEvent((EntityUid ent, JukeboxComponent comp, ref EntityPausedEvent _) => CheckCanPlay(ent, comp));
SubscribeLocalEvent((EntityUid ent, JukeboxComponent comp, ref EntityUnpausedEvent _) => CheckCanPlay(ent, comp));
SubscribeLocalEvent((EntityUid ent, JukeboxComponent comp, ref PowerChangedEvent _) => CheckCanPlay(ent, comp));
SubscribeLocalEvent<JukeboxComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<JukeboxComponent, RefreshPartsEvent>(OnRefreshParts);
SubscribeLocalEvent<JukeboxComponent, UpgradeExamineEvent>(OnExamineParts);
SubscribeLocalEvent<JukeboxComponent, DamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<JukeboxComponent, SignalReceivedEvent>(OnSignalReceived);

SubscribeLocalEvent<JukeboxComponent, JukeboxPlayButtonPressedMessage>((ent, comp, _) => TryTogglePause(ent, comp));
SubscribeLocalEvent<JukeboxComponent, JukeboxSkipButtonPressedMessage>((ent, comp, _) => TrySkipSong(ent, comp));
SubscribeLocalEvent<JukeboxComponent, JukeboxSongSelectedMessage>((ent, comp, msg) => TryQueueSong(ent, msg.Song, comp));
}

/// <summary>
/// Simply checks if the Jukebox can play songs on init.
/// Handles setting up a Jukebox.
/// </summary>
private void OnComponentInit(EntityUid uid, JukeboxComponent component, ComponentInit args)
private void OnComponentInit(EntityUid uid, JukeboxComponent component, MapInitEvent args)
{
CheckCanPlay(uid, component);

Expand All @@ -33,14 +54,6 @@ private void OnComponentInit(EntityUid uid, JukeboxComponent component, Componen
UpdateState(uid, component);
}

/// <summary>
/// Handles cleanup when the Jukebox is shut down.
/// </summary>
private void OnComponentShutdown(EntityUid uid, JukeboxComponent component, ComponentShutdown args)
{
Clean(uid, component);
}

/// <summary>
/// Handles setting the Jukebox's state to emagged.
/// </summary>
Expand All @@ -51,61 +64,6 @@ private void OnEmagged(EntityUid jukeBox, JukeboxComponent jukeboxComp, ref GotE
UpdateState(jukeBox, jukeboxComp);
}

/// <summary>
/// Handles when a Jukebox entity is paused in game terms.
/// </summary>
private void OnPaused(EntityUid jukeBox, JukeboxComponent jukeboxComp, ref EntityPausedEvent args)
{
StopSong(jukeBox, jukeboxComp);
}

/// <summary>
/// Handles when a Jukebox entity is unpaused in game terms.
/// </summary>
private void OnUnpaused(EntityUid jukeBox, JukeboxComponent jukeboxComp, ref EntityUnpausedEvent args)
{
CheckCanPlay(jukeBox, jukeboxComp);
}

/// <summary>
/// Checks if the Jukebox can play songs when its power state changes.
/// </summary>
private void OnPowerChanged(EntityUid uid, JukeboxComponent component, ref PowerChangedEvent args)
{
CheckCanPlay(uid, component);
}

/// <summary>
/// Handles the Jukebox's play button being pressed to toggle between playing and paused.
/// </summary>
private void OnPlayButtonPressed(EntityUid jukeBox, JukeboxComponent jukeboxComp, JukeboxPlayButtonPressedMessage msg)
{
if (jukeboxComp.Paused)
{
TryUnPauseSong(jukeBox, jukeboxComp);
}
else
{
DoPauseSong(jukeBox, jukeboxComp);
}
}

/// <summary>
/// Handles the Jukebox's skip button being pressed to skip the current song.
/// </summary>
private void OnSkipButtonPressed(EntityUid jukeBox, JukeboxComponent jukeboxComp, JukeboxSkipButtonPressedMessage msg)
{
TrySkipSong(jukeBox, jukeboxComp);
}

/// <summary>
/// Handles a song being selected in the Jukebox's ui.
/// </summary>
private void OnSongSelected(EntityUid jukeBox, JukeboxComponent jukeboxComp, JukeboxSongSelectedMessage msg)
{
TryQueueSong(jukeBox, msg.Song, jukeboxComp);
}

/// <summary>
/// Generates a valid serial number for the jukeboxes. This is just goofy.
/// </summary>
Expand All @@ -120,7 +78,10 @@ public string GenerateSerialNumber()
digits[4] = (digits[0] + digits[1]) % 10;
digits[5] = digits[2] - digits[3];
digits[6] = digits[4] * digits[5];
digits[7] = (digits[0] + digits[1] + digits[2] + digits[3]) % 5;
digits[7] = (digits[0] + digits[1] + digits[5] + digits[4]) % 5;

for (var i = 0; i < digits.Length; i++)
digits[i] = Math.Abs(digits[i]);

var serial = $"{digits[0]}{digits[1]}{digits[2]}{digits[3]}{digits[4]}{digits[5]}{digits[6]}";

Expand Down
Loading

0 comments on commit 29fa453

Please sign in to comment.