Implemented in-flight menu open/close fading with hover over.
[VesselSimulator.git] / KerbalEngineer / Flight / FlightAppLauncher.cs
blob:a/KerbalEngineer/Flight/FlightAppLauncher.cs -> blob:b/KerbalEngineer/Flight/FlightAppLauncher.cs
--- a/KerbalEngineer/Flight/FlightAppLauncher.cs
+++ b/KerbalEngineer/Flight/FlightAppLauncher.cs
@@ -1,27 +1,143 @@
-namespace KerbalEngineer.Flight
+// 
+//     Kerbal Engineer Redux
+// 
+//     Copyright (C) 2016 CYBUTEK
+// 
+//     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
+//     the Free Software Foundation, either version 3 of the License, or
+//     (at your option) any later version.
+// 
+//     This program is distributed in the hope that it will be useful,
+//     but WITHOUT ANY WARRANTY; without even the implied warranty of
+//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//     GNU General Public License for more details.
+//     You should have received a copy of the GNU General Public License
+//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//  
+
+namespace KerbalEngineer.Flight
 {
-    using System;
+    using System.Collections.Generic;
+    using KSP.UI;
+    using Sections;
+    using Unity.Flight;
     using UnityEngine;
 
     [KSPAddon(KSPAddon.Startup.Flight, false)]
-    public class FlightAppLauncher : AppLauncherButton
+    public class FlightAppLauncher : AppLauncherButton, IFlightAppLauncher
     {
         private static FlightAppLauncher m_Instance;
+        private FlightMenu m_FlightMenu;
         private GameObject m_MenuObject;
         private GameObject m_MenuPrefab;
 
-        public static event Action ButtonHover;
-        public static event Action MenuClosed;
-
         /// <summary>
         ///     Gets the current instance of the FlightAppLauncher object.
         /// </summary>
-        public static FlightAppLauncher instance
+        public static FlightAppLauncher Instance
         {
             get
             {
                 return m_Instance;
             }
+        }
+
+        /// <summary>
+        ///     Applies the KSP theme to a game object and its children.
+        /// </summary>
+        public void ApplyTheme(GameObject gameObject)
+        {
+            StyleManager.Process(gameObject);
+        }
+
+        /// <summary>
+        ///     Clamps the given rect transform within the screen bounds.
+        /// </summary>
+        public void ClampToScreen(RectTransform rectTransform)
+        {
+            UIMasterController.ClampToScreen(rectTransform, Vector2.zero);
+        }
+
+        /// <summary>
+        ///     Gets a list of custom sections.
+        /// </summary>
+        IList<ISectionModule> IFlightAppLauncher.GetCustomSections()
+        {
+            return new List<ISectionModule>(SectionLibrary.CustomSections.ToArray());
+        }
+
+        /// <summary>
+        ///     Gets a list of stock sections.
+        /// </summary>
+        IList<ISectionModule> IFlightAppLauncher.GetStockSections()
+        {
+            return new List<ISectionModule>(SectionLibrary.StockSections.ToArray());
+        }
+
+        /// <summary>
+        ///     Gets or sets the control bar's visibility.
+        /// </summary>
+        public bool IsControlBarVisible
+        {
+            get
+            {
+                if (DisplayStack.Instance != null)
+                {
+                    return DisplayStack.Instance.ShowControlBar;
+                }
+
+                return false;
+            }
+            set
+            {
+                if (DisplayStack.Instance != null)
+                {
+                    DisplayStack.Instance.ShowControlBar = value;
+                }
+            }
+        }
+
+        /// <summary>
+        ///     Gets or sets the display stack's visibility.
+        /// </summary>
+        public bool IsDisplayStackVisible
+        {
+            get
+            {
+                if (DisplayStack.Instance != null)
+                {
+                    return DisplayStack.Instance.Hidden == false;
+                }
+
+                return false;
+            }
+            set
+            {
+                if (DisplayStack.Instance != null)
+                {
+                    DisplayStack.Instance.Hidden = !value;
+                }
+            }
+        }
+
+        /// <summary>
+        ///     Creates and initialises a new custom section.
+        /// </summary>
+        public ISectionModule NewCustomSection()
+        {
+            SectionModule section = new SectionModule
+            {
+                Name = "Custom " + (SectionLibrary.CustomSections.Count + 1),
+                Abbreviation = "CUST " + (SectionLibrary.CustomSections.Count + 1),
+                IsVisible = true,
+                IsCustom = true,
+                IsEditorVisible = true
+            };
+
+            SectionLibrary.CustomSections.Add(section);
+
+            return section;
         }
 
         protected override void Awake()
@@ -32,9 +148,9 @@
             m_Instance = this;
 
             // cache menu prefab
-            if (m_MenuPrefab == null && AssetBundleLoader.prefabs != null)
-            {
-                m_MenuPrefab = AssetBundleLoader.prefabs.LoadAsset<GameObject>("flight-menu");
+            if (m_MenuPrefab == null && AssetBundleLoader.Prefabs != null)
+            {
+                m_MenuPrefab = AssetBundleLoader.Prefabs.LoadAsset<GameObject>("FlightMenu");
             }
         }
 
@@ -46,12 +162,11 @@
         protected override void OnHover()
         {
             Open();
-            ButtonHover?.Invoke();
         }
 
         protected override void OnHoverOut()
         {
-            if (isOn == false)
+            if (IsOn == false)
             {
                 Close();
             }
@@ -67,26 +182,28 @@
         /// </summary>
         private void Close()
         {
-            if (m_MenuObject == null)
-            {
+            if (m_FlightMenu != null)
+            {
+                m_FlightMenu.Close();
+            }
+            else if (m_MenuObject != null)
+            {
+                Destroy(m_MenuObject);
+            }
+        }
+
+        /// <summary>
+        ///     Opens the menu.
+        /// </summary>
+        private void Open()
+        {
+            // fade menu in if already open
+            if (m_FlightMenu != null)
+            {
+                m_FlightMenu.FadeIn();
                 return;
             }
 
-            if (MenuClosed != null)
-            {
-                MenuClosed();
-            }
-            else
-            {
-                Destroy(m_MenuObject);
-            }
-        }
-
-        /// <summary>
-        ///     Opens the menu.
-        /// </summary>
-        private void Open()
-        {
             if (m_MenuPrefab == null || m_MenuObject != null)
             {
                 return;
@@ -99,8 +216,17 @@
                 return;
             }
 
+            StyleManager.Process(m_MenuObject);
+
             // set object as a child of the main canvas
             m_MenuObject.transform.SetParent(MainCanvasUtil.MainCanvas.transform);
+
+            // set menu's reference to this object for cross-communication
+            m_FlightMenu = m_MenuObject.GetComponent<FlightMenu>();
+            if (m_FlightMenu != null)
+            {
+                m_FlightMenu.SetFlightAppLauncher(this);
+            }
         }
     }
 }