Skip to content

Commit

Permalink
Toolbar implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DMagic1 committed Feb 1, 2017
1 parent 846dcaa commit 8528295
Showing 1 changed file with 260 additions and 9 deletions.
269 changes: 260 additions & 9 deletions SCANsat/SCAN_Toolbar/SCANappLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,99 @@
using SCANsat.SCAN_UI;
using SCANsat.SCAN_UI.UI_Framework;
using SCANsat.SCAN_Unity;
using SCANsat.Unity.Unity;
using SCANsat.Unity.Interfaces;
using KSP.UI;
using KSP.UI.Screens;
using UnityEngine;

namespace SCANsat.SCAN_Toolbar
{
class SCANappLauncher : SCAN_MBE
class SCANappLauncher : MonoBehaviour, ISCAN_Toolbar
{
private ApplicationLauncherButton SCANappLauncherButton = null;
private bool _inMenu;
private bool _sticky;
private bool _hovering;
private SCANsat.Unity.Unity.SCAN_Toolbar uiElement;
private static SCANappLauncher instance;

protected override void Start()
public static SCANappLauncher Instance
{
get { return instance; }
}

public SCANsat.Unity.Unity.SCAN_Toolbar UIElement
{
get { return uiElement; }
}

public ApplicationLauncherButton SCANAppButton
{
get { return SCANappLauncherButton; }
}

public Canvas TooltipCanvas
{
get { return UIMasterController.Instance.tooltipCanvas; }
}

public bool TooltipsOn
{
get { return SCAN_Settings_Config.Instance.WindowTooltips; }
}

public bool IsVisible
{
get { return _sticky && uiElement != null; }
}

public float Scale
{
get { return GameSettings.UI_SCALE_APPS; }
}

public void ProcessTooltips()
{
if (uiElement != null)
uiElement.ProcessTooltips();
}

private void Start()
{
instance = this;

setupToolbar();

GameEvents.OnGameSettingsApplied.Add(settingsApplied);
}

protected override void OnDestroy()
public void ToggleToolbarType()
{
removeButton(HighLogic.LoadedScene);

StartCoroutine(addButton());
}

private void OnDestroy()
{
GameEvents.onGUIApplicationLauncherUnreadifying.Remove(removeButton);

if (SCANappLauncherButton != null)
removeButton(HighLogic.LoadedScene);

GameEvents.OnGameSettingsApplied.Remove(settingsApplied);
}

private void settingsApplied()
{
if (!_sticky || uiElement == null)
return;

uiElement.gameObject.SetActive(false);
DestroyImmediate(uiElement.gameObject);

OpenMenu();
}

private void setupToolbar()
Expand All @@ -49,7 +123,18 @@ IEnumerator addButton()

if (HighLogic.LoadedScene == GameScenes.FLIGHT)
{
SCANappLauncherButton = ApplicationLauncher.Instance.AddModApplication(toggleFlight, toggleFlight, null, null, null, null, ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.MAPVIEW, SCANskins.SCAN_SmallMapAppIcon);
if (SCAN_Settings_Config.Instance.ToolbarMenu)
{
SCANappLauncherButton = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, HoverMenuIn, HoverMenuOut, null, null, ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.MAPVIEW, SCANskins.SCAN_SmallMapAppIcon);
ApplicationLauncher.Instance.EnableMutuallyExclusive(SCANappLauncherButton);
}
else
{
SCANappLauncherButton = ApplicationLauncher.Instance.AddModApplication(toggleFlight, toggleFlight, null, null, null, null, ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.MAPVIEW, SCANskins.SCAN_SmallMapAppIcon);

if (SCAN_UI_MainMap.Instance.IsVisible)
SCANappLauncherButton.SetTrue(false);
}
}
else if (HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedScene == GameScenes.TRACKSTATION)
{
Expand All @@ -61,6 +146,12 @@ IEnumerator addButton()

private void removeButton(GameScenes scene)
{
if (uiElement != null)
{
uiElement.gameObject.SetActive(false);
Destroy(uiElement.gameObject);
}

if (SCANappLauncherButton != null)
{
ApplicationLauncher.Instance.RemoveModApplication(SCANappLauncherButton);
Expand All @@ -78,11 +169,171 @@ private void toggleFlight()

private void toggleKSC()
{
//if (SCANcontroller.controller != null)
//{
// SCANcontroller.controller.kscMap.Visible = !SCANcontroller.controller.kscMap.Visible;
// SCANcontroller.controller.kscMapVisible = !SCANcontroller.controller.kscMapVisible;
//}
if (SCAN_UI_BigMap.Instance.IsVisible)
SCAN_UI_BigMap.Instance.Close();
else
SCAN_UI_BigMap.Instance.Open();
}

private void OnTrue()
{
_sticky = true;
}

private void OnFalse()
{
_sticky = false;

CloseMenu();

uiElement = null;
}

private void HoverMenuIn()
{
_hovering = true;

if (_sticky || uiElement != null)
return;

OpenMenu();
}

private void HoverMenuOut()
{
_hovering = false;

if (!_sticky)
StartCoroutine(HoverOutWait());
}

private IEnumerator HoverOutWait()
{
int timer = 0;

while (timer < 4)
{
timer++;
yield return null;
}

if (!_inMenu)
CloseMenu();
}

public Vector3 GetAnchor()
{
if (SCANappLauncherButton == null)
return Vector3.zero;

Vector3 anchor = SCANappLauncherButton.GetAnchor();

anchor.x -= 3;
anchor.y += 20;

return anchor;
}

private void OpenMenu()
{
if (SCAN_UI_Loader.ToolbarPrefab == null)
return;

uiElement = (Instantiate(SCAN_UI_Loader.ToolbarPrefab, GetAnchor(), Quaternion.identity) as GameObject).GetComponent<SCANsat.Unity.Unity.SCAN_Toolbar>();

if (uiElement == null)
return;

uiElement.transform.SetParent(UIMasterController.Instance.appCanvas.transform);

uiElement.Setup(this);
}

private void CloseMenu()
{
if (uiElement != null)
uiElement.FadeOut();
}

public bool MainMap
{
get { return SCAN_UI_MainMap.Instance.IsVisible; }
set
{
if (value)
SCAN_UI_MainMap.Instance.Open();
else
SCAN_UI_MainMap.Instance.Close();
}
}

public bool BigMap
{
get { return SCAN_UI_BigMap.Instance.IsVisible; }
set
{
if (value)
SCAN_UI_BigMap.Instance.Open();
else
SCAN_UI_BigMap.Instance.Close();
}
}

public bool ZoomMap
{
get { return SCANcontroller.controller.zoomMap.Visible; }
set
{
SCANcontroller.controller.zoomMap.Visible = !SCANcontroller.controller.zoomMap.Visible;
}
}

public bool Overlay
{
get { return SCAN_UI_Overlay.Instance.IsVisible; }
set
{
if (value)
SCAN_UI_Overlay.Instance.Open();
else
SCAN_UI_Overlay.Instance.Close();
}
}

public bool Instruments
{
get { return SCAN_UI_Instruments.Instance.IsVisible; }
set
{
if (value)
SCAN_UI_Instruments.Instance.Open();
else
SCAN_UI_Instruments.Instance.Close();
}
}

public bool Settings
{
get { return SCAN_UI_Settings.Instance.IsVisible; }
set
{
if (value)
SCAN_UI_Settings.Instance.Open();
else
SCAN_UI_Settings.Instance.Close();
}
}

public bool InMenu
{
get { return _inMenu; }
set
{
_inMenu = value;

if (!value && !_hovering && !_sticky && uiElement != null)
CloseMenu();
}
}
}
}

0 comments on commit 8528295

Please sign in to comment.