Build overlay update for 1.1 GUI changes exp
Build overlay update for 1.1 GUI changes

// QuantumStrutsContinued // QuantumStrutsContinued
// //
// QuantumStrut.cs // QuantumStrut.cs
// //
// Continued from QuantumStruts by BoJaN. Used by permission. // Continued from QuantumStruts by BoJaN. Used by permission.
// //
// ModuleManager patches © 2014 K3|Chris. Used by permission. // ModuleManager patches © 2014 K3|Chris. Used by permission.
// //
// Copyright © 2014, toadicus // Copyright © 2014, toadicus
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, // Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met: // are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, // 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, // 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution. // materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
using KSP; using KSP;
using System; using System;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
namespace QuantumStrut namespace QuantumStrut
{ {
public class QuantumStrut : PartModule public class QuantumStrut : PartModule
{ {
public static Material LaserMaterial; public static Material LaserMaterial;
Strut strut = null; Strut strut = null;
GameObject lineObj; GameObject lineObj;
LineRenderer lr; LineRenderer lr;
bool Editor = false;  
int I = 0; int I = 0;
   
#region Fields #region Fields
   
[KSPField(isPersistant = true)] [KSPField(isPersistant = true)]
public bool IsEnabled = true; public bool IsEnabled = true;
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public float PowerConsumption = 0; public float PowerConsumption = 0;
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public string TransformName = ""; public string TransformName = "";
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public Vector3 Start = new Vector3(0, 0, 0); public Vector3 Start = new Vector3(0, 0, 0);
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public Vector3 Dir = new Vector3(0, 1, 0); public Vector3 Dir = new Vector3(0, 1, 0);
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public string Material = "Particles/Additive"; public string Material = "Particles/Additive";
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public Vector3 StartColor = Vector3.zero; public Vector3 StartColor = Vector3.zero;
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public Vector3 EndColor = Vector3.zero; public Vector3 EndColor = Vector3.zero;
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public float StartSize = 0.03f; public float StartSize = 0.03f;
[KSPField(isPersistant = false)] [KSPField(isPersistant = false)]
public float EndSize = 0.015f; public float EndSize = 0.015f;
   
[KSPField( [KSPField(
isPersistant = false, guiActiveEditor = true, isPersistant = false, guiActiveEditor = true,
guiName = "Max. Strut Length", guiUnits = "m", guiFormat = "F0" guiName = "Max. Strut Length", guiUnits = "m", guiFormat = "F0"
)] )]
[UI_FloatRange(minValue = 2f, maxValue = 50f, stepIncrement = 2f)] [UI_FloatRange(minValue = 2f, maxValue = 50f, stepIncrement = 2f)]
public float MaxStrutLength = 10f; public float MaxStrutLength = 10f;
   
#endregion #endregion
   
#region Actions #region Actions
   
[KSPAction("Toggle")] [KSPAction("Toggle")]
public void ToggleStrut(KSPActionParam param) public void ToggleStrut(KSPActionParam param)
{ {
IsEnabled = !IsEnabled; IsEnabled = !IsEnabled;
CheckHit(); CheckHit();
} }
   
[KSPAction("Activate")] [KSPAction("Activate")]
public void ActivateStrut(KSPActionParam param) public void ActivateStrut(KSPActionParam param)
{ {
this.ActivateStrut(); this.ActivateStrut();
} }
   
[KSPAction("Deactivate")] [KSPAction("Deactivate")]
public void DeactivateStrut(KSPActionParam param) public void DeactivateStrut(KSPActionParam param)
{ {
this.DeactivateStrut(); this.DeactivateStrut();
} }
   
#endregion #endregion
   
#region Events #region Events
   
[KSPEvent(guiActive = true, guiName = "Activate", active = true, guiActiveUnfocused = true, unfocusedRange = 2f)] [KSPEvent(guiActive = true, guiName = "Activate", active = true, guiActiveUnfocused = true, unfocusedRange = 2f)]
public void ActivateStrut() public void ActivateStrut()
{ {
IsEnabled = true; IsEnabled = true;
CheckHit(); CheckHit();
this.Events["ActivateStrut"].guiActiveEditor = false; this.Events["ActivateStrut"].guiActiveEditor = false;
this.Events["DeactivateStrut"].guiActiveEditor = true; this.Events["DeactivateStrut"].guiActiveEditor = true;
} }
   
[KSPEvent(guiActive = true, guiName = "Deactivate", active = false, guiActiveUnfocused = true, unfocusedRange = 2f)] [KSPEvent(guiActive = true, guiName = "Deactivate", active = false, guiActiveUnfocused = true, unfocusedRange = 2f)]
public void DeactivateStrut() public void DeactivateStrut()
{ {
IsEnabled = false; IsEnabled = false;
CheckHit(); CheckHit();
this.Events["ActivateStrut"].guiActiveEditor = true; this.Events["ActivateStrut"].guiActiveEditor = true;
this.Events["DeactivateStrut"].guiActiveEditor = false; this.Events["DeactivateStrut"].guiActiveEditor = false;
} }
   
#endregion #endregion
   
public Material material = null; public Material material = null;
public Color startColor = Color.white; public Color startColor = Color.white;
public Color endColor = Color.white; public Color endColor = Color.white;
   
public Color Vector3toColor(Vector3 vec) public Color Vector3toColor(Vector3 vec)
{ {
return new Color(vec.x / 255, vec.y / 255, vec.z / 255); return new Color(vec.x / 255, vec.y / 255, vec.z / 255);
} }
   
Transform getTransform() Transform getTransform()
{ {
if (TransformName == "") if (TransformName == "")
return part.transform; return part.transform;
else else
return part.FindModelTransform(TransformName); return part.FindModelTransform(TransformName);
} }
   
public void print(object body, params object[] args) public void print(object body, params object[] args)
{ {
string final = body.ToString(); string final = body.ToString();
for (int I = 0; I < args.Length; I++) for (int I = 0; I < args.Length; I++)
{ {
final = final.Replace("{" + I + "}", args[I].ToString()); final = final.Replace("{" + I + "}", args[I].ToString());
} }
MonoBehaviour.print("[AutoStrut] " + final); MonoBehaviour.print("[AutoStrut] " + final);
} }
   
public override void OnLoad(ConfigNode node) public override void OnLoad(ConfigNode node)
{ {
base.OnLoad(node); base.OnLoad(node);
} }
   
public override void OnActive() public override void OnActive()
{ {
InitLaser(); InitLaser();
   
base.OnActive(); base.OnActive();
} }
   
public override void OnInactive() public override void OnInactive()
{ {
DestroyLaser(); DestroyLaser();
   
base.OnInactive(); base.OnInactive();
} }
   
public override string GetInfo() public override string GetInfo()
{ {
return "Requires:\n- ElectricCharge (" + PowerConsumption + "/s.)\n\n Costs 5 to create strut."; return "Requires:\n- ElectricCharge (" + PowerConsumption + "/s.)\n\n Costs 5 to create strut.";
} }
   
public override void OnStart(PartModule.StartState state) public override void OnStart(PartModule.StartState state)
{ {
try try
{ {
print("Material: {0}", Material); print("Material: {0}", Material);
material = new Material(Shader.Find(Material.Trim())); material = new Material(Shader.Find(Material.Trim()));
} }
catch catch
{ {
material = null; material = null;
} }
   
base.stagingEnabled = false; base.stagingEnabled = false;
   
startColor = Vector3toColor(StartColor); startColor = Vector3toColor(StartColor);
endColor = Vector3toColor(EndColor); endColor = Vector3toColor(EndColor);
   
if (!Util.isValid(LaserMaterial)) if (!Util.isValid(LaserMaterial))
LaserMaterial = new Material(Shader.Find("Particles/Additive")); LaserMaterial = new Material(Shader.Find("Particles/Additive"));
   
if (state == StartState.Docked) switch (state)
CheckHit(); {
  case StartState.Editor:
if (state == StartState.Editor) InitLaser();
{ break;
Editor = true; case StartState.Docked:
RenderingManager.AddToPostDrawQueue(0, DrawBuildOverlay); CheckHit();
InitLaser(); DestroyLaser();
} break;
else default:
{ DestroyLaser();
Editor = false; break;
RenderingManager.RemoveFromPostDrawQueue(0, DrawBuildOverlay);  
DestroyLaser();  
} }
   
base.OnStart(state); base.OnStart(state);
} }
   
public override bool IsStageable() public override bool IsStageable()
{ {
return false; return false;
} }
   
public void FixedUpdate() public void FixedUpdate()
{ {
Events["ActivateStrut"].guiActiveEditor = Events["ActivateStrut"].active = !IsEnabled; Events["ActivateStrut"].guiActiveEditor = Events["ActivateStrut"].active = !IsEnabled;
Events["DeactivateStrut"].guiActiveEditor = Events["DeactivateStrut"].active = IsEnabled; Events["DeactivateStrut"].guiActiveEditor = Events["DeactivateStrut"].active = IsEnabled;
   
if (IsEnabled) if (IsEnabled)
{ {
I = I + 1 % 255; I = I + 1 % 255;
   
if (strut != null && !strut.isDestroyed) if (strut != null && !strut.isDestroyed)
{ {
if (PowerConsumption == 0 || (Util.GetEnergy(part.vessel) > PowerConsumption * TimeWarp.fixedDeltaTime && part.RequestResource( if (PowerConsumption == 0 || (Util.GetEnergy(part.vessel) > PowerConsumption * TimeWarp.fixedDeltaTime && part.RequestResource(
"ElectricCharge", "ElectricCharge",
PowerConsumption * TimeWarp.fixedDeltaTime PowerConsumption * TimeWarp.fixedDeltaTime
) > 0)) ) > 0))
strut.Update(); strut.Update();
else else
strut.Destroy(); strut.Destroy();
} }
else else
{ {
if ((I % 10) == 0) if ((I % 10) == 0)
{ {
CheckHit(); CheckHit();
} }
} }
} }
else else
{ {
if (strut != null) if (strut != null)
{ {
strut.Destroy(); strut.Destroy();
strut = null; strut = null;
} }
} }
   
base.OnUpdate(); base.OnUpdate();
} }
   
  public void OnGUI()
  {
  if (HighLogic.LoadedSceneIsEditor && Util.isValid(part))
  {
  this.DrawBuildOverlay();
  }
  else
  {
  DestroyLaser();
  }
  }
   
  public void OnDestroy()
  {
  DestroyLaser();
  }
   
void CheckHit() void CheckHit()
{ {
if (HighLogic.LoadedSceneIsEditor) if (HighLogic.LoadedSceneIsEditor)
{ {
Logging.PostDebugMessage(this, "Checking bailing out: in the editor!"); Logging.PostDebugMessage(this, "Checking bailing out: in the editor!");
return; return;
} }
   
if (!isEnabled) if (!isEnabled)
{ {
Logging.PostDebugMessage(this, "Destroying strut."); Logging.PostDebugMessage(this, "Destroying strut.");
   
strut.Destroy(); strut.Destroy();
strut = null; strut = null;
return; return;
} }
   
Logging.PostDebugMessage(this, "Checking for ray hit."); Logging.PostDebugMessage(this, "Checking for ray hit.");
   
Logging.PostDebugMessage(this, "Enabled, continuing."); Logging.PostDebugMessage(this, "Enabled, continuing.");
   
if (strut == null || strut.isDestroyed) if (strut == null || strut.isDestroyed)
{ {
Logging.PostDebugMessage(this, "strut is {0}", strut == null ? "null" : strut.isDestroyed.ToString()); Logging.PostDebugMessage(this, "strut is {0}", strut == null ? "null" : strut.isDestroyed.ToString());
   
Vector3 dir = getTransform().TransformDirection(Dir); Vector3 dir = getTransform().TransformDirection(Dir);
Vector3 start = getTransform().TransformPoint(Start); Vector3 start = getTransform().TransformPoint(Start);
   
Logging.PostDebugMessage(this, "Got transforms. Checking for raycast hit."); Logging.PostDebugMessage(this, "Got transforms. Checking for raycast hit.");
   
UnityEngine.RaycastHit info = new RaycastHit(); UnityEngine.RaycastHit info = new RaycastHit();
bool hit = Physics.Raycast(new UnityEngine.Ray(start + (dir * 0.05f), dir), out info, MaxStrutLength); bool hit = Physics.Raycast(new UnityEngine.Ray(start + (dir * 0.05f), dir), out info, MaxStrutLength);
   
if (hit) if (hit)
{ {
Logging.PostDebugMessage(this, "Found raycast hit. Fetching target part."); Logging.PostDebugMessage(this, "Found raycast hit. Fetching target part.");
   
Part targetPart = Util.partFromRaycast(info); Part targetPart = Util.partFromRaycast(info);
   
Logging.PostDebugMessage(this, Logging.PostDebugMessage(this,
"Found target part {0} on {1}.", "Found target part {0} on {1}.",
targetPart.partName, targetPart.partName,
targetPart.vessel == null ? "null vessel" : targetPart.vessel.vesselName targetPart.vessel == null ? "null vessel" : targetPart.vessel.vesselName
); );
   
if ( if (
targetPart && vessel.parts.Contains(targetPart) && targetPart && vessel.parts.Contains(targetPart) &&
Util.GetEnergy(part.vessel) > 5 * TimeWarp.fixedDeltaTime Util.GetEnergy(part.vessel) > 5 * TimeWarp.fixedDeltaTime
) )
{ {
Logging.PostDebugMessage(this, "Target part is in our vessel and we have the energy to continue."); Logging.PostDebugMessage(this, "Target part is in our vessel and we have the energy to continue.");
   
strut = new Strut( strut = new Strut(
part, part,
targetPart, targetPart,
targetPart.transform.InverseTransformPoint(info.point), targetPart.transform.InverseTransformPoint(info.point),
getTransform() getTransform()
); );
   
Logging.PostDebugMessage(this, "Built a new strut, setting material, colors, and sizes."); Logging.PostDebugMessage(this, "Built a new strut, setting material, colors, and sizes.");
   
strut.Material = material; strut.Material = material;
strut.StartColor = startColor; strut.StartColor = startColor;
strut.EndColor = endColor; strut.EndColor = endColor;
strut.StartSize = StartSize; strut.StartSize = StartSize;
strut.EndSize = EndSize; strut.EndSize = EndSize;
   
Logging.PostDebugMessage(this, "Strut all done!"); Logging.PostDebugMessage(this, "Strut all done!");
} }
} }
} }
} }
   
void InitLaser() void InitLaser()
{ {
if (!Util.isValid(lr)) if (!Util.isValid(lr))
{ {
lineObj = new GameObject(); lineObj = new GameObject();
   
lr = lineObj.AddComponent<LineRenderer>(); lr = lineObj.AddComponent<LineRenderer>();
lr.useWorldSpace = true; lr.useWorldSpace = true;
   
lr.material = material; lr.material = material;
lr.SetColors(startColor, endColor); lr.SetColors(startColor, endColor);
lr.SetWidth(StartSize, EndSize); lr.SetWidth(StartSize, EndSize);
   
lr.SetVertexCount(2); lr.SetVertexCount(2);
lr.SetPosition(0, Vector3.zero); lr.SetPosition(0, Vector3.zero);
lr.SetPosition(1, Vector3.zero); lr.SetPosition(1, Vector3.zero);
lr.castShadows = false; lr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
lr.receiveShadows = true; lr.receiveShadows = true;
} }
} }
   
void DestroyLaser() void DestroyLaser()
{ {
if (Util.isValid(lr)) if (Util.isValid(lr))
LineRenderer.DestroyImmediate(lr); LineRenderer.DestroyImmediate(lr);
   
if (Util.isValid(lineObj)) if (Util.isValid(lineObj))
GameObject.DestroyImmediate(lineObj); GameObject.DestroyImmediate(lineObj);
} }
   
public void DrawBuildOverlay() public void DrawBuildOverlay()
{ {
if (Util.isValid(part)) if (Util.isValid(lr))
{ {
if (!Editor) Vector3 dir = getTransform().TransformDirection(Dir);
return; Vector3 start = getTransform().TransformPoint(Start);
   
if (Util.isValid(lr)) UnityEngine.RaycastHit info = new RaycastHit();
{ bool hit = Physics.Raycast(new UnityEngine.Ray(start + (dir * 0.05f), dir), out info, MaxStrutLength);
Vector3 dir = getTransform().TransformDirection(Dir); if (hit && IsEnabled)
Vector3 start = getTransform().TransformPoint(Start); {
  if (Util.isValid(material))
UnityEngine.RaycastHit info = new RaycastHit(); lr.material = material;
bool hit = Physics.Raycast(new UnityEngine.Ray(start + (dir * 0.05f), dir), out info, MaxStrutLength);  
if (hit && IsEnabled) lr.SetColors(startColor, endColor);
{ lr.SetWidth(StartSize, EndSize);
if (Util.isValid(material))  
lr.material = material; lr.SetPosition(0, start);
  lr.SetPosition(1, info.point);
lr.SetColors(startColor, endColor); }
lr.SetWidth(StartSize, EndSize); else
  {
lr.SetPosition(0, start); lr.material = LaserMaterial;
lr.SetPosition(1, info.point); lr.SetColors(Color.red, Color.red);
} lr.SetWidth(0.01f, 0.01f);
else  
{ lr.SetPosition(0, start);
lr.material = LaserMaterial; lr.SetPosition(1, start + (dir * MaxStrutLength));
lr.SetColors(Color.red, Color.red); }
lr.SetWidth(0.01f, 0.01f);  
   
lr.SetPosition(0, start);  
lr.SetPosition(1, start + (dir * MaxStrutLength));  
}  
}  
}  
else  
{  
DestroyLaser();  
RenderingManager.RemoveFromPostDrawQueue(0, DrawBuildOverlay);  
} }
} }
} }
} }