-
Notifications
You must be signed in to change notification settings - Fork 0
/
VesselRenamer.cs
152 lines (123 loc) · 4.64 KB
/
VesselRenamer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using BepInEx;
using BepInEx.Logging;
using KSP.Game;
using KSP.Sim.impl;
using KSP.UI.Binding;
using SpaceWarp;
using SpaceWarp.API.Assets;
using SpaceWarp.API.Mods;
using SpaceWarp.API.UI;
using SpaceWarp.API.UI.Appbar;
using UnityEngine;
namespace VesselRenamer
{
[BepInPlugin("com.github.ColinZeidler.VesselRenamer", "VesselRenamer", "1.2.0")]
[BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)]
public class VesselRenamerMod : BaseSpaceWarpPlugin
{
private bool showGui = false;
private ManualLogSource logger = null;
private Rect windowRect;
private string newName = "";
private bool gameInputState = true;
private GameInstance game = null;
public readonly string vesselNameInput = "VesselRenamer.Input";
private int width = 300;
private int height = 100;
public override void OnInitialized()
{
base.OnInitialized();
game = GameManager.Instance.Game;
logger = BepInEx.Logging.Logger.CreateLogSource("com.github.ColinZeidler.VesselRenamer");
Appbar.RegisterAppButton(
"Rename Vessel",
"BTN-ReV",
AssetManager.GetAsset<Texture2D>($"{SpaceWarpMetadata.ModID}/images/icon.png"),
ToggleButton);
logger.LogInfo("Initialized Plugin");
}
void ToggleButton(bool toggle)
{
showGui = toggle;
GameObject.Find("BTN-ReV")?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(toggle);
}
void Awake()
{
// Set initial position for window
windowRect = new Rect((Screen.width * 0.7f) - (width / 2), (Screen.height / 2) - (height / 2), 0, 0);
}
void OnGUI()
{
if (showGui)
{
GUI.skin = Skins.ConsoleSkin;
windowRect = GUILayout.Window(
GUIUtility.GetControlID(FocusType.Passive),
windowRect,
PopulateGui,
"<color=#696DFF>// Vessel Renamer</color>",
GUILayout.Height(height),
GUILayout.Width(width));
if (gameInputState && GUI.GetNameOfFocusedControl().Equals(vesselNameInput))
{
gameInputState = false;
game.Input.Flight.Disable();
}
if (!gameInputState && !GUI.GetNameOfFocusedControl().Equals(vesselNameInput))
{
gameInputState=true;
game.Input.Flight.Enable();
}
} else
{
if (!gameInputState)
{
gameInputState = true;
game.Input.Flight.Enable();
}
}
}
private void PopulateGui(int WindowID)
{
VesselComponent vessel = game.ViewController.GetActiveVehicle(true)?.GetSimVessel(true);
GUILayout.BeginVertical();
var exitRect = new Rect(windowRect.width - 18, 2, 16, 16);
string exitFont = "<size=8>x</size>";
if (exitRect.Contains(Event.current.mousePosition))
{
exitFont = "<color=red><size=8>x</size></color>";
}
if (GUI.Button(exitRect, exitFont))
{
if (showGui)
{
ToggleButton(false);
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Active Vehicle:");
GUILayout.FlexibleSpace();
GUILayout.Label(vessel?.Name);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("New name:");
GUI.SetNextControlName(vesselNameInput);
newName = GUILayout.TextField(newName);
GUILayout.EndHorizontal();
if (GUILayout.Button("Rename") && newName != "")
{
vessel.SimulationObject.Name = newName;
// Doesn't trigger an immediate update for everything. (Breadcrumbs)
}
// Indication to User that its safe to type, or why vessel controls aren't working
GUILayout.BeginHorizontal();
string inputStateString = gameInputState ? "Enabled" : "Disabled";
GUILayout.Label($"Game Input: {inputStateString}");
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow(new Rect(0, 0, height, width));
}
}
}