code cleanup

This commit is contained in:
Chr1Z93 2023-10-16 16:45:18 +02:00
parent 11f59ebe1c
commit 2edc6e5233

View File

@ -158,7 +158,7 @@ function onLoad(savedData)
-- initialization of loadable objects library (delay to let Navigation Overlay build) -- initialization of loadable objects library (delay to let Navigation Overlay build)
Wait.time(function() Wait.time(function()
requestObj = WebRequest.get(SOURCE_REPO .. '/library.json', completed_list_update) WebRequest.get(SOURCE_REPO .. '/library.json', completed_list_update)
end, 1) end, 1)
end end
@ -640,7 +640,6 @@ end
-- forwards the requested content type to the update function and sets highlight to clicked tab -- forwards the requested content type to the update function and sets highlight to clicked tab
---@param id String Id of the clicked tab ---@param id String Id of the clicked tab
function onClick_tab(_, _, id) function onClick_tab(_, _, id)
local contentToShow
for listId, listContent in pairs(idTable) do for listId, listContent in pairs(idTable) do
if listId == id then if listId == id then
UI.setClass(listId, 'downloadTab activeTab') UI.setClass(listId, 'downloadTab activeTab')
@ -649,7 +648,7 @@ function onClick_tab(_, _, id)
UI.setClass(listId, 'downloadTab') UI.setClass(listId, 'downloadTab')
end end
end end
update_list(contentToShow) updateDownloadItemList()
-- select the first item -- select the first item
Wait.time(function() onClick_select(_, _, contentToShow .. "_1") end, 0.1) Wait.time(function() onClick_select(_, _, contentToShow .. "_1") end, 0.1)
@ -663,19 +662,10 @@ function onClick_select(_, _, id)
UI.setAttribute(contentToShow .. "_" .. currentListItem, "color", "white") UI.setAttribute(contentToShow .. "_" .. currentListItem, "color", "white")
end end
-- update contentToShow & currentListItem
parseKey(id)
UI.setAttribute("panel" .. currentListItem, "color", "grey")
UI.setAttribute(contentToShow .. "_" .. currentListItem, "color", "black")
updateAndShowPreviewWindow()
end
-- parses the identification key (contentToShow_currentListItem) -- parses the identification key (contentToShow_currentListItem)
function parseKey(unparsedStr)
contentToShow = nil contentToShow = nil
currentListItem = nil currentListItem = nil
for str in string.gmatch(unparsedStr, "([^_]+)") do for str in string.gmatch(id, "([^_]+)") do
if not contentToShow then if not contentToShow then
-- grab the first part to know the content type -- grab the first part to know the content type
contentToShow = str contentToShow = str
@ -685,14 +675,21 @@ function parseKey(unparsedStr)
break break
end end
end end
UI.setAttribute("panel" .. currentListItem, "color", "grey")
UI.setAttribute(contentToShow .. "_" .. currentListItem, "color", "black")
updateAndShowPreviewWindow()
end end
-- click function for the download button in the preview window
function onClick_download() function onClick_download()
startContentDownload(library[contentToShow][currentListItem]) placeholder_download(library[contentToShow][currentListItem])
end end
-- download requested content -- the download button on the placeholder objects calls this to directly initiate a download
function startContentDownload(param) ---@param param Table contains url and guid of replacement object
function placeholder_download(param)
local url = SOURCE_REPO .. '/' .. param.url local url = SOURCE_REPO .. '/' .. param.url
requestObj = WebRequest.get(url, function (request) complete_obj_download(request, param) end) requestObj = WebRequest.get(url, function (request) complete_obj_download(request, param) end)
startLuaCoroutine(Global, 'downloadCoroutine') startLuaCoroutine(Global, 'downloadCoroutine')
@ -710,9 +707,21 @@ function downloadCoroutine()
end end
UI.setAttribute('download_progress', 'percentage', 100) UI.setAttribute('download_progress', 'percentage', 100)
-- wait 30 frames
for i = 1, 30 do
coroutine.yield(0)
end
-- hide progress bar -- hide progress bar
UI.setAttribute('download_button', 'active', true) UI.setAttribute('download_button', 'active', true)
UI.setAttribute('download_progress', 'active', false) UI.setAttribute('download_progress', 'active', false)
-- hide download window
if xmlVisibility.downloadWindow then
xmlVisibility.downloadWindow = false
UI.hide('downloadWindow')
UI.hide("previewWindow")
end
return 1 return 1
end end
@ -854,8 +863,7 @@ function formatLibrary(json_response)
end end
-- updates the window content to the requested content -- updates the window content to the requested content
---@param contentToShow String Type of content to show function updateDownloadItemList()
function update_list(contentToShow)
if not library then return end if not library then return end
-- set title according to type -- set title according to type
@ -896,64 +904,56 @@ end
-- called after the webrequest of downloading an item -- called after the webrequest of downloading an item
-- deletes the placeholder and spawns the downloaded item -- deletes the placeholder and spawns the downloaded item
function complete_obj_download(request, param) function complete_obj_download(request, param)
assert(request.is_done) requestObj = nil
-- error handling
if request.is_error or request.response_code ~= 200 then if request.is_error or request.response_code ~= 200 then
print('error: ' .. request.error) print('Error: ' .. request.error)
else return
if pcall(function() end
local replacedObject
pcall(function() -- initiate content spawning
local spawnTable = { json = request.text }
if param.replace then if param.replace then
replacedObject = getObjectFromGUID(param.replace) local replacedObject = getObjectFromGUID(param.replace)
end
end)
if replacedObject then if replacedObject then
local pos = replacedObject.getPosition() spawnTable.position = replacedObject.getPosition()
local rot = replacedObject.getRotation() spawnTable.rotation = replacedObject.getRotation()
local scale = replacedObject.getScale() spawnTable.scale = replacedObject.getScale()
destroyObject(replacedObject) destroyObject(replacedObject)
Wait.frames(function()
spawnObjectJSON({
json = request.text,
position = pos,
rotation = rot,
scale = scale
})
end, 1)
else
spawnObjectJSON({ json = request.text })
end end
end) then end
-- if spawned from menu, ping the position
if param.name then
spawnTable["callback_function"] = function(obj)
Player.getPlayers()[1].pingTable(obj.getPosition())
end
end
if pcall(function() spawnObjectJSON(spawnTable) end) then
print('Object loaded.') print('Object loaded.')
else else
print('Error loading object.') print('Error loading object.')
end end
end end
requestObj = nil
end
-- the download button on the placeholder objects calls this to directly initiate a download
---@param param Table contains url and guid of replacement object
function placeholder_download(param)
startContentDownload(param)
end
-- downloading of the library file -- downloading of the library file
function completed_list_update(request) function completed_list_update(request)
assert(request.is_done)
if request.is_error or request.response_code ~= 200 then if request.is_error or request.response_code ~= 200 then
print('error: ' .. request.error) print('error: ' .. request.error)
else return
end
local json_response = nil local json_response = nil
if pcall(function () json_response = JSON.decode(request.text) end) then if pcall(function () json_response = JSON.decode(request.text) end) then
formatLibrary(json_response) formatLibrary(json_response)
update_list("campaigns") contentToShow = "campaigns"
updateDownloadItemList()
else else
print('error parsing downloaded library') print('error parsing downloaded library')
end end
end end
requestObj = nil
end
-- loops through an XML table and returns the specified object -- loops through an XML table and returns the specified object
---@param ui Table XmlTable (get this via getXmlTable) ---@param ui Table XmlTable (get this via getXmlTable)