-
Notifications
You must be signed in to change notification settings - Fork 58
/
OSD.cs
75 lines (65 loc) · 2.02 KB
/
OSD.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TweakScale
{
// ReSharper disable once InconsistentNaming
public class OSD
{
private class Message
{
public String Text;
public Color Color;
public float HideAt;
}
private readonly List<Message> _msgs = new List<Message>();
private static GUIStyle CreateStyle(Color color)
{
var style = new GUIStyle
{
stretchWidth = true,
alignment = TextAnchor.MiddleCenter,
fontSize = 16,
fontStyle = FontStyle.Bold,
normal = { textColor = color }
};
return style;
}
private float CalcHeight()
{
var style = CreateStyle(Color.white);
return _msgs.Aggregate(.0f, (a, m) => a + style.CalcSize(new GUIContent(m.Text)).y);
}
public void Update()
{
if (_msgs.Count == 0) return;
_msgs.RemoveAll(m => Time.time >= m.HideAt);
var h = CalcHeight();
GUILayout.BeginArea(new Rect(0, Screen.height * 0.1f, Screen.width, h), CreateStyle(Color.white));
_msgs.ForEach(m => GUILayout.Label(m.Text, CreateStyle(m.Color)));
GUILayout.EndArea();
}
public void Error(String text)
{
AddMessage(text, XKCDColors.LightRed);
}
public void Warn(String text)
{
AddMessage(text, XKCDColors.Yellow);
}
public void Success(String text)
{
AddMessage(text, XKCDColors.Cerulean);
}
public void Info(String text)
{
AddMessage(text, XKCDColors.OffWhite);
}
public void AddMessage(String text, Color color, float shownFor = 3)
{
var msg = new Message { Text = text, Color = color, HideAt = Time.time + shownFor };
_msgs.Add(msg);
}
}
}