Skip to content

Commit

Permalink
packaging: Properly pack coil and switch sound components.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Feb 8, 2025
1 parent 85ac173 commit c878caf
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override VisualElement CreateInspectorGUI()
var inspectorUi = inspectorXml.Instantiate();
root.Add(inspectorUi);
var coilNameDropdown = root.Q<DropdownField>("coil-name");
var coilNameProp = serializedObject.FindProperty(nameof(CoilSoundComponent._coilName));
var coilNameProp = serializedObject.FindProperty(nameof(CoilSoundComponent.CoilName));
var availableCoils = GetAvailableCoils();
ConfigureDropdown(coilNameDropdown, coilNameProp, availableCoils);
return root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override VisualElement CreateInspectorGUI()
var inspectorUi = inspectorXml.Instantiate();
root.Add(inspectorUi);
var switchNameDropdown = root.Q<DropdownField>("switch-name");
var switchNameProp = serializedObject.FindProperty(nameof(SwitchSoundComponent._switchName));
var switchNameProp = serializedObject.FindProperty(nameof(SwitchSoundComponent.SwitchName));
var availableSwitches = GetAvailableSwitches();
ConfigureDropdown(switchNameDropdown, switchNameProp, availableSwitches);
return root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,45 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// ReSharper disable InconsistentNaming

using UnityEngine;
using UnityEngine.Serialization;

