forked from RequiDev/ReMod.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationSystem.cs
215 lines (177 loc) · 9.29 KB
/
NotificationSystem.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using MelonLoader;
using UnhollowerRuntimeLib;
using UnityEngine;
using Logger = VRC.Core.Logger;
using Object = UnityEngine.Object;
namespace ReMod.Core.Notification
{
public class NotificationSystem
{
public static Sprite DefaultIcon;
public static Color DefaultColour = new Color(0.1764f, 0.2549f, .3333f, 1f);
public static MelonPreferences_Entry<float> NotificationAlpha;
public static MelonPreferences_Entry<string> NotificationAlignment;
public static bool UseVRChatNotificationSystem;
//AssetBundle Parts
private static AssetBundle _notifBundle;
private static GameObject _notificationPrefab;
private static GameObject _hudContent;
private static GameObject _notificationGO;
private static RectTransform _notificationRect;
private static NotificationController _controllerInstance;
public static void SetupNotifications()
{
_hudContent = GameObject.Find("/UserInterface/UnscaledUI/HudContent_Old");
var notificationTransform = _hudContent.transform.Find("Notification (Clone)");
if (notificationTransform != null)
{
//Notification system already initialized
_notificationGO = notificationTransform.gameObject;
_controllerInstance = _notificationGO.GetComponent<NotificationController>();
}
LoadAssets();
NotificationController.RegisterSafe();
MelonPreferences.CreateCategory("ReModCore", "ReMod.Core");
NotificationAlpha = MelonPreferences.CreateEntry("ReModCore", "NotificationAlpha", .7f, "Notification Alpha", "Controls transparency of the notification system.");
NotificationAlignment = MelonPreferences.CreateEntry("ReModCore", "NotificationAlignment", "centerMiddle", "Notification Alignment");
NotificationAlignment.OnValueChanged += UpdateNotificationAlignment;
//Create UIX settings enum
RegSettingsEnum("ReModCore", "NotificationAlignment", new[] {("centerMiddle", "Middle Centered"), ("topCenter", "Top Centered"), ("topLeft", "Top Left"), ("topRight", "Top Right"), ("bottomCenter", "Bottom Centered"), ("bottomLeft", "Bottom Left"), ("bottomRight", "Bottom Right")});
if (_notificationPrefab == null)
throw new Exception("NotificationSystem failed to load, prefab missing!");
//Instantiate prefab and let NotificationController setup!
_notificationGO = Object.Instantiate(_notificationPrefab, _hudContent.transform);
_controllerInstance = _notificationGO.AddComponent<NotificationController>();
//Get the RectTransform for us to set the alignment
_notificationRect = _notificationGO.GetComponent<RectTransform>();
UpdateNotificationAlignment(null, null);
_controllerInstance.defaultSprite = DefaultIcon;
}
/// <summary>
/// Enqueue a new notification
/// </summary>
/// <param name="title">Title shown in the top of the notification</param>
/// <param name="description">Main description, scales based on size</param>
/// <param name="displayLength">How long in seconds you want it shown</param>
/// <param name="icon">Optional icon sprite, defaults to Megaphone</param>
public static void EnqueueNotification(string title, string description, float displayLength = 5f, Sprite icon = null)
{
EnqueueNotification(title, description, DefaultColour, displayLength, icon);
}
/// <summary>
/// Enqueue a new notification
/// </summary>
/// <param name="title">Title shown in the top of the notification</param>
/// <param name="description">Main description, scales based on size</param>
/// <param name="backgroundColour">Background colour of the notification</param>
/// <param name="displayLength">How long in seconds you want it shown</param>
/// <param name="icon">Optional icon sprite, defaults to Megaphone</param>
public static void EnqueueNotification(string title, string description, Color backgroundColour, float displayLength = 5f, Sprite icon = null)
{
var notif = new NotificationObject(title, description, icon, displayLength, backgroundColour);
if(_controllerInstance == null)
SetupNotifications();
_controllerInstance.EnqueueNotification(notif);
}
public static void ClearNotification()
{
if(_controllerInstance == null)
SetupNotifications();
_controllerInstance.ClearNotifications();
}
private static void UpdateNotificationAlignment(string sender, string args)
{
if (_notificationRect == null) return;
switch (NotificationAlignment.Value)
{
case "centerMiddle":
_notificationRect.anchorMin = new Vector2(0.5f, 0.5f);
_notificationRect.anchorMax = new Vector2(0.5f, 0.5f);
_notificationRect.pivot = new Vector2(0.5f, 0.5f);
break;
case "topCenter":
_notificationRect.anchorMin = new Vector2(0.5f, 1f);
_notificationRect.anchorMax = new Vector2(0.5f, 1f);
_notificationRect.pivot = new Vector2(0.5f, 1f);
break;
case "topLeft":
_notificationRect.anchorMin = new Vector2(0f, 1f);
_notificationRect.anchorMax = new Vector2(0f, 1f);
_notificationRect.pivot = new Vector2(0f, 1f);
break;
case "topRight":
_notificationRect.anchorMin = new Vector2(1f, 1f);
_notificationRect.anchorMax = new Vector2(1f, 1f);
_notificationRect.pivot = new Vector2(1f, 1f);
break;
case "bottomCenter":
_notificationRect.anchorMin = new Vector2(0.5f, 0f);
_notificationRect.anchorMax = new Vector2(0.5f, 0f);
_notificationRect.pivot = new Vector2(0.5f, 0f);
break;
case "bottomLeft":
_notificationRect.anchorMin = new Vector2(0f, 0f);
_notificationRect.anchorMax = new Vector2(0f, 0f);
_notificationRect.pivot = new Vector2(0f, 0f);
break;
case "bottomRight":
_notificationRect.anchorMin = new Vector2(1f, 0f);
_notificationRect.anchorMax = new Vector2(1f, 0f);
_notificationRect.pivot = new Vector2(1f, 0f);
break;
}
}
private static void LoadAssets()
{
using (var assetStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ReMod.Core.Notification.notification"))
{
if (assetStream != null)
{
using var tempStream = new MemoryStream((int) assetStream.Length);
assetStream.CopyTo(tempStream);
_notifBundle = AssetBundle.LoadFromMemory_Internal(tempStream.ToArray(), 0);
_notifBundle.hideFlags |= HideFlags.DontUnloadUnusedAsset;
}
}
if (_notifBundle != null)
{
//Load Sprites
DefaultIcon = _notifBundle.LoadAsset_Internal("Megaphone", Il2CppType.Of<Sprite>()).Cast<Sprite>();
DefaultIcon.hideFlags |= HideFlags.DontUnloadUnusedAsset;
//Prefab
_notificationPrefab = _notifBundle.LoadAsset_Internal("Notification", Il2CppType.Of<GameObject>()).Cast<GameObject>();
_notificationPrefab.hideFlags |= HideFlags.DontUnloadUnusedAsset;
}
}
#region UIXAdapter
private static bool? _uixAvailable;
private static MethodInfo _regSettingEnum;
private static bool _methodsGetRan;
private static bool IsUIXAvailable()
{
_uixAvailable ??= MelonHandler.IsModAlreadyLoaded("UI Expansion Kit");
return _uixAvailable.Value;
}
private static bool GetUIXMethods()
{
if (_methodsGetRan) return true;
Type expandedMenu = Type.GetType("UIExpansionKit.API.ExpansionKitApi, UIExpansionKit");
if (expandedMenu == null) return false;
_regSettingEnum = expandedMenu.GetMethod("RegisterSettingAsStringEnum", BindingFlags.Public | BindingFlags.Static);
_methodsGetRan = true;
return true;
}
private static bool RegSettingsEnum(string settingsCat, string settingsName, IList<(string value, string desc)> values)
{
if (!IsUIXAvailable()) return false;
if (!GetUIXMethods()) return false;
_regSettingEnum.Invoke(null, new object[] { settingsCat, settingsName, values });
return true;
}
#endregion
}
}