Skip to content

Commit

Permalink
Changed mod location, updated mod UI, added Logging Console on (alt-c)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Feb 26, 2023
1 parent 85062ca commit 1ad2aa7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 8 deletions.
11 changes: 8 additions & 3 deletions SpaceWarp/API/SpaceWarpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SpaceWarpManager : Manager
private BaseModLogger _modLogger;

private const string MODS_FOLDER_NAME = "Mods";
public static string MODS_FULL_PATH = Application.dataPath + "/" + MODS_FOLDER_NAME;
public static string MODS_FULL_PATH = Directory.GetCurrentDirectory() + "/SpaceWarp/" + MODS_FOLDER_NAME;

private const string SPACE_WARP_CONFIG_FILE_NAME = "space_warp_config.json";
private static string SPACEWARP_CONFIG_FULL_PATH = MODS_FULL_PATH + "/" + SPACE_WARP_CONFIG_FILE_NAME;
Expand Down Expand Up @@ -480,13 +480,18 @@ internal void InvokePostInitializeModsAfterAllModsLoaded()
/// </summary>
private void InitModUI()
{
GameObject modUIObject = new GameObject();
GameObject modUIObject = new GameObject("Space Warp Mod UI");
DontDestroyOnLoad(modUIObject);
modUIObject.transform.SetParent(transform.parent);
ModListUI UI = modUIObject.AddComponent<ModListUI>();
UI.manager = this;

modUIObject.SetActive(true);

GameObject consoleUIObject = new GameObject("Space Warp Console");
DontDestroyOnLoad(consoleUIObject);
consoleUIObject.transform.SetParent(transform.parent);
SpaceWarpConsole con = consoleUIObject.AddComponent<SpaceWarpConsole>();
consoleUIObject.SetActive(true);
}
}
}
1 change: 1 addition & 0 deletions SpaceWarp/SpaceWarp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI\ModConfigurationUI.cs" />
<Compile Include="UI\ModListUI.cs" />
<Compile Include="UI\SpaceWarpConsole.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
14 changes: 9 additions & 5 deletions SpaceWarp/UI/ModListUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ModListUI : KerbalMonoBehaviour
private Rect windowRect;
private int windowWidth = 350;
private int windowHeight = 700;
public SpaceWarpManager manager;
public void Start()
{
if (loaded)
Expand Down Expand Up @@ -69,14 +68,19 @@ private void FillWindow(int windowID)
GUILayout.BeginVertical();
scrollPositionMods = GUILayout.BeginScrollView(scrollPositionMods, false, true,
GUILayout.Height((float)(windowHeight * 0.8)), GUILayout.Width(300));
foreach ((string modID, ModInfo modInfo) in manager.LoadedMods)
SpaceWarpManager manager;
if (ManagerLocator.TryGet(out manager))
{
if (GUILayout.Button(modID))
foreach ((string modID, ModInfo modInfo) in manager.LoadedMods)
{
selectedMod = modID;
selectedModInfo = modInfo;
if (GUILayout.Button(modID))
{
selectedMod = modID;
selectedModInfo = modInfo;
}
}
}

GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.BeginVertical();
Expand Down
122 changes: 122 additions & 0 deletions SpaceWarp/UI/SpaceWarpConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using KSP.Game;
using KSP.Sim.impl;
using SpaceWarp.API;
using SpaceWarp.API.Configuration;
using SpaceWarp.API.Managers;
using UnityEngine;

using KSP.Logging;
namespace SpaceWarp.UI
{
public class SpaceWarpConsole : KerbalBehavior
{
static bool loaded = false;
private bool drawUI = false;
private Rect windowRect;
private int windowWidth = 350;
private int windowHeight = 700;


public void Start()
{
if (loaded)
{
Destroy(this);
}

loaded = true;
}

void Awake()
{
KspLogManager.AddLogCallback(LogCallback);
windowWidth = (int)(Screen.width * 0.5f);
windowHeight = (int)(Screen.height * 0.5f);
windowRect = new Rect((Screen.width * 0.15f), (Screen.height * 0.15f),
0, 0);
}
private void OnGUI()
{
if (drawUI)
{
windowRect = GUILayout.Window(
GUIUtility.GetControlID(FocusType.Passive),
windowRect,
DrawConsole,
"Space Warp Console",
GUILayout.Width((float)(windowWidth * 0.8)),
GUILayout.Height((float)(windowHeight * 0.8))
);
}
}

private static GUIStyle boxStyle;
private static Vector2 scrollPosition;

private List<string> debugMessages = new List<string>();

private void LogCallback(string condition, string stackTrace, LogType type)
{
switch (type)
{
case LogType.Error:
debugMessages.Add($"[ERR] {condition}");
break;
case LogType.Assert:
debugMessages.Add($"[AST] {condition}");
break;
case LogType.Warning:
debugMessages.Add($"[WRN] {condition}");
break;
case LogType.Log:
debugMessages.Add($"[LOG] {condition}");
break;
case LogType.Exception:
debugMessages.Add($"[EXC] {condition}");
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
private void Update()
{
if (Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.C))
{
drawUI = !drawUI;
}
}

private void DrawConsole(int windowID)
{
boxStyle = GUI.skin.GetStyle("Box");
GUILayout.BeginVertical();
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
foreach (var debugMessage in debugMessages)
{
GUILayout.Label(debugMessage);
}

GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Close"))
{
drawUI = false;
}

if (GUILayout.Button("Clear"))
{
debugMessages.Clear();
}

if (GUILayout.Button("Clear Control Locks"))
{
KSP.Game.GameManager.Instance.Game.ViewController.inputLockManager.ClearControlLocks();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow(new Rect(0, 0, 10000, 500));
}
}
}

0 comments on commit 1ad2aa7

Please sign in to comment.