Updated licensing information.
[VesselSimulator.git] / KerbalEngineer / Flight / ActionMenu.cs
blob:a/KerbalEngineer/Flight/ActionMenu.cs -> blob:b/KerbalEngineer/Flight/ActionMenu.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 //
// it under the terms of the GNU General Public License as published by // This program is free software: you can redistribute it and/or modify
// the Free Software Foundation, either version 3 of the License, or // it under the terms of the GNU General Public License as published by
// (at your option) any later version. // 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 // This program is distributed in the hope that it will be useful,
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details. // 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/>. // You should have received a copy of the GNU General Public License
// // along with this program. If not, see <http://www.gnu.org/licenses/>.
  //
#region Using Directives  
  #region Using Directives
using System.Collections.Generic;  
  using System;
using KerbalEngineer.Flight.Sections;  
using KerbalEngineer.Settings; using UnityEngine;
using KerbalEngineer.Simulation;  
  #endregion
using UnityEngine;  
  namespace KerbalEngineer.Flight
#endregion {
  /// <summary>
namespace KerbalEngineer.Flight /// Graphical controller for section interaction in the form of a menu system.
{ /// </summary>
/// <summary> [KSPAddon(KSPAddon.Startup.Flight, false)]
/// Graphical controller for section interaction in the form of a menu system. public class ActionMenu : MonoBehaviour
/// </summary> {
public class ActionMenu : MonoBehaviour #region Fields
{  
#region Constants private ActionMenuGui actionMenuGui;
  private ApplicationLauncherButton button;
private const float ScrollSpeed = 2.0f;  
  #endregion
#endregion  
  #region Methods: protected
#region Fields  
  protected void Awake()
private bool isOpen = true; {
private int numberOfSections; try
private float scrollPercent; {
private int windowId; GameEvents.onGUIApplicationLauncherReady.Add(this.OnGuiAppLauncherReady);
private Rect windowPosition = new Rect((Screen.width * 0.25f) - 100.0f, 0, 200.0f, 0); }
  catch (Exception ex)
#endregion {
  Logger.Exception(ex);
#region Constructors }
  Logger.Log("ActionMenu was created.");
/// <summary> }
/// Initialises object's state on creation.  
/// </summary> protected void OnDestroy()
private void Start() {
{ try
this.windowId = this.GetHashCode(); {
this.InitialiseStyles(); GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);
this.Load(); GameEvents.onHideUI.Remove(this.OnHide);
RenderingManager.AddToPostDrawQueue(0, this.Draw); GameEvents.onShowUI.Remove(this.OnShow);
} ApplicationLauncher.Instance.RemoveModApplication(this.button);
  }
#endregion catch (Exception ex)
  {
#region GUIStyles Logger.Exception(ex);
  }
private GUIStyle boxStyle; Logger.Log("ActionMenu was destroyed.");
private GUIStyle buttonStyle; }
private GUIStyle windowStyle;  
  protected void Update()
/// <summary> {
/// Initialises all the styles required for this object. try
/// </summary> {
private void InitialiseStyles() if (this.button == null)
{ {
this.windowStyle = new GUIStyle(); return;
  }
this.boxStyle = new GUIStyle(HighLogic.Skin.window) if (FlightEngineerCore.Instance != null && this.button.State == RUIToggleButton.ButtonState.DISABLED)
{ {
margin = new RectOffset(), this.button.Enable();
padding = new RectOffset(3, 3, 3, 3) }
}; else if (FlightEngineerCore.Instance == null && this.button.State != RUIToggleButton.ButtonState.DISABLED)
  {
this.buttonStyle = new GUIStyle(HighLogic.Skin.button) this.button.Disable();
{ }
normal = }
{ catch (Exception ex)
textColor = Color.white {
}, Logger.Exception(ex);
margin = new RectOffset(), }
padding = new RectOffset(), }
alignment = TextAnchor.MiddleCenter,  
fontSize = 11, #endregion
fontStyle = FontStyle.Bold,  
fixedHeight = 20.0f, #region Methods: private
};  
} private void OnFalse()
  {
#endregion try
  {
#region Drawing this.actionMenuGui.enabled = false;
  this.actionMenuGui.StayOpen = false;
/// <summary> }
/// Called to draw the menu when the UI is enabled. catch (Exception ex)
/// </summary> {
private void Draw() Logger.Exception(ex);
{ }
if (this.numberOfSections != SectionLibrary.Instance.NumberOfSections) }
{  
this.numberOfSections = SectionLibrary.Instance.NumberOfSections; private void OnGuiAppLauncherReady()
this.windowPosition.height = 0; {
} try
this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle); {
this.ScrollMechanism(); this.button = ApplicationLauncher.Instance.AddModApplication(
} this.OnTrue,
  this.OnFalse,
/// <summary> this.OnHover,
/// Draws the menu window. this.OnHoverOut,
/// </summary> null,
private void Window(int windowId) null,
{ ApplicationLauncher.AppScenes.ALWAYS,
GUILayout.BeginVertical(this.boxStyle); GameDatabase.Instance.GetTexture("KerbalEngineer/Textures/ToolbarIcon", false));
  this.actionMenuGui = this.button.gameObject.AddComponent<ActionMenuGui>();
this.DrawControlBarButton(); this.actionMenuGui.transform.parent = this.button.transform;
this.DrawSections(SectionLibrary.Instance.StockSections); ApplicationLauncher.Instance.EnableMutuallyExclusive(this.button);
this.DrawSections(SectionLibrary.Instance.CustomSections); GameEvents.onHideUI.Add(this.OnHide);
this.DrawNewButton(); GameEvents.onShowUI.Add(this.OnShow);
  }
GUILayout.EndVertical(); catch (Exception ex)
  {
if (GUILayout.Button("FLIGHT ENGINEER", this.buttonStyle)) Logger.Exception(ex);
{ }
this.isOpen = !this.isOpen; }
}  
} private void OnHide()
  {
/// <summary> try
/// Draws and performs the control bar button action. {
/// </summary> this.actionMenuGui.Hidden = true;
private void DrawControlBarButton() }
{ catch (Exception ex)
if (GUILayout.Toggle(DisplayStack.Instance.ShowControlBar, "CONTROL BAR", this.buttonStyle) != DisplayStack.Instance.ShowControlBar) {
{ Logger.Exception(ex);
DisplayStack.Instance.ShowControlBar = !DisplayStack.Instance.ShowControlBar; }
DisplayStack.Instance.RequestResize(); }
}  
} private void OnHover()
  {
/// <summary> try
/// Draws an action list for the supplied sections. {
/// </summary> this.actionMenuGui.enabled = true;
private void DrawSections(IEnumerable<SectionModule> sections) this.actionMenuGui.Hovering = true;
{ }
foreach (var section in sections) catch (Exception ex)
{ {
GUILayout.BeginHorizontal(); Logger.Exception(ex);
section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Name.ToUpper(), this.buttonStyle); }
section.IsEditorVisible = GUILayout.Toggle(section.IsEditorVisible, "EDIT", this.buttonStyle, GUILayout.Width(50.0f)); }
GUILayout.EndHorizontal();  
} private void OnHoverOut()
} {
  try
/// <summary> {
/// Draws and performs the new section button action. this.actionMenuGui.Hovering = false;
/// </summary> }
private void DrawNewButton() catch (Exception ex)
{ {
GUILayout.BeginHorizontal(); Logger.Exception(ex);
GUI.skin = HighLogic.Skin; }
SimManager.minSimTime = (long)GUILayout.HorizontalSlider(SimManager.minSimTime, 0, 1000.0f); }
GUI.skin = null;  
  private void OnShow()
if (GUILayout.Button("NEW", this.buttonStyle, GUILayout.Width(50.0f))) {
{ try
SectionLibrary.Instance.CustomSections.Add(new SectionModule {
{ this.actionMenuGui.Hidden = false;
Name = "Custom " + (SectionLibrary.Instance.CustomSections.Count + 1), }
Abbreviation = "CUST " + (SectionLibrary.Instance.CustomSections.Count + 1), catch (Exception ex)
IsVisible = true, {
IsCustom = true, Logger.Exception(ex);
IsEditorVisible = true }
}); }
}  
GUILayout.EndHorizontal(); private void OnTrue()
} {
  try
/// <summary> {
/// Controls the dynamics of the scrolling mechanism. this.actionMenuGui.enabled = true;
/// </summary> this.actionMenuGui.StayOpen = true;
private void ScrollMechanism() }
{ catch (Exception ex)
if (this.isOpen && this.windowPosition.y != 0) {
{ Logger.Exception(ex);
this.scrollPercent += Time.deltaTime * ScrollSpeed; }
this.windowPosition.y = Mathf.Lerp(this.windowPosition.y, 0, this.scrollPercent); }
}  
else if (!this.isOpen && this.windowPosition.y != 20.0f - this.windowPosition.height) #endregion
{ }
this.scrollPercent += Time.deltaTime * ScrollSpeed;  
this.windowPosition.y = Mathf.Lerp(this.windowPosition.y, 20.0f - this.windowPosition.height, this.scrollPercent);  
}  
else  
{  
this.scrollPercent = 0;  
}  
}  
   
#endregion  
   
#region Destruction  
   
/// <summary>  
/// Runs when the object is destroyed.  
/// </summary>  
private void OnDestroy()  
{  
this.Save();  
   
RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);  
}  
   
#endregion  
   
#region Saving and Loading  
   
/// <summary>  
/// Saves the menu's state.  
/// </summary>  
private void Save()  
{  
var handler = new SettingHandler();  
handler.Set("isOpen", this.isOpen);  
handler.Set("windowPositionY", this.windowPosition.y);  
handler.Set("windowPositionHeight", this.windowPosition.height);  
handler.Save("ActionMenu.xml");  
}  
   
/// <summary>  
/// Loads the menu's state.  
/// </summary>  
private void Load()  
{  
var handler = SettingHandler.Load("ActionMenu.xml");  
handler.Get("isOpen", ref this.isOpen);  
this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);  
this.windowPosition.height = handler.Get("windowPositionHeight", this.windowPosition.height);  
}  
   
#endregion  
}  
} }