/
mainmenu.js
300 lines (250 loc) · 8.45 KB
/
mainmenu.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
var currentSubmenuType; // contains submenu type
var MARGIN = 4; // menu border size
var g_ShowSplashScreens;
warn("now");
/**
* Available backdrops
*/
var g_BackgroundLayerData = [];
/**
* Chosen backdrop
*/
var g_BackgroundLayerset;
var g_T0 = Date.now();
var g_LastTickTime = Date.now();
function init(initData, hotloadData)
{
initMusic();
global.music.setState(global.music.states.MENU);
// Initialize currentSubmenuType with placeholder to avoid null when switching
currentSubmenuType = "submenuSinglePlayer";
EnableUserReport(Engine.IsUserReportEnabled());
// Only show splash screen(s) once at startup, but not again after hotloading
g_ShowSplashScreens = hotloadData ? hotloadData.showSplashScreens : initData && initData.isStartup;
// Pick a random background and initialise it
g_BackgroundLayerset = pickRandom(g_BackgroundLayerData);
for (let i = 0; i < g_BackgroundLayerset.length; ++i)
{
let guiObj = Engine.GetGUIObjectByName("background[" + i + "]");
guiObj.hidden = false;
guiObj.sprite = g_BackgroundLayerset[i].sprite;
guiObj.z = i;
}
Engine.GetGUIObjectByName("structreeButton").tooltip = colorizeHotkey(
translate("%(hotkey)s: View the structure tree of civilizations featured in 0 A.D."),
"structree");
Engine.GetGUIObjectByName("civInfoButton").tooltip = colorizeHotkey(
translate("%(hotkey)s: Learn about the many civilizations featured in 0 A.D."),
"civinfo");
Engine.GetGUIObjectByName("lobbyButton").tooltip = colorizeHotkey(
translate("%(hotkey)s: Launch the multiplayer lobby to join and host publicly visible games and chat with other players."),
"lobby");
}
function getHotloadData()
{
return { "showSplashScreens": g_ShowSplashScreens };
}
function scrollBackgrounds()
{
for (let i = 0; i < g_BackgroundLayerset.length; ++i)
{
let guiObj = Engine.GetGUIObjectByName("background[" + i + "]");
let screen = guiObj.parent.getComputedSize();
let h = screen.bottom - screen.top;
let w = h * 16/9;
let iw = h * 2;
let offset = g_BackgroundLayerset[i].offset((Date.now() - g_T0) / 1000, w);
if (g_BackgroundLayerset[i].tiling)
{
let left = offset % iw;
if (left >= 0)
left -= iw;
guiObj.size = new GUISize(left, screen.top, screen.right, screen.bottom);
}
else
guiObj.size = new GUISize(screen.right/2 - h + offset, screen.top, screen.right/2 + h + offset, screen.bottom);
}
}
function submitUserReportMessage()
{
let input = Engine.GetGUIObjectByName("userReportMessageInput");
if (input.caption.length)
Engine.SubmitUserReport("message", 1, input.caption);
input.caption = "";
}
function formatUserReportStatus(status)
{
let d = status.split(/:/, 3);
if (d[0] == "disabled")
return translate("disabled");
if (d[0] == "connecting")
return translate("connecting to server");
if (d[0] == "sending")
return sprintf(translate("uploading (%f%%)"), Math.floor(100 * d[1]));
if (d[0] == "completed")
{
let httpCode = d[1];
if (httpCode == 200)
return translate("upload succeeded");
return sprintf(translate("upload failed (%(errorCode)s)"), { "errorCode": httpCode });
}
if (d[0] == "failed")
return sprintf(translate("upload failed (%(errorMessage)s)"), { "errorMessage": d[2] });
return translate("unknown");
}
function onTick()
{
let now = Date.now();
let tickLength = Date.now() - g_LastTickTime;
g_LastTickTime = now;
scrollBackgrounds();
updateMenuPosition(tickLength);
if (Engine.IsUserReportEnabled())
Engine.GetGUIObjectByName("userReportEnabledText").caption =
'[font="sans-bold-16"]' + translate("Thank you for helping improve 0 A.D.!") + "[/font]\n\n" +
translate("Anonymous feedback is currently enabled.") + "\n" +
sprintf(translate("Status: %(status)s."), {
"status": formatUserReportStatus(Engine.GetUserReportStatus())
});
// Show splash screens here, so we don't interfere with main menu hotloading
if (g_ShowSplashScreens)
{
g_ShowSplashScreens = false;
if (Engine.ConfigDB_GetValue("user", "gui.splashscreen.enable") === "true" ||
Engine.ConfigDB_GetValue("user", "gui.splashscreen.version") < Engine.GetFileMTime("gui/splashscreen/splashscreen.txt"))
Engine.PushGuiPage("page_splashscreen.xml", { "page": "splashscreen", "callback": "SplashScreenClosedCallback" });
else
ShowRenderPathMessage();
}
}
function ShowRenderPathMessage()
{
// Warn about removing fixed render path
if (Engine.Renderer_GetRenderPath() == "fixed")
messageBox(
600, 300,
"[font=\"sans-bold-16\"]" +
sprintf(translate("%(warning)s You appear to be using non-shader (fixed function) graphics. This option will be removed in a future 0 A.D. release, to allow for more advanced graphics features. We advise upgrading your graphics card to a more recent, shader-compatible model."), {
"warning": coloredText("Warning:", "200 20 20")
}) +
"\n\n" +
// Translation: This is the second paragraph of a warning. The
// warning explains that the user is using “non-shader“ graphics,
// and that in the future this will not be supported by the game, so
// the user will need a better graphics card.
translate("Please press \"Read More\" for more information or \"OK\" to continue."),
translate("WARNING!"),
[translate("OK"), translate("Read More")],
[ null, function() { Engine.OpenURL("http://www.wildfiregames.com/forum/index.php?showtopic=16734"); } ]
);
}
function SplashScreenClosedCallback()
{
ShowRenderPathMessage();
}
function EnableUserReport(Enabled)
{
Engine.GetGUIObjectByName("userReportDisabled").hidden = Enabled;
Engine.GetGUIObjectByName("userReportEnabled").hidden = !Enabled;
Engine.SetUserReportEnabled(Enabled);
}
/**
* Slide menu.
*/
function updateMenuPosition(dt)
{
let submenu = Engine.GetGUIObjectByName("submenu");
if (submenu.hidden == false)
{
// Number of pixels per millisecond to move
let SPEED = 1.2;
let maxOffset = Engine.GetGUIObjectByName("mainMenu").size.right - submenu.size.left;
if (maxOffset > 0)
{
let offset = Math.min(SPEED * dt, maxOffset);
let size = submenu.size;
size.left += offset;
size.right += offset;
submenu.size = size;
}
}
}
/**
* Opens the menu by revealing the screen which contains the menu.
*/
function openMenu(newSubmenu, position, buttonHeight, numButtons)
{
currentSubmenuType = newSubmenu;
Engine.GetGUIObjectByName(currentSubmenuType).hidden = false;
let submenu = Engine.GetGUIObjectByName("submenu");
let top = position - MARGIN;
let bottom = position + ((buttonHeight + MARGIN) * numButtons);
submenu.size = new GUISize(submenu.size.left, top, submenu.size.right, bottom);
// Blend in right border of main menu into the left border of the submenu
blendSubmenuIntoMain(top, bottom);
submenu.hidden = false;
}
function closeMenu()
{
Engine.GetGUIObjectByName(currentSubmenuType).hidden = true;
let submenu = Engine.GetGUIObjectByName("submenu");
submenu.hidden = true;
submenu.size = Engine.GetGUIObjectByName("mainMenu").size;
Engine.GetGUIObjectByName("MainMenuPanelRightBorderTop").size = "100%-2 0 100% 100%";
}
/**
* Sizes right border on main menu panel to match the submenu.
*/
function blendSubmenuIntoMain(topPosition, bottomPosition)
{
Engine.GetGUIObjectByName("MainMenuPanelRightBorderTop").size = "100%-2 0 100% " + (topPosition + MARGIN);
Engine.GetGUIObjectByName("MainMenuPanelRightBorderBottom").size = "100%-2 " + (bottomPosition) + " 100% 100%";
}
function getBuildString()
{
return sprintf(translate("Build: %(buildDate)s (%(revision)s)"), {
"buildDate": Engine.GetBuildTimestamp(0),
"revision": Engine.GetBuildTimestamp(2)
});
}
function exitGamePressed()
{
closeMenu();
messageBox(
400, 200,
translate("Are you sure you want to quit 0 A.D.?"),
translate("Confirmation"),
[translate("No"), translate("Yes")],
[null, Engine.Exit]
);
}
function pressedScenarioEditorButton()
{
closeMenu();
if (Engine.AtlasIsAvailable())
messageBox(
400, 200,
translate("Are you sure you want to quit 0 A.D. and open the Scenario Editor?"),
translate("Confirmation"),
[translate("No"), translate("Yes")],
[null, Engine.RestartInAtlas]
);
else
messageBox(
400, 200,
translate("The scenario editor is not available or failed to load. See the game logs for additional information."),
translate("Error")
);
}
function getLobbyDisabledByBuild()
{
return translate("Launch the multiplayer lobby to join and host publicly visible games and chat with other players. \\[DISABLED BY BUILD]");
}
function getTechnicalDetails()
{
return translate("Technical Details");
}
function getManual()
{
return translate("Manual");
}