From b879335dce93afde945c4e10851a574c5cd75124 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Fri, 11 Oct 2024 01:10:39 +0200 Subject: [PATCH] updated script --- src/core/Global.ttslua | 127 ++++++++++++++++++++++++++-------- xml/Global/DownloadWindow.xml | 3 +- 2 files changed, 101 insertions(+), 29 deletions(-) diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index 1f31bea9..65b3f8e5 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -42,8 +42,9 @@ local hideTitleSplashWaitFunctionId = nil -- online functionality related variables local MOD_VERSION = "4.0.0" local SOURCE_REPO = "https://github.com/Chr1Z93/SCED-downloads/releases/latest/download/" -local library, requestObj, modMeta +local library, requestObj, modMeta, searchFilter, authorFilter local acknowledgedUpgradeVersions = {} +local authorList = {} local contentToShow = "campaign" local currentListItem = 1 local tabIdTable = { @@ -959,22 +960,54 @@ end -- Content Importing and XML functions --------------------------------------------------------- -function onSearchValueChanged(a, b, c) - log(a) - log(b) - log(c) +-- callback function for the search field in the download menu +function onSearchValueSubmit(_, value) + -- this event seems to be called 5x at once, so we use this flag to just execute it once + if ignoreSubmit then return end + ignoreSubmit = true - -- TODO: some kind of wait + -- store input value in global var + if value == "" then + searchFilter = nil + else + searchFilter = value + end + + -- update XML so that the settings persists + UI.setAttribute("searchField", "text", value) + + -- turn off flag after 0.1s + Wait.time(function() ignoreSubmit = false end, 0.1) + + -- update list (including new filter setting) updateDownloadItemList(true) end -function onAuthorFilterChanged(a, b, c) - log(a) - log(b) - log(c) +-- callback function for the "author" dropdown in the download menu +function onAuthorFilterChanged(_, value) + -- store input value in global var + if value == "All authors" then + authorFilter = nil + else + authorFilter = value + end + + -- update XML so that the settings persists + UI.setAttribute("authorDropdown", "value", returnAuthorId(value)) + + -- update list (including new filter setting) updateDownloadItemList(true) end +-- helper function to get the ID of the dropdown selection +function returnAuthorId(name) + for index, optionName in ipairs(authorList) do + if optionName == name then + return index + end + end +end + -- forwards the requested content type to the update function and sets highlight to clicked tab ---@param tabId string Id of the clicked tab function onClick_tab(_, _, tabId) @@ -987,6 +1020,12 @@ function onClick_tab(_, _, tabId) end end currentListItem = 1 + + -- reset filters + authorFilter, searchFilter = nil, nil + UI.setAttribute("searchField", "text", "") + UI.setAttribute("authorDropdown", "value", 0) + updateDownloadItemList() end @@ -1358,34 +1397,50 @@ function updateDownloadItemList(skipAuthorUpdate) local globalXml = UI.getXmlTable() local contentList = getXmlTableElementById(globalXml, 'contentList') + -- reset the list of authors unless skipping + if not skipAuthorUpdate then + authorList = {} + end + contentList.children = {} for i, v in ipairs(library[contentToShow]) do - -- TODO: filter here + -- if there's a filter, apply it (both for name and author) + if (searchFilter == nil or string.contains(string.lower(v.name), searchFilter)) and + (authorFilter == nil or v.author == authorFilter) then - -- TODO: store authors here - table.insert(contentList.children, - { - tag = "Panel", - attributes = { id = "panel" .. i }, - children = { - tag = 'Text', - value = v.name, - attributes = { - id = contentToShow .. "_" .. i, - onClick = 'onClick_select', - alignment = 'MiddleLeft' + -- start collecting authors unless skipping + if not skipAuthorUpdate then + table.insert(authorList, v.author) + end + + table.insert(contentList.children, + { + tag = "Panel", + attributes = { id = "panel" .. i }, + children = { + tag = 'Text', + value = v.name, + attributes = { + id = contentToShow .. "_" .. i, + onClick = 'onClick_select', + alignment = 'MiddleLeft' + } } - } - }) + }) + end end contentList.attributes.height = #contentList.children * 27 - -- TODO: maybe update authors + -- populate the author dropdown with options unless skipping if not skipAuthorUpdate then + authorList = removeDuplicatesAndSort(authorList) + local authorDropdown = getXmlTableElementById(globalXml, 'authorDropdown') - - + authorDropdown.children = { { tag = "Option", value = "All authors" } } + for _, author in ipairs(authorList) do + table.insert(authorDropdown.children, { tag = "Option", value = author }) + end end updateGlobalXml(globalXml) @@ -2684,3 +2739,19 @@ function deepCopy(data) end return copiedList end + +function removeDuplicatesAndSort(t) + local seen = {} + local result = {} + + for _, value in ipairs(t) do + if not seen[value] then + seen[value] = true + table.insert(result, value) + end + end + + table.sort(result) + + return result +end diff --git a/xml/Global/DownloadWindow.xml b/xml/Global/DownloadWindow.xml index 07d36b17..08cc315d 100644 --- a/xml/Global/DownloadWindow.xml +++ b/xml/Global/DownloadWindow.xml @@ -83,10 +83,11 @@ textColor="Black" textOffset="5 5 5 5" font="font_teutonic-arkham" - onValueChanged="onSearchValueChanged"/> + onSubmit="onSearchValueSubmit"/>