General cleanup of SimManager start-up and calling.
[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 KerbalEngineer.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
{ {
[VOID_Scenes(GameScenes.EDITOR)] [VOID_Scenes(GameScenes.EDITOR)]
public class VOID_EditorHUD : VOID_HUDModule public class VOID_EditorHUD : VOID_HUDModule
{ {
/* /*
* Fields * Fields
* */ * */
protected HUDWindow ehudWindow; protected HUDWindow ehudWindow;
protected EditorVesselOverlays _vesselOverlays; protected EditorVesselOverlays _vesselOverlays;
   
[AVOID_SaveValue("snapToLeft")] [AVOID_SaveValue("snapToLeft")]
protected VOID_SaveValue<bool> snapToLeft; protected VOID_SaveValue<bool> snapToLeft;
   
/* /*
* Properties * Properties
* */ * */
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 = true; this.Active = true;
   
this.snapToLeft.value = true; this.snapToLeft.value = true;
   
this.ehudWindow = new HUDWindow( this.ehudWindow = new HUDWindow(
"editorHUD", "editorHUD",
this.ehudWindowFunc, this.ehudWindowFunc,
new Rect(EditorPanels.Instance.partsPanelWidth + 10f, 125f, 300f, 64f) new Rect(EditorPanels.Instance.partsPanelWidth + 10f, 125f, 300f, 64f)
); );
this.Windows.Add(this.ehudWindow); this.Windows.Add(this.ehudWindow);
   
Tools.PostDebugMessage (this.GetType().Name + ": Constructed."); Tools.PostDebugMessage (this.GetType().Name + ": Constructed.");
} }
   
public void ehudWindowFunc(int id) public void ehudWindowFunc(int id)
{ {
StringBuilder hudString = new StringBuilder(); StringBuilder hudString = Tools.GetStringBuilder();
   
if (this.core.LastStage == null) if (this.core.LastStage == null)
{ {
return; return;
} }
   
VOID_Styles.labelHud.alignment = TextAnchor.UpperLeft; VOID_Styles.labelHud.alignment = TextAnchor.UpperLeft;
   
hudString.Append("Total Mass: "); hudString.Append("Total Mass: ");
hudString.Append(this.core.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(this.core.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(this.core.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(this.core.LastStage.thrustToWeight.ToString("F3")); hudString.Append(this.core.LastStage.thrustToWeight.ToString("F3"));
   
Tools.PostDebugMessage(this, Tools.PostDebugMessage(this,
"CoMmarker.gameObject.activeInHierarchy: {0};" + "CoMmarker.gameObject.activeInHierarchy: {0};" +
"CoTmarker.gameObject.activeInHierarchy: {1}", "CoTmarker.gameObject.activeInHierarchy: {1}",
this.CoMmarker.gameObject.activeInHierarchy, this.CoMmarker.gameObject.activeInHierarchy,
this.CoTmarker.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."); 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 #if DEBUG
else else
{ {
Tools.PostDebugMessage(this, "CoM and CoT markers are not active, thrust offset skipped."); Tools.PostDebugMessage(this, "CoM and CoT markers are not active, thrust offset skipped.");
} }
#endif #endif
   
GUILayout.Label( GUILayout.Label(
hudString.ToString(), hudString.ToString(),
VOID_Styles.labelHud, VOID_Styles.labelHud,
GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true),
GUILayout.ExpandHeight(true) GUILayout.ExpandHeight(true)
); );
   
if (!this.positionsLocked) if (!this.positionsLocked)
{ {
GUI.DragWindow(); GUI.DragWindow();
} }
   
GUI.BringWindowToBack(id); GUI.BringWindowToBack(id);
   
  Tools.PutStringBuilder(hudString);
} }
   
public override void DrawGUI() public override void DrawGUI()
{ {
float hudLeft; float hudLeft;
   
if (EditorLogic.fetch.editorScreen == EditorScreen.Parts) if (EditorLogic.fetch.editorScreen == EditorScreen.Parts)
{ {
hudLeft = EditorPanels.Instance.partsPanelWidth + 10f; hudLeft = EditorPanels.Instance.partsPanelWidth + 10f;
hudLeft += EditorPartList.Instance.transformTopLeft.position.x - hudLeft += EditorPartList.Instance.transformTopLeft.position.x -
EditorPartList.Instance.transformTopLeft.parent.parent.position.x - EditorPartList.Instance.transformTopLeft.parent.parent.position.x -
72f; 72f;
} }
else if (EditorLogic.fetch.editorScreen == EditorScreen.Actions) else if (EditorLogic.fetch.editorScreen == EditorScreen.Actions)
{ {
hudLeft = EditorPanels.Instance.actionsPanelWidth + 10f; hudLeft = EditorPanels.Instance.actionsPanelWidth + 10f;
} }
else else
{ {
return; return;
} }
   
Tools.PostDebugMessage(this, Tools.PostDebugMessage(this,
"EditorPartList topLeft.parent.parent.position: {0}\n" + "EditorPartList topLeft.parent.parent.position: {0}\n" +
"EditorPartList topLeft.parent.position: {1}\n" + "EditorPartList topLeft.parent.position: {1}\n" +
"EditorPartList topLeft.position: {2}\n" + "EditorPartList topLeft.position: {2}\n" +
"snapToEdge: {3} (pos.Xmin: {4}; hudLeft: {5})", "snapToEdge: {3} (pos.Xmin: {4}; hudLeft: {5})",
EditorPartList.Instance.transformTopLeft.parent.parent.position, EditorPartList.Instance.transformTopLeft.parent.parent.position,
EditorPartList.Instance.transformTopLeft.parent.position, EditorPartList.Instance.transformTopLeft.parent.position,
EditorPartList.Instance.transformTopLeft.position, EditorPartList.Instance.transformTopLeft.position,
this.snapToLeft, this.ehudWindow.WindowPos.xMin, hudLeft this.snapToLeft, this.ehudWindow.WindowPos.xMin, hudLeft
); );
   
base.DrawGUI(); base.DrawGUI();
   
Rect hudPos = this.ehudWindow.WindowPos; Rect hudPos = this.ehudWindow.WindowPos;
   
if (this.snapToLeft && this.positionsLocked) if (this.snapToLeft && this.positionsLocked)
{ {
hudPos.xMin = hudLeft; hudPos.xMin = hudLeft;
} }
else else
{ {
hudPos.xMin = Mathf.Max(hudLeft, hudPos.xMin); hudPos.xMin = Mathf.Max(hudLeft, hudPos.xMin);
} }
   
hudPos.width = this.ehudWindow.defaultWindowPos.width; hudPos.width = this.ehudWindow.defaultWindowPos.width;
   
this.ehudWindow.WindowPos = hudPos; this.ehudWindow.WindowPos = hudPos;
   
this.snapToLeft.value = Mathf.Abs(this.ehudWindow.WindowPos.xMin - hudLeft) < 15f; this.snapToLeft.value = Mathf.Abs(this.ehudWindow.WindowPos.xMin - hudLeft) < 15f;
} }
} }
} }