Skip to content

Commit

Permalink
Merge pull request #116 from SpaceWarpDev/close_buttons
Browse files Browse the repository at this point in the history
Added close buttons
  • Loading branch information
cheese3660 authored Mar 3, 2023
2 parents eb8959c + ea6e823 commit a9d2428
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions SpaceWarp/API/UI/Appbar/Appbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static T RegisterGameAppbarMenu<T>(string text, string title, string id,
toolBarUIObject.Persist();
AppbarMenu menu = toolBarUIObject.AddComponent<T>();
menu.Title = title;
menu.ID = id;
toolBarUIObject.transform.SetParent(Chainloader.ManagerObject.transform);
toolBarUIObject.SetActive(true);
ButtonsToBeLoaded.Add((text, icon, id, menu.ToggleGUI));
Expand Down
25 changes: 22 additions & 3 deletions SpaceWarp/API/UI/Appbar/AppbarMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KSP.Game;
using KSP.Sim.impl;
using KSP.UI.Binding;
using SpaceWarp.API.Assets;
using UnityEngine;

Expand All @@ -9,7 +10,9 @@ public abstract class AppbarMenu : KerbalBehavior
{
private GUISkin _spaceWarpConsoleSkin = null;
private bool _drawing = false;

internal string ID;
private GUIStyle _closeButtonStyle;

public abstract float Width
{
get;
Expand All @@ -36,7 +39,7 @@ public virtual GUISkin Skin
{
if (_spaceWarpConsoleSkin == null)
{
AssetManager.TryGetAsset($"space_warp/swconsoleui/swconsoleUI/spacewarpConsole.guiskin", out _spaceWarpConsoleSkin);
_spaceWarpConsoleSkin = Skins.ConsoleSkin;
}

return _spaceWarpConsoleSkin;
Expand All @@ -55,8 +58,12 @@ public void OnGUI()
{
if (!_drawing
|| GameManager.Instance.Game.GlobalGameState.GetState() != GameState.FlightView) return;

GUI.skin = Skin;

_closeButtonStyle ??= new GUIStyle(GUI.skin.button)
{
fontSize = 8
};
int controlID = GUIUtility.GetControlID(FocusType.Passive);

GUILayoutOption width = GUILayout.Width(Width);
Expand All @@ -77,9 +84,21 @@ public void ToggleGUI()

private void DoDrawing(int windowID)
{
Rect closeButtonRect = new Rect(Width - 23, 6, 16, 16);
if (GUI.Button(new Rect(_windowRect.width - 18, 2, 16, 16), "x", _closeButtonStyle))
{
CloseWindow();
GUIUtility.ExitGUI();
}
DrawWindow(windowID);
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}

public abstract void DrawWindow(int windowID);

public void CloseWindow()
{
ToggleGUI(false);
GameObject.Find(ID)?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(false);
}
}
18 changes: 14 additions & 4 deletions SpaceWarp/UI/ModListUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using KSP.Game;
using SpaceWarp.API.Mods.JSON;
using UnityEngine;
using BepInEx.Configuration;

namespace SpaceWarp.UI;

Expand All @@ -18,6 +19,8 @@ public class ModListUI : KerbalMonoBehaviour
private static Vector2 _scrollPositionMods;
private static Vector2 _scrollPositionInfo;

private static GUIStyle _closeButtonStyle = null;

private ModInfo _selectedMetaData = null;

public void Start()
Expand Down Expand Up @@ -46,6 +49,11 @@ private void OnGUI()
return;
}

_closeButtonStyle ??= new GUIStyle(GUI.skin.button)
{
fontSize = 8
};

int controlID = GUIUtility.GetControlID(FocusType.Passive);
const string header = "spacewarp.modlist";
GUILayoutOption width = GUILayout.Width((float)(_windowWidth * 0.8));
Expand All @@ -66,6 +74,11 @@ private void Update()
private void FillWindow(int windowID)
{
_boxStyle = GUI.skin.GetStyle("Box");
if (GUI.Button(new Rect(_windowRect.width - 18, 2, 16, 16), "x", _closeButtonStyle))
{
_drawUI = false;
GUIUtility.ExitGUI();
}
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();

Expand Down Expand Up @@ -99,11 +112,8 @@ private void FillWindow(int windowID)
GUILayout.EndScrollView();
GUILayout.EndVertical();
}

GUILayout.EndHorizontal();
if (GUILayout.Button("Close"))
{
ToggleVisible();
}
GUI.DragWindow();
}

Expand Down
25 changes: 19 additions & 6 deletions SpaceWarp/UI/SpaceWarpConsole.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KSP.Game;
using KSP.Sim.impl;
using KSP.UI.Binding;
using SpaceWarp.API.Assets;
using SpaceWarp.API.UI.Appbar;
using UnityEngine;
Expand All @@ -22,6 +23,7 @@ public sealed class SpaceWarpConsole : KerbalMonoBehaviour
private static Vector2 _scrollView;

private string _search = "";
private GUIStyle _closeButtonStyle;

private void Awake()
{
Expand Down Expand Up @@ -50,6 +52,11 @@ private void OnGUI()
return;
}

_closeButtonStyle ??= new GUIStyle(GUI.skin.button)
{
fontSize = 8
};

int controlID = GUIUtility.GetControlID(FocusType.Passive);
string header = "spacewarp.console";
GUILayoutOption width = GUILayout.Width((float)(_windowWidth * 0.8));
Expand All @@ -68,6 +75,12 @@ private void Update()

private void DrawConsole(int windowID)
{
if (GUI.Button(new Rect(_windowRect.width - 18, 2, 16, 16), "x", _closeButtonStyle))
{
SpaceWarpManager.Logger.LogInfo("Generating console close button");
CloseWindow();
GUIUtility.ExitGUI();
}
GUILayout.BeginVertical();
_search = GUILayout.TextField(_search);
_scrollView = GUILayout.BeginScrollView(_scrollPosition, false, true);
Expand All @@ -90,12 +103,6 @@ private void DrawConsole(int windowID)
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();

if (GUILayout.Button("Close"))
{
_drawUI = false;
GUIUtility.ExitGUI();
}

if (GUILayout.Button("Clear"))
{
SpaceWarpConsoleLogListener.DebugMessages.Clear();
Expand All @@ -117,4 +124,10 @@ public void ToggleVisible(bool shouldDraw)
_drawUI = shouldDraw;
Game.ViewController.inputLockManager.SetControlLock(_drawUI ? ConsoleLocks : ControlTypes.None, ConsoleLockID);
}

public void CloseWindow()
{
ToggleVisible(false);
GameObject.Find("BTN-SWConsole")?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(false);
}
}

0 comments on commit a9d2428

Please sign in to comment.