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

Abstract away settings logic using composition #856

Merged
merged 11 commits into from
Jul 11, 2020
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
4 changes: 2 additions & 2 deletions Samples/BasicMenu/Components/MenuChangeVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public override void DoAction()
{
base.DoAction();

float volume = DualityApp.UserData.SoundMasterVol;
float volume = DualityApp.UserData.Instance.SoundMasterVol;
volume += (this.changeAmount / 10f);

// make sure that the volume is between 0 and 1
volume = MathF.Min(MathF.Max(volume, 0), 1);

DualityApp.UserData.SoundMasterVol = volume;
DualityApp.UserData.Instance.SoundMasterVol = volume;
}
}
}
2 changes: 1 addition & 1 deletion Samples/BasicMenu/VolumeRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void ICmpUpdatable.OnUpdate()
}

// update the volume value
volumeText.Text.SourceText = String.Format("Volume {0:0.0}", DualityApp.UserData.SoundMasterVol);
volumeText.Text.SourceText = String.Format("Volume {0:0.0}", DualityApp.UserData.Instance.SoundMasterVol);
}
}
}
4 changes: 2 additions & 2 deletions Samples/Benchmarks/BenchmarkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ public void EnterBenchmarkMode()
this.renderSetup = ContentProvider.GetAvailableContent<BenchmarkRenderSetup>().FirstOrDefault();

// Make sure the benchmark setup is used globally
DualityApp.AppData.RenderingSetup = this.renderSetup.As<RenderSetup>();
DualityApp.AppData.Instance.RenderingSetup = this.renderSetup.As<RenderSetup>();
}
public void LeaveBenchmarkMode()
{
// Uninstall the benchmark setup we set globally
DualityApp.AppData.RenderingSetup = null;
DualityApp.AppData.Instance.RenderingSetup = null;

// Discard local references to content, since we know the controller
// itself won't be discarded due to being static
Expand Down
2 changes: 1 addition & 1 deletion Samples/Benchmarks/RenderSetupInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void ICmpInitializable.OnActivate()
{
if (DualityApp.ExecContext == DualityApp.ExecutionContext.Game)
{
this.renderSetup = DualityApp.AppData.RenderingSetup.As<BenchmarkRenderSetup>();
this.renderSetup = DualityApp.AppData.Instance.RenderingSetup.As<BenchmarkRenderSetup>();
this.text = new FormattedText();
this.text.LineAlign = Alignment.Right;
this.text.MaxWidth = 200;
Expand Down
2 changes: 1 addition & 1 deletion Samples/Physics/PhysicsSampleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void ICmpRenderer.Draw(IDrawDevice device)

// When the mouse is hovering over the game area and the system cursor
// is disabled, draw a custom cursor as a replacement
if (!DualityApp.UserData.SystemCursorVisible && DualityApp.Mouse.IsAvailable)
if (!DualityApp.UserData.Instance.SystemCursorVisible && DualityApp.Mouse.IsAvailable)
{
canvas.State.ColorTint = (this.dragObj != null) ? this.interactionColor : this.defaultColor;
canvas.FillThickLine(
Expand Down
28 changes: 14 additions & 14 deletions Source/Core/Duality/Audio/SoundDevice.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Diagnostics;

using Duality.Resources;
using Duality.Components;
Expand All @@ -23,7 +20,6 @@ public sealed class SoundDevice : IDisposable
private int numPlaying3D = 0;
private bool mute = false;


/// <summary>
/// [GET / SET] The current listener object. This is automatically set to an available
/// <see cref="SoundListener"/>.
Expand Down Expand Up @@ -113,10 +109,14 @@ public IEnumerable<SoundInstance> Playing
get { return this.sounds; }
}

private readonly SettingsContainer<DualityAppData> appData;
private readonly SettingsContainer<DualityUserData> userData;

public SoundDevice()
public SoundDevice(SettingsContainer<DualityAppData> appData, SettingsContainer<DualityUserData> userData)
{
DualityApp.AppDataChanged += this.DualityApp_AppDataChanged;
this.appData = appData;
this.userData = userData;
this.appData.Changed += this.AppDataChanged;
UpdateWorldSettings();
}
~SoundDevice()
Expand All @@ -133,7 +133,7 @@ private void Dispose(bool manually)
if (!this.disposed)
{
this.disposed = true;
DualityApp.AppDataChanged -= this.DualityApp_AppDataChanged;
this.appData.Changed -= this.AppDataChanged;

// Clear all playing sounds
foreach (SoundInstance inst in this.sounds) inst.Dispose();
Expand Down Expand Up @@ -225,8 +225,8 @@ private void UpdateListener()
private void UpdateWorldSettings()
{
DualityApp.AudioBackend.UpdateWorldSettings(
DualityApp.AppData.SpeedOfSound, // Already in meters per second / audio units
DualityApp.AppData.SoundDopplerFactor);
this.appData.Instance.SpeedOfSound, // Already in meters per second / audio units
this.appData.Instance.SoundDopplerFactor);
}

/// <summary>
Expand All @@ -236,7 +236,7 @@ private void UpdateWorldSettings()
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound(ContentRef<Sound> snd)
{
SoundInstance inst = new SoundInstance(snd);
SoundInstance inst = new SoundInstance(this.userData, snd);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -248,7 +248,7 @@ public SoundInstance PlaySound(ContentRef<Sound> snd)
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, Vector3 pos)
{
SoundInstance inst = new SoundInstance(snd, pos);
SoundInstance inst = new SoundInstance(this.userData, snd, pos);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -261,7 +261,7 @@ public SoundInstance PlaySound3D(ContentRef<Sound> snd, Vector3 pos)
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, bool trackVelocity)
{
SoundInstance inst = new SoundInstance(snd, attachTo, trackVelocity);
SoundInstance inst = new SoundInstance(this.userData, snd, attachTo, trackVelocity);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -275,7 +275,7 @@ public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, boo
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, Vector3 relativePos, bool trackVelocity)
{
SoundInstance inst = new SoundInstance(snd, attachTo, trackVelocity);
SoundInstance inst = new SoundInstance(this.userData, snd, attachTo, trackVelocity);
inst.Pos = relativePos;
this.sounds.Add(inst);
return inst;
Expand All @@ -291,7 +291,7 @@ public void StopAll()
}
}

private void DualityApp_AppDataChanged(object sender, EventArgs e)
private void AppDataChanged(object sender, EventArgs e)
{
UpdateWorldSettings();
}
Expand Down
23 changes: 12 additions & 11 deletions Source/Core/Duality/Audio/SoundInstance.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

using Duality.Resources;
using Duality.Backend;
Expand Down Expand Up @@ -193,6 +190,7 @@ public Vector3 Vel
set { this.vel = value; }
}

private readonly SettingsContainer<DualityUserData> userData;

~SoundInstance()
{
Expand Down Expand Up @@ -228,8 +226,9 @@ private void OnDisposed(bool manually)
}


internal SoundInstance(ContentRef<Sound> sound, GameObject attachObj, bool trackVelocity)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound, GameObject attachObj, bool trackVelocity)
{
this.userData = userData;
if (attachObj != null && trackVelocity)
attachObj.AddComponent<VelocityTracker>();

Expand All @@ -238,15 +237,17 @@ internal SoundInstance(ContentRef<Sound> sound, GameObject attachObj, bool track
this.sound = sound;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
}
internal SoundInstance(ContentRef<Sound> sound, Vector3 pos)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound, Vector3 pos)
{
this.userData = userData;
this.pos = pos;
this.is3D = true;
this.sound = sound;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
}
internal SoundInstance(ContentRef<Sound> sound)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound)
{
this.userData = userData;
this.sound = sound;
this.is3D = false;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
Expand Down Expand Up @@ -387,22 +388,22 @@ private float GetTypeVolFactor()
switch (this.sound.IsAvailable ? this.sound.Res.Type : SoundType.World)
{
case SoundType.UserInterface:
optVolFactor = DualityApp.UserData.SoundEffectVol;
optVolFactor = this.userData.Instance.SoundEffectVol;
break;
case SoundType.World:
optVolFactor = DualityApp.UserData.SoundEffectVol;
optVolFactor = this.userData.Instance.SoundEffectVol;
break;
case SoundType.Speech:
optVolFactor = DualityApp.UserData.SoundSpeechVol;
optVolFactor = this.userData.Instance.SoundSpeechVol;
break;
case SoundType.Music:
optVolFactor = DualityApp.UserData.SoundMusicVol;
optVolFactor = this.userData.Instance.SoundMusicVol;
break;
default:
optVolFactor = 1.0f;
break;
}
return optVolFactor * DualityApp.UserData.SoundMasterVol * 0.5f;
return optVolFactor * this.userData.Instance.SoundMasterVol * 0.5f;
}
private void RegisterPlaying()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Duality/Backend/Dummy/DummyNativeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void INativeWindow.Run()
while (DualityApp.ExecContext != DualityApp.ExecutionContext.Terminated)
{
DualityApp.Update();
DualityApp.Render(null, new Rect(DualityApp.UserData.WindowSize), DualityApp.UserData.WindowSize);
DualityApp.Render(null, new Rect(DualityApp.UserData.Instance.WindowSize), DualityApp.UserData.Instance.WindowSize);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Duality/Components/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public RenderSetup ActiveRenderSetup
{
return
this.renderSetup.Res ??
DualityApp.AppData.RenderingSetup.Res ??
DualityApp.AppData.Instance.RenderingSetup.Res ??
RenderSetup.Default.Res;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Duality/Components/Physics/PhysicsWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public float InterpolateAlpha
/// </summary>
public bool IsFixedTimestep
{
get { return DualityApp.AppData.PhysicsFixedTime && !this.lowFramerateMode; }
get { return DualityApp.AppData.Instance.PhysicsFixedTime && !this.lowFramerateMode; }
}


Expand Down Expand Up @@ -384,7 +384,7 @@ public bool QueryRect(Vector2 worldCoord, Vector2 size, List<RigidBody> queriedB

private void UpdateNativeSettings()
{
Settings.VelocityThreshold = PhysicsUnit.VelocityToPhysical * DualityApp.AppData.PhysicsVelocityThreshold;
Settings.VelocityThreshold = PhysicsUnit.VelocityToPhysical * DualityApp.AppData.Instance.PhysicsVelocityThreshold;
}
}
}
Loading