Skip to content
This repository was archived by the owner on Jan 24, 2023. It is now read-only.

Notification update #23

Merged
merged 2 commits into from
May 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Notification/NotificationController.cs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ public class NotificationController : MonoBehaviour
//Objects
private Animator _notificationAnimator;
private Image _iconImage;
private Image _backgroundImage;
private TextMeshProUGUI _titleText;
private TextMeshProUGUI _descriptionText;

@@ -68,6 +69,7 @@ public static void RegisterSafe()
private void Start()
{
_notificationAnimator = gameObject.GetComponent<Animator>();
_backgroundImage = gameObject.transform.Find("Content/Background").GetComponent<Image>();
_iconImage = gameObject.transform.Find("Content/Icon").gameObject.GetComponent<Image>();
_titleText = gameObject.transform.Find("Content/Title").gameObject.GetComponent<TextMeshProUGUI>();
_descriptionText = gameObject.transform.Find("Content/Description").gameObject.GetComponent<TextMeshProUGUI>();
@@ -96,6 +98,8 @@ private void Update()
_descriptionText.text = _currentNotification.Description;
_iconImage.sprite = _currentNotification.Icon == null ? defaultSprite : _currentNotification.Icon;
_iconImage.enabled = true;
_currentNotification.BackgroundColor.a = NotificationSystem.NotificationAlpha.Value;
_backgroundImage.color = _currentNotification.BackgroundColor;

OpenNotification();
}
4 changes: 3 additions & 1 deletion Notification/NotificationObject.cs
Original file line number Diff line number Diff line change
@@ -8,13 +8,15 @@ public class NotificationObject
public string Description;
public Sprite Icon;
public float DisplayLength;
public Color BackgroundColor;

public NotificationObject(string title, string description, Sprite icon, float displayLength)
public NotificationObject(string title, string description, Sprite icon, float displayLength, Color backgroundColor)
{
Title = title;
Description = description;
Icon = icon;
DisplayLength = displayLength;
BackgroundColor = backgroundColor;
}
}
}
117 changes: 116 additions & 1 deletion Notification/NotificationSystem.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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
@@ -18,6 +24,7 @@ public class NotificationSystem

private static GameObject _hudContent;
private static GameObject _notificationGO;
private static RectTransform _notificationRect;
private static NotificationController _controllerInstance;

public static void SetupNotifications()
@@ -36,12 +43,24 @@ public static void SetupNotifications()

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;
}
@@ -55,7 +74,20 @@ public static void SetupNotifications()
/// <param name="icon">Optional icon sprite, defaults to Megaphone</param>
public static void EnqueueNotification(string title, string description, float displayLength = 5f, Sprite icon = null)
{
var notif = new NotificationObject(title, description, icon, displayLength);
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();
@@ -71,6 +103,50 @@ public static void ClearNotification()
_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"))
@@ -96,5 +172,44 @@ private static void LoadAssets()
_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
}
}
3 changes: 3 additions & 0 deletions ReMod.Core.csproj
Original file line number Diff line number Diff line change
@@ -95,6 +95,9 @@
<HintPath>$(MlPath)\Managed\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UIModule">
<HintPath>$(MlPath)\Managed\UnityEngine.UIModule.dll</HintPath>
</Reference>
<Reference Include="VRC.UI.Core">
<HintPath>$(MlPath)\Managed\VRC.UI.Core.dll</HintPath>
<Private>False</Private>