Skip to content

Commit 24b2a67

Browse files
authored
Merge pull request #1 from Falki-git/semi-transparent-overlay
Update to 0.2.0
2 parents 61ce660 + 0eae440 commit 24b2a67

File tree

6 files changed

+80
-17
lines changed

6 files changed

+80
-17
lines changed

CustomizableUI.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.5.33530.505
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2D7AEF3F-B5A9-48BD-8410-96B1FFC4436B}") = "CustomizableUI", "CustomizableUIProject\CustomizableUI.csproj", "{2EB62170-127C-40D8-8F6B-0E7D85948250}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomizableUI", "CustomizableUIProject\CustomizableUI.csproj", "{2EB62170-127C-40D8-8F6B-0E7D85948250}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,4 +19,7 @@ Global
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8402FA6F-97E8-459E-B4D9-6320FAC66346}
24+
EndGlobalSection
2225
EndGlobal

CustomizableUI/swinfo.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"mod_id": "CustomizableUI",
2+
"spec": "1.2",
33
"author": "Falki",
44
"name": "I Wish They Made UI Customizable",
55
"description": "Move flight UI elements wherever you want.",
66
"source": "https://github.com/Falki-git/I-Wish-They-Made-UI-Customizable",
7-
"version": "0.1.0",
7+
"version": "0.2.0",
88
"version_check": "https://raw.githubusercontent.com/Falki-git/I-Wish-They-Made-UI-Customizable/master/CustomizableUI/swinfo.json",
99
"dependencies": [
1010
{
1111
"id": "SpaceWarp",
1212
"version": {
13-
"min": "1.1.1",
13+
"min": "1.2.0",
1414
"max": "*"
1515
}
1616
}

CustomizableUIProject/CustomizableUIPlugin.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override void OnInitialized()
3535
Appbar.RegisterAppButton(
3636
"IWTM UI Customizable",
3737
ToolbarFlightButtonID,
38-
AssetManager.GetAsset<Texture2D>($"{SpaceWarpMetadata.ModID}/images/icon.png"),
38+
AssetManager.GetAsset<Texture2D>($"{Info.Metadata.GUID}/images/icon.png"),
3939
isOpen =>
4040
{
4141
UI.Instance.IsWindowOpen = isOpen;
@@ -65,5 +65,7 @@ private void SubscribeToMessages()
6565
private void OnGUI()
6666
{
6767
UI.Instance.OnGUI();
68-
}
68+
}
69+
70+
public void Update() => Manager.Instance.Update();
6971
}

CustomizableUIProject/Manager.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BepInEx.Logging;
22
using KSP.Game;
33
using KSP.Messages;
4-
using KSP.UI.Binding;
54
using UnityEngine;
65

76
namespace CustomizableUI
@@ -32,6 +31,16 @@ public static Manager Instance
3231
}
3332
}
3433

34+
public void Update()
35+
{
36+
// I wish we don't have to do this, but initialization often fails when triggered via OnGameStateEntered, since the app.bar sometimes doesn't finish initializing when scene is triggered
37+
if (!IsInitialized && Utility.Instance.GameState == GameState.FlightView)
38+
{
39+
Initialize();
40+
Utility.Instance.LoadData();
41+
}
42+
}
43+
3544
public void Initialize()
3645
{
3746
try
@@ -53,16 +62,10 @@ public void OnGameStateEntered(MessageCenterMessage obj)
5362
{
5463
_logger.LogInfo("OnGameStateEntered triggered.");
5564

56-
if (/*!IsInitialized && */Utility.Instance.GameState == GameState.FlightView)
65+
if (Utility.Instance.GameState == GameState.FlightView)
5766
{
5867
Initialize();
5968
Utility.Instance.LoadData();
60-
61-
// Temporary
62-
/*
63-
UI.Instance.IsWindowOpen = true;
64-
GameObject.Find("BTN-CustomizableUI")?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(true);
65-
*/
6669
}
6770

6871
}
@@ -106,7 +109,6 @@ public void RecalculatePositionsOfGroupsAttachedToNavball(TopLevelGroup topGroup
106109
group.Transform.position += deltaDistance;
107110
group.Position += deltaDistance;
108111
}
109-
110112
}
111113
}
112114

