-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTooltipManager.cs
More file actions
113 lines (93 loc) · 3.65 KB
/
TooltipManager.cs
File metadata and controls
113 lines (93 loc) · 3.65 KB
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
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using BepInEx.Configuration;
using BepInEx.Logging;
namespace ShowCombatEncounterDetail;
public class TooltipManager
{
private GameObject _tooltipCanvas;
private Image _tooltipImage;
public string ToolTipCardName { get; set; } = "";
public bool IsPveEncounter { get; set; } = false;
private readonly ConfigEntry<float> _configRelativePosX;
private readonly ConfigEntry<float> _configRelativePosY;
private readonly ManualLogSource _logger;
public TooltipManager(ConfigEntry<float> configRelativePosX, ConfigEntry<float> configRelativePosY, ManualLogSource logger)
{
_configRelativePosX = configRelativePosX;
_configRelativePosY = configRelativePosY;
_logger = logger;
}
public void CleanDestroy()
{
if (!_tooltipCanvas) return;
Object.Destroy(_tooltipCanvas);
_tooltipCanvas = null;
_tooltipImage = null;
}
public void CreateImageDisplayFromCardName()
{
if (!IsPveEncounter || string.IsNullOrEmpty(ToolTipCardName) || TheBazaar.Data.IsInCombat)
{
return;
}
_logger.LogDebug($"Creating Image Display for {ToolTipCardName}");
string imagePath = $"{BepInEx.Paths.PluginPath}/ShowCombatEncounterDetail/Assets/{ToolTipCardName}.png";
if (!File.Exists(imagePath))
{
return;
}
CleanDestroy();
CreateTooltipCanvas(imagePath);
}
private void CreateTooltipCanvas(string imagePath)
{
_tooltipCanvas = new GameObject("TooltipCanvas");
var canvas = _tooltipCanvas.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.sortingOrder = 100;
var scaler = _tooltipCanvas.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(Screen.width, Screen.height);
var imageObj = new GameObject("TooltipImage");
imageObj.transform.SetParent(_tooltipCanvas.transform, false);
_tooltipImage = imageObj.AddComponent<Image>();
var texture = LoadTextureFromFile(imagePath);
if (!texture)
{
_logger.LogError("Failed to load image texture.");
CleanDestroy();
return;
}
_tooltipImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
SetImageSize(texture.width, texture.height);
}
private void SetImageSize(float textureWidth, float textureHeight)
{
var screenHeight = Screen.height;
var screenWidth = Screen.width;
var imageAspectRatio = textureWidth / textureHeight;
var targetWidth = screenWidth * 0.5f;
var targetHeight = targetWidth / imageAspectRatio;
// Adjust size if height exceeds half the screen height
if (targetHeight > screenHeight * 0.5f)
{
var scale = (screenHeight * 0.5f) / targetHeight;
targetWidth *= scale;
targetHeight *= scale;
}
var rectTransform = _tooltipImage.rectTransform;
rectTransform.sizeDelta = new Vector2(targetWidth, targetHeight);
rectTransform.anchoredPosition = new Vector2(
screenWidth * _configRelativePosX.Value,
screenHeight * _configRelativePosY.Value
);
}
private static Texture2D LoadTextureFromFile(string filePath)
{
var fileData = File.ReadAllBytes(filePath);
var texture = new Texture2D(2, 2);
return texture.LoadImage(fileData) ? texture : null;
}
}