-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTooltippery.cs
113 lines (103 loc) · 4.37 KB
/
Tooltippery.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
using FrooxEngine;
using FrooxEngine.UIX;
using HarmonyLib;
using NeosModLoader;
using System;
using System.Collections.Generic;
namespace Tooltippery
{
public class Tooltippery : NeosMod
{
public override string Name => "Tooltippery";
public override string Author => "Psychpsyo";
public override string Version => "1.0.1";
public override string Link => "https://github.com/Psychpsyo/Tooltippery";
private static Dictionary<IButton, Tooltip> openTooltips = new Dictionary<IButton, Tooltip>();
public static List<Func<IButton, ButtonEventData, string>> labelProviders = new List<Func<IButton, ButtonEventData, string>>();
[AutoRegisterConfigKey]
public static ModConfigurationKey<BaseX.color> textColor = new ModConfigurationKey<BaseX.color>("Text Color", "Sets the text color of a tooltip.", () => new BaseX.color(1, 1, 1, 1));
[AutoRegisterConfigKey]
public static ModConfigurationKey<BaseX.color> bgColor = new ModConfigurationKey<BaseX.color>("Background Color", "Sets the background color of a tooltip.", () => new BaseX.color(0, 0, 0, .75f));
[AutoRegisterConfigKey]
public static ModConfigurationKey<float> textScale = new ModConfigurationKey<float>("Text Size", "Sets the size of the text on a tooltip.", () => 1);
public static ModConfiguration config;
public override void OnEngineInit()
{
config = GetConfiguration();
labelProviders.Insert(0, commentLabels);
Harmony harmony = new Harmony("Psychpsyo.Tooltippery");
harmony.PatchAll();
}
public static Tooltip showTooltip(string label, Slot parent, BaseX.float3 localPosition)
{
Tooltip newTooltip = new Tooltip(label, parent, localPosition);
return newTooltip;
}
public static void hideTooltip(Tooltip tooltip)
{
tooltip.hide();
}
private static string determineLabel(IButton button, ButtonEventData eventData)
{
string label = null;
foreach (Func<IButton, ButtonEventData, string> provider in labelProviders)
{
label = provider(button, eventData);
if (label != null)
{
return label;
}
}
return null;
}
private static string commentLabels(IButton button, ButtonEventData eventData)
{
string comment = button.Slot.GetComponent<Comment>()?.Text.Value;
if (comment == null) return null;
if (comment.StartsWith("TooltipperyLabel:")) return comment.Substring(17);
return null;
}
// UIX button tooltips
[HarmonyPatch(typeof(Button), "RunHoverEnter")]
class ButtonTooltipOpen
{
static void Postfix(Button __instance, ButtonEventData eventData)
{
if (openTooltips.ContainsKey(__instance)) return;
string label = determineLabel(__instance, eventData);
if (label != null)
{
Slot tooltipParent = __instance.Slot.GetComponentInParents<Canvas>().Slot;
openTooltips.Add(__instance, showTooltip(label, tooltipParent, new BaseX.float3(eventData.localPoint.X, eventData.localPoint.Y, -1 * __instance.World.LocalUserViewScale.Z * (.001f / tooltipParent.GlobalScale.Z))));
}
}
}
[HarmonyPatch(typeof(Button), "RunHoverLeave")]
class ButtonTooltipClose
{
static void Postfix(Button __instance)
{
Tooltip toClose;
if (openTooltips.TryGetValue(__instance, out toClose))
{
openTooltips.Remove(__instance);
hideTooltip(toClose);
}
}
}
// closes all tooltips for a button when that button gets destroyed.
[HarmonyPatch(typeof(Button), "OnDispose")]
class ButtonTooltipDispose
{
static void Postfix(Button __instance)
{
Tooltip toClose;
if (openTooltips.TryGetValue(__instance, out toClose))
{
openTooltips.Remove(__instance);
hideTooltip(toClose);
}
}
}
}
}