SCED/src/playercards/CardSearch.ttslua
2024-02-12 10:29:43 +01:00

122 lines
4.3 KiB
Plaintext

require("playercards/PlayerCardSpawner")
local allCardsBagApi = require("playercards/AllCardsBagApi")
local BUTTON_LABELS = {}
BUTTON_LABELS["spawn"] = {}
BUTTON_LABELS["spawn"][true] = "All matching cards"
BUTTON_LABELS["spawn"][false] = "First matching card"
BUTTON_LABELS["search"] = {}
BUTTON_LABELS["search"][true] = "Name equals search term"
BUTTON_LABELS["search"][false] = "Name contains search term"
local inputParameters = {}
inputParameters.label = "Click to enter card name"
inputParameters.input_function = "input_func"
inputParameters.function_owner = self
inputParameters.alignment = 2
inputParameters.position = { x = 0, y = 0.1, z = -0.62 }
inputParameters.width = 3750
inputParameters.height = 380
inputParameters.font_size = 350
inputParameters.scale = { 0.1, 1, 0.1 }
inputParameters.color = { 0.9, 0.7, 0.5 }
inputParameters.font_color = { 0, 0, 0 }
function onSave() return JSON.encode({ spawnAll, searchExact, inputParameters.value }) end
function onLoad(savedData)
local loadedData = JSON.decode(savedData)
spawnAll = loadedData[1] or false
searchExact = loadedData[2] or false
inputParameters.value = loadedData[3] or ""
self.createInput(inputParameters)
-- shared parameters
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.font_size = 180
buttonParameters.scale = { 0.1, 1, 0.1 }
buttonParameters.hover_color = { 0.4, 0.6, 0.8 }
buttonParameters.color = { 0.9, 0.7, 0.5 }
-- index 0: button for spawn mode
buttonParameters.click_function = "toggleSpawnMode"
buttonParameters.label = BUTTON_LABELS["spawn"][spawnAll]
buttonParameters.position = { x = 0.16, y = 0.1, z = 0.565 }
buttonParameters.height = 375
buttonParameters.width = 2300
self.createButton(buttonParameters)
-- index 1: button for search mode
buttonParameters.click_function = "toggleSearchMode"
buttonParameters.label = BUTTON_LABELS["search"][searchExact]
buttonParameters.position = { x = 0.16, y = 0.1, z = 0.652 }
self.createButton(buttonParameters)
-- index 2: start search
buttonParameters.click_function = "startSearch"
buttonParameters.label = ""
buttonParameters.position = { x = 0, y = 0, z = 0.806 }
buttonParameters.height = 600
buttonParameters.width = 2800
self.createButton(buttonParameters)
end
function toggleSpawnMode()
spawnAll = not spawnAll
self.editButton({ index = 0, label = BUTTON_LABELS["spawn"][spawnAll] })
end
function toggleSearchMode()
searchExact = not searchExact
self.editButton({ index = 1, label = BUTTON_LABELS["search"][searchExact] })
end
-- if "Enter press" (\n) is found, start search and recreate input
function input_func(_, _, input, stillEditing)
if not stillEditing then
inputParameters.value = input
elseif string.find(input, "%\n") ~= nil then
inputParameters.value = input.gsub(input, "%\n", "")
startSearch()
self.removeInput(0)
self.createInput(inputParameters)
end
end
function startSearch()
if inputParameters.value == nil or string.len(inputParameters.value) == 0 then
printToAll("Please enter a search string.", "Yellow")
return
end
if string.len(inputParameters.value) < 3 then
printToAll("Please enter a longer search string.", "Yellow")
return
end
if not allCardsBagApi.isBagPresent() then
printToAll("Player card bag couldn't be found.", "Red")
return
end
-- search all objects in bag
local cardList = allCardsBagApi.getCardsByName(inputParameters.value, searchExact)
if cardList == nil or #cardList == 0 then
printToAll("No match found.", "Red")
return
end
if (#cardList > 100) then
printToAll("Matched more than 100 cards, please try a more specific search.", "Yellow")
return
end
-- sort table by name (reverse for multiple results, because bottom card spawns first)
table.sort(cardList, function(k1, k2) return spawnAll == (k1.data.Nickname > k2.data.Nickname) end)
local rot = self.getRotation()
local pos = self.positionToWorld(Vector(0, 2, -0.08))
Spawner.spawnCards(cardList, pos, rot, true)
end