Skip to content

Commit

Permalink
Handheld radio (Vault-Overseers#390)
Browse files Browse the repository at this point in the history
* Handheld radio with frequency

* Handheld radio

* some fix

* some fix 2
  • Loading branch information
Sh1ntra authored Jul 24, 2024
1 parent 66f958e commit 9b06c4f
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 5 deletions.
59 changes: 59 additions & 0 deletions Content.Client/_NC/Radio/UI/HandheldRadioBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Content.Shared._NC.Radio;
using JetBrains.Annotations;
using Robust.Client.GameObjects;

namespace Content.Client._NC.Radio.UI;


[UsedImplicitly]
public sealed class HandheldRadioBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private HandheldRadioMenu? _menu;

public HandheldRadioBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{

}

protected override void Open()
{
base.Open();

_menu = new();

_menu.OnMicPressed += enabled =>
{
SendMessage(new ToggleHandheldRadioMicMessage(enabled));
};
_menu.OnSpeakerPressed += enabled =>
{
SendMessage(new ToggleHandheldRadioSpeakerMessage(enabled));
};
_menu.OnFrequencyChanged += frequency =>
{
SendMessage(new SelectHandheldRadioFrequencyMessage(frequency));
};

_menu.OnClose += Close;
_menu.OpenCentered();
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_menu?.Close();
}

protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);

if (state is not HandheldRadioBoundUIState msg)
return;

_menu?.Update(msg);
}
}
29 changes: 29 additions & 0 deletions Content.Client/_NC/Radio/UI/HandheldRadioMenu.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
Title="{Loc 'handheld-radio-menu-title'}"
MinSize="200 170"
SetSize="200 170">
<BoxContainer Orientation="Vertical"
HorizontalExpand="True"
VerticalExpand="True"
Margin="5 0 5 0">
<Control MinHeight="10"/>
<BoxContainer Orientation="Vertical" SeparationOverride="4" MinWidth="150">
<Label Name="CurrentTextFrequency" Text="{Loc 'handheld-radio-current-text-frequency'}" />
<LineEdit Name="FrequencyLineEdit" />
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" HorizontalAlignment="Right" Margin="5 0 5 5">
<Button Name="MicButton" ToggleMode="True" Text="{Loc 'handheld-radio-button-text-mic'}" StyleClasses="OpenRight" MinWidth="70"/>
<Button Name="SpeakerButton" ToggleMode="True" Text="{Loc 'handheld-radio-button-text-speaker'}" StyleClasses="OpenLeft" MinWidth="70"/>
</BoxContainer>
<BoxContainer Orientation="Vertical">
<PanelContainer StyleClasses="LowDivider" />
<BoxContainer Orientation="Horizontal" Margin="10 2 5 0" VerticalAlignment="Bottom">
<Label Text="{Loc 'handheld-radio-flavor-text-left'}" StyleClasses="WindowFooterText"
HorizontalAlignment="Left" HorizontalExpand="True" Margin="0 0 5 0" />
<TextureRect StyleClasses="NTLogoDark" Stretch="KeepAspectCentered"
VerticalAlignment="Center" HorizontalAlignment="Right" SetSize="19 19"/>
</BoxContainer>
</BoxContainer>
</BoxContainer>
</controls:FancyWindow>
35 changes: 35 additions & 0 deletions Content.Client/_NC/Radio/UI/HandheldRadioMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Content.Client.UserInterface.Controls;
using Content.Shared._NC.Radio;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;

namespace Content.Client._NC.Radio.UI;

[GenerateTypedNameReferences]
public sealed partial class HandheldRadioMenu : FancyWindow
{

public event Action<bool>? OnMicPressed;
public event Action<bool>? OnSpeakerPressed;
public event Action<string>? OnFrequencyChanged;

public HandheldRadioMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

MicButton.OnPressed += args => OnMicPressed?.Invoke(args.Button.Pressed);
SpeakerButton.OnPressed += args => OnSpeakerPressed?.Invoke(args.Button.Pressed);

FrequencyLineEdit.OnTextEntered += e => OnFrequencyChanged?.Invoke(e.Text);
FrequencyLineEdit.OnFocusExit += e => OnFrequencyChanged?.Invoke(e.Text);
}