namespace VisualPinball.Unity
{
/// <summary>
/// Start and or stop a sound when an event occurs that represents a binary state change,
/// such as a switch closing or a coil being energized.
/// </summary>
public abstract class BinaryEventSoundComponent<TEventSource, TEventArgs>
: EventSoundComponent<TEventSource, TEventArgs>
where TEventSource : class
public abstract class BinaryEventSoundComponent<TEventSource, TEventArgs> : EventSoundComponent<TEventSource, TEventArgs> where TEventSource : class
{
public enum StartWhen { TurnedOn, TurnedOff };
public enum StopWhen { Never, TurnedOn, TurnedOff };
[FormerlySerializedAs("_startWhen")]
public StartWhen StartWhen = StartWhen.TurnedOn;

[SerializeField] private StartWhen _startWhen = StartWhen.TurnedOn;
[SerializeField] private StopWhen _stopWhen = StopWhen.Never;
[FormerlySerializedAs("_stopWhen")]
public StopWhen StopWhen = StopWhen.Never;

protected override async void OnEvent(object sender, TEventArgs e)
{
bool isEnabled = InterpretAsBinary(e);
if ((isEnabled && _stopWhen == StopWhen.TurnedOn) ||
(!isEnabled && _stopWhen == StopWhen.TurnedOff))
if ((isEnabled && StopWhen == StopWhen.TurnedOn) ||
(!isEnabled && StopWhen == StopWhen.TurnedOff))
{
Stop(allowFade: true);
}

if ((isEnabled && _startWhen == StartWhen.TurnedOn) ||
(!isEnabled && _startWhen == StartWhen.TurnedOff))
if ((isEnabled && StartWhen == StartWhen.TurnedOn) ||
(!isEnabled && StartWhen == StartWhen.TurnedOff))
{
await Play();
}
}

protected abstract bool InterpretAsBinary(TEventArgs e);

public override bool SupportsLoopingSoundAssets()
=> _stopWhen != StopWhen.Never;
public override bool SupportsLoopingSoundAssets() => StopWhen != StopWhen.Never;
}

public enum StartWhen { TurnedOn, TurnedOff }
public enum StopWhen { Never, TurnedOn, TurnedOff }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using UnityEngine;
using System;
using UnityEngine.Serialization;

namespace VisualPinball.Unity
{
Expand All @@ -26,10 +27,11 @@ namespace VisualPinball.Unity
/// </summary>
[PackAs("CoilSound")]
[AddComponentMenu("Visual Pinball/Sound/Coil Sound")]
public class CoilSoundComponent : BinaryEventSoundComponent<IApiCoil, NoIdCoilEventArgs>
public class CoilSoundComponent : BinaryEventSoundComponent<IApiCoil, NoIdCoilEventArgs>, IPackable
{
[SerializeField, HideInInspector]
public string _coilName;
[FormerlySerializedAs("_coilName")]
[HideInInspector]
public string CoilName;

public override Type GetRequiredType() => typeof(ICoilDeviceComponent);

Expand All @@ -42,7 +44,7 @@ protected override bool TryFindEventSource(out IApiCoil coil)
}

foreach (var component in GetComponents<ICoilDeviceComponent>()) {
coil = player.Coil(component, _coilName);
coil = player.Coil(component, CoilName);
if (coil != null) {
return true;
}
Expand All @@ -55,5 +57,15 @@ protected override bool TryFindEventSource(out IApiCoil coil)
protected override void Unsubscribe(IApiCoil coil) => coil.CoilStatusChanged -= OnEvent;

protected override bool InterpretAsBinary(NoIdCoilEventArgs e) => e.IsEnergized;

#region Packaging

// refs are handled by SoundComponent

public new byte[] Pack() => CoilSoundPackable.Pack(this);

public new void Unpack(byte[] bytes) => CoilSoundPackable.Unpack(bytes, this);

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public void UnpackReferences(byte[] data, Transform root, PackNameLookup lookup,

#endregion


protected override void OnEnableAfterAfterAwake()
{
base.OnEnableAfterAfterAwake();
Expand Down
58 changes: 56 additions & 2 deletions VisualPinball.Unity/VisualPinball.Unity/Sound/SoundPackable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace VisualPinball.Unity
{
public struct SoundPackable {
public class SoundPackable {

public bool Interrupt;
public float Volume;
Expand All @@ -39,6 +39,60 @@ public static void Unpack(byte[] bytes, SoundComponent comp)
}
}

public class BinaryEventSoundPackable : SoundPackable
{
public StartWhen StartWhen;
public StopWhen StopWhen;
}

public class SwitchSoundPackable : BinaryEventSoundPackable
{
public string SwitchName;

public static byte[] Pack(SwitchSoundComponent comp)
=> PackageApi.Packer.Pack(new SwitchSoundPackable {
Interrupt = comp.Interrupt,
Volume = comp.Volume,
StartWhen = comp.StartWhen,
StopWhen = comp.StopWhen,
SwitchName = comp.SwitchName
});

public static void Unpack(byte[] bytes, SwitchSoundComponent comp)
{
var data = PackageApi.Packer.Unpack<SwitchSoundPackable>(bytes);
comp.Interrupt = data.Interrupt;
comp.Volume = data.Volume;
comp.StartWhen = data.StartWhen;
comp.StopWhen = data.StopWhen;
comp.SwitchName = data.SwitchName;
}
}

public class CoilSoundPackable : BinaryEventSoundPackable
{
public string CoilName;

public static byte[] Pack(CoilSoundComponent comp)
=> PackageApi.Packer.Pack(new CoilSoundPackable {
Interrupt = comp.Interrupt,
Volume = comp.Volume,
StartWhen = comp.StartWhen,
StopWhen = comp.StopWhen,
CoilName = comp.CoilName
});

public static void Unpack(byte[] bytes, CoilSoundComponent comp)
{
var data = PackageApi.Packer.Unpack<CoilSoundPackable>(bytes);
comp.Interrupt = data.Interrupt;
comp.Volume = data.Volume;
comp.StartWhen = data.StartWhen;
comp.StopWhen = data.StopWhen;
comp.CoilName = data.CoilName;
}
}

public struct SoundReferencesPackable {

public int SoundAssetRef;
Expand All @@ -53,8 +107,8 @@ public static byte[] PackReferences(SoundComponent comp, PackagedFiles files)
// pack asset
var assetRef = files.AddAsset(comp.SoundAsset);

var clipRefs = comp.SoundAsset.Clips != null
// pack sound files
var clipRefs = comp.SoundAsset.Clips != null
? comp.SoundAsset.Clips.Select(files.Add).ToArray()
: Array.Empty<string>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace VisualPinball.Unity
{
Expand All @@ -26,10 +27,11 @@ namespace VisualPinball.Unity
/// </summary>
[PackAs("SwitchSound")]
[AddComponentMenu("Visual Pinball/Sound/Switch Sound")]
public class SwitchSoundComponent : BinaryEventSoundComponent<IApiSwitch, SwitchEventArgs>
public class SwitchSoundComponent : BinaryEventSoundComponent<IApiSwitch, SwitchEventArgs>, IPackable
{
[SerializeField, HideInInspector]
public string _switchName;
[FormerlySerializedAs("_switchName")]
[HideInInspector]
public string SwitchName;

public override Type GetRequiredType() => typeof(ISwitchDeviceComponent);

Expand All @@ -42,7 +44,7 @@ protected override bool TryFindEventSource(out IApiSwitch @switch)
}

foreach (var component in GetComponents<ISwitchDeviceComponent>()) {
@switch = player.Switch(component, _switchName);
@switch = player.Switch(component, SwitchName);
if (@switch != null) {
return true;
}
Expand All @@ -55,5 +57,15 @@ protected override bool TryFindEventSource(out IApiSwitch @switch)
protected override void Unsubscribe(IApiSwitch eventSource) => eventSource.Switch -= OnEvent;

protected override bool InterpretAsBinary(SwitchEventArgs e) => e.IsEnabled;

#region Packaging

// refs are handled by SoundComponent

public new byte[] Pack() => SwitchSoundPackable.Pack(this);

public new void Unpack(byte[] bytes) => SwitchSoundPackable.Unpack(bytes, this);

#endregion
}
}

0 comments on commit c878caf

Please sign in to comment.