CustomizableUIProject/Styles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void Initialize()
7474
private static void InitializeTextures()
7575
{
7676
// Icons from https://icons8.com
77-
CloseButtonTexture = LoadTexture($"{CustomizableUIPlugin.Instance.SpaceWarpMetadata.ModID}/images/close-15.png");
77+
CloseButtonTexture = LoadTexture($"{CustomizableUIPlugin.Instance.Info.Metadata.GUID}/images/close-15.png");
7878
}
7979

8080
private static Texture2D LoadTexture(string path)

CustomizableUIProject/UI.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using KSP.UI.Binding;
1+
using KSP.Game;
2+
using KSP.UI;
3+
using KSP.UI.Binding;
24
using UnityEngine;
35

46
namespace CustomizableUI
@@ -9,6 +11,8 @@ public class UI
911

1012
public bool IsWindowOpen;
1113
private Rect _windowRect = new Rect(Screen.width / 2 - Styles.WindowWidth / 2, 200, 0, 0);
14+
private Rect _overlay = new Rect();
15+
//private Rect scaleFactorRect = new Rect(0, 0, 0, 0);
1216

1317

1418
private UI()
@@ -39,6 +43,27 @@ public void OnGUI()
3943
GUILayout.Height(0),
4044
GUILayout.Width(Styles.WindowWidth)
4145
);
46+
47+
DrawOverlayOverSelectedGroup();
48+
49+
/*
50+
scaleFactorRect = GUILayout.Window(
51+
GUIUtility.GetControlID(FocusType.Passive),
52+
scaleFactorRect,
53+
a =>
54+
{
55+
GUILayout.BeginHorizontal();
56+
GUILayout.Label("GameManager.Instance.Game.UI._mainCanvas.scaleFactor: ");
57+
GUILayout.Label(String.Format("{0:F5}", GameManager.Instance.Game.UI._mainCanvas.scaleFactor));
58+
GUILayout.EndHorizontal();
59+
GameManager.Instance.Game.UI._mainCanvas.scaleFactor = GUILayout.HorizontalSlider(GameManager.Instance.Game.UI._mainCanvas.scaleFactor, 0, 2);
60+
GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
61+
},
62+
"",
63+
GUILayout.Height(0),
64+
GUILayout.Width(Styles.WindowWidth)
65+
);
66+
*/
4267
}
4368
}
4469

@@ -123,6 +148,37 @@ private void FillWindow(int _)
123148
GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
124149
}
125150

151+
private void DrawOverlayOverSelectedGroup()
152+
{
153+
var topGroup = Manager.Instance.Groups[TopLevelGroup.SelectedIndex];
154+
Rect topGroupRect = ((RectTransform)topGroup.Transform).rect;
155+
156+
// We calculate at what % of the screen the UI group is.
157+
// Since Transform.position (0, 0, z) indicates a pixel at the middle of the screen and since each UI group can be offset differently, we need to do some algebra hacks.
158+
// Reference resolution is 1920 x 1080, but Main canvas uses something more like 1815 x 1023 (?!). 907.5 is half of horizontal res, 511.5 half of vertical.
159+
160+
float topGroupPercent_X = (topGroup.Transform.position.x + 907.5f - (topGroup.ToCenterOffset.x + topGroupRect.width / 2)) / 1815.0f;
161+
_overlay.x = Screen.width * topGroupPercent_X;
162+
163+
// For Y, Transform.position and IMGUI coordinates are reversed. Transfer.position counts bottom-up and IMGUI draws up-bottom. So we reverse the percent.
164+
float topGroupPercent_Y = (topGroup.Transform.position.y + 511.5f - topGroup.ToCenterOffset.y + topGroupRect.height / 2) / 1023.0f;
165+
_overlay.y = Screen.height * (1 - topGroupPercent_Y);
166+
167+
_overlay.width = topGroupRect.width * Manager.Instance.ScaleFactor;
168+
_overlay.height = topGroupRect.height * Manager.Instance.ScaleFactor;
169+
170+
var previousColor = GUI.color;
171+
// color the overlay in transparent yellow
172+
GUI.color = new Color(1f, 1f, 0f, 0.75f);
173+
_overlay = GUI.Window(
174+
GUIUtility.GetControlID(FocusType.Passive),
175+
_overlay,
176+
w => { },
177+
""
178+
);
179+
GUI.color = previousColor;
180+
}
181+
126182
/*
127183
private void DrawChild(ChildGroup group, int level)
128184
{

0 commit comments

Comments
 (0)