-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.cs
191 lines (162 loc) · 6.74 KB
/
Plugin.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using BepInEx;
using BepInEx.Logging;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using HarmonyLib;
using System.Reflection;
using UnityEngine;
namespace ShowCombatEncounterDetail_bepin5;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class InfarctusPluginCombatEncounterInfo : BaseUnityPlugin
{
public static string ToolTip_CardName = "";
public static bool isPVEEncounter = false;
public static GameObject canvasObjectBoard=null;
public static GameObject imageObjectBoard=null;
public static GameObject canvasObjectWeapon=null;
public static GameObject imageObjectWeapon=null;
public static Canvas canvas_board;
public static Canvas canvas_items;
internal static new ManualLogSource Logger;
private void Awake()
{
// Plugin startup logic
Logger = base.Logger;
Logger.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
}
public static void initalization(){
clean_destroy();
canvasObjectBoard = new GameObject("ImageCanvasBoard");
canvasObjectWeapon = new GameObject("ImageCanvasWeapon");
imageObjectBoard = new GameObject("DisplayedImageBoard");
imageObjectWeapon = new GameObject("DisplayedImageWeapon");
}
public static void clean_destroy(){
if(canvasObjectBoard!=null){
Destroy(canvasObjectBoard);
canvasObjectBoard=null;
}
if(canvasObjectWeapon!=null){
Destroy(canvasObjectWeapon);
canvasObjectWeapon=null;
}
if(imageObjectBoard!=null){
Destroy(imageObjectBoard);
imageObjectBoard=null;
}
if(imageObjectWeapon!=null){
Destroy(imageObjectWeapon);
imageObjectWeapon=null;
}
if(canvas_board!=null){
Destroy(canvas_board);
canvas_board=null;
}
if(canvas_items!=null){
Destroy(canvas_items);
canvas_items=null;
}
}
public static void CreateImageDisplayFromCardName()
{
if(isPVEEncounter && ToolTip_CardName!=""){
clean_destroy();
Logger.LogDebug("Creating Image Display for "+ToolTip_CardName);
initalization();
CreateImageDisplay(ToolTip_CardName +"/board",-0.25f,-0.25f,canvasObjectBoard,imageObjectBoard,canvas_board);
CreateImageDisplay(ToolTip_CardName +"/items",0.25f,-0.25f,canvasObjectWeapon,imageObjectWeapon,canvas_items);
}
}
private static void CreateImageDisplay(string filename,float relativeposX,float relativeposY,GameObject canvasObject,GameObject imageObject,Canvas canvas, string extension = ".png")
{
if(!File.Exists("BepInEx/plugins/ShowCombatEncounterDetail/Assets/" + filename + extension)){
return;
}
// Get the camera size to set the image size proportionally
var (width, height) = GetMainCameraSize();
if (width == 0 || height == 0)
{
Logger.LogWarning("Main Camera not found, unable to create image display.");
return;
}
// Set up the canvas
canvas = canvasObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.sortingOrder = 100;
// Add a CanvasScaler to handle scaling
CanvasScaler canvasScaler = canvasObject.AddComponent<CanvasScaler>();
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = new Vector2((float)width, (float)height);
// Initialize the imageObject as a child of canvasObject
imageObject.transform.SetParent(canvasObject.transform);
Image imageComponent = imageObject.AddComponent<Image>();
Texture2D image_texture = null;
// Load the image as a Texture2D
try{
image_texture = LoadTextureFromFile("BepInEx/plugins/ShowCombatEncounterDetail/Assets/" + filename + extension);
}catch{}
if (image_texture != null)
{
// Convert Texture2D to a Sprite
Rect rect = new Rect(0, 0, image_texture.width, image_texture.height);
Sprite sprite = Sprite.Create(image_texture, rect, new Vector2(0.5f, 0.5f));
imageComponent.sprite = sprite;
// Calculate the aspect ratio of the original image
float imageAspectRatio = (float)image_texture.width / image_texture.height;
// Set the size of the Image while preserving aspect ratio
float scaleFactor = 0.5f; // Adjust this scale factor to fit your needs
float displayWidth = (float)width * scaleFactor;
float displayHeight = displayWidth / imageAspectRatio; // Calculate height based on aspect ratio
if(displayHeight> (float)height/2){
float coeff_reduce = (float)height/2.0f/displayHeight;
displayHeight*=coeff_reduce;
displayWidth*=coeff_reduce;
}
RectTransform rectTransform = imageObject.GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(displayWidth, displayHeight);
// Center the image on screen
rectTransform.anchoredPosition = new Vector2((float)(width * relativeposX), (float)(height * relativeposY));
}
else
{
Logger.LogError("Failed to load image texture.");
}
}
private static Texture2D LoadTextureFromFile(string filePath)
{
if (File.Exists(filePath))
{
byte[] fileData = File.ReadAllBytes(filePath);
Texture2D texture = new Texture2D(2, 2); // Create a small texture to be replaced
if (texture.LoadImage(fileData)) // Load the image file data
{
return texture;
}
}
return null;
}
private static (double,double) GetMainCameraSize()
{
// Attempt to find the camera at the specified path
GameObject cameraObject = GameObject.Find("DefaultSceneCameras/MainCamera");
if (cameraObject != null)
{
Camera mainCamera = cameraObject.GetComponent<Camera>();
if (mainCamera != null)
{
// Log the scaledPixelWidth and scaledPixelHeight
int width = mainCamera.scaledPixelWidth;
int height = mainCamera.scaledPixelHeight;
return (width, height);
}
else
{
Logger.LogWarning("Camera component not found on MainCamera object.");
return (0,0);
}
}
return (0,0);
}
}