Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/campaigns/default_menu/CampaignMenu.js
- This file was added.
/** | |||||
* This is the main menu screen of the campaign. | |||||
* It shows you the currently available scenarios, scenarios you've already completed, etc. | |||||
* This particular variant is extremely simple and shows a list similar to Age 1's campaigns, | |||||
* but conceptually nothing really prevents more complex systems. | |||||
*/ | |||||
class CampaignMenu extends DefaultPage | |||||
{ | |||||
constructor(campaignRun, finishedLevel, won) | |||||
{ | |||||
super(); | |||||
this.run = campaignRun; | |||||
this.selectedLevel = -1; | |||||
this.levelSelection = _("levelSelection"); | |||||
this.levelSelection.onSelectionChange = () => { this.selectedLevel = this.levelSelection.selected; }; | |||||
this.levelSelection.onMouseLeftDoubleClickItem = () => this.startScenario(); | |||||
_('startButton').onPress = () => this.startScenario(); | |||||
_('backToMain').onPress = () => this.goBackToMainMenu(); | |||||
Freagarach: >>! In D11#116393, @wraitii wrote:
> This also introduces a super-magical way to do rendering… | |||||
Done Inline ActionsNo, it's actually the inheritance from DefaultPage which uses a Proxy to call Render when a property is set. This is just shorthand for "getGUiObjectByName" or whatever it's called. wraitii: No, it's actually the inheritance from DefaultPage which uses a Proxy to call Render when a… | |||||
_('savedGamesButton').onPress = () => Engine.PushGuiPage('page_loadgame.xml', { | |||||
'campaignRun': this.run.filename | |||||
}); | |||||
this._ready = true; | |||||
} | |||||
goBackToMainMenu() | |||||
{ | |||||
this.run.save(); | |||||
messageBox( | |||||
400, 200, | |||||
translate("Are you sure you want to go back? Your progress will be saved."), | |||||
translate("Confirmation"), | |||||
[translate("No"), translate("Yes")], | |||||
[null, () => { | |||||
Engine.SwitchGuiPage("page_pregame.xml", {}); | |||||
}] | |||||
); | |||||
} | |||||
startScenario() | |||||
{ | |||||
let level = this.getSelectedLevelData(); | |||||
Engine.SwitchGuiPage("page_gamesetup.xml", { | |||||
"mapType": level.Map.split('/')[0], | |||||
"map": "maps/" + level.Map, | |||||
"autostart": true, | |||||
"campaignData": { | |||||
"run": this.run.filename, | |||||
"levelID": this.levelSelection.list_data[this.selectedLevel], | |||||
"data": this.run.data | |||||
} | |||||
}); | |||||
} | |||||
getSelectedLevelData() | |||||
{ | |||||
if (this.selectedLevel === -1) | |||||
return undefined; | |||||
return this.run.template.Levels[this.levelSelection.list_data[this.selectedLevel]]; | |||||
} | |||||
shouldShowLevel(levelData) | |||||
{ | |||||
if (this.run.template.ShowUnavailable) | |||||
return true; | |||||
return this.run.meetsRequirements(levelData); | |||||
} | |||||
displayLevelsList() | |||||
{ | |||||
let list = []; | |||||
for (let key in this.run.template.Levels) | |||||
{ | |||||
let level = this.run.template.Levels[key]; | |||||
if (!this.shouldShowLevel(level)) | |||||
continue; | |||||
Done Inline ActionsStart with capital? Freagarach: Start with capital? | |||||
Done Inline ActionsSetStringTags? Freagarach: SetStringTags? | |||||
let status = ""; | |||||
let name = translate(level.Name); | |||||
if (!this.run.meetsRequirements(level)) | |||||
{ | |||||
Done Inline Actionss/Won/Completed? Freagarach: s/Won/Completed? | |||||
status = translate("not unlocked yet"); | |||||
name = "[color=\"gray\"]" + name + "[/color]"; | |||||
} | |||||
else if (this.run.isCompleted(key)) | |||||
{ | |||||
status = translateWithContext("campaign status", "Won"); | |||||
} | |||||
list.push({ "ID": key, "name": name, "status": status }); | |||||
} | |||||
list.sort((a, b) => this.run.template.Order.indexOf(a.ID) - this.run.template.Order.indexOf(b.ID)); | |||||
Not Done Inline ActionsWhy? Freagarach: Why? | |||||
Done Inline ActionsNo idea atm. I think I copied this from somewhere else maybe? wraitii: No idea atm. I think I copied this from somewhere else maybe? | |||||
Done Inline ActionsIt's a confirmed COList behaviour, but I'm not sure _why_ exactly. Probably we ought to use a function for this instead. wraitii: It's a confirmed COList behaviour, but I'm not sure _why_ exactly. Probably we ought to use a… | |||||
list = prepareForDropdown(list); | |||||
this.levelSelection.list_name = list.name || []; | |||||
this.levelSelection.list_status = list.status || []; | |||||
// These must be changed last or things crash. | |||||
this.levelSelection.list = list.ID || []; | |||||
this.levelSelection.list_data = list.ID || []; | |||||
} | |||||
displayLevelDetails() | |||||
{ | |||||
if (this.selectedLevel === -1) | |||||
{ | |||||
_("startButton").enabled = false; | |||||
_("startButton").hidden = false; | |||||
return; | |||||
} | |||||
let level = this.getSelectedLevelData(); | |||||
Not Done Inline ActionsDuplication, can it be prevented? Freagarach: Duplication, can it be prevented? | |||||
Done Inline ActionsOstensibly yes but I can't actually use the mapPreview() code in common() because it's too specific. I don't think it's worth it. wraitii: Ostensibly yes but I can't actually use the mapPreview() code in common() because it's too… | |||||
_("scenarioName").caption = translate(level.Name); | |||||
_("scenarioDesc").caption = translate(level.Description); | |||||
if (level.Preview) | |||||
_('levelPreviewBox').sprite = "cropped:" + 400/512 + "," + 300/512 + ":" + level.Preview; | |||||
else | |||||
_('levelPreviewBox').sprite = "cropped:" + 400/512 + "," + 300/512 + ":session/icons/mappreview/nopreview.png"; | |||||
_("startButton").enabled = this.run.meetsRequirements(level); | |||||
_("startButton").hidden = false; | |||||
_("loadSavedButton").hidden = true; | |||||
} | |||||
render() | |||||
{ | |||||
_("campaignTitle").caption = this.run.generateLabel(); | |||||
this.displayLevelDetails(); | |||||
this.displayLevelsList(); | |||||
} | |||||
} | |||||
var g_CampaignMenu; | |||||
function init(initData) | |||||
{ | |||||
let run = new CampaignRun(initData.filename).load(); | |||||
Done Inline ActionsUnnecessary. Freagarach: Unnecessary. | |||||
g_CampaignMenu = new CampaignMenu(run, initData.finishedLevel || null, initData.won || null); | |||||
} |
Wildfire Games · Phabricator
This?