Stock sections can be renamed, and custom ones hidden from the control bar.
[VesselSimulator.git] / KerbalEngineer / Flight / DisplayStack.cs
blob:a/KerbalEngineer/Flight/DisplayStack.cs -> blob:b/KerbalEngineer/Flight/DisplayStack.cs
--- a/KerbalEngineer/Flight/DisplayStack.cs
+++ b/KerbalEngineer/Flight/DisplayStack.cs
@@ -33,36 +33,66 @@
 
 namespace KerbalEngineer.Flight
 {
+    using KeyBinding;
+    using Upgradeables;
+
     /// <summary>
     ///     Graphical controller for displaying stacked sections.
     /// </summary>
     [KSPAddon(KSPAddon.Startup.Flight, false)]
     public class DisplayStack : MonoBehaviour
     {
-        #region Instance
-
-        /// <summary>
-        ///     Gets the current instance of the DisplayStack.
-        /// </summary>
-        public static DisplayStack Instance { get; private set; }
-
-        #endregion
-
         #region Fields
 
+        private GUIStyle buttonStyle;
         private int numberOfStackSections;
         private bool resizeRequested;
+        private bool showControlBar = true;
+        private GUIStyle titleStyle;
         private int windowId;
         private Rect windowPosition;
-
-        #endregion
-
-        #region Constructors
+        private GUIStyle windowStyle;
+
+        #endregion
+
+        #region Properties
+
+        /// <summary>
+        ///     Gets the current instance of the DisplayStack.
+        /// </summary>
+        public static DisplayStack Instance { get; private set; }
+
+        public bool Hidden { get; set; }
+
+        /// <summary>
+        ///     Gets and sets the visibility of the control bar.
+        /// </summary>
+        public bool ShowControlBar
+        {
+            get { return this.showControlBar; }
+            set { this.showControlBar = value; }
+        }
+
+        #endregion
+
+        #region Methods: public
+
+        /// <summary>
+        ///     Request that the display stack's size is reset in the next draw call.
+        /// </summary>
+        public void RequestResize()
+        {
+            this.resizeRequested = true;
+        }
+
+        #endregion
+
+        #region Methods: protected
 
         /// <summary>
         ///     Sets the instance to this object.
         /// </summary>
-        private void Awake()
+        protected void Awake()
         {
             try
             {
@@ -79,14 +109,31 @@
             }
             catch (Exception ex)
             {
-                Logger.Exception(ex, "DisplayStack->Awake");
-            }
+                Logger.Exception(ex);
+            }
+        }
+
+        /// <summary>
+        ///     Runs when the object is destroyed.
+        /// </summary>
+        protected void OnDestroy()
+        {
+            try
+            {
+                this.Save();
+                RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+            Logger.Log("ActionMenu->OnDestroy");
         }
 
         /// <summary>
         ///     Initialises the object's state on creation.
         /// </summary>
-        private void Start()
+        protected void Start()
         {
             try
             {
@@ -98,112 +145,33 @@
             }
             catch (Exception ex)
             {
-                Logger.Exception(ex, "DisplayStack->Start");
-            }
-        }
-
-        #endregion
-
-        #region Properties
-
-        private bool showControlBar = true;
-
-        /// <summary>
-        ///     Gets and sets the visibility of the control bar.
-        /// </summary>
-        public bool ShowControlBar
-        {
-            get { return this.showControlBar; }
-            set { this.showControlBar = value; }
-        }
-
-        public bool Hidden { get; set; }
-
-        #endregion
-
-        #region GUIStyles
-
-        private GUIStyle buttonStyle;
-        private GUIStyle titleStyle;
-        private GUIStyle windowStyle;
-
-        /// <summary>
-        ///     Initialises all the styles required for this object.
-        /// </summary>
-        private void InitialiseStyles()
-        {
-            try
-            {
-                this.windowStyle = new GUIStyle(HighLogic.Skin.window)
-                {
-                    margin = new RectOffset(),
-                    padding = new RectOffset(5, 5, 0, 5)
-                };
-
-                this.titleStyle = new GUIStyle(HighLogic.Skin.label)
-                {
-                    margin = new RectOffset(0, 0, 5, 3),
-                    padding = new RectOffset(),
-                    alignment = TextAnchor.MiddleCenter,
-                    fontSize = (int)(13 * GuiDisplaySize.Offset),
-                    fontStyle = FontStyle.Bold,
-                    stretchWidth = true
-                };
-
-                this.buttonStyle = new GUIStyle(HighLogic.Skin.button)
-                {
-                    normal =
-                    {
-                        textColor = Color.white
-                    },
-                    margin = new RectOffset(),
-                    padding = new RectOffset(),
-                    alignment = TextAnchor.MiddleCenter,
-                    fontSize = (int)(11 * GuiDisplaySize.Offset),
-                    fontStyle = FontStyle.Bold,
-                    fixedWidth = 60.0f * GuiDisplaySize.Offset,
-                    fixedHeight = 25.0f * GuiDisplaySize.Offset,
-                };
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->InitialiseStyles");
-            }
-        }
-
-        private void OnSizeChanged()
-        {
-            this.InitialiseStyles();
-            this.RequestResize();
-        }
-
-        #endregion
-
-        #region Updating
-
-        private void Update()
-        {
-            try
-            {
-                if (FlightEngineerCore.Instance == null)
+                Logger.Exception(ex);
+            }
+        }
+
+        protected void Update()
+        {
+            try
+            {
+                if (!FlightEngineerCore.IsDisplayable)
                 {
                     return;
                 }
 
-                if (Input.GetKeyDown(KeyCode.Backslash))
+                if (Input.GetKeyDown(KeyBinder.FlightShowHide))
                 {
                     this.Hidden = !this.Hidden;
                 }
             }
             catch (Exception ex)
             {
-                Logger.Exception(ex, "DisplayStack->Update");
-            }
-        }
-
-        #endregion
-
-        #region Drawing
+                Logger.Exception(ex);
+            }
+        }
+
+        #endregion
+
+        #region Methods: private
 
         /// <summary>
         ///     Called to draw the display stack when the UI is enabled.
