diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2025-02-24 12:19:59 -0500 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2025-02-24 12:19:59 -0500 |
| commit | 6494004f64d93381768e95eec04a886ac53df838 (patch) | |
| tree | 024e1fca259db36a4b0c6d3ba501fcf9f61694ba /Notifications.cs | |
| parent | 42bc500a77f4b29d952058aede6abfaf91bd74c8 (diff) | |
| download | manifold-garden-archipelago-6494004f64d93381768e95eec04a886ac53df838.tar.gz manifold-garden-archipelago-6494004f64d93381768e95eec04a886ac53df838.tar.bz2 manifold-garden-archipelago-6494004f64d93381768e95eec04a886ac53df838.zip | |
Created basic notifications
They don't look great yet but hey we can see items being sent out or received in game now!
Diffstat (limited to 'Notifications.cs')
| -rw-r--r-- | Notifications.cs | 205 |
1 files changed, 205 insertions, 0 deletions
| diff --git a/Notifications.cs b/Notifications.cs new file mode 100644 index 0000000..20e5e51 --- /dev/null +++ b/Notifications.cs | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | using System; | ||
| 2 | using System.Collections.Generic; | ||
| 3 | using TMPro; | ||
| 4 | using UnityEngine; | ||
| 5 | |||
| 6 | namespace ManifoldGardenArchipelago | ||
| 7 | { | ||
| 8 | public class NotificationManager : MonoBehaviour | ||
| 9 | { | ||
| 10 | public static NotificationManager InitializeNotifications(UIManager uiManager) | ||
| 11 | { | ||
| 12 | GameObject gameObject = new("Notifications"); | ||
| 13 | gameObject.transform.parent = uiManager.transform; | ||
| 14 | |||
| 15 | Vector3 groupPosition = new(1260f, 680f, 0f); | ||
| 16 | gameObject.transform.set_position_Injected(ref groupPosition); | ||
| 17 | |||
| 18 | NotificationManager notificationManager = gameObject.AddComponent<NotificationManager>(); | ||
| 19 | |||
| 20 | Plugin.Logger.LogInfo("Notification manager initialized"); | ||
| 21 | |||
| 22 | return notificationManager; | ||
| 23 | } | ||
| 24 | |||
| 25 | public const int MAX_VISIBLE = 5; | ||
| 26 | |||
| 27 | private readonly List<NotificationLine> _notifications = []; | ||
| 28 | private readonly List<string> _queuedMessages = []; | ||
| 29 | private int _counter = 0; | ||
| 30 | |||
| 31 | void Update() | ||
| 32 | { | ||
| 33 | while (_notifications.Count > 0 && _notifications[_notifications.Count - 1].ReadyToBeDestroyed()) | ||
| 34 | { | ||
| 35 | NotificationLine toDestroy = _notifications[_notifications.Count - 1]; | ||
| 36 | GameObject.Destroy(toDestroy.gameObject); | ||
| 37 | _notifications.RemoveAt(_notifications.Count - 1); | ||
| 38 | } | ||
| 39 | |||
| 40 | if (_queuedMessages.Count > 0) | ||
| 41 | { | ||
| 42 | if (_notifications.Count > 0 && !_notifications[0].ReadyToBeSurplanted()) | ||
| 43 | { | ||
| 44 | if (!_notifications[0].IsMoving() && | ||
| 45 | !(_notifications.Count >= MAX_VISIBLE && !_notifications[_notifications.Count - 1].IsFadingOut())) | ||
| 46 | { | ||
| 47 | foreach (var notification in _notifications) | ||
| 48 | { | ||
| 49 | notification.SlideDown(); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | else | ||
| 54 | { | ||
| 55 | foreach (var notification in _notifications) | ||
| 56 | { | ||
| 57 | notification.ConfirmMovement(); | ||
| 58 | } | ||
| 59 | |||
| 60 | GameObject newNotify = new($"Message_{_counter}"); | ||
| 61 | _counter += 1; | ||
| 62 | |||
| 63 | newNotify.transform.parent = transform; | ||
| 64 | |||
| 65 | Vector3 freshPos = new(); | ||
| 66 | newNotify.transform.set_localPosition_Injected(ref freshPos); | ||
| 67 | |||
| 68 | TextMeshProUGUI textMeshPro = newNotify.AddComponent<TextMeshProUGUI>(); | ||
| 69 | textMeshPro.alignment = TextAlignmentOptions.TopRight; | ||
| 70 | textMeshPro.enableWordWrapping = false; | ||
| 71 | textMeshPro.text = _queuedMessages[0]; | ||
| 72 | textMeshPro.alpha = 0f; | ||
| 73 | textMeshPro.autoSizeTextContainer = true; | ||
| 74 | |||
| 75 | Vector2 pivot = new(1f, 0.5f); | ||
| 76 | textMeshPro.rectTransform.set_pivot_Injected(ref pivot); | ||
| 77 | |||
| 78 | _queuedMessages.RemoveAt(0); | ||
| 79 | |||
| 80 | NotificationLine newLine = newNotify.AddComponent<NotificationLine>(); | ||
| 81 | _notifications.Insert(0, newLine); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | public void AddMessage(string message) | ||
| 87 | { | ||
| 88 | _queuedMessages.Add(message); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | public class NotificationLine : MonoBehaviour | ||
| 93 | { | ||
| 94 | public const float ALPHA_SPEED = 1f; | ||
| 95 | public const float SLIDE_SPEED = 75f; | ||
| 96 | public const float SLIDE_DISPLACEMENT = 50f; | ||
| 97 | |||
| 98 | private enum FadeState | ||
| 99 | { | ||
| 100 | FadingIn, | ||
| 101 | Waiting, | ||
| 102 | FadingOut, | ||
| 103 | ReadyToDestroy, | ||
| 104 | } | ||
| 105 | |||
| 106 | private enum MoveState | ||
| 107 | { | ||
| 108 | Waiting, | ||
| 109 | Moving, | ||
| 110 | Ready, | ||
| 111 | } | ||
| 112 | |||
| 113 | private TextMeshProUGUI _textMeshPro; | ||
| 114 | |||
| 115 | private float _alphaTarget = 1f; | ||
| 116 | private FadeState _fadeState = FadeState.FadingIn; | ||
| 117 | private float _waitTimer = 10f; | ||
| 118 | |||
| 119 | private MoveState _moveState = MoveState.Waiting; | ||
| 120 | private float _targetY = 0f; | ||
| 121 | |||
| 122 | void Start() | ||
| 123 | { | ||
| 124 | _textMeshPro = GetComponent<TextMeshProUGUI>(); | ||
| 125 | } | ||
| 126 | |||
| 127 | void Update() | ||
| 128 | { | ||
| 129 | if (_textMeshPro.alpha != _alphaTarget) | ||
| 130 | { | ||
| 131 | float alpha = _textMeshPro.alpha; | ||
| 132 | |||
| 133 | if (alpha < _alphaTarget) | ||
| 134 | { | ||
| 135 | alpha = Math.Min(_alphaTarget, alpha + ALPHA_SPEED * Time.deltaTime); | ||
| 136 | } | ||
| 137 | else | ||
| 138 | { | ||
| 139 | alpha = Math.Max(_alphaTarget, alpha - ALPHA_SPEED * Time.deltaTime); | ||
| 140 | } | ||
| 141 | |||
| 142 | _textMeshPro.alpha = alpha; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (_fadeState == FadeState.FadingIn) | ||
| 146 | { | ||
| 147 | if (_textMeshPro.alpha >= _alphaTarget) | ||
| 148 | { | ||
| 149 | _fadeState = FadeState.Waiting; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | else if (_fadeState == FadeState.Waiting) | ||
| 153 | { | ||
| 154 | _waitTimer -= ALPHA_SPEED * Time.deltaTime; | ||
| 155 | |||
| 156 | if (_waitTimer < 0) | ||
| 157 | { | ||
| 158 | _fadeState = FadeState.FadingOut; | ||
| 159 | _alphaTarget = 0f; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | else if (_fadeState == FadeState.FadingOut) | ||
| 163 | { | ||
| 164 | if (_textMeshPro.alpha <= _alphaTarget) | ||
| 165 | { | ||
| 166 | _fadeState = FadeState.ReadyToDestroy; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | if (_moveState == MoveState.Moving) | ||
| 171 | { | ||
| 172 | if (transform.localPosition.y > _targetY) | ||
| 173 | { | ||
| 174 | Vector3 localPos = transform.localPosition; | ||
| 175 | localPos.y = Math.Max(_targetY, localPos.y - SLIDE_SPEED * Time.deltaTime); | ||
| 176 | transform.set_localPosition_Injected(ref localPos); | ||
| 177 | |||
| 178 | if (transform.localPosition.y <= _targetY) | ||
| 179 | { | ||
| 180 | _moveState = MoveState.Ready; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | public bool ReadyToBeSurplanted() => _moveState == MoveState.Ready; | ||
| 187 | |||
| 188 | public bool IsMoving() => _moveState == MoveState.Moving; | ||
| 189 | |||
| 190 | public void SlideDown() | ||
| 191 | { | ||
| 192 | _moveState = MoveState.Moving; | ||
| 193 | _targetY = transform.localPosition.y - SLIDE_DISPLACEMENT; | ||
| 194 | } | ||
| 195 | |||
| 196 | public void ConfirmMovement() | ||
| 197 | { | ||
| 198 | _moveState = MoveState.Waiting; | ||
| 199 | } | ||
| 200 | |||
| 201 | public bool IsFadingOut() => _fadeState == FadeState.FadingOut; | ||
| 202 | |||
| 203 | public bool ReadyToBeDestroyed() => _fadeState == FadeState.ReadyToDestroy; | ||
| 204 | } | ||
| 205 | } | ||