public void Update(HandheldRadioBoundUIState state)
{
MicButton.Pressed = state.MicEnabled;
SpeakerButton.Pressed = state.SpeakerEnabled;
FrequencyLineEdit.Text = state.Frequency.ToString();
}
}
7 changes: 7 additions & 0 deletions Content.Server/Radio/Components/RadioMicrophoneComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public sealed partial class RadioMicrophoneComponent : Component
/// </summary>
[DataField("unobstructedRequired")]
public bool UnobstructedRequired = false;

// Nuclear-14
/// <summary>
// The radio frequency on which the message will be transmitted
/// </summary>
[DataField]
public int Frequency = 1459; // Common channel frequency
}
65 changes: 62 additions & 3 deletions Content.Server/Radio/EntitySystems/RadioDeviceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Content.Shared.Interaction;
using Content.Shared.Radio;
using Content.Shared.Radio.Components;
using Content.Shared._NC.Radio;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;

Expand Down Expand Up @@ -53,6 +54,12 @@ public override void Initialize()
SubscribeLocalEvent<IntercomComponent, ToggleIntercomMicMessage>(OnToggleIntercomMic);
SubscribeLocalEvent<IntercomComponent, ToggleIntercomSpeakerMessage>(OnToggleIntercomSpeaker);
SubscribeLocalEvent<IntercomComponent, SelectIntercomChannelMessage>(OnSelectIntercomChannel);
// Nuclear-14-Start
SubscribeLocalEvent<RadioMicrophoneComponent, BeforeActivatableUIOpenEvent>(OnBeforeHandheldRadioUiOpen);
SubscribeLocalEvent<RadioMicrophoneComponent, ToggleHandheldRadioMicMessage>(OnToggleHandheldRadioMic);
SubscribeLocalEvent<RadioMicrophoneComponent, ToggleHandheldRadioSpeakerMessage>(OnToggleHandheldRadioSpeaker);
SubscribeLocalEvent<RadioMicrophoneComponent, SelectHandheldRadioFrequencyMessage>(OnChangeHandheldRadioFrequency);
// Nuclear-14-End
}

public override void Update(float frameTime)
Expand Down Expand Up @@ -177,7 +184,7 @@ private void OnExamine(EntityUid uid, RadioMicrophoneComponent component, Examin

using (args.PushGroup(nameof(RadioMicrophoneComponent)))
{
args.PushMarkup(Loc.GetString("handheld-radio-component-on-examine", ("frequency", proto.Frequency)));
args.PushMarkup(Loc.GetString("handheld-radio-component-on-examine", ("frequency", /*Nuclear-14-start*/ component.Frequency /*Nuclear-14-end*/)));
args.PushMarkup(Loc.GetString("handheld-radio-component-chennel-examine",
("channel", proto.LocalizedName)));
}
Expand All @@ -189,7 +196,7 @@ private void OnListen(EntityUid uid, RadioMicrophoneComponent component, ListenE
return; // no feedback loops please.

if (_recentlySent.Add((args.Message, args.Source)))
_radio.SendRadioMessage(args.Source, args.Message, _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel), uid);
_radio.SendRadioMessage(args.Source, args.Message, _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel), uid, /*Nuclear-14-start*/ frequency: component.Frequency /*Nuclear-14-end*/);
}