@@ -212,7 +180,7 @@
         {
             try
             {
-                if (FlightEngineerCore.Instance == null)
+                if (!FlightEngineerCore.IsDisplayable)
                 {
                     return;
                 }
@@ -238,33 +206,7 @@
             }
             catch (Exception ex)
             {
-                Logger.Exception(ex, "DisplayStack->Draw");
-            }
-        }
-
-        /// <summary>
-        ///     Draws the display stack window.
-        /// </summary>
-        private void Window(int windowId)
-        {
-            try
-            {
-                if (this.ShowControlBar)
-                {
-                    this.DrawControlBar();
-                }
-
-                if (SectionLibrary.NumberOfStackSections > 0)
-                {
-                    this.DrawSections(SectionLibrary.StockSections);
-                    this.DrawSections(SectionLibrary.CustomSections);
-                }
-
-                GUI.DragWindow();
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->Widnow");
+                Logger.Exception(ex);
             }
         }
 
@@ -273,17 +215,10 @@
         /// </summary>
         private void DrawControlBar()
         {
-            try
-            {
-                GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.AssemblyVersion, this.titleStyle);
-
-                this.DrawControlBarButtons(SectionLibrary.StockSections);
-                this.DrawControlBarButtons(SectionLibrary.CustomSections);
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->DrawControlBar");
-            }
+            GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.ASSEMBLY_VERSION, this.titleStyle);
+
+            this.DrawControlBarButtons(SectionLibrary.StockSections);
+            this.DrawControlBarButtons(SectionLibrary.CustomSections);
         }
 
         /// <summary>
@@ -291,30 +226,23 @@
         /// </summary>
         private void DrawControlBarButtons(IEnumerable<SectionModule> sections)
         {
-            try
-            {
-                var index = 0;
-                foreach (var section in sections.Where(s => !string.IsNullOrEmpty(s.Abbreviation) || !s.IsCustom))
-                {
-                    if (index % 4 == 0)
+            var index = 0;
+            foreach (var section in sections.Where(s => !string.IsNullOrEmpty(s.Abbreviation) || !s.IsCustom))
+            {
+                if (index % 4 == 0)
+                {
+                    if (index > 0)
                     {
-                        if (index > 0)
-                        {
-                            GUILayout.EndHorizontal();
-                        }
-                        GUILayout.BeginHorizontal();
+                        GUILayout.EndHorizontal();
                     }
-                    section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle);
-                    index++;
-                }
-                if (index > 0)
-                {
-                    GUILayout.EndHorizontal();
-                }
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->DrawControlBarButtons");
+                    GUILayout.BeginHorizontal();
+                }
+                section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle);
+                index++;
+            }
+            if (index > 0)
+            {
+                GUILayout.EndHorizontal();
             }
         }
 
@@ -323,46 +251,76 @@
         /// </summary>
         private void DrawSections(IEnumerable<SectionModule> sections)
         {
-            try
-            {
-                foreach (var section in sections)
-                {
-                    if (!section.IsFloating)
-                    {
-                        section.Draw();
-                    }
-                }
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->DrawSections");
-            }
-        }
-
-        #endregion
-
-        #region Destruction
-
-        /// <summary>
-        ///     Runs when the object is destroyed.
-        /// </summary>
-        private void OnDestroy()
-        {
-            try
-            {
-                this.Save();
-                RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);
-                Logger.Log("ActionMenu->OnDestroy");
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->OnDestroy");
-            }
-        }
-
-        #endregion
-
-        #region Saving and Loading
+            foreach (var section in sections)
+            {
+                if (!section.IsFloating)
+                {
+                    section.Draw();
+                }
+            }
+        }
+
+        /// <summary>
+        ///     Initialises all the styles required for this object.
+        /// </summary>
+        private void InitialiseStyles()
+        {
+            this.windowStyle = new GUIStyle(HighLogic.Skin.window)
+            {
+                margin = new RectOffset(),
+                padding = new RectOffset(5, 5, 0, 5)
+            };
+
+            this.titleStyle = new GUIStyle(HighLogic.Skin.label)
+            {
+                margin = new RectOffset(0, 0, 5, 3),
+                padding = new RectOffset(),
+                alignment = TextAnchor.MiddleCenter,
+                fontSize = (int)(13 * GuiDisplaySize.Offset),
+                fontStyle = FontStyle.Bold,
+                stretchWidth = true
+            };
+
+            this.buttonStyle = new GUIStyle(HighLogic.Skin.button)
+            {
+                normal =
+                {
+                    textColor = Color.white
+                },
+                margin = new RectOffset(),
+                padding = new RectOffset(),
+                alignment = TextAnchor.MiddleCenter,
+                fontSize = (int)(11 * GuiDisplaySize.Offset),
+                fontStyle = FontStyle.Bold,
+                fixedWidth = 60.0f * GuiDisplaySize.Offset,
+                fixedHeight = 25.0f * GuiDisplaySize.Offset,
+            };
+        }
+
+        /// <summary>
+        ///     Load the stack's state.
+        /// </summary>
+        private void Load()
+        {
+            try
+            {
+                var handler = SettingHandler.Load("DisplayStack.xml");
+                this.Hidden = handler.Get("hidden", this.Hidden);
+                this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar);
+                this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x);
+                this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex, "DisplayStack->Load");
+            }
+        }
+
+        private void OnSizeChanged()
+        {
+            this.InitialiseStyles();
+            this.RequestResize();
+        }
 
         /// <summary>
         ///     Saves the stack's state.
@@ -372,6 +330,7 @@
             try
             {
                 var handler = new SettingHandler();
+                handler.Set("hidden", this.Hidden);
                 handler.Set("showControlBar", this.ShowControlBar);
                 handler.Set("windowPositionX", this.windowPosition.x);
                 handler.Set("windowPositionY", this.windowPosition.y);
@@ -384,33 +343,29 @@
         }
 
         /// <summary>
-        ///     Load the stack's state.
-        /// </summary>
-        private void Load()
-        {
-            try
-            {
-                var handler = SettingHandler.Load("DisplayStack.xml");
-                this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar);
-                this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x);
-                this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex, "DisplayStack->Load");
-            }
-        }
-
-        #endregion
-
-        #region Methods
-
-        /// <summary>
-        ///     Request that the display stack's size is reset in the next draw call.
-        /// </summary>
-        public void RequestResize()
-        {
-            this.resizeRequested = true;
+        ///     Draws the display stack window.
+        /// </summary>
+        private void Window(int windowId)
+        {
+            try
+            {
+                if (this.ShowControlBar)
+                {
+                    this.DrawControlBar();
+                }
+
+                if (SectionLibrary.NumberOfStackSections > 0)
+                {
+                    this.DrawSections(SectionLibrary.StockSections);
+                    this.DrawSections(SectionLibrary.CustomSections);
+                }
+
+                GUI.DragWindow();
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex, "DisplayStack->Widnow");
+            }
         }
 
         #endregion