0ad/binaries/data/mods/public/gui/maps/mapbrowser/grid/MapGridBrowser.js
wraitii ae9ea5b859 Map browser, used in the gamesetup and in the main menu.
This grid-based system allows browsing all available maps at a glance,
encouraging more diversity and making it nicer to pick a map.

Moves the mapCache and the map filters controller to the gui maps/
folders, and include that folder where it is used, instead of them being
in common/ or the gamesetup.

Comments By: Freagarach
Patch By: Nani (reworked by elexis then wraitii)
Fixes #5315 (though further work, such as proper scrolling, would be
nice).

Differential Revision: https://code.wildfiregames.com/D1703
This was SVN commit r24459.
2020-12-27 15:26:19 +00:00

102 lines
3.1 KiB
JavaScript

class MapGridBrowser extends GridBrowser
{
constructor(mapBrowserPage, setupWindow)
{
super(Engine.GetGUIObjectByName("mapBrowserContainer"));
this.setupWindow = setupWindow;
this.mapBrowserPage = mapBrowserPage;
this.mapCache = mapBrowserPage.mapCache;
this.mapFilters = mapBrowserPage.mapFilters;
this.mapList = [];
this.items = this.container.children.map((imageObject, itemIndex) =>
new MapGridBrowserItem(mapBrowserPage, this, imageObject, itemIndex));
this.mapBrowserPage.registerOpenPageHandler(this.onOpenPage.bind(this));
this.mapBrowserPage.registerClosePageHandler(this.onClosePage.bind(this));
this.mapBrowserPage.mapBrowserPageDialog.onMouseWheelUp = this.nextPage.bind(this);
this.mapBrowserPage.mapBrowserPageDialog.onMouseWheelDown = this.previousPage.bind(this);
}
onOpenPage()
{
this.updateMapList();
this.setSelectedIndex(this.mapList.findIndex(map => map.file == g_GameAttributes.map));
this.goToPageOfSelected();
this.container.onWindowResized = this.onWindowResized.bind(this);
Engine.SetGlobalHotkey(this.HotkeyConfigNext, "Press", this.nextPage.bind(this));
Engine.SetGlobalHotkey(this.HotkeyConfigPrevious, "Press", this.previousPage.bind(this));
}
onClosePage()
{
delete this.container.onWindowResized;
Engine.UnsetGlobalHotkey(this.HotkeyConfigNext, "Press");
Engine.UnsetGlobalHotkey(this.HotkeyConfigPrevious, "Press");
}
updateMapList()
{
let selectedMap =
this.mapList[this.selected] &&
this.mapList[this.selected].file || undefined;
let mapList = this.mapFilters.getFilteredMaps(
this.mapBrowserPage.controls.MapFiltering.getSelectedMapType(),
this.mapBrowserPage.controls.MapFiltering.getSelectedMapFilter());
let filterText = this.mapBrowserPage.controls.MapFiltering.getSearchText();
if (filterText)
{
mapList = MatchSort.get(filterText, mapList, "name");
if (!mapList.length)
{
let filter = "all";
for (let type of g_MapTypes.Name)
for (let map of this.mapFilters.getFilteredMaps(type, filter))
mapList.push(Object.assign({ "type": type, "filter": filter }, map));
mapList = MatchSort.get(filterText, mapList, "name");
}
}
if (this.mapBrowserPage.controls.MapFiltering.getSelectedMapType() == "random")
{
mapList = [{
"file": "random",
"name": "Random",
"description": "Pick a map at random.",
}, ...mapList];
}
this.mapList = mapList;
this.itemCount = this.mapList.length;
this.resizeGrid();
this.setSelectedIndex(this.mapList.findIndex(map => map.file == selectedMap));
}
submitMapSelection()
{
if (!g_IsController)
return;
let map = this.mapList[this.selected] || undefined;
if (!map)
return;
g_GameAttributes.mapType = map.type ? map.type :
this.mapBrowserPage.controls.MapFiltering.getSelectedMapType();
g_GameAttributes.mapFilter = map.filter ? map.filter :
this.mapBrowserPage.controls.MapFiltering.getSelectedMapFilter();
g_GameAttributes.map = map.file;
this.setupWindow.controls.gameSettingsControl.updateGameAttributes();
}
}
MapGridBrowser.prototype.ItemRatio = 4 / 3;
MapGridBrowser.prototype.DefaultItemWidth = 200;
MapGridBrowser.prototype.MinItemWidth = 100;