Skip to content

Commit

Permalink
Updates to controller configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisAdderley committed Sep 21, 2024
1 parent ba09e0f commit 45babe9
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 34 deletions.
Binary file modified GameData/Waterfall/Plugins/Waterfall.dll
Binary file not shown.
15 changes: 11 additions & 4 deletions Source/Waterfall/EffectControllers/EngineEventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace Waterfall
public class EngineEventController : WaterfallController
{
[Persistent] public string eventName;
[Persistent] public string engineID;

public FloatCurve eventCurve = new();
[Persistent] public float eventDuration = 1f;
private ModuleEngines engineModule;
private MultiModeEngine multiEngine;

private Func<ModuleEngines, bool> getEngineStateFunc; // when the result of this function transitions from false -> true, the event should fire
private bool eventPlaying;
Expand Down Expand Up @@ -47,12 +49,17 @@ public override void Initialize(ModuleWaterfallFX host)

values = new float[1];

engineModule = host.GetComponents<ModuleEngines>().FirstOrDefault(x => x.engineID == host.engineID);
if (engineModule == null)
engineModule = host.part.FindModuleImplementing<ModuleEngines>();

engineModule = host.GetComponents<ModuleEngines>().FirstOrDefault(x => x.engineID == engineID);
if (engineModule == null)
Utils.LogError($"[EngineEventController] Could not find engine module for waterfall moduleID '{host.moduleID}' engine '{host.engineID}' in part '{host.part.name}' on Initialize");
{
Utils.Log($"[EngineEventController] Could not find engine ID {engineID}, using first module");
engineModule = host.part.FindModuleImplementing<ModuleEngines>();
}
multiEngine = host.GetComponent<MultiModeEngine>();
if (multiEngine == null)
{
}

EngineStateFuncs.TryGetValue(eventName, out getEngineStateFunc);

Expand Down
24 changes: 11 additions & 13 deletions Source/Waterfall/EffectControllers/RandomnessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,19 @@ public class RandomnessController : WaterfallController
[Persistent] public Vector2 range;
[Persistent] public string noiseType = RandomNoiseName;

public int seed;
[Persistent] public bool randomSeed;
[Persistent] public int seed;
[Persistent] public float scale = 1f;
[Persistent] public float minimum;
[Persistent] public float speed = 1f;

private NoiseFunction noiseFunc;

public RandomnessController() : base() { }
public RandomnessController(ConfigNode node) : base(node)
{
// Randomize seed if not specified
if (!node.TryGetValue(nameof(seed), ref seed))
{
seed = Random.Range(0, 10000);
}
}

public override ConfigNode Save()
{
var c = base.Save();
if (noiseType == PerlinNoiseName)
c.AddValue(nameof(seed), seed);
return c;
}

public override void Initialize(ModuleWaterfallFX host)
{
Expand All @@ -52,9 +42,17 @@ public override void Initialize(ModuleWaterfallFX host)
values = new float[1];

if (noiseType == PerlinNoiseName)
{
noiseFunc = PerlinNoise;
if (randomSeed)
{
seed = Random.Range(0, int.MaxValue);
}
}
else if (noiseType == RandomNoiseName)
{
noiseFunc = RandomNoise;
}
else
noiseFunc = RandomNoise;
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Waterfall/EffectControllers/WaterfallController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public abstract class WaterfallController
public const string LegacyControllerTypeNodeName = "linkedTo";

[Persistent] public string name = "unnamedController";

public ModuleWaterfallFX ParentModule => parentModule;

public bool overridden
{
get { return _overridden; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UniLinq;

namespace Waterfall.UI.EffectControllersUI
{
public class EngineEventControllerUIOptions : DefaultEffectControllerUIOptions<EngineEventController>
{

private readonly string[] eventTypes = { "ignition", "flameout" };
private readonly Vector2 curveButtonDims = new(100f, 50f);
private string[] engineIDOptions;
private int engineIndex;

private readonly Vector2 curveButtonDims = new(100f, 50f);
private readonly int texWidth = 80;
private readonly int texHeight = 30;
private int eventFlag;
Expand All @@ -30,6 +34,16 @@ public EngineEventControllerUIOptions()

public override void DrawOptions()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Engine ID", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
if (engineIDOptions != null && engineIDOptions.Length != 0)
{
engineIndex = GUILayout.SelectionGrid(engineIndex, engineIDOptions, 2);
}
else
{
GUILayout.Label("0");
}
GUILayout.Label("Event name");
int eventFlagChanged = GUILayout.SelectionGrid(eventFlag, eventTypes, Mathf.Min(eventTypes.Length, 4), UIResources.GetStyle("radio_text_button"));

Expand Down Expand Up @@ -60,16 +74,28 @@ protected override void LoadOptions(EngineEventController controller)
eventCurve = controller.eventCurve;
eventDuration = controller.eventDuration;
eventDurationString = controller.eventDuration.ToString();
List<ModuleEngines> engineOptions = controller.ParentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = engineIDOptions.ToList().IndexOf(controller.engineID);
engineIndex = engineIndex == -1 ? 0 : engineIndex;

GenerateCurveThumbs();
}

public override void DefaultOptions(ModuleWaterfallFX parentModule)
{
List<ModuleEngines> engineOptions = parentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = 0;
}

protected override EngineEventController CreateControllerInternal() =>
new()
{
eventName = eventTypes[eventFlag],
eventCurve = eventCurve,
eventDuration = eventDuration
eventDuration = eventDuration,
engineID = engineIDOptions[engineIndex]
};

private void EditCurve(FloatCurve toEdit, CurveUpdateFunction function)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public interface IEffectControllerUIOptions
/// </summary>
void LoadOptions(WaterfallController controller);

/// <summary>
/// Can be used to initialized default options from the base module
/// </summary>
/// <param name="fx"></param>
void DefaultOptions(ModuleWaterfallFX fx);

/// <summary>
/// Create new effect controller with options from UI state.
/// </summary>
Expand All @@ -43,7 +49,8 @@ public void LoadOptions(WaterfallController controller)
throw new ArgumentException(nameof(controller));
}
}

public virtual void DefaultOptions(ModuleWaterfallFX fx) { }

public WaterfallController CreateController() => CreateControllerInternal();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class RandomnessControllerUIOptions : DefaultEffectControllerUIOptions<Ra
private float perlinMin;
private float perlinScale = 1f;
private float perlinSpeed = 1f;
private bool randomSeed = false;

public RandomnessControllerUIOptions() { }

Expand Down Expand Up @@ -51,15 +52,19 @@ public override void DrawOptions()

if (randTypes[randFlag] == RandomnessController.PerlinNoiseName)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Seed", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
randomStrings[0] = GUILayout.TextArea(randomStrings[0], GUILayout.MaxWidth(60f));
if (Int32.TryParse(randomStrings[0], out int intParsed))
randomSeed = GUILayout.Toggle(randomSeed, "Random Seed");
if (!randomSeed)
{
perlinSeed = intParsed;
GUILayout.BeginHorizontal();
GUILayout.Label("Seed", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
randomStrings[0] = GUILayout.TextArea(randomStrings[0], GUILayout.MaxWidth(60f));
if (Int32.TryParse(randomStrings[0], out int intParsed))
{
perlinSeed = intParsed;
}
GUILayout.EndHorizontal();
}

GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Minimum", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
randomStrings[3] = GUILayout.TextArea(randomStrings[3], GUILayout.MaxWidth(60f));
Expand Down Expand Up @@ -106,6 +111,7 @@ protected override void LoadOptions(RandomnessController controller)
randomStrings[1] = controller.scale.ToString();
randomStrings[2] = controller.speed.ToString();
randomStrings[3] = controller.minimum.ToString();
randomSeed = controller.randomSeed;
}
}

Expand All @@ -116,6 +122,7 @@ protected override RandomnessController CreateControllerInternal() =>
scale = perlinScale,
minimum = perlinMin,
seed = perlinSeed,
randomSeed = randomSeed,
speed = perlinSpeed,
noiseType = randTypes[randFlag]
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UniLinq;

namespace Waterfall.UI.EffectControllersUI
{
public class ThrottleControllerUIOptions : DefaultEffectControllerUIOptions<ThrottleController>
{
private readonly string[] throttleStrings;
private string[] engineIDOptions;
private int engineIndex;

private float rampRateUp = 100f;
private float rampRateDown = 100f;

private float rampRateUp = 100f;
private float rampRateDown = 100f;

public ThrottleControllerUIOptions()
{
throttleStrings = new[] { rampRateUp.ToString(), rampRateDown.ToString() };
throttleStrings = new[] { rampRateUp.ToString(), rampRateDown.ToString() };
}

public override void DrawOptions()
{

GUILayout.BeginHorizontal();
GUILayout.Label("Engine ID", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
if (engineIDOptions != null && engineIDOptions.Length != 0)
{
engineIndex = GUILayout.SelectionGrid(engineIndex, engineIDOptions, 2);
}
else
{
GUILayout.Label("0");
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Ramp Rate Up", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
throttleStrings[0] = GUILayout.TextArea(throttleStrings[0], GUILayout.MaxWidth(60f));
Expand All @@ -43,13 +60,25 @@ protected override void LoadOptions(ThrottleController controller)
{
throttleStrings[0] = controller.responseRateUp.ToString();
throttleStrings[1] = controller.responseRateDown.ToString();
List<ModuleEngines> engineOptions = controller.ParentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = engineIDOptions.ToList().IndexOf(controller.engineID);
engineIndex = engineIndex == -1 ? 0 : engineIndex;
}

public override void DefaultOptions(ModuleWaterfallFX parentModule)
{
List<ModuleEngines> engineOptions = parentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = 0;
}

protected override ThrottleController CreateControllerInternal() =>
new()
{
responseRateUp = rampRateUp,
responseRateDown = rampRateDown
responseRateUp = rampRateUp,
responseRateDown = rampRateDown,
engineID = engineIDOptions[engineIndex]
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
namespace Waterfall.UI.EffectControllersUI
using System.Collections.Generic;
using UnityEngine;
using UniLinq;

namespace Waterfall.UI.EffectControllersUI
{
public class ThrustControllerUIOptions : DefaultEffectControllerUIOptions<ThrustController> { }
public class ThrustControllerUIOptions : DefaultEffectControllerUIOptions<ThrustController>
{
private string[] engineIDOptions;
private int engineIndex;

public ThrustControllerUIOptions() { }

public override void DrawOptions()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Engine ID", UIResources.GetStyle("data_header"), GUILayout.MaxWidth(160f));
if (engineIDOptions != null && engineIDOptions.Length != 0)
{
engineIndex = GUILayout.SelectionGrid(engineIndex, engineIDOptions, 2);
}
else
{
GUILayout.Label("0");
}
GUILayout.EndHorizontal();
}

protected override void LoadOptions(ThrustController controller)
{
List<ModuleEngines> engineOptions = controller.ParentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = engineIDOptions.ToList().IndexOf(controller.engineID);
engineIndex = engineIndex == -1 ? 0 : engineIndex;
}

public override void DefaultOptions(ModuleWaterfallFX parentModule)
{
List<ModuleEngines> engineOptions = parentModule.part.FindModulesImplementing<ModuleEngines>();
engineIDOptions = engineOptions.Select(x => x.engineID).ToArray();
engineIndex = 0;
}

protected override ThrustController CreateControllerInternal() =>
new()
{
engineID = engineIDOptions[engineIndex]
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ protected void DrawModify()

private void UpdateControllerSelection()
{
selectedControllerIndex = GUILayout.SelectionGrid(selectedControllerIndex, controllersGridValues, Mathf.Min(controllersGridValues.Length, 4), UIResources.GetStyle("radio_text_button"));
int newControllerIndex = GUILayout.SelectionGrid(selectedControllerIndex, controllersGridValues, Mathf.Min(controllersGridValues.Length, 4), UIResources.GetStyle("radio_text_button"));
if (newControllerIndex != selectedControllerIndex)
{
selectedControllerIndex = newControllerIndex;
var options = controllerOptions[selectedControllerIndex];
options.DefaultOptions(fxMod);
}
}

private WaterfallController CreateNewController()
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ v0.10.0
- Updated bundled ModuleManger to 4.2.3
- Performance updates (placeholder)
- Fixed engine light layers targeting IVA layers (#130)
- Thrust and Throttle controllers can now have their EngineID set in the UI (#125)
- Added a UI option for Perlin noise to have its seed to an option that will randomize it on effect startup (#127)
- Various logging cleanups and improvements
- Reworked most Editor window UIs to look cleaner and work better
- Refactored a lot of UI code for more coherency

v0.9.0
------
Expand Down

0 comments on commit 45babe9

Please sign in to comment.