Relicensed to Modified BSD.
[VOID.git] / VOID_EditorHUD.cs
blob:a/VOID_EditorHUD.cs -> blob:b/VOID_EditorHUD.cs
// VOID // VOID
// //
// VOID_EditorHUD.cs // VOID_EditorHUD.cs
// //
// 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.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be used // 3. Neither the name of the copyright holder nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission. // to endorse or promote products derived from this software without specific prior written permission.
// //
// 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 Engineer.VesselSimulator; using KerbalEngineer.VesselSimulator;
using KSP; using KSP;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;  
using System.Text; using System.Text;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
namespace VOID namespace VOID
{ {
public class VOID_EditorHUD : VOID_Module, IVOID_EditorModule [VOID_Scenes(GameScenes.EDITOR)]
  public class VOID_EditorHUD : VOID_HUDModule
{ {
/* /*
* Fields * Fields
* */ * */
[AVOID_SaveValue("colorIndex")] protected HUDWindow ehudWindow;
protected VOID_SaveValue<int> _colorIndex = 0;  
   
protected List<Color> textColors = new List<Color>();  
   
protected GUIStyle labelStyle;  
   
protected EditorVesselOverlays _vesselOverlays; protected EditorVesselOverlays _vesselOverlays;
   
  [AVOID_SaveValue("snapToLeft")]
  protected VOID_SaveValue<bool> snapToLeft;
   
/* /*
* Properties * Properties
* */ * */
public int ColorIndex  
{  
get  
{  
return this._colorIndex;  
}  
set  
{  
if (this._colorIndex >= this.textColors.Count - 1)  
{  
this._colorIndex = 0;  
return;  
}  
   
this._colorIndex = value;  
}  
}  
   
protected EditorVesselOverlays vesselOverlays protected EditorVesselOverlays vesselOverlays
{ {
get get
{ {
if (this._vesselOverlays == null) if (this._vesselOverlays == null)
{ {
this._vesselOverlays = (EditorVesselOverlays)Resources UnityEngine.Object[] overlayObjs = Resources.FindObjectsOfTypeAll(typeof(EditorVesselOverlays));
.FindObjectsOfTypeAll(typeof(EditorVesselOverlays))  
.FirstOrDefault(); if (overlayObjs.Length > 0)
  {
  this._vesselOverlays = (EditorVesselOverlays)overlayObjs[0];
  }
} }
   
return this._vesselOverlays; return this._vesselOverlays;
} }
} }
   
protected EditorMarker_CoM CoMmarker protected EditorMarker_CoM CoMmarker
{ {
get get
{ {
if (this.vesselOverlays == null) if (this.vesselOverlays == null)
{ {
return null; return null;
} }
   
return this.vesselOverlays.CoMmarker; return this.vesselOverlays.CoMmarker;
} }
} }
   
protected EditorMarker_CoT CoTmarker protected EditorMarker_CoT CoTmarker
{ {
get get
{ {
if (this.vesselOverlays == null) if (this.vesselOverlays == null)
{ {
return null; return null;
} }
   
return this.vesselOverlays.CoTmarker; return this.vesselOverlays.CoTmarker;
} }
} }
   
/* /*
* Methods * Methods
* */ * */
public VOID_EditorHUD() : base() public VOID_EditorHUD() : base()
{ {
this._Name = "Heads-Up Display"; this.Name = "Heads-Up Display";
   
this._Active.value = true; this.Active = true;
   
this.textColors.Add(Color.green); this.snapToLeft.value = true;
this.textColors.Add(Color.black);  
this.textColors.Add(Color.white); this.ehudWindow = new HUDWindow(
this.textColors.Add(Color.red); "editorHUD",
this.textColors.Add(Color.blue); this.ehudWindowFunc,
this.textColors.Add(Color.yellow); new Rect(EditorPanels.Instance.partsPanelWidth + 10f, 125f, 300f, 64f)
this.textColors.Add(Color.gray); );
this.textColors.Add(Color.cyan); this.Windows.Add(this.ehudWindow);
this.textColors.Add(Color.magenta);  
   
this.labelStyle = new GUIStyle ();  
// this.labelStyle.alignment = TextAnchor.UpperRight;  
this.labelStyle.normal.textColor = this.textColors [this.ColorIndex];  
   
Tools.PostDebugMessage (this.GetType().Name + ": Constructed."); Tools.PostDebugMessage (this.GetType().Name + ": Constructed.");
} }
   
public override void DrawGUI() public void ehudWindowFunc(int id)
{ {
SimManager.RequestSimulation(); StringBuilder hudString = Tools.GetStringBuilder();
   
if (SimManager.LastStage == null) if (this.core.LastStage == null)
{ {
return; return;
} }
   
float hudLeft; VOID_Styles.labelHud.alignment = TextAnchor.UpperLeft;
StringBuilder hudString;  
   
if (EditorLogic.fetch.editorScreen == EditorLogic.EditorScreen.Parts)  
{  
hudLeft = EditorPanels.Instance.partsPanelWidth + 10;  
}  
else if (EditorLogic.fetch.editorScreen == EditorLogic.EditorScreen.Actions)  
{  
hudLeft = EditorPanels.Instance.actionsPanelWidth + 10;  
}  
else  
{  
return;  
}  
   
Rect hudPos = new Rect (hudLeft, 48, 300, 32);  
   
hudString = new StringBuilder();  
   
// GUI.skin = AssetBase.GetGUISkin("KSP window 2");  
   
labelStyle.normal.textColor = textColors [ColorIndex];  
   
hudString.Append("Total Mass: "); hudString.Append("Total Mass: ");
hudString.Append(SimManager.LastStage.totalMass.ToString("F3")); hudString.Append(this.core.LastStage.totalMass.ToString("F3"));
hudString.Append('t'); hudString.Append('t');
   
hudString.Append(' '); hudString.Append(' ');
   
hudString.Append("Part Count: "); hudString.Append("Part Count: ");
hudString.Append(EditorLogic.SortedShipList.Count); hudString.Append(EditorLogic.SortedShipList.Count);
   
hudString.Append('\n'); hudString.Append('\n');
   
hudString.Append("Total Delta-V: "); hudString.Append("Total Delta-V: ");
hudString.Append(Tools.MuMech_ToSI(SimManager.LastStage.totalDeltaV)); hudString.Append(Tools.MuMech_ToSI(this.core.LastStage.totalDeltaV));
hudString.Append("m/s"); hudString.Append("m/s");
   
hudString.Append('\n'); hudString.Append('\n');
   
hudString.Append("Bottom Stage Delta-V"); hudString.Append("Bottom Stage Delta-V");
hudString.Append(Tools.MuMech_ToSI(SimManager.LastStage.deltaV)); hudString.Append(Tools.MuMech_ToSI(this.core.LastStage.deltaV));
hudString.Append("m/s"); hudString.Append("m/s");
   
hudString.Append('\n'); hudString.Append('\n');
   
hudString.Append("Bottom Stage T/W Ratio: "); hudString.Append("Bottom Stage T/W Ratio: ");
hudString.Append(SimManager.LastStage.thrustToWeight.ToString("F3")); hudString.Append(this.core.LastStage.thrustToWeight.ToString("F3"));
   
  Tools.PostDebugMessage(this,
  "CoMmarker.gameObject.activeInHierarchy: {0};" +
  "CoTmarker.gameObject.activeInHierarchy: {1}",
  this.CoMmarker.gameObject.activeInHierarchy,
  this.CoTmarker.gameObject.activeInHierarchy
  );
   
if (this.CoMmarker.gameObject.activeInHierarchy && this.CoTmarker.gameObject.activeInHierarchy) if (this.CoMmarker.gameObject.activeInHierarchy && this.CoTmarker.gameObject.activeInHierarchy)
{ {
  Tools.PostDebugMessage(this, "CoM and CoT markers are active, doing thrust offset.");
hudString.Append('\n'); hudString.Append('\n');
   
hudString.Append("Thrust Offset: "); hudString.Append("Thrust Offset: ");
hudString.Append( hudString.Append(
Vector3.Cross( Vector3.Cross(
this.CoTmarker.dirMarkerObject.transform.forward, this.CoTmarker.dirMarkerObject.transform.forward,
this.CoMmarker.posMarkerObject.transform.position - this.CoTmarker.posMarkerObject.transform.position this.CoMmarker.posMarkerObject.transform.position - this.CoTmarker.posMarkerObject.transform.position
).ToString("F3")); ).ToString("F3"));
} }
  #if DEBUG
GUI.Label ( else
hudPos, {
  Tools.PostDebugMessage(this, "CoM and CoT markers are not active, thrust offset skipped.");
  }
  #endif
   
  GUILayout.Label(
hudString.ToString(), hudString.ToString(),
labelStyle); VOID_Styles.labelHud,
} GUILayout.ExpandWidth(true),
  GUILayout.ExpandHeight(true)
public override void DrawConfigurables() );
{  
if (GUILayout.Button ("Change HUD color", GUILayout.ExpandWidth (false))) if (!this.positionsLocked)
{ {
++this.ColorIndex; GUI.DragWindow();
} }
   
  GUI.BringWindowToBack(id);
   
  Tools.PutStringBuilder(hudString);
  }
   
  public override void DrawGUI()
  {
  float hudLeft;
   
  if (EditorLogic.fetch.editorScreen == EditorScreen.Parts)
  {
  hudLeft = EditorPanels.Instance.partsPanelWidth + 10f;
  hudLeft += EditorPartList.Instance.transformTopLeft.position.x -
  EditorPartList.Instance.transformTopLeft.parent.parent.position.x -
  72f;
  }
  else if (EditorLogic.fetch.editorScreen == EditorScreen.Actions)
  {
  hudLeft = EditorPanels.Instance.actionsPanelWidth + 10f;
  }
  else
  {
  return;
  }
   
  Tools.PostDebugMessage(this,
  "EditorPartList topLeft.parent.parent.position: {0}\n" +
  "EditorPartList topLeft.parent.position: {1}\n" +
  "EditorPartList topLeft.position: {2}\n" +
  "snapToEdge: {3} (pos.Xmin: {4}; hudLeft: {5})",
  EditorPartList.Instance.transformTopLeft.parent.parent.position,
  EditorPartList.Instance.transformTopLeft.parent.position,
  EditorPartList.Instance.transformTopLeft.position,
  this.snapToLeft, this.ehudWindow.WindowPos.xMin, hudLeft
  );
   
  base.DrawGUI();
   
  Rect hudPos = this.ehudWindow.WindowPos;
   
  if (this.snapToLeft && this.positionsLocked)
  {
  hudPos.xMin = hudLeft;
  }
  else
  {
  hudPos.xMin = Mathf.Max(hudLeft, hudPos.xMin);
  }
   
  hudPos.width = this.ehudWindow.defaultWindowPos.width;
   
  this.ehudWindow.WindowPos = hudPos;
   
  this.snapToLeft.value = Mathf.Abs(this.ehudWindow.WindowPos.xMin - hudLeft) < 15f;
} }
} }
} }