Changed windowId to object hash.
[VesselSimulator.git] / KerbalEngineer / Flight / Sections / SectionEditor.cs
blob:a/KerbalEngineer/Flight/Sections/SectionEditor.cs -> blob:b/KerbalEngineer/Flight/Sections/SectionEditor.cs
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
#region Using Directives #region Using Directives
using System; using System;
using KerbalEngineer.Extensions; using KerbalEngineer.Extensions;
using KerbalEngineer.Flight.Readouts; using KerbalEngineer.Flight.Readouts;
using UnityEngine; using UnityEngine;
#endregion #endregion
namespace KerbalEngineer.Flight.Sections namespace KerbalEngineer.Flight.Sections
{ {
public class SectionEditor : MonoBehaviour public class SectionEditor : MonoBehaviour
{ {
#region Constants #region Constants
public const float Width = 500.0f; public const float Width = 500.0f;
public const float Height = 500.0f; public const float Height = 500.0f;
#endregion #endregion
#region Fields #region Fields
private readonly int windowId = new Guid().GetHashCode(); private int windowId;
   
private Vector2 scrollPositionAvailable; private Vector2 scrollPositionAvailable;
private Vector2 scrollPositionInstalled; private Vector2 scrollPositionInstalled;
private ReadoutCategory selectedCategory = ReadoutCategory.Orbital; private ReadoutCategory selectedCategory = ReadoutCategory.Orbital;
private Rect windowPosition; private Rect windowPosition;
#endregion #endregion
#region Constructors #region Constructors
/// <summary> /// <summary>
/// Initialises the object's state on creation. /// Initialises the object's state on creation.
/// </summary> /// </summary>
private void Start() private void Start()
{ {
  this.windowId = this.GetHashCode();
this.InitialiseStyles(); this.InitialiseStyles();
RenderingManager.AddToPostDrawQueue(0, this.Draw); RenderingManager.AddToPostDrawQueue(0, this.Draw);
} }
#endregion #endregion
#region Properties #region Properties
/// <summary> /// <summary>
/// Gets and sets the parent section for the section editor. /// Gets and sets the parent section for the section editor.
/// </summary> /// </summary>
public SectionModule ParentSection { get; set; } public SectionModule ParentSection { get; set; }
/// <summary> /// <summary>
/// Gets and sets the window position. /// Gets and sets the window position.
/// </summary> /// </summary>
public Rect WindowPosition public Rect WindowPosition
{ {
get { return this.windowPosition; } get { return this.windowPosition; }
set { this.windowPosition = value; } set { this.windowPosition = value; }
} }
#endregion #endregion
#region GUIStyles #region GUIStyles
private GUIStyle categoryButtonStyle; private GUIStyle categoryButtonStyle;
private GUIStyle helpBoxStyle; private GUIStyle helpBoxStyle;
private GUIStyle helpTextStyle; private GUIStyle helpTextStyle;
private GUIStyle panelTitleStyle; private GUIStyle panelTitleStyle;
private GUIStyle readoutButtonStyle; private GUIStyle readoutButtonStyle;
private GUIStyle readoutNameStyle; private GUIStyle readoutNameStyle;
private GUIStyle textStyle; private GUIStyle textStyle;
private GUIStyle windowStyle; private GUIStyle windowStyle;
/// <summary> /// <summary>
/// Initialises all the styles required for this object. /// Initialises all the styles required for this object.
/// </summary> /// </summary>
private void InitialiseStyles() private void InitialiseStyles()
{ {
this.windowStyle = new GUIStyle(HighLogic.Skin.window); this.windowStyle = new GUIStyle(HighLogic.Skin.window);
this.categoryButtonStyle = new GUIStyle(HighLogic.Skin.button) this.categoryButtonStyle = new GUIStyle(HighLogic.Skin.button)
{ {
normal = normal =
{ {
textColor = Color.white textColor = Color.white
}, },
alignment = TextAnchor.MiddleCenter, alignment = TextAnchor.MiddleCenter,
fontSize = 12, fontSize = 12,
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
fixedHeight = 30.0f, fixedHeight = 30.0f,
}; };
this.panelTitleStyle = new GUIStyle(HighLogic.Skin.label) this.panelTitleStyle = new GUIStyle(HighLogic.Skin.label)
{ {
normal = normal =
{ {
textColor = Color.white textColor = Color.white
}, },
margin = new RectOffset(), margin = new RectOffset(),
padding = new RectOffset(), padding = new RectOffset(),
alignment = TextAnchor.MiddleLeft, alignment = TextAnchor.MiddleLeft,
fontSize = 12, fontSize = 12,
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
fixedHeight = 30.0f, fixedHeight = 30.0f,
stretchWidth = true stretchWidth = true
}; };
this.textStyle = new GUIStyle(HighLogic.Skin.textField) this.textStyle = new GUIStyle(HighLogic.Skin.textField)
{ {
margin = new RectOffset(3, 3, 3, 3), margin = new RectOffset(3, 3, 3, 3),
alignment = TextAnchor.MiddleLeft, alignment = TextAnchor.MiddleLeft,
stretchWidth = true, stretchWidth = true,
stretchHeight = true stretchHeight = true
}; };
this.readoutNameStyle = new GUIStyle(HighLogic.Skin.label) this.readoutNameStyle = new GUIStyle(HighLogic.Skin.label)
{ {
normal = normal =
{ {
textColor = Color.white textColor = Color.white
}, },
margin = new RectOffset(), margin = new RectOffset(),
padding = new RectOffset(10, 0, 0, 0), padding = new RectOffset(10, 0, 0, 0),
alignment = TextAnchor.MiddleLeft, alignment = TextAnchor.MiddleLeft,
fontSize = 12, fontSize = 12,
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
stretchWidth = true, stretchWidth = true,
stretchHeight = true stretchHeight = true
}; };
this.readoutButtonStyle = new GUIStyle(HighLogic.Skin.button) this.readoutButtonStyle = new GUIStyle(HighLogic.Skin.button)
{ {
normal = normal =
{ {
textColor = Color.white textColor = Color.white
}, },
margin = new RectOffset(2, 2, 2, 2), margin = new RectOffset(2, 2, 2, 2),
padding = new RectOffset(), padding = new RectOffset(),
alignment = TextAnchor.MiddleCenter, alignment = TextAnchor.MiddleCenter,
fontSize = 12, fontSize = 12,
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
stretchHeight = true stretchHeight = true
}; };
this.helpBoxStyle = new GUIStyle(HighLogic.Skin.box) this.helpBoxStyle = new GUIStyle(HighLogic.Skin.box)
{ {
margin = new RectOffset(2, 2, 2, 10), margin = new RectOffset(2, 2, 2, 10),
padding = new RectOffset(10, 10, 10, 10) padding = new RectOffset(10, 10, 10, 10)
}; };
this.helpTextStyle = new GUIStyle(HighLogic.Skin.label) this.helpTextStyle = new GUIStyle(HighLogic.Skin.label)
{ {
normal = normal =
{ {
textColor = Color.yellow textColor = Color.yellow
}, },
margin = new RectOffset(), margin = new RectOffset(),
padding = new RectOffset(), padding = new RectOffset(),
alignment = TextAnchor.MiddleLeft, alignment = TextAnchor.MiddleLeft,
fontSize = 13, fontSize = 13,
fontStyle = FontStyle.Normal, fontStyle = FontStyle.Normal,
stretchWidth = true, stretchWidth = true,
richText = true richText = true
}; };
} }
#endregion #endregion
#region Drawing #region Drawing
/// <summary> /// <summary>
/// Called to draw the editor when the UI is enabled. /// Called to draw the editor when the UI is enabled.
/// </summary> /// </summary>
private void Draw() private void Draw()
{ {
this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, "EDIT SECTION - " + this.ParentSection.Name.ToUpper(), this.windowStyle).ClampToScreen(); this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, "EDIT SECTION - " + this.ParentSection.Name.ToUpper(), this.windowStyle).ClampToScreen();
this.ParentSection.EditorPositionX = this.windowPosition.x; this.ParentSection.EditorPositionX = this.windowPosition.x;
this.ParentSection.EditorPositionY = this.windowPosition.y; this.ParentSection.EditorPositionY = this.windowPosition.y;
} }
/// <summary> /// <summary>
/// Draws the editor window. /// Draws the editor window.
/// </summary> /// </summary>
private void Window(int windowId) private void Window(int windowId)
{ {
this.DrawCustomOptions(); this.DrawCustomOptions();
this.DrawCategorySelector(); this.DrawCategorySelector();
this.DrawAvailableReadouts(); this.DrawAvailableReadouts();
GUILayout.Space(5.0f); GUILayout.Space(5.0f);
this.DrawInstalledReadouts(); this.DrawInstalledReadouts();
if (GUILayout.Button("CLOSE EDITOR", this.categoryButtonStyle)) if (GUILayout.Button("CLOSE EDITOR", this.categoryButtonStyle))
{ {
this.ParentSection.IsEditorVisible = false; this.ParentSection.IsEditorVisible = false;
} }
GUI.DragWindow(); GUI.DragWindow();
} }
/// <summary> /// <summary>
/// Draws the options for editing custom sections. /// Draws the options for editing custom sections.
/// </summary> /// </summary>
private void DrawCustomOptions() private void DrawCustomOptions()
{ {
if (!this.ParentSection.IsCustom) if (!this.ParentSection.IsCustom)
{ {
return; return;
} }
GUILayout.BeginHorizontal(GUILayout.Height(25.0f)); GUILayout.BeginHorizontal(GUILayout.Height(25.0f));
this.ParentSection.Name = GUILayout.TextField(this.ParentSection.Name, this.textStyle); this.ParentSection.Name = GUILayout.TextField(this.ParentSection.Name, this.textStyle);
this.ParentSection.Abbreviation = GUILayout.TextField(this.ParentSection.Abbreviation, this.textStyle, GUILayout.Width(75.0f)); this.ParentSection.Abbreviation = GUILayout.TextField(this.ParentSection.Abbreviation, this.textStyle, GUILayout.Width(75.0f));
if (GUILayout.Button("DELETE SECTION", this.readoutButtonStyle, GUILayout.Width(125.0f))) if (GUILayout.Button("DELETE SECTION", this.readoutButtonStyle, GUILayout.Width(125.0f)))
{ {
this.ParentSection.IsFloating = false; this.ParentSection.IsFloating = false;
this.ParentSection.IsEditorVisible = false; this.ParentSection.IsEditorVisible = false;
SectionLibrary.Instance.CustomSections.Remove(this.ParentSection); SectionLibrary.Instance.CustomSections.Remove(this.ParentSection);
DisplayStack.Instance.RequestResize(); DisplayStack.Instance.RequestResize();
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
/// <summary> /// <summary>
/// Draws the category selection list. /// Draws the category selection list.
/// </summary> /// </summary>
private void DrawCategorySelector() private void DrawCategorySelector()
{ {
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
var isSelected = this.selectedCategory == ReadoutCategory.Orbital; var isSelected = this.selectedCategory == ReadoutCategory.Orbital;
if (GUILayout.Toggle(isSelected, ReadoutCategory.Orbital.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected) if (GUILayout.Toggle(isSelected, ReadoutCategory.Orbital.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected)
{ {
this.selectedCategory = ReadoutCategory.Orbital; this.selectedCategory = ReadoutCategory.Orbital;
} }
isSelected = this.selectedCategory == ReadoutCategory.Surface; isSelected = this.selectedCategory == ReadoutCategory.Surface;
if (GUILayout.Toggle(isSelected, ReadoutCategory.Surface.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected) if (GUILayout.Toggle(isSelected, ReadoutCategory.Surface.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected)
{ {
this.selectedCategory = ReadoutCategory.Surface; this.selectedCategory = ReadoutCategory.Surface;
} }
isSelected = this.selectedCategory == ReadoutCategory.Vessel; isSelected = this.selectedCategory == ReadoutCategory.Vessel;
if (GUILayout.Toggle(isSelected, ReadoutCategory.Vessel.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected) if (GUILayout.Toggle(isSelected, ReadoutCategory.Vessel.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected)
{ {
this.selectedCategory = ReadoutCategory.Vessel; this.selectedCategory = ReadoutCategory.Vessel;
} }
isSelected = this.selectedCategory == ReadoutCategory.Rendezvous; isSelected = this.selectedCategory == ReadoutCategory.Rendezvous;
if (GUILayout.Toggle(isSelected, ReadoutCategory.Rendezvous.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected) if (GUILayout.Toggle(isSelected, ReadoutCategory.Rendezvous.ToString().ToUpper(), this.categoryButtonStyle, GUILayout.Width(100.0f)) && !isSelected)
{ {
this.selectedCategory = ReadoutCategory.Rendezvous; this.selectedCategory = ReadoutCategory.Rendezvous;
} }
isSelected = this.selectedCategory == ReadoutCategory.Misc; isSelected = this.selectedCategory == ReadoutCategory.Misc;
if (GUILayout.Toggle(isSelected, ReadoutCategory.Misc.ToString().ToUpper(), this.categoryButtonStyle) && !isSelected) if (GUILayout.Toggle(isSelected, ReadoutCategory.Misc.ToString().ToUpper(), this.categoryButtonStyle) && !isSelected)
{ {
this.selectedCategory = ReadoutCategory.Misc; this.selectedCategory = ReadoutCategory.Misc;
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
/// <summary> /// <summary>
/// Draws the available readouts panel. /// Draws the available readouts panel.
/// </summary> /// </summary>
private void DrawAvailableReadouts() private void DrawAvailableReadouts()
{ {
GUI.skin = HighLogic.Skin; GUI.skin = HighLogic.Skin;
this.scrollPositionAvailable = GUILayout.BeginScrollView(this.scrollPositionAvailable, false, true, GUILayout.Height(200.0f)); this.scrollPositionAvailable = GUILayout.BeginScrollView(this.scrollPositionAvailable, false, true, GUILayout.Height(200.0f));
GUI.skin = null; GUI.skin = null;
GUILayout.Label("AVAILABLE", this.panelTitleStyle); GUILayout.Label("AVAILABLE", this.panelTitleStyle);
foreach (var readout in ReadoutLibrary.Instance.GetCategory(this.selectedCategory)) foreach (var readout in ReadoutLibrary.Instance.GetCategory(this.selectedCategory))
{ {
if (!this.ParentSection.ReadoutModules.Contains(readout)) if (!this.ParentSection.ReadoutModules.Contains(readout))
{ {
GUILayout.BeginHorizontal(GUILayout.Height(30.0f)); GUILayout.BeginHorizontal(GUILayout.Height(30.0f));
GUILayout.Label(readout.Name, this.readoutNameStyle); GUILayout.Label(readout.Name, this.readoutNameStyle);
readout.ShowHelp = GUILayout.Toggle(readout.ShowHelp, "?", this.readoutButtonStyle, GUILayout.Width(30.0f)); readout.ShowHelp = GUILayout.Toggle(readout.ShowHelp, "?", this.readoutButtonStyle, GUILayout.Width(30.0f));
if (GUILayout.Button("INSTALL", this.readoutButtonStyle, GUILayout.Width(125.0f))) if (GUILayout.Button("INSTALL", this.readoutButtonStyle, GUILayout.Width(125.0f)))
{ {
this.ParentSection.ReadoutModules.Add(readout); this.ParentSection.ReadoutModules.Add(readout);
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
this.ShowHelpMessage(readout); this.ShowHelpMessage(readout);
} }
} }
GUILayout.EndScrollView(); GUILayout.EndScrollView();
} }
/// <summary> /// <summary>
/// Draws the installed readouts panel. /// Draws the installed readouts panel.
/// </summary> /// </summary>
private void DrawInstalledReadouts() private void DrawInstalledReadouts()
{ {
GUI.skin = HighLogic.Skin; GUI.skin = HighLogic.Skin;
this.scrollPositionInstalled = GUILayout.BeginScrollView(this.scrollPositionInstalled, false, true); this.scrollPositionInstalled = GUILayout.BeginScrollView(this.scrollPositionInstalled, false, true);
GUI.skin = null; GUI.skin = null;
GUILayout.Label("INSTALLED", this.panelTitleStyle); GUILayout.Label("INSTALLED", this.panelTitleStyle);
ReadoutModule removeReadout = null; ReadoutModule removeReadout = null;
foreach (var readout in this.ParentSection.ReadoutModules) foreach (var readout in this.ParentSection.ReadoutModules)
{ {
GUILayout.BeginHorizontal(GUILayout.Height(30.0f)); GUILayout.BeginHorizontal(GUILayout.Height(30.0f));
GUILayout.Label(readout.Name, this.readoutNameStyle); GUILayout.Label(readout.Name, this.readoutNameStyle);
if (GUILayout.Button("▲", this.readoutButtonStyle, GUILayout.Width(30.0f))) if (GUILayout.Button("▲", this.readoutButtonStyle, GUILayout.Width(30.0f)))
{ {
var index = this.ParentSection.ReadoutModules.IndexOf(readout); var index = this.ParentSection.ReadoutModules.IndexOf(readout);
if (index > 0) if (index > 0)
{ {
this.ParentSection.ReadoutModules[index] = this.ParentSection.ReadoutModules[index - 1]; this.ParentSection.ReadoutModules[index] = this.ParentSection.ReadoutModules[index - 1];
this.ParentSection.ReadoutModules[index - 1] = readout; this.ParentSection.ReadoutModules[index - 1] = readout;
} }
} }
if (GUILayout.Button("▼", this.readoutButtonStyle, GUILayout.Width(30.0f))) if (GUILayout.Button("▼", this.readoutButtonStyle, GUILayout.Width(30.0f)))
{ {
var index = this.ParentSection.ReadoutModules.IndexOf(readout); var index = this.ParentSection.ReadoutModules.IndexOf(readout);
if (index < this.ParentSection.ReadoutModules.Count - 1) if (index < this.ParentSection.ReadoutModules.Count - 1)
{ {
this.ParentSection.ReadoutModules[index] = this.ParentSection.ReadoutModules[index + 1]; this.ParentSection.ReadoutModules[index] = this.ParentSection.ReadoutModules[index + 1];
this.ParentSection.ReadoutModules[index + 1] = readout; this.ParentSection.ReadoutModules[index + 1] = readout;
} }
} }
readout.ShowHelp = GUILayout.Toggle(readout.ShowHelp, "?", this.readoutButtonStyle, GUILayout.Width(30.0f)); readout.ShowHelp = GUILayout.Toggle(readout.ShowHelp, "?", this.readoutButtonStyle, GUILayout.Width(30.0f));
if (GUILayout.Button("REMOVE", this.readoutButtonStyle, GUILayout.Width(125.0f))) if (GUILayout.Button("REMOVE", this.readoutButtonStyle, GUILayout.Width(125.0f)))
{ {
removeReadout = readout; removeReadout = readout;
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
this.ShowHelpMessage(readout); this.ShowHelpMessage(readout);
} }
GUILayout.EndScrollView(); GUILayout.EndScrollView();
if (removeReadout != null) if (removeReadout != null)
{ {
this.ParentSection.ReadoutModules.Remove(removeReadout); this.ParentSection.ReadoutModules.Remove(removeReadout);
} }
} }
private void ShowHelpMessage(ReadoutModule readout) private void ShowHelpMessage(ReadoutModule readout)
{ {
if (readout.ShowHelp) if (readout.ShowHelp)
{ {
GUILayout.BeginVertical(this.helpBoxStyle); GUILayout.BeginVertical(this.helpBoxStyle);
if (readout.HelpString != null && readout.HelpString.Length > 0) if (readout.HelpString != null && readout.HelpString.Length > 0)
{ {
GUILayout.Label(readout.HelpString, this.helpTextStyle); GUILayout.Label(readout.HelpString, this.helpTextStyle);
} }
else else
{ {
GUILayout.Label("Sorry, no help information has been provided for this readout module.", this.helpTextStyle); GUILayout.Label("Sorry, no help information has been provided for this readout module.", this.helpTextStyle);
} }
GUILayout.EndVertical(); GUILayout.EndVertical();
} }
} }
#endregion #endregion
#region Destruction #region Destruction
/// <summary> /// <summary>
/// Runs when the object is destroyed. /// Runs when the object is destroyed.
/// </summary> /// </summary>
private void OnDestroy() private void OnDestroy()
{ {
RenderingManager.RemoveFromPostDrawQueue(0, this.Draw); RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);
} }
#endregion #endregion
} }
} }