Index: binaries/data/mods/mod/gui/common/modern/styles.xml =================================================================== --- binaries/data/mods/mod/gui/common/modern/styles.xml +++ binaries/data/mods/mod/gui/common/modern/styles.xml @@ -30,7 +30,6 @@ scrollbar_style="ModernScrollBar" sprite="ModernDarkBoxGoldNoTop" sprite_selectarea="ModernDarkBoxWhite" - sprite_heading="ModernDarkBoxGoldNoBottom" textcolor="white" textcolor_selected="white" text_align="left" Index: binaries/data/mods/public/campaigns/example.json =================================================================== --- /dev/null +++ binaries/data/mods/public/campaigns/example.json @@ -0,0 +1,38 @@ +{ + "Name": "Example Campaign", + "Description": "Lorem Ipsum and so on and so on", + "Interface": "default_menu", + "Levels": { + "Example_1": { + "Name": "Example 1", + "Map": "scenarios/Serengeti.xml", + "Description": "Whatever" + }, + "Example_2": { + "Name": "Example 2", + "Map": "", + "Description": "None", + "Requires": "Example_1" + }, + "Example_3": { + "Name": "This one requires 1 and 2", + "Map": "", + "Description": "None", + "Requires": "Example_1+Example_2" + }, + "Example_4": { + "Name": "This one requires 2 or 3", + "Map": "", + "Description": "None", + "Requires": "Example_2 Example_3" + }, + "Example_5": { + "Name": "This one unavailable if 1 isn't completed", + "Map": "", + "Description": "None", + "Requires": "!Example_1" + } + }, + "Order": ["Example_1", "Example_2", "Example_3", "Example_4", "Example_5"], + "ShowUnavailable": true +} Index: binaries/data/mods/public/campaigns/tutorial.json =================================================================== --- /dev/null +++ binaries/data/mods/public/campaigns/tutorial.json @@ -0,0 +1,21 @@ +{ + "Name": "Tutorial", + "Description": "Learn how to play 0 A.D.", + "Image": "session/icons/mappreview/Introductory_Tutorial.png", + "Levels": { + "introduction": { + "Name": "Introductory Tutorial", + "Map": "tutorials/Introductory_Tutorial.xml", + "Description": "This is a basic tutorial to get you started playing 0 A.D.", + "Preview": "session/icons/mappreview/Introductory_Tutorial.png" + }, + "eco_walkthrough": { + "Name": "Economy Walkthrough", + "Map": "tutorials/starting_economy_walkthrough.xml", + "Description": "This map will give a rough guide for starting the game effectively. Early in the game the most important thing is to gather resources as fast as possible so you are able to build enough troops later. Warning: This is very fast at the start, be prepared to run through the initial bit several times.", + "Requires": "introduction" + } + }, + "Order": ["introduction", "eco_walkthrough"], + "ShowUnavailable": true +} Index: binaries/data/mods/public/gui/campaign/common/CampaignRun.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gui/campaign/common/CampaignRun.js @@ -0,0 +1,97 @@ +/** + * A campaign "Run" saves metadata on a campaign progession. + * It is equivalent to a saved game for a game. + * It is named a "run" in an attempt to disambiguate with saved games from campaign runs, + * campaign templates, and the actual concept of a campaign at large. + */ +class CampaignRun +{ + static getCurrentRuns() + { + let names = Engine.ListDirectoryFiles("campaignsaves/", "*.0adcampaign", false); + return names.map(path => { + let filename = path.replace("campaignsaves/", "").replace(".0adcampaign", ""); + return new CampaignRun(filename).load(); + }); + } + + constructor(name = "") + { + this.filename = name; + this.meta = {}; + this.data = { + "completed": [] + }; + this.template = null; + } + + setTemplate(template) + { + this.template = template; + return this; + } + + setMeta(description) + { + this.meta.userDescription = description; + return this; + } + + markLevelComplete(levelID) + { + if (this.data.completed.indexOf(levelID) === -1) + this.data.completed.push(levelID); + this.save(); + } + + meetsRequirements(levelData) + { + if (!levelData.Requires) + return true; + + if (!this.data.completed) + return false; + + return MatchesClassList(this.data.completed, levelData.Requires); + } + + getMenuPath() + { + return "campaign/" + this.template.interface + "/page.xml"; + } + + generateLabel() + { + return sprintf(translate("%(userDesc)s - %(templateName)s"), { + "userDesc": this.meta.userDescription, + "templateName": this.template.Name + }); + } + + + load() + { + let data = Engine.ReadJSONFile("campaignsaves/" + this.filename + ".0adcampaign"); + this.data = data.data; + this.meta = data.meta; + this.template = CampaignTemplate.getTemplate(data.template_identifier); + return this; + } + + save() + { + let data = { + "data": this.data, + "meta": this.meta, + "template_identifier": this.template.identifier + }; + Engine.WriteJSONFile("campaignsaves/" + this.filename + ".0adcampaign", data); + return this; + } + + destroy() + { + Engine.DeleteFile("campaignsaves/" + this.filename + ".0adcampaign"); + // hang in memory for a while. + } +} Index: binaries/data/mods/public/gui/campaign/common/CampaignTemplate.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gui/campaign/common/CampaignTemplate.js @@ -0,0 +1,54 @@ +// TODO: replace this with a static member once we hit SM 75. +var g_CachedTemplates; + +class CampaignTemplate +{ + /** + * @returns a dictionary of campaign templates, as [ { 'identifier': id, 'data': data }, ... ] + */ + static getAvailableTemplates() + { + if (g_CachedTemplates) + return g_CachedTemplates; + + let campaigns = Engine.ListDirectoryFiles("campaigns/", "*.json", false); + + g_CachedTemplates = []; + + for (let filename of campaigns) + // Use file name as identifier to guarantee unicity. + g_CachedTemplates.push(new CampaignTemplate(filename.slice("campaigns/".length, -".json".length))); + + return g_CachedTemplates; + } + + static getTemplate(identifier) + { + if (!g_CachedTemplates) + CampaignTemplate.getAvailableTemplates(); + let temp = g_CachedTemplates.filter(t => t.identifier == identifier); + if (!temp.length) + return null; + return temp[0]; + } + + constructor(identifier) + { + Object.assign(this, Engine.ReadJSONFile("campaigns/" + identifier + ".json")); + + this.identifier = identifier; + + if (this.Interface) + this.interface = this.Interface; + else + this.interface = "default_menu"; + + if (!this.isValid()) + throw ("Campaign template " + this.identifier + ".json is not a valid campaign template."); + } + + isValid() + { + return this.Name; + } +} Index: binaries/data/mods/public/gui/campaign/common/utils.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gui/campaign/common/utils.js @@ -0,0 +1,41 @@ +function _(obj) +{ + return Engine.GetGUIObjectByName(obj); +} + +function _watch(object, callback) +{ + return new Proxy(object, { + "get": (obj, key) => { + return obj[key]; + }, + "set": (obj, key, value) => { + obj[key] = value; + callback(); + return true; + } + }); +} + +class DefaultPage +{ + _watch(object, lambda = null) + { + if (lambda == null) + lambda = () => { + if (this._ready) + this.render(); + }; + return _watch(object, lambda); + } + + constructor() + { + this._ready = false; + return this._watch(this); + } + + render() + { + } +} Index: binaries/data/mods/public/gui/campaign/default_menu/CampaignMenu.js =================================================================== --- /dev/null +++ binaries/data/mods/public/gui/campaign/default_menu/CampaignMenu.js @@ -0,0 +1,132 @@ +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(); + 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] + } + }); + } + + 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; + + let status = ""; + let name = translate(level.Name); + if (!this.run.meetsRequirements(level)) + { + status = translate("not unlocked yet"); + name = "[color=\"gray\"]" + name + "[/color]"; + } + 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 || []; + + // 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(); + + _("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() + { + this.displayLevelDetails(); + this.displayLevelsList(); + } +} + + +var g_CampaignMenu; + +function init(initData) +{ + let run = new CampaignRun(initData.filename).load(); + g_CampaignMenu = new CampaignMenu(run, initData.finishedLevel || null, initData.won || null); +} Index: binaries/data/mods/public/gui/campaign/default_menu/campaignmenu.xml =================================================================== --- /dev/null +++ binaries/data/mods/public/gui/campaign/default_menu/campaignmenu.xml @@ -0,0 +1,85 @@ + + + +