Index: binaries/data/mods/public/gui/common/GridBrowser.js =================================================================== --- binaries/data/mods/public/gui/common/GridBrowser.js (nonexistent) +++ binaries/data/mods/public/gui/common/GridBrowser.js (working copy) @@ -0,0 +1,118 @@ +/* + * Function that arranges a list of "boxed-like" xml objects with centered and page based way mode. + * Needs an object as container and a object to display the page numbering(if not make hidden object and assign it to it) + * childWidth,childHeight: dimensions of each grid box + * The grid will ajust in accordance to the screen size from when init() method is called + * + * To add advanced, more detailed, objects inside each box redefine + * the function "defineChildren" after the your GridBrowser is instantiated. + * + * For example look at mapbrowser.js implementation. + */ +function GridBrowser(containerName, pageCounterName) +{ + // object name which contains all the grid objects + this.containerName = containerName; + // object name which constains a text object type for the numbering of each page + this.pageCounterName = pageCounterName; +} + +// the list of whatever you are storing +GridBrowser.prototype.init = function(list, childWidth, childHeight) +{ + this.container = Engine.GetGUIObjectByName(this.containerName); + this.pageCounter = Engine.GetGUIObjectByName(this.pageCounterName); + this.children = this.container.children; // list of xml objects + + this.numBoxesCreated = this.children.length; //hardcoded in the xml file, maximum capable of showing + + this.list = list; + this.currentPage = 0; + + this.setChildDimensions(childWidth, childHeight); +} + +GridBrowser.prototype.defineChildren = function(n, pageList) +{ + //empty onpurpose, add modifications only at the object instances +} + +GridBrowser.prototype.setChildDimensions = function(childWidth, childHeight) +{ + this.child = {}; + this.child.width = childWidth; + this.child.height = childHeight; + this.updateGrid(); +} + +GridBrowser.prototype.updateGrid = function() +{ + let rect = this.container.getComputedSize(); + rect.width = Math.max(rect.right - rect.left, 0); + rect.height = Math.max(rect.bottom - rect.top, 0); + + this.nColumns = Math.max(1, Math.floor(rect.width / this.child.width)); + this.nRows = Math.max(1, Math.floor(rect.height / this.child.height)); + this.maxNumBoxesInScreen = Math.min(this.nColumns * this.nRows, this.numBoxesCreated); + this.xCenter = this.child.width * this.nColumns / 2; + + this.generateGrid(); + this.currentPage = 0; + this.numOfPages = this.calcNumPages(); + this.generatePage(); +} + +GridBrowser.prototype.generateGrid = function() +{ + for (let n = 0; n < this.numBoxesCreated; n++) + { + const x = n % this.nColumns; + const y = Math.floor(n / this.nColumns); + + let xmin = this.child.width * x; + let xmax = this.child.width * (x + 1); + let ymin = this.child.height * y; + let ymax = this.child.height * (y + 1); + xmin = "50%" + "-" + this.xCenter + "+" + xmin; + xmax = "50%" + "-" + this.xCenter + "+" + xmax; + this.children[n].size = [xmin, ymin, xmax, ymax].join(" "); + } +} + +GridBrowser.prototype.generatePage = function(page = undefined) +{ + this.currentPage = Math.max(Math.min(page == undefined ? this.currentPage : page, this.numOfPages - 1), 0); + let nubOfBoxesToShow = (this.currentPage == this.numOfPages - 1) ? this.list.length % this.maxNumBoxesInScreen : this.maxNumBoxesInScreen; + let subList = this.list.slice(this.currentPage * this.maxNumBoxesInScreen, this.currentPage * this.maxNumBoxesInScreen + nubOfBoxesToShow); + + for (let n = 0; n < nubOfBoxesToShow; n++) + { + this.children[n].hidden = false; + this.defineChildren(n, subList); + } + + for (let n = nubOfBoxesToShow; n < this.numBoxesCreated; ++n) + { + this.children[n].hidden = true; + } + this.updatePageCounter() +} + +GridBrowser.prototype.calcNumPages = function() +{ + return Math.ceil(this.list.length / this.maxNumBoxesInScreen); +} +GridBrowser.prototype.updatePageCounter = function() +{ + this.pageCounter.caption = `${this.currentPage+1}/${this.numOfPages}`; +} +GridBrowser.prototype.nextPage = function() +{ + this.currentPage = (this.currentPage + 1) % this.numOfPages; + this.generatePage(); +} +GridBrowser.prototype.previousPage = function() +{ + this.currentPage = (this.currentPage + this.numOfPages - 1) % this.numOfPages; + this.generatePage(); +} \ No newline at end of file Index: binaries/data/mods/public/gui/gamesetup/gamesetup.js =================================================================== --- binaries/data/mods/public/gui/gamesetup/gamesetup.js (revision 21902) +++ binaries/data/mods/public/gui/gamesetup/gamesetup.js (working copy) @@ -1829,6 +1829,7 @@ function loadMapData(name) { + if (!name || !g_MapPath[g_GameAttributes.mapType]) return undefined; @@ -2407,6 +2408,20 @@ updateGUIObjects(); } +function MapBrowserOpen() +{ + Engine.PushGuiPage("page_mapbrowser.xml", { + "callback": "MapBrowserCallback" + }); +} +/** + * Called after closing the dialog. + */ +function MapBrowserCallback(mapSelected) +{ + +} + function openAIConfig(playerSlot) { g_LastViewedAIPlayer = playerSlot; Index: binaries/data/mods/public/gui/gamesetup/gamesetup.xml =================================================================== --- binaries/data/mods/public/gui/gamesetup/gamesetup.xml (revision 21902) +++ binaries/data/mods/public/gui/gamesetup/gamesetup.xml (working copy) @@ -137,8 +137,11 @@ - + + + MapBrowserOpen(); + Index: binaries/data/mods/public/gui/mapbrowser/mapbrowser.js =================================================================== --- binaries/data/mods/public/gui/mapbrowser/mapbrowser.js (nonexistent) +++ binaries/data/mods/public/gui/mapbrowser/mapbrowser.js (working copy) @@ -0,0 +1,98 @@ + +var g_MapPath = { + "random": "maps/random/", + "scenario": "maps/scenarios/", + "skirmish": "maps/skirmishes/" +}; + +function loadMapData(name) +{ + if (name == "random") + return { + "settings": + { + "Name": "", + "Description": "" + } + }; + + if (!g_MapData[name]) + g_MapData[name] = g_GameAttributes.mapType == "random" ? + Engine.ReadJSONFile(name + ".json") : + Engine.LoadMapSettings(name); + + return g_MapData[name]; +} + +function getFilteredMaps() +{ + let maps = []; + for (let mapType of Object.keys(g_MapPath)) + { + const mapFilePath = g_MapPath[mapType]; + const mapFileExtension = (mapType == "random") ? ".json" : ".xml"; + + for (let mapFileName of listFiles(mapFilePath, mapFileExtension, false)) + { + if (mapFileName.startsWith("_")) + continue; + maps.push( + { + "loaded": false, + "filePath": mapFilePath, + "fileName": mapFileName, + "fileExtension": mapFileExtension, + "type": mapType, + }); + } + } + return maps; +} + + + +function getMapPreview(mapType, mapFullFileName) +{ + let mapData = (mapType == "random") ? Engine.ReadJSONFile(mapFullFileName + ".json") : Engine.LoadMapSettings(mapFullFileName); + if (!mapData || !mapData.settings || !mapData.settings.Preview) + return "nopreview.png"; + return mapData.settings.Preview; +} + + +let mapBrowser = new GridBrowser("MapBrowserContainer", "currentPageNum") + +mapBrowser.defineChildren = function(n, pageList) +{ + let map = pageList[n]; + + if (!map.loaded) + { + map.preview = getMapPreview(map.type, map.filePath + map.fileName) + map.loaded = true; + } + + Engine.GetGUIObjectByName(`mapPreview[${n}]`).sprite = "cropped:0.78125,0.5859375:session/icons/mappreview/" + map.preview; + Engine.GetGUIObjectByName(`mapInfoName[${n}]`).caption = map.fileName; + Engine.GetGUIObjectByName(`mapSelect[${n}]`).onPress = () => true; + +} + + + + +function init(settings) +{ + let mapList = getFilteredMaps(); + mapBrowser.init(mapList,400 * 0.65, 300 * 0.65); +} + +function MapBrowserReturn() +{ + // Pop the page before calling the callback, so the callback runs + // in the parent GUI page's context + Engine.PopGuiPageCB( + { + "dummy": "dummy", + }); +} \ No newline at end of file Index: binaries/data/mods/public/gui/mapbrowser/mapbrowser.xml =================================================================== --- binaries/data/mods/public/gui/mapbrowser/mapbrowser.xml (nonexistent) +++ binaries/data/mods/public/gui/mapbrowser/mapbrowser.xml (working copy) @@ -0,0 +1,53 @@ + + + + +