-
Notifications
You must be signed in to change notification settings - Fork 324
/
PopupText.cs
140 lines (125 loc) · 4.7 KB
/
PopupText.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DaggerfallConnect.Arena2;
namespace DaggerfallWorkshop.Game.UserInterface
{
/// <summary>
/// Popup text for notifications, etc.
/// </summary>
public class PopupText : Panel
{
const float textSpacing = 1;
const float popDelay = 1.0f;
const int maxRows = 7;
const float rubberbandFactor = 0.8f;
LinkedList<TextLabel> textRows = new LinkedList<TextLabel>();
// Text scrolls away when timer becomes negative
float timer;
float nextPopDelay = popDelay;
public PopupText()
: base()
{
//TextLabel label = DaggerfallUI.AddTextLabel(DaggerfallUI.DefaultFont, Vector2.zero, "This is some test text", this);
//label.HorizontalAlignment = HorizontalAlignment.Center;
}
public override void Update()
{
base.Update();
if (textRows.Count > 0)
{
int rowsLate = textRows.Count - maxRows;
if (rowsLate > 0)
{
// Accelerate to keep up
float speedup = 1f + rubberbandFactor * rowsLate;
timer -= Time.deltaTime * speedup;
}
else
timer -= Time.deltaTime;
while (timer < -popDelay)
{
timer += nextPopDelay;
// Remove item from front of list
textRows.RemoveFirst();
// Reset pop delay to default
nextPopDelay = popDelay;
}
}
}
public override void Draw()
{
base.Draw();
// Draw text
int count = 0;
float y = 4;
if (textRows.Count > 0 && timer < 0)
y += (textRows.First.Value.TextHeight + textSpacing) * timer / popDelay;
int maxCount = (textRows.Count > maxRows) ? maxRows : textRows.Count;
IEnumerator enumerator = textRows.GetEnumerator();
while (enumerator.MoveNext())
{
TextLabel label = enumerator.Current as TextLabel;
if (label != null)
{
label.Position = new Vector2(0, y);
label.Draw();
y += label.TextHeight + textSpacing;
}
if (++count > maxCount)
break;
}
}
/// <summary>
/// Adds text with custom delay.
/// Delay affects this item only. Subsequent text items can override delay.
/// Delay will return to default after time elapsed.
/// </summary>
/// <param name="text">Text to display.</param>
/// <param name="delayInSeconds">Time in seconds before removing text.</param>
public void AddText(string text, float delayInSeconds = popDelay)
{
if (textRows.Count == 0)
// set no-scroll delay
timer = delayInSeconds;
else if (timer >= 0)
// retrigger no-scroll delay
timer = Mathf.Max(timer, delayInSeconds);
else
// set next no-scroll delay
nextPopDelay = Mathf.Max(nextPopDelay, delayInSeconds);
TextLabel label = DaggerfallUI.AddTextLabel(DaggerfallUI.DefaultFont, Vector2.zero, text);
label.HorizontalAlignment = HorizontalAlignment.Center;
label.Parent = Parent;
textRows.AddLast(label);
GameManager.Instance.PlayerEntity.Notebook.AddMessage(text);
}
/// <summary>
/// Add text from tokens with a custom delay.
/// </summary>
/// <param name="tokens">Tokens. One line added per text token.</param>
/// <param name="delayInSeconds">Delay per line.</param>
public void AddText(TextFile.Token[] tokens, float delayInSeconds)
{
string[] lines = TextFile.GetTokenLines(tokens);
if (lines != null && lines.Length > 0)
{
foreach(string line in lines)
{
AddText(line, delayInSeconds);
}
}
}
}
}