-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMODToaster.cs
112 lines (99 loc) · 2.88 KB
/
MODToaster.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
using Assets.Scripts.Core;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace MOD.Scripts.UI
{
/// <summary>
/// This class is responsible for showing toast notifications
/// </summary>
class MODToaster
{
static MODToaster Instance;
string toastText;
MODSimpleTimer toastNotificationTimer;
public MODToaster()
{
this.toastText = "";
this.toastNotificationTimer = new MODSimpleTimer();
Instance = this;
}
public void Update()
{
toastNotificationTimer.Update();
}
public void OnGUIFragment()
{
MODStyleManager styleManager = MODStyleManager.OnGUIInstance;
if (toastNotificationTimer.Running())
{
// This scrolls the toast notification off the window when it's nearly finished
float toastYPosition = Math.Min(50f, 200f * toastNotificationTimer.timeLeft - 50f);
float toastWidth = styleManager.Group.toastWidth;
float toastXPosition = (Screen.width - toastWidth) / 2.0f;
GUILayout.BeginArea(new Rect(toastXPosition, toastYPosition, toastWidth, Screen.height));
GUILayout.TextArea(toastText, styleManager.Group.bigToastLabelStyle);
GUILayout.EndArea();
}
}
/// <summary>
/// Displays a toast notification. It will appear on top of everything else on the screen.
/// </summary>
/// <param name="toastText">The text to display in the toast</param>
/// <param name="toastDuration">The duration the toast will be shown for.
/// The toast will slide off the screen for the last part of this duration.</param>
public static void Show(string toastText, GUISound? maybeSound = GUISound.Click, float toastDuration = 3)
{
if(Instance == null)
{
return;
}
Instance.toastText = toastText;
Instance.toastNotificationTimer.Start(toastDuration);
// Try to play sound effect, if requested
try
{
if (maybeSound is GUISound sound)
{
GameSystem.Instance.AudioController.PlaySystemSound(MODSound.GetSoundPathFromEnum(sound));
}
}
catch (Exception e)
{
// If an exception occurs here, it probably means the gamesystem is not setup yet
// Since this only affects whether the sound is played, just ignore any errors that happen here.
}
}
public static void Show(string toastText, bool isEnable, float toastDuration = 3)
{
Show(toastText, isEnable ? GUISound.Enable : GUISound.Disable, toastDuration);
}
public static void Show(string toastText, int numberedSound, float toastDuration = 3)
{
GUISound sound = GUISound.Click;
switch (numberedSound)
{
case 0:
sound = GUISound.Pluck0;
break;
case 1:
sound = GUISound.Pluck1;
break;
case 2:
sound = GUISound.Pluck2;
break;
case 3:
sound = GUISound.Pluck3;
break;
case 4:
sound = GUISound.Pluck4;
break;
case 5:
sound = GUISound.Pluck5;
break;
}
Show(toastText, sound, toastDuration);
}
}
}