/
UICredits.cs
150 lines (122 loc) · 3.84 KB
/
UICredits.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
141
142
143
144
145
146
147
148
149
150
namespace Template;
public partial class UICredits : Node
{
const float STARTING_SPEED = 0.75f;
VBoxContainer vbox;
Button btnPause;
Button btnSpeed;
bool paused;
byte curSpeedSetting = 1;
float speed = STARTING_SPEED;
public override void _Ready()
{
btnPause = GetNode<Button>("%Pause");
btnSpeed = GetNode<Button>("%Speed");
vbox = new VBoxContainer
{
SizeFlagsVertical = Control.SizeFlags.ShrinkBegin
};
// Read the contents from credits.txt and construct the credits
FileAccess file =
FileAccess.Open("res://credits.txt", FileAccess.ModeFlags.Read);
while (!file.EofReached())
{
string line = Tr(file.GetLine());
int size = 16;
if (line.Contains("[h1]"))
{
size = 32;
line = line.Replace("[h1]", "");
}
if (line.Contains("[h2]"))
{
size = 24;
line = line.Replace("[h2]", "");
}
string translatedLine = "";
foreach (string word in line.Split(' '))
translatedLine += Tr(word) + " ";
if (translatedLine.Contains("http"))
AddTextWithLink(translatedLine);
else
if (string.IsNullOrWhiteSpace(translatedLine))
vbox.AddChild(new GPadding(0, 10));
else
vbox.AddChild(new GLabel(translatedLine, size));
}
file.Close();
AddChild(vbox);
// Set starting position of the credits
vbox.Position = new Vector2(
DisplayServer.WindowGetSize().X / 2 - vbox.Size.X / 2,
DisplayServer.WindowGetSize().Y);
// Re-center credits when window size is changed
GetViewport().SizeChanged += () =>
{
vbox.Position = new Vector2(
DisplayServer.WindowGetSize().X / 2 - vbox.Size.X / 2,
vbox.Size.Y);
};
}
public override void _PhysicsProcess(double delta)
{
// Animate the credits
Vector2 pos = vbox.Position;
pos.Y -= speed;
vbox.Position = pos;
// Go back to the main menu when the credits are finished
if (pos.Y <= -vbox.Size.Y)
{
Global.Services.Get<AudioManager>().PlayMusic(Music.Menu);
Global.Services.Get<SceneManager>().SwitchScene("main_menu");
}
if (Input.IsActionJustPressed("ui_cancel"))
{
Global.Services.Get<AudioManager>().PlayMusic(Music.Menu);
Global.Services.Get<SceneManager>().SwitchScene("main_menu");
}
}
void AddTextWithLink(string text)
{
int indexOfHttp = text.IndexOf("http");
string textDesc = text.Substring(0, indexOfHttp);
string textLink = text.Substring(indexOfHttp);
var hbox = new HBoxContainer {
SizeFlagsHorizontal = Control.SizeFlags.ShrinkCenter
};
var labelText = new GLabel(textDesc);
var btnLink = new GLinkButton(textLink);
hbox.AddChild(labelText);
hbox.AddChild(btnLink);
vbox.AddChild(hbox);
}
void _on_pause_pressed()
{
paused = !paused;
if (paused)
{
SetPhysicsProcess(false);
btnPause.Text = "Resume";
}
else
{
SetPhysicsProcess(true);
btnPause.Text = "Pause";
}
}
void _on_speed_pressed()
{
if (curSpeedSetting < 3)
{
curSpeedSetting++;
btnSpeed.Text = $"{curSpeedSetting}.0x";
speed += 1;
}
else
{
curSpeedSetting = 1;
btnSpeed.Text = $"{curSpeedSetting}.0x";
speed = STARTING_SPEED;
}
}
}