Skip to content

Commit

Permalink
Merge pull request #1 from Falki-git/semi-transparent-overlay
Browse files Browse the repository at this point in the history
Update to 0.2.0
  • Loading branch information
Falki-git authored May 27, 2023
2 parents 61ce660 + 0eae440 commit 24b2a67
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
5 changes: 4 additions & 1 deletion CustomizableUI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2D7AEF3F-B5A9-48BD-8410-96B1FFC4436B}") = "CustomizableUI", "CustomizableUIProject\CustomizableUI.csproj", "{2EB62170-127C-40D8-8F6B-0E7D85948250}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomizableUI", "CustomizableUIProject\CustomizableUI.csproj", "{2EB62170-127C-40D8-8F6B-0E7D85948250}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -19,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8402FA6F-97E8-459E-B4D9-6320FAC66346}
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions CustomizableUI/swinfo.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"mod_id": "CustomizableUI",
"spec": "1.2",
"author": "Falki",
"name": "I Wish They Made UI Customizable",
"description": "Move flight UI elements wherever you want.",
"source": "https://github.com/Falki-git/I-Wish-They-Made-UI-Customizable",
"version": "0.1.0",
"version": "0.2.0",
"version_check": "https://raw.githubusercontent.com/Falki-git/I-Wish-They-Made-UI-Customizable/master/CustomizableUI/swinfo.json",
"dependencies": [
{
"id": "SpaceWarp",
"version": {
"min": "1.1.1",
"min": "1.2.0",
"max": "*"
}
}
Expand Down
6 changes: 4 additions & 2 deletions CustomizableUIProject/CustomizableUIPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void OnInitialized()
Appbar.RegisterAppButton(
"IWTM UI Customizable",
ToolbarFlightButtonID,
AssetManager.GetAsset<Texture2D>($"{SpaceWarpMetadata.ModID}/images/icon.png"),
AssetManager.GetAsset<Texture2D>($"{Info.Metadata.GUID}/images/icon.png"),
isOpen =>
{
UI.Instance.IsWindowOpen = isOpen;
Expand Down Expand Up @@ -65,5 +65,7 @@ private void SubscribeToMessages()
private void OnGUI()
{
UI.Instance.OnGUI();
}
}

public void Update() => Manager.Instance.Update();
}
20 changes: 11 additions & 9 deletions CustomizableUIProject/Manager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BepInEx.Logging;
using KSP.Game;
using KSP.Messages;
using KSP.UI.Binding;
using UnityEngine;

namespace CustomizableUI
Expand Down Expand Up @@ -32,6 +31,16 @@ public static Manager Instance
}
}

public void Update()
{
// 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
if (!IsInitialized && Utility.Instance.GameState == GameState.FlightView)
{
Initialize();
Utility.Instance.LoadData();
}
}

public void Initialize()
{
try
Expand All @@ -53,16 +62,10 @@ public void OnGameStateEntered(MessageCenterMessage obj)
{
_logger.LogInfo("OnGameStateEntered triggered.");

if (/*!IsInitialized && */Utility.Instance.GameState == GameState.FlightView)
if (Utility.Instance.GameState == GameState.FlightView)
{
Initialize();
Utility.Instance.LoadData();

// Temporary
/*
UI.Instance.IsWindowOpen = true;
GameObject.Find("BTN-CustomizableUI")?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(true);
*/
}

}
Expand Down Expand Up @@ -106,7 +109,6 @@ public void RecalculatePositionsOfGroupsAttachedToNavball(TopLevelGroup topGroup
group.Transform.position += deltaDistance;
group.Position += deltaDistance;
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion CustomizableUIProject/Styles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void Initialize()
private static void InitializeTextures()
{
// Icons from https://icons8.com
CloseButtonTexture = LoadTexture($"{CustomizableUIPlugin.Instance.SpaceWarpMetadata.ModID}/images/close-15.png");
CloseButtonTexture = LoadTexture($"{CustomizableUIPlugin.Instance.Info.Metadata.GUID}/images/close-15.png");
}

private static Texture2D LoadTexture(string path)
Expand Down
58 changes: 57 additions & 1 deletion CustomizableUIProject/UI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using KSP.UI.Binding;
using KSP.Game;
using KSP.UI;
using KSP.UI.Binding;
using UnityEngine;

namespace CustomizableUI
Expand All @@ -9,6 +11,8 @@ public class UI

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


private UI()
Expand Down Expand Up @@ -39,6 +43,27 @@ public void OnGUI()
GUILayout.Height(0),
GUILayout.Width(Styles.WindowWidth)
);

DrawOverlayOverSelectedGroup();

/*
scaleFactorRect = GUILayout.Window(
GUIUtility.GetControlID(FocusType.Passive),
scaleFactorRect,
a =>
{
GUILayout.BeginHorizontal();
GUILayout.Label("GameManager.Instance.Game.UI._mainCanvas.scaleFactor: ");
GUILayout.Label(String.Format("{0:F5}", GameManager.Instance.Game.UI._mainCanvas.scaleFactor));
GUILayout.EndHorizontal();
GameManager.Instance.Game.UI._mainCanvas.scaleFactor = GUILayout.HorizontalSlider(GameManager.Instance.Game.UI._mainCanvas.scaleFactor, 0, 2);
GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
},
"",
GUILayout.Height(0),
GUILayout.Width(Styles.WindowWidth)
);
*/
}
}

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

private void DrawOverlayOverSelectedGroup()
{
var topGroup = Manager.Instance.Groups[TopLevelGroup.SelectedIndex];
Rect topGroupRect = ((RectTransform)topGroup.Transform).rect;

// We calculate at what % of the screen the UI group is.
// 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.
// 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.

float topGroupPercent_X = (topGroup.Transform.position.x + 907.5f - (topGroup.ToCenterOffset.x + topGroupRect.width / 2)) / 1815.0f;
_overlay.x = Screen.width * topGroupPercent_X;

// For Y, Transform.position and IMGUI coordinates are reversed. Transfer.position counts bottom-up and IMGUI draws up-bottom. So we reverse the percent.
float topGroupPercent_Y = (topGroup.Transform.position.y + 511.5f - topGroup.ToCenterOffset.y + topGroupRect.height / 2) / 1023.0f;
_overlay.y = Screen.height * (1 - topGroupPercent_Y);

_overlay.width = topGroupRect.width * Manager.Instance.ScaleFactor;
_overlay.height = topGroupRect.height * Manager.Instance.ScaleFactor;

var previousColor = GUI.color;
// color the overlay in transparent yellow
GUI.color = new Color(1f, 1f, 0f, 0.75f);
_overlay = GUI.Window(
GUIUtility.GetControlID(FocusType.Passive),
_overlay,
w => { },
""
);
GUI.color = previousColor;
}

/*
private void DrawChild(ChildGroup group, int level)
{
Expand Down

0 comments on commit 24b2a67

Please sign in to comment.