Skip to content

Commit

Permalink
Merge pull request #622 from yozozchomutova/favourite_maps
Browse files Browse the repository at this point in the history
Simple "favourite" system for maps
  • Loading branch information
AntlerForce authored Apr 3, 2024
2 parents e89f377 + b76600f commit ce05991
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
Binary file added LuaMenu/images/star_off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LuaMenu/images/star_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions LuaMenu/widgets/chobby/components/sortable_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function SortableList:init(holder, headings, itemHeight, defaultSort, sortDirect
self.sortDataById = {}
self.items = 0
self.identifierList = {}
self.priorityList = {}

self.headingButtons = {}

Expand Down Expand Up @@ -218,6 +219,13 @@ end
function SortableList:UpdateOrder()
local function SortFunction(a, b)
local noNil = self.sortDataById[a] and self.sortDataById[b] and self.sortDataById[a][self.sortBy] and self.sortDataById[b][self.sortBy]

if self.priorityList[a] == nil and self.priorityList[b] ~= nil then
return noNil and false
elseif self.priorityList[a] ~= nil and self.priorityList[b] == nil then
return noNil and true
end

if self.smallToLarge then
return noNil and self.sortDataById[a][self.sortBy] < self.sortDataById[b][self.sortBy]
else
Expand Down
80 changes: 75 additions & 5 deletions LuaMenu/widgets/gui_maplist_panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ end
local mapListWindow
local lobby
local loadRate = 1
local IMG_READY = LUA_DIRNAME .. "images/downloadready.png"
local IMG_UNREADY = LUA_DIRNAME .. "images/downloadnotready.png"
local favMaps = {}
local FILE_FAV_MAPS = "favourite_maps.txt"
local IMG_READY = LUA_DIRNAME .. "images/downloadready.png"
local IMG_UNREADY = LUA_DIRNAME .. "images/downloadnotready.png"
local IMG_STAR_OFF = LUA_DIRNAME .. "images/star_off.png"
local IMG_STAR_ON = LUA_DIRNAME .. "images/star_on.png"

local MINIMAP_TOOLTIP_PREFIX = "minimap_tooltip_"

Expand Down Expand Up @@ -134,6 +138,7 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
local Configuration = WG.Chobby.Configuration

local haveMap = VFS.HasArchive(mapName)
local isFavourite = favMaps[mapName] ~= nil;

local mapButtonCaption = nil

Expand All @@ -143,15 +148,28 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
mapButtonCaption = i18n("click_to_pick_map")
end

local root = Panel:New {
x = 0,
y = 0,
width = 774,
height = 60,
resizable = false,
draggable = false,
padding = {0,0,0,0},
noFont = true,
}

local mapButton = Button:New {
x = 0,
y = 0,
width = "100%",
width = 748,
height = "100%",
caption = "",
resizable = false,
draggable = false,
classname = "battle_default_button",
padding = {0, 0, 0, 0},
parent = root,
tooltip = MINIMAP_TOOLTIP_PREFIX .. mapName .. "|" .. mapButtonCaption,
objectOverrideFont = WG.Chobby.Configuration:GetFont(2),
OnClick = {
Expand Down Expand Up @@ -224,6 +242,28 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
parent = mapButton,
}

local favouriteImg = Image:New {
x = 748,
y = 2,
width = 24,
height = 24,
file = (isFavourite and IMG_STAR_ON) or IMG_STAR_OFF,
parent = root,
OnClick = {
function (self)
if isFavourite then
isFavourite = false;
favMaps[mapName] = nil;
else
isFavourite = true;
favMaps[mapName] = 1;
end

self.file = (isFavourite and IMG_STAR_ON) or IMG_STAR_OFF;
end
}
}

local sortData
if mapData then
local mapSizeText = (mapData.Width or " ?") .. "x" .. (mapData.Height or " ?")
Expand Down Expand Up @@ -284,7 +324,7 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
sortData[5] = (haveMap and 1) or 0 -- This line is pretty evil.
end

return mapButton, sortData, externalFunctions
return root, sortData, externalFunctions
end

--------------------------------------------------------------------------------
Expand All @@ -303,7 +343,7 @@ local function InitializeControls()
classname = "main_window",
parent = WG.Chobby.lobbyInterfaceHolder,
height = math.max(700, WG.Chobby.lobbyInterfaceHolder.height -100),
width = 810,
width = 834,
resizable = false,
draggable = false,
padding = {0, 0, 0, 0},
Expand Down Expand Up @@ -340,6 +380,14 @@ local function InitializeControls()

local function CloseFunc()
mapListWindow:Hide()

--Save "favourite maps" data to file
local favMapsFile = io.open(FILE_FAV_MAPS, "w");
favMapsFile:write(Spring.Utilities.GetEngineVersion() .. "\n"); --Game version is always first line
for mapName in pairs(favMaps) do
favMapsFile:write(mapName .. "\n");
end
io.close(favMapsFile);
end

local filterTerms
Expand Down Expand Up @@ -408,6 +456,7 @@ local function InitializeControls()
local featuredMapIndex = 1
local mapFuncs = {}
local mapList = WG.Chobby.SortableList(listHolder, headings, 60, 1, true, false, ItemInFilter)
mapList.priorityList = favMaps

local function AddTheNextBatchOfMaps()
local mapItems = {}
Expand Down Expand Up @@ -454,6 +503,27 @@ local function InitializeControls()
mapList:UpdateOrder()
end

local function FetchFavouriteMaps()
--Clear table first
for mapName, _ in pairs(favMaps) do
favMaps[mapName] = nil
end

--Load new
if VFS.FileExists(FILE_FAV_MAPS) then
local favouriteMapsData = VFS.LoadFile(FILE_FAV_MAPS)
local fileVersion --In which game version was file created (useful in case of changing file write/read structure in future)
for mapName in string.gmatch(favouriteMapsData, "[^\r\n]+") do
if fileVersion == nil then --First line will always be file version
fileVersion = mapName
else
favMaps[mapName] = 1
end
end
end --Otherwise -> no favourite maps/empty table
end

FetchFavouriteMaps()
WG.Delay(AddTheNextBatchOfMaps, 0.5 / loadRate)

-------------------------
Expand Down

0 comments on commit ce05991

Please sign in to comment.