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 AutoWatcher | |||||
{ | |||||
constructor(campaignRun) | |||||
{ | |||||
super("render"); | |||||
this.run = campaignRun; | |||||
this.selectedLevel = -1; | |||||
this.levelSelection = Engine.GetGUIObjectByName("levelSelection"); | |||||
this.levelSelection.onSelectionChange = () => { this.selectedLevel = this.levelSelection.selected; }; | |||||
this.levelSelection.onMouseLeftDoubleClickItem = () => this.startScenario(); | |||||
Engine.GetGUIObjectByName('startButton').onPress = () => this.startScenario(); | |||||
Engine.GetGUIObjectByName('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… | |||||
Engine.GetGUIObjectByName('savedGamesButton').onPress = () => Engine.PushGuiPage('page_loadgame.xml', { | |||||
'campaignRun': this.run.filename | |||||
}); | |||||
this.mapCache = new MapCache(); | |||||
this._ready = true; | |||||
} | |||||
goBackToMainMenu() | |||||
{ | |||||
this.run.save(); | |||||
Engine.SwitchGuiPage("page_pregame.xml", {}); | |||||
} | |||||
startScenario() | |||||
{ | |||||
let level = this.getSelectedLevelData(); | |||||
if (!meetsRequirements(this.run, level)) | |||||
return; | |||||
Engine.SwitchGuiPage("page_gamesetup.xml", { | |||||
"mapType": level.MapType, | |||||
"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 meetsRequirements(this.run, levelData); | |||||
} | |||||
getLevelName(levelData) | |||||
{ | |||||
if (levelData.Name) | |||||
return translate(levelData.Name); | |||||
return translate(this.mapCache.getTranslatableMapName(levelData.MapType, "maps/" + levelData.Map)); | |||||
} | |||||
getLevelDescription(levelData) | |||||
{ | |||||
if (levelData.Description) | |||||
return translate(levelData.Description); | |||||
return this.mapCache.getTranslatedMapDescription(levelData.MapType, "maps/" + levelData.Map); | |||||
Done Inline ActionsStart with capital? Freagarach: Start with capital? | |||||
} | |||||
Done Inline ActionsSetStringTags? Freagarach: SetStringTags? | |||||
displayLevelsList() | |||||
{ | |||||
let list = []; | |||||
Done Inline Actionss/Won/Completed? Freagarach: s/Won/Completed? | |||||
for (let key in this.run.template.Levels) | |||||
{ | |||||
let level = this.run.template.Levels[key]; | |||||
if (!this.shouldShowLevel(level)) | |||||
continue; | |||||
let status = ""; | |||||
let name = this.getLevelName(level); | |||||
if (isCompleted(this.run, key)) | |||||
status = translateWithContext("campaign status", "Completed"); | |||||
else if (meetsRequirements(this.run, level)) | |||||
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… | |||||
status = coloredText(translateWithContext("campaign status", "Available"), "green"); | |||||
else | |||||
name = coloredText(name, "gray"); | |||||
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)); | |||||
list = prepareForDropdown(list); | |||||
this.levelSelection.list_name = list.name || []; | |||||
this.levelSelection.list_status = list.status || []; | |||||
// COList needs these changed last or crashes. | |||||
this.levelSelection.list = list.ID || []; | |||||
this.levelSelection.list_data = list.ID || []; | |||||
} | |||||
displayLevelDetails() | |||||
{ | |||||
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… | |||||
if (this.selectedLevel === -1) | |||||
{ | |||||
Engine.GetGUIObjectByName("startButton").enabled = false; | |||||
Engine.GetGUIObjectByName("startButton").hidden = false; | |||||
return; | |||||
} | |||||
let level = this.getSelectedLevelData(); | |||||
Engine.GetGUIObjectByName("scenarioName").caption = this.getLevelName(level); | |||||
Engine.GetGUIObjectByName("scenarioDesc").caption = this.getLevelDescription(level); | |||||
if (level.Preview) | |||||
Engine.GetGUIObjectByName('levelPreviewBox').sprite = "cropped:" + 400/512 + "," + 300/512 + ":" + level.Preview; | |||||
else | |||||
Engine.GetGUIObjectByName('levelPreviewBox').sprite = "cropped:" + 400/512 + "," + 300/512 + ":session/icons/mappreview/nopreview.png"; | |||||
Engine.GetGUIObjectByName("startButton").enabled = meetsRequirements(this.run, level); | |||||
Engine.GetGUIObjectByName("startButton").hidden = false; | |||||
Engine.GetGUIObjectByName("loadSavedButton").hidden = true; | |||||
} | |||||
render() | |||||
{ | |||||
Engine.GetGUIObjectByName("campaignTitle").caption = this.run.generateLabel(); | |||||
this.displayLevelDetails(); | |||||
this.displayLevelsList(); | |||||
Done Inline ActionsUnnecessary. Freagarach: Unnecessary. | |||||
} | |||||
} | |||||
var g_CampaignMenu; | |||||
function init(initData) | |||||
{ | |||||
let run; | |||||
try { | |||||
run = new CampaignRun(initData.filename).load(); | |||||
} catch (err) { | |||||
error(sprintf(translate("Error loading campaign run %s: %s."), initData.filename, err)); | |||||
Engine.SwitchGuiPage("page_pregame.xml", {}); | |||||
} | |||||
g_CampaignMenu = new CampaignMenu(run); | |||||
} |
Wildfire Games · Phabricator
This?