Created basic prefab based in-flight Application Launcher button and menu. (still requires functionality implementation)
[VesselSimulator.git] / KerbalEngineer / AppLauncherButton.cs
blob:a/KerbalEngineer/AppLauncherButton.cs -> blob:b/KerbalEngineer/AppLauncherButton.cs
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2016 CYBUTEK // Copyright (C) 2016 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/>.
// //
   
namespace KerbalEngineer namespace KerbalEngineer
{ {
using KSP.UI; using KSP.UI;
using KSP.UI.Screens; using KSP.UI.Screens;
using UnityEngine; using UnityEngine;
   
public class AppLauncherButton : MonoBehaviour public class AppLauncherButton : MonoBehaviour
{ {
private static Texture m_IconTexture; private static Texture m_IconTexture;
private ApplicationLauncherButton m_Button; private ApplicationLauncherButton m_Button;
   
/// <summary> /// <summary>
/// Gets or sets the toggle button state. /// Gets or sets the toggle button state.
/// </summary> /// </summary>
public bool isOn public bool isOn
{ {
get get
{ {
return m_Button != null && return m_Button != null &&
m_Button.toggleButton.Button.interactable && m_Button.toggleButton.Button.interactable &&
m_Button.toggleButton.CurrentState == UIRadioButton.State.True; m_Button.toggleButton.CurrentState == UIRadioButton.State.True;
} }
set set
{ {
if (m_Button == null) if (m_Button == null)
{ {
return; return;
} }
   
if (value) if (value)
{ {
SetOn(); SetOn();
} }
else else
{ {
SetOff(); SetOff();
} }
} }
} }
   
/// <summary> /// <summary>
/// Disables the button if not already disabled. /// Disables the button if not already disabled.
/// </summary> /// </summary>
public void Disable() public void Disable()
{ {
if (m_Button != null && m_Button.toggleButton.Button.interactable) if (m_Button != null && m_Button.toggleButton.Button.interactable)
{ {
m_Button.Disable(); m_Button.Disable();
} }
} }
   
/// <summary> /// <summary>
/// Enables the button if not already enabled. /// Enables the button if not already enabled.
/// </summary> /// </summary>
public void Enable() public void Enable()
{ {
if (m_Button != null && m_Button.toggleButton.Button.interactable == false) if (m_Button != null && m_Button.toggleButton.Button.interactable == false)
{ {
m_Button.Enable(); m_Button.Enable();
} }
} }
   
  /// <summary>
  /// Gets the anchor position for pop-up content.
  /// </summary>
  public Vector3 GetAnchor()
  {
  if (m_Button == null)
  {
  return Vector3.zero;
  }
   
  Vector3 anchor = m_Button.GetAnchor();
   
  anchor.x -= 3.0f;
   
  return anchor;
  }
   
/// <summary> /// <summary>
/// Enables and sets the button to off. /// Enables and sets the button to off.
/// </summary> /// </summary>
public void SetOff() public void SetOff()
{ {
Enable(); Enable();
   
if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.False) if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.False)
{ {
m_Button.SetTrue(); m_Button.SetTrue();
} }
} }
   
/// <summary> /// <summary>
/// Enables and sets the button to on. /// Enables and sets the button to on.
/// </summary> /// </summary>
public void SetOn() public void SetOn()
{ {
Enable(); Enable();
   
if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.True) if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.True)
{ {
m_Button.SetTrue(); m_Button.SetTrue();
} }
} }
   
protected virtual void Awake() protected virtual void Awake()
{ {
// cache icon texture // cache icon texture
if (m_IconTexture == null && AssetBundleLoader.images != null) if (m_IconTexture == null && AssetBundleLoader.images != null)
{ {
m_IconTexture = AssetBundleLoader.images.LoadAsset<Texture2D>("app-launcher-icon"); m_IconTexture = AssetBundleLoader.images.LoadAsset<Texture2D>("app-launcher-icon");
} }
   
// subscribe event listeners // subscribe event listeners
GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady); GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
GameEvents.onGUIApplicationLauncherUnreadifying.Add(OnGUIApplicationLauncherUnreadifying); GameEvents.onGUIApplicationLauncherUnreadifying.Add(OnGUIApplicationLauncherUnreadifying);
} }
   
protected virtual void OnDestroy() protected virtual void OnDestroy()
{ {
// unsubscribe event listeners // unsubscribe event listeners
GameEvents.onGUIApplicationLauncherReady.Remove(OnGUIApplicationLauncherReady); GameEvents.onGUIApplicationLauncherReady.Remove(OnGUIApplicationLauncherReady);
GameEvents.onGUIApplicationLauncherUnreadifying.Remove(OnGUIApplicationLauncherUnreadifying); GameEvents.onGUIApplicationLauncherUnreadifying.Remove(OnGUIApplicationLauncherUnreadifying);
} }
   
/// <summary> /// <summary>
/// Called on button being disabled. /// Called on button being disabled.
/// </summary> /// </summary>
protected virtual void OnDisable() { } protected virtual void OnDisable() { }
   
/// <summary> /// <summary>
/// Called on button being enabled. /// Called on button being enabled.
/// </summary> /// </summary>
protected virtual void OnEnable() { } protected virtual void OnEnable() { }
   
/// <summary> /// <summary>
/// Called on button being toggled off. /// Called on button being toggled off.
/// </summary> /// </summary>
protected virtual void OnFalse() { } protected virtual void OnFalse() { }
   
/// <summary> /// <summary>
/// Called on mouse hovering. /// Called on mouse hovering.
/// </summary> /// </summary>
protected virtual void OnHover() { } protected virtual void OnHover() { }
   
/// <summary> /// <summary>
/// Called on mouse exiting hover. /// Called on mouse exiting hover.
/// </summary> /// </summary>
protected virtual void OnHoverOut() { } protected virtual void OnHoverOut() { }
   
/// <summary> /// <summary>
/// Called on button being ready. /// Called on button being ready.
/// </summary> /// </summary>
protected virtual void OnReady() { } protected virtual void OnReady() { }
   
/// <summary> /// <summary>
/// Called after the application launcher is ready and the button created. /// Called after the application launcher is ready and the button created.
/// </summary> /// </summary>
protected virtual void OnTrue() { } protected virtual void OnTrue() { }
   
/// <summary> /// <summary>
/// Called after the application launcher is unreadified and the button removed. /// Called after the application launcher is unreadified and the button removed.
/// </summary> /// </summary>
protected virtual void OnUnreadifying() { } protected virtual void OnUnreadifying() { }
   
private void OnGUIApplicationLauncherReady() private void OnGUIApplicationLauncherReady()
{ {
// create button // create button
if (ApplicationLauncher.Instance != null) if (ApplicationLauncher.Instance != null)
{ {
m_Button = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, OnHover, OnHoverOut, OnEnable, OnDisable, ApplicationLauncher.AppScenes.ALWAYS, m_IconTexture); m_Button = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, OnHover, OnHoverOut, OnEnable, OnDisable, ApplicationLauncher.AppScenes.ALWAYS, m_IconTexture);
} }
   
OnReady(); OnReady();
} }
   
private void OnGUIApplicationLauncherUnreadifying(GameScenes scene) private void OnGUIApplicationLauncherUnreadifying(GameScenes scene)
{ {
// remove button // remove button
if (ApplicationLauncher.Instance != null && m_Button != null) if (ApplicationLauncher.Instance != null && m_Button != null)
{ {
ApplicationLauncher.Instance.RemoveModApplication(m_Button); ApplicationLauncher.Instance.RemoveModApplication(m_Button);
} }
   
OnUnreadifying(); OnUnreadifying();
} }
} }
} }