private void OnAttemptListen(EntityUid uid, RadioMicrophoneComponent component, ListenAttemptEvent args)
Expand Down Expand Up @@ -242,11 +249,14 @@ private void OnSelectIntercomChannel(EntityUid uid, IntercomComponent component,
if (component.RequiresPower && !this.IsPowered(uid, EntityManager) || args.Session.AttachedEntity is not { })
return;

if (!_protoMan.TryIndex<RadioChannelPrototype>(args.Channel, out _) || !component.SupportedChannels.Contains(args.Channel))
if (!_protoMan.TryIndex<RadioChannelPrototype>(args.Channel, out var /*Nuclear-14-start*/ channel /*Nuclear-14-end*/) || !component.SupportedChannels.Contains(args.Channel))
return;

if (TryComp<RadioMicrophoneComponent>(uid, out var mic))
{
mic.BroadcastChannel = args.Channel;
mic.Frequency = _radio.GetFrequency(uid, channel); // Nuclear-14
}
if (TryComp<RadioSpeakerComponent>(uid, out var speaker))
speaker.Channels = new(){ args.Channel };
UpdateIntercomUi(uid, component);
Expand All @@ -264,4 +274,53 @@ private void UpdateIntercomUi(EntityUid uid, IntercomComponent component)
var state = new IntercomBoundUIState(micEnabled, speakerEnabled, availableChannels, selectedChannel);
_ui.TrySetUiState(uid, IntercomUiKey.Key, state);
}

// Nuclear-14-Start
#region Handheld Radio

private void OnBeforeHandheldRadioUiOpen(Entity<RadioMicrophoneComponent> microphone, ref BeforeActivatableUIOpenEvent args)
{
UpdateHandheldRadioUi(microphone);
}

private void OnToggleHandheldRadioMic(Entity<RadioMicrophoneComponent> microphone, ref ToggleHandheldRadioMicMessage args)
{
if (args.Session.AttachedEntity is not { } user)
return;

SetMicrophoneEnabled(microphone, user, args.Enabled, true);
UpdateHandheldRadioUi(microphone);
}

private void OnToggleHandheldRadioSpeaker(Entity<RadioMicrophoneComponent> microphone, ref ToggleHandheldRadioSpeakerMessage args)
{
if (args.Session.AttachedEntity is not { } user)
return;

SetSpeakerEnabled(microphone, user, args.Enabled, true);
UpdateHandheldRadioUi(microphone);
}

private void OnChangeHandheldRadioFrequency(Entity<RadioMicrophoneComponent> microphone, ref SelectHandheldRadioFrequencyMessage args)
{
if (args.Session.AttachedEntity is not { })
return;

microphone.Comp.Frequency = args.Frequency;
UpdateHandheldRadioUi(microphone);
}

private void UpdateHandheldRadioUi(Entity<RadioMicrophoneComponent> radio)
{
var speakerComp = CompOrNull<RadioSpeakerComponent>(radio);
var frequency = radio.Comp.Frequency;

var micEnabled = radio.Comp.Enabled;
var speakerEnabled = speakerComp?.Enabled ?? false;
var state = new HandheldRadioBoundUIState(micEnabled, speakerEnabled, frequency);
_ui.TrySetUiState(radio, HandheldRadioUiKey.Key, state);
}

#endregion
// Nuclear-14-End
}
25 changes: 24 additions & 1 deletion Content.Server/Radio/EntitySystems/RadioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Content.Shared.Radio;
using Content.Shared.Radio.Components;
using Content.Shared.Speech;
using Content.Shared.Ghost; // Nuclear-14
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Player;
Expand Down Expand Up @@ -53,6 +54,18 @@ private void OnIntrinsicSpeak(EntityUid uid, IntrinsicRadioTransmitterComponent
}
}

//Nuclear-14
/// <summary>
/// Gets the message frequency, if there is no such frequency, returns the standard channel frequency.
/// </summary>
public int GetFrequency(EntityUid source, RadioChannelPrototype channel)
{
if (TryComp<RadioMicrophoneComponent>(source, out var radioMicrophone))
return radioMicrophone.Frequency;

return channel.Frequency;
}

private void OnIntrinsicReceive(EntityUid uid, IntrinsicRadioReceiverComponent component, ref RadioReceiveEvent args)
{
if (TryComp(uid, out ActorComponent? actor))
Expand Down Expand Up @@ -80,7 +93,7 @@ public void SendRadioMessage(EntityUid messageSource, string message, ProtoId<Ra
/// </summary>
/// <param name="messageSource">Entity that spoke the message</param>
/// <param name="radioSource">Entity that picked up the message and will send it, e.g. headset</param>
public void SendRadioMessage(EntityUid messageSource, string message, RadioChannelPrototype channel, EntityUid radioSource, LanguagePrototype? language = null, bool escapeMarkup = true)
public void SendRadioMessage(EntityUid messageSource, string message, RadioChannelPrototype channel, EntityUid radioSource, LanguagePrototype? language = null, /*Nuclear-14-Start*/ int? frequency = null /*Nuclear-14-End*/, bool escapeMarkup = true)
{
if (language == null)
language = _language.GetLanguage(messageSource);
Expand Down Expand Up @@ -136,6 +149,12 @@ public void SendRadioMessage(EntityUid messageSource, string message, RadioChann

var speakerQuery = GetEntityQuery<RadioSpeakerComponent>();
var radioQuery = EntityQueryEnumerator<ActiveRadioComponent, TransformComponent>();

/*Nuclear-14-Start*/
if (frequency == null)
frequency = GetFrequency(messageSource, channel);
/*Nuclear-14-End*/

while (canSend && radioQuery.MoveNext(out var receiver, out var radio, out var transform))
{
if (!radio.ReceiveAllChannels)
Expand All @@ -145,6 +164,10 @@ public void SendRadioMessage(EntityUid messageSource, string message, RadioChann
continue;
}

// Nuclear-14
if (!HasComp<GhostComponent>(receiver) && GetFrequency(receiver, channel) != frequency)
continue;

if (!channel.LongRange && transform.MapID != sourceMapId && !radio.GlobalReceive)
continue;

Expand Down
57 changes: 57 additions & 0 deletions Content.Shared/_NC/Radio/SharedHandheldRadio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Robust.Shared.Serialization;

namespace Content.Shared._NC.Radio;

[Serializable, NetSerializable]
public enum HandheldRadioUiKey : byte
{
Key,
}

[Serializable, NetSerializable]
public sealed class HandheldRadioBoundUIState : BoundUserInterfaceState
{
public bool MicEnabled;
public bool SpeakerEnabled;
public int Frequency;

public HandheldRadioBoundUIState(bool micEnabled, bool speakerEnabled, int frequency)
{
MicEnabled = micEnabled;
SpeakerEnabled = speakerEnabled;
Frequency = frequency;
}
}

[Serializable, NetSerializable]
public sealed class ToggleHandheldRadioMicMessage : BoundUserInterfaceMessage
{
public bool Enabled;

public ToggleHandheldRadioMicMessage(bool enabled)
{
Enabled = enabled;
}
}

[Serializable, NetSerializable]
public sealed class ToggleHandheldRadioSpeakerMessage : BoundUserInterfaceMessage
{
public bool Enabled;

public ToggleHandheldRadioSpeakerMessage(bool enabled)
{
Enabled = enabled;
}
}

[Serializable, NetSerializable]
public sealed class SelectHandheldRadioFrequencyMessage : BoundUserInterfaceMessage
{
public int Frequency;

public SelectHandheldRadioFrequencyMessage(string frequency)
{
int.TryParse(frequency.Trim(), out Frequency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ handheld-radio-component-on-examine = It's set to broadcast over the {$frequency
handheld-radio-component-on-state = on
handheld-radio-component-off-state = off
handheld-radio-component-channel-set = Channel set to {$channel}
handheld-radio-component-chennel-examine = The current channel is {$channel}.
handheld-radio-component-chennel-examine = The current channel is {$channel}.
# Nuclear-14-Start
handheld-radio-menu-title = Handheld radio.
handheld-radio-current-text-frequency = Broadcast frequency
handheld-radio-button-text-mic = Mic.
handheld-radio-button-text-speaker = Speak
handheld-radio-flavor-text-left = Wiretapping of closed frequencies
is punishable by law.
# Nuclear-14-End
Loading

0 comments on commit 9b06c4f

Please sign in to comment.