Redone action menu to better support the stock toolbar.
[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 // 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 System.Threading;  
   
using UnityEngine; using UnityEngine;
   
#endregion #endregion
   
namespace KerbalEngineer.Flight namespace KerbalEngineer.Flight
{ {
  using KSP.UI.Screens;
   
/// <summary> /// <summary>
/// Graphical controller for section interaction in the form of a menu system. /// Graphical controller for section interaction in the form of a menu system.
/// </summary> /// </summary>
[KSPAddon(KSPAddon.Startup.Flight, false)] [KSPAddon(KSPAddon.Startup.Flight, false)]
public class ActionMenu : MonoBehaviour public class ActionMenu : MonoBehaviour
{ {
#region Fields #region Fields
   
private ActionMenuGui actionMenuGui; private ActionMenuGui actionMenuGui;
private ApplicationLauncherButton button; private ApplicationLauncherButton button;
   
#endregion #endregion
   
#region Initialisation #region Methods: protected
   
private void Awake() protected void Awake()
{ {
try try
{ {
GameEvents.onGUIApplicationLauncherReady.Add(this.OnGuiAppLauncherReady); GameEvents.onGUIApplicationLauncherReady.Add(this.OnGuiAppLauncherReady);
Logger.Log("ActionMenu was created."); }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  Logger.Log("ActionMenu was created.");
  }
   
  protected void Start()
  {
  if (button == null)
  {
  OnGuiAppLauncherReady();
  }
  }
   
  protected void OnDestroy()
  {
  try
  {
  GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);
  GameEvents.onHideUI.Remove(this.OnHide);
  GameEvents.onShowUI.Remove(this.OnShow);
  if (button != null)
  {
  ApplicationLauncher.Instance.RemoveModApplication(this.button);
  }
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  Logger.Log("ActionMenu was destroyed.");
  }
   
  protected void Update()
  {
  try
  {
  if (this.button == null)
  {
  return;
  }
  if (FlightEngineerCore.IsDisplayable && this.button.toggleButton.Interactable == false)
  {
  this.button.Enable();
  }
  else if (!FlightEngineerCore.IsDisplayable && this.button.toggleButton.Interactable)
  {
  this.button.Disable();
  }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
#endregion #endregion
   
#region Callbacks #region Methods: private
   
  private void OnFalse()
  {
  try
  {
  this.actionMenuGui.enabled = false;
  this.actionMenuGui.StayOpen = false;
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  }
   
private void OnGuiAppLauncherReady() private void OnGuiAppLauncherReady()
{ {
try try
{ {
this.button = ApplicationLauncher.Instance.AddModApplication( this.button = ApplicationLauncher.Instance.AddModApplication(
this.OnTrue, this.OnTrue,
this.OnFalse, this.OnFalse,
this.OnHover, this.OnHover,
this.OnHoverOut, this.OnHoverOut,
null, null,
null, null,
ApplicationLauncher.AppScenes.ALWAYS, ApplicationLauncher.AppScenes.ALWAYS,
GameDatabase.Instance.GetTexture("KerbalEngineer/Textures/ToolbarIcon", false)); GameDatabase.Instance.GetTexture("KerbalEngineer/Textures/ToolbarIcon", false));
this.actionMenuGui = this.button.gameObject.AddComponent<ActionMenuGui>(); this.actionMenuGui = this.button.gameObject.AddComponent<ActionMenuGui>();
this.actionMenuGui.transform.parent = this.button.transform; this.actionMenuGui.transform.parent = this.button.transform;
ApplicationLauncher.Instance.EnableMutuallyExclusive(this.button); ApplicationLauncher.Instance.EnableMutuallyExclusive(this.button);
  GameEvents.onHideUI.Add(this.OnHide);
  GameEvents.onShowUI.Add(this.OnShow);
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  }
   
  private void OnHide()
  {
  try
  {
  this.actionMenuGui.Hidden = true;
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  }
   
  private void OnHover()
  {
  try
  {
  this.actionMenuGui.enabled = true;
  this.actionMenuGui.Hovering = true;
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  }
   
  private void OnHoverOut()
  {
  try
  {
  this.actionMenuGui.Hovering = false;
  }
  catch (Exception ex)
  {
  Logger.Exception(ex);
  }
  }
   
  private void OnShow()
  {
  try
  {
  this.actionMenuGui.Hidden = false;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
private void OnTrue() private void OnTrue()
{ {
try try
{ {
this.actionMenuGui.enabled = true; this.actionMenuGui.enabled = true;
this.actionMenuGui.StayOpen = true; this.actionMenuGui.StayOpen = true;
}  
catch (Exception ex)  
{  
Logger.Exception(ex);  
}  
}  
   
private void OnFalse()  
{  
try  
{  
this.actionMenuGui.enabled = false;  
this.actionMenuGui.StayOpen = false;  
}  
catch (Exception ex)  
{  
Logger.Exception(ex);  
}  
}  
   
private void OnHover()  
{  
try  
{  
this.actionMenuGui.enabled = true;  
this.actionMenuGui.Hovering = true;  
}  
catch (Exception ex)  
{  
Logger.Exception(ex);  
}  
}  
   
private void OnHoverOut()  
{  
try  
{  
this.actionMenuGui.Hovering = false;  
}  
catch (Exception ex)  
{  
Logger.Exception(ex);  
}  
}  
   
#endregion  
   
#region Destruction  
   
private void OnDestroy()  
{  
try  
{  
GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);  
ApplicationLauncher.Instance.RemoveModApplication(this.button);  
Logger.Log("ActionMenu was destroyed.");  
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
#endregion #endregion
} }
} }