-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifications.cs
More file actions
126 lines (100 loc) · 3.75 KB
/
Notifications.cs
File metadata and controls
126 lines (100 loc) · 3.75 KB
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
using Sandbox.Engine.Platform.VideoMode;
using Sandbox.Game.World;
using Sandbox.Graphics;
using Sandbox.Graphics.GUI;
using VRage.Collections;
using VRage.Utils;
using VRageMath;
using VRageRender;
namespace Digi.ParticleEditor
{
public class Notifications : EditorComponentBase
{
struct Message
{
public readonly string Text;
public readonly Vector2 TextSize;
public readonly Color Color;
public readonly double ExpiresAt;
public readonly bool Fade;
public Message(string text, Color color, double expiresAt, bool fade)
{
Text = text;
TextSize = MyGuiManager.MeasureString(Font, Text, TextScale);
Color = color;
ExpiresAt = expiresAt;
Fade = fade;
}
}
static Notifications Instance;
const string Font = "Debug";
const double FadeOutTime = 0.5;
static float TextScale => MyGuiSandbox.GetDefaultTextScaleWithLanguage() * 1.2f;
readonly Vector2 Position = new Vector2(0.01f, 0.4f);
readonly MyConcurrentList<Message> Messages = new MyConcurrentList<Message>();
public Notifications(Editor editor) : base(editor)
{
AlwaysUpdate = true;
Instance = this;
MySession.OnUnloaded += SessionUnloaded;
}
public override void Dispose()
{
Instance = null;
MySession.OnUnloaded -= SessionUnloaded;
}
void SessionUnloaded()
{
Messages.Clear();
}
public override void Update()
{
int totalMessages = Messages.Count;
if(totalMessages == 0)
return;
Vector2 position = Position;
bool isTrippleHead = MyVideoSettingsManager.IsTripleHead();
float scale = TextScale;
int limit = 5;
double time = MySession.Static.ElapsedGameTime.TotalSeconds;
for(int i = (totalMessages - 1); i >= 0; i--)
{
Message msg = Messages[i];
Color color = msg.Color;
if((time + FadeOutTime) >= msg.ExpiresAt)
{
if(time >= msg.ExpiresAt)
{
Messages.RemoveAt(i);
continue;
}
if(msg.Fade)
{
color *= (float)MathHelper.Clamp((msg.ExpiresAt - time) / FadeOutTime, 0, 1);
}
}
//position.X = Position.X - msg.TextSize.X / 2f;
//MyGuiManager.DrawString(Font, line.Text, position, scale, color, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, isTrippleHead);
// draws always on top of HUD
// TODO: a bit crooked...
MyRenderProxy.DebugDrawText2D(MyGuiManager.GetHudPixelCoordFromNormalizedCoord(position), msg.Text, color, scale, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP);
position.Y += msg.TextSize.Y;
if(--limit <= 0)
break;
}
}
/// <summary>
/// Thread-safe
/// </summary>
public static void Show(string message, double liveSeconds = 2, Color? color = null)
{
if(Instance == null)
return;
double expireAt = MySession.Static.ElapsedGameTime.TotalSeconds + liveSeconds;
if(!color.HasValue)
color = Color.White;
bool fade = liveSeconds > (FadeOutTime + 0.5);
Instance.Messages.Add(new Message(message, color.Value, expireAt, fade));
}
}
}