Skip to content

Commit

Permalink
Merge with master and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Fruh committed Apr 22, 2016
1 parent e1e64c6 commit c4252f8
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 56 deletions.
69 changes: 69 additions & 0 deletions KSP-LogiRGB/KSP-LogiRGB/Animations/AnimationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using KSP_LogiRGB.ColorSchemes;

namespace KSP_LogiRGB
{
/// <summary>
/// Handles all animations to avoid confusion and multiple animations running at the same time.
/// </summary>
internal class AnimationManager
{
/// <summary>
/// Singleton instance
/// </summary>
private static AnimationManager instance;

/// <summary>
/// The currently running animation or null
/// </summary>
private KeyboardAnimation activeAnimation;

/// <summary>
/// Private constructor to avoid out of singleton instantiation
/// </summary>
private AnimationManager()
{
}

/// <summary>
/// Instance getter
/// </summary>
public static AnimationManager Instance
{
get
{
if (instance == null)
{
instance = new AnimationManager();
}
return instance;
}
}

/// <summary>
/// Set the current animation
/// </summary>
/// <param name="animation">the animation to display</param>
public void setAnimation(KeyboardAnimation animation)
{
activeAnimation = animation;
}

/// <summary>
/// Fetches one frame from the animation
/// </summary>
/// <returns>the current animation frame</returns>
public ColorScheme getFrame()
{
return animationRunning() ? activeAnimation.getFrame() : new ColorScheme();
}

/// <summary>
/// Checks if there is still an animation running.
/// </summary>
/// <returns></returns>
public bool animationRunning()
{
return activeAnimation != null && !activeAnimation.isFinished();
}
}
}
22 changes: 22 additions & 0 deletions KSP-LogiRGB/KSP-LogiRGB/Animations/KeyboardAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using KSP_LogiRGB.ColorSchemes;

namespace KSP_LogiRGB
{
/// <summary>
/// Implement this to create an animation on your keyboard.
/// </summary>
public interface KeyboardAnimation
{
/// <summary>
/// Returns the current animation frame.
/// </summary>
/// <returns>the current animation frame.</returns>
ColorScheme getFrame();

/// <summary>
/// Checks if the animation is complete.
/// </summary>
/// <returns>true, if the animation is finished.</returns>
bool isFinished();
}
}
71 changes: 71 additions & 0 deletions KSP-LogiRGB/KSP-LogiRGB/Animations/PowerLostAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Linq;
using KSP_LogiRGB.ColorSchemes;
using UnityEngine;

namespace KSP_LogiRGB
{
/// <summary>
/// Displays a warning on the keyboard, indicating that the vessel is currently out of power and cannot
/// be controlled. Consists of two frames alternating at 1fps.
/// </summary>
internal class PowerLostAnimation : KeyboardAnimation
{
/// <summary>
/// The red frame
/// </summary>
private static readonly ColorScheme red = new ColorScheme(Color.red);

/// <summary>
/// The blue frame
/// </summary>
private static readonly ColorScheme blue = new ColorScheme(Color.blue);

/// <summary>
/// Static constructor adds lightning bolts in different colors to both frames
/// </summary>
static PowerLostAnimation()
{
KeyCode[] lightningKeys =
{
/// Left lightning
KeyCode.F2, KeyCode.Alpha3, KeyCode.W, KeyCode.E, KeyCode.R, KeyCode.D, KeyCode.X, KeyCode.LeftAlt,

/// Right lightning
KeyCode.F9, KeyCode.Alpha0, KeyCode.O, KeyCode.P, KeyCode.LeftBracket, KeyCode.Semicolon, KeyCode.Period,
KeyCode.RightAlt
};

blue.SetKeysToColor(lightningKeys, Color.white);
red.SetKeysToColor(lightningKeys, Color.blue);
}

/// <summary>
/// <see cref="KeyboardAnimation.getFrame" />
/// </summary>
/// <returns>the current animation frame.</returns>
public ColorScheme getFrame()
{
return (int) Time.realtimeSinceStartup%2 == 0 ? red : blue;
}

/// <summary>
/// <see cref="KeyboardAnimation.isFinished" />
/// </summary>
/// <returns>true, if the animation is finished, false if not.</returns>
public bool isFinished()
{
/// Exit if the scene changes.
if (HighLogic.LoadedScene != GameScenes.FLIGHT)
return true;

/// Check if the vessel needs power, and if empty, continue blinking
var resource = FlightGlobals.ActiveVessel.GetActiveResources()
.Where(res => res.info.name.Equals("ElectricCharge"));
if (resource.Count() > 0)
return resource.First().amount > 0.0001;

/// No energy stored on the ship, end the animation.
return true;
}
}
}
32 changes: 15 additions & 17 deletions KSP-LogiRGB/KSP-LogiRGB/ColorSchemes/FlightScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ internal class FlightScheme : ColorScheme
/// </summary>
public FlightScheme()
{
KeyCode[] whitekeys = {KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.Q, KeyCode.E, KeyCode.Space};
SetKeysToColor(whitekeys, Color.green);

KeyCode[] yellowkeys = {KeyCode.H, KeyCode.N, KeyCode.J, KeyCode.K, KeyCode.L, KeyCode.I};
KeyCode[] yellowkeys =
{
GameSettings.TRANSLATE_BACK.primary,
GameSettings.TRANSLATE_FWD.primary,
GameSettings.TRANSLATE_LEFT.primary,
GameSettings.TRANSLATE_RIGHT.primary,
GameSettings.TRANSLATE_UP.primary,
GameSettings.TRANSLATE_DOWN.primary
};
SetKeysToColor(yellowkeys, Color.yellow);

KeyCode[] redkeys = {KeyCode.LeftShift, KeyCode.LeftControl, KeyCode.X, KeyCode.Z};
SetKeysToColor(redkeys, Color.red);

KeyCode[] cyankeys = {KeyCode.L, KeyCode.R, KeyCode.B, KeyCode.LeftAlt};
SetKeysToColor(cyankeys, Color.cyan);

KeyCode[] greenkeys = {KeyCode.R, KeyCode.T, KeyCode.F, KeyCode.B};
SetKeysToColor(greenkeys, Color.green);

KeyCode[] bluekeys =
KeyCode[] redkeys =
{
KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4,
KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9,
KeyCode.Alpha0
GameSettings.THROTTLE_FULL.primary, GameSettings.THROTTLE_CUTOFF.primary,
GameSettings.THROTTLE_UP.primary, GameSettings.THROTTLE_DOWN.primary
};
SetKeysToColor(redkeys, Color.red);

KeyCode[] bluekeys = {GameSettings.FOCUS_NEXT_VESSEL.primary, GameSettings.FOCUS_PREV_VESSEL.primary};
SetKeysToColor(bluekeys, Color.blue);
}
}
Expand Down
8 changes: 6 additions & 2 deletions KSP-LogiRGB/KSP-LogiRGB/ColorSchemes/VabScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ public VabScheme()
{
SetKeysToColor(new[]
{
KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.Q, KeyCode.E
GameSettings.Editor_pitchUp.primary, GameSettings.Editor_pitchDown.primary,
GameSettings.Editor_rollLeft.primary, GameSettings.Editor_rollRight.primary,
GameSettings.Editor_yawLeft.primary, GameSettings.Editor_yawRight.primary
}, new Color(1f, 1f, 0f));
SetKeysToColor(new[]
{
KeyCode.LeftShift, KeyCode.Space, KeyCode.F
GameSettings.Editor_fineTweak.primary,
GameSettings.Editor_resetRotation.primary,
GameSettings.Editor_coordSystem.primary
}, Color.magenta);
}
}
Expand Down
3 changes: 3 additions & 0 deletions KSP-LogiRGB/KSP-LogiRGB/DataDrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace KSP_LogiRGB
{
/// <summary>
/// Implement this to use the mod with other devices.
/// </summary>
internal interface DataDrain
{
/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions KSP-LogiRGB/KSP-LogiRGB/KSP-LogiRGB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@
<Reference Include="UnityEngine">
<HintPath>D:\Spiele\Steam\steamapps\common\Kerbal Space Program\KSP_x64_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Spiele\Steam\steamapps\common\Kerbal Space Program\KSP_x64_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Animations\AnimationManager.cs" />
<Compile Include="Animations\KeyboardAnimation.cs" />
<Compile Include="Animations\PowerLostAnimation.cs" />
<Compile Include="ColorSchemes\ColorScheme.cs" />
<Compile Include="ColorSchemes\EVAScheme.cs" />
<Compile Include="ColorSchemes\FlightScheme.cs" />
Expand Down
38 changes: 26 additions & 12 deletions KSP-LogiRGB/KSP-LogiRGB/KSPLogiRGBPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,52 @@ namespace KSP_LogiRGB
/// uses.
/// </summary>
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class KSPLogiRGBPlugin : MonoBehaviour
public class KSPChromaPlugin : MonoBehaviour
{
public static KSPChromaPlugin fetch;
private readonly List<DataDrain> dataDrains = new List<DataDrain>();

/// <summary>
/// The UDP network socket to send keyboard appearance orders to the server.
/// </summary>
private readonly SceneManager flightSceneManager = new FlightSceneManager();

private readonly SceneManager vabSceneManager = new VABSceneManager();

/// <summary>
/// Called by unity during the launch of this addon.
/// </summary>
private void Awake()
{
fetch = this;
dataDrains.Add(new LogitechDrain());
}

/// <summary>
/// Called by unity on every physics frame.
/// </summary>
private void FixedUpdate()
private void Update()
{
ColorScheme scheme;

switch (HighLogic.LoadedScene)
if (AnimationManager.Instance.animationRunning())
{
scheme = AnimationManager.Instance.getFrame();
}
else
{
case GameScenes.FLIGHT:
scheme = flightSceneManager.getScheme();
break;
case GameScenes.EDITOR:
scheme = vabSceneManager.getScheme();
break;
default:
scheme = new LogoScheme();
break;
switch (HighLogic.LoadedScene)
{
case GameScenes.FLIGHT:
scheme = flightSceneManager.getScheme();
break;
case GameScenes.EDITOR:
scheme = vabSceneManager.getScheme();
break;
default:
scheme = new LogoScheme();
break;
}
}

dataDrains.ForEach(drain => drain.send(scheme));
Expand Down
2 changes: 1 addition & 1 deletion KSP-LogiRGB/KSP-LogiRGB/LogitechDrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void send(ColorScheme scheme)
~LogitechDrain()
{
// Not strictly necessary, frees up memory, but causes keys blinking on scene switches
LogitechGSDK.LogiLedShutdown();
//LogitechGSDK.LogiLedShutdown(); // Causes functions to stop working after a while
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions KSP-LogiRGB/KSP-LogiRGB/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.

[assembly: AssemblyTitle("KSP-LogiRGB")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
Expand All @@ -17,9 +17,11 @@
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.

[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird

[assembly: Guid("ad5d4913-01df-4771-827a-16937f3fd115")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
Expand All @@ -32,5 +34,6 @@
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit c4252f8

Please sign in to comment.