From d5c2749a1f2a256558ed6937d65d558e2e72ef55 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Sat, 12 Nov 2022 14:24:02 +0100 Subject: [PATCH] updated Search-A-Card --- src/playercards/CardSearch.ttslua | 183 +++++++++++++++--------------- 1 file changed, 90 insertions(+), 93 deletions(-) diff --git a/src/playercards/CardSearch.ttslua b/src/playercards/CardSearch.ttslua index 15a5647e..23b3086c 100644 --- a/src/playercards/CardSearch.ttslua +++ b/src/playercards/CardSearch.ttslua @@ -1,68 +1,65 @@ -- Search-A-Card -- made by: Chr1Z --- description: spawns the card with the specified name +-- description: spawns the card(s) with the specified name information = { - version = "1.1", - last_updated = "10.10.2022" + version = "1.2", + last_updated = "12.11.2022" } -local BUTTON_PARAMETERS = {} -BUTTON_PARAMETERS.function_owner = self -BUTTON_PARAMETERS.height = 200 -BUTTON_PARAMETERS.width = 1200 -BUTTON_PARAMETERS.font_size = 75 +local buttonParameters = {} +buttonParameters.function_owner = self +buttonParameters.height = 200 +buttonParameters.width = 1200 +buttonParameters.font_size = 75 --- save selected options -function onSave() return JSON.encode({ spawnAll, searchExact }) end +local BUTTON_LABELS = {} +BUTTON_LABELS["spawn"] = {} +BUTTON_LABELS["spawn"][true] = "Mode: Spawn all matching cards " +BUTTON_LABELS["spawn"][false] = "Mode: Spawn first matching card" +BUTTON_LABELS["search"] = {} +BUTTON_LABELS["search"][true] = "Mode: Name matches search term" +BUTTON_LABELS["search"][false] = "Mode: 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 = { 0, 0.05, -1.6 } +inputParameters.width = 1200 +inputParameters.height = 130 +inputParameters.font_size = 107 + +local allCardsBag = getObjectFromGUID("15bb07") + +-- main code +function onSave() return JSON.encode({ spawnAll, searchExact, inputParameters.value }) end function onLoad(saved_data) - -- loading saved data - local loaded_data = JSON.decode(saved_data) - spawnAll = loaded_data[1] or false - searchExact = loaded_data[2] or false - - allCardsBag = getObjectFromGUID("15bb07") - INPUT_BOX = "" - - self.createInput({ - input_function = "input_func", - function_owner = self, - label = "Click to enter card name", - alignment = 2, - position = { x = 0, y = 0.05, z = -1.6 }, - width = 1200, - height = 130, - font_size = 107 - }) + local loaded_data = JSON.decode(saved_data) + spawnAll = loaded_data[1] or false + searchExact = loaded_data[2] or false + inputParameters.value = loaded_data[3] or "" -- index 0: button for spawn mode - BUTTON_PARAMETERS.click_function = "search" - BUTTON_PARAMETERS.label = "Spawn matching card(s)!" - BUTTON_PARAMETERS.position = { x = 0, y = 0.05, z = 1.15 } - self.createButton(BUTTON_PARAMETERS) + buttonParameters.click_function = "search" + buttonParameters.label = "Spawn matching card(s)!" + buttonParameters.position = { 0, 0.06, 1.15 } + self.createButton(buttonParameters) -- index 1: button for spawn mode - if spawnAll then - BUTTON_PARAMETERS.label = "Mode: Spawn all matching cards " - else - BUTTON_PARAMETERS.label = "Mode: Spawn first matching card" - end - - BUTTON_PARAMETERS.click_function = "spawnMode" - BUTTON_PARAMETERS.position.z = 1.55 - self.createButton(BUTTON_PARAMETERS) + buttonParameters.click_function = "spawnMode" + buttonParameters.label = BUTTON_LABELS["spawn"][spawnAll] + buttonParameters.position[3] = buttonParameters.position[3] + 0.4 + self.createButton(buttonParameters) -- index 2: button for search mode - if searchExact then - BUTTON_PARAMETERS.label = "Mode: Name matches search term" - else - BUTTON_PARAMETERS.label = "Mode: Name contains search term" - end - - BUTTON_PARAMETERS.click_function = "searchMode" - BUTTON_PARAMETERS.position.z = 1.95 - self.createButton(BUTTON_PARAMETERS) + buttonParameters.click_function = "searchMode" + buttonParameters.label = BUTTON_LABELS["search"][searchExact] + buttonParameters.position[3] = buttonParameters.position[3] + 0.4 + self.createButton(buttonParameters) + self.createInput(inputParameters) self.addContextMenuItem("More Information", function() printToAll("------------------------------", "White") printToAll("Search-A-Card v" .. information["version"] .. " by Chr1Z", "Orange") @@ -70,14 +67,35 @@ function onLoad(saved_data) end) end --- main function +function spawnMode() + spawnAll = not spawnAll + self.editButton({ index = 1, label = BUTTON_LABELS["spawn"][spawnAll] }) +end + +function searchMode() + searchExact = not searchExact + self.editButton({ index = 2, 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", "") + search() + self.removeInput(0) + self.createInput(inputParameters) + end +end + function search() - if INPUT_BOX == nil or string.len(INPUT_BOX) == 0 then + if inputParameters.value == nil or string.len(inputParameters.value) == 0 then printToAll("Please enter a search string.", "Yellow") return end - if string.len(INPUT_BOX) < 4 then + if string.len(inputParameters.value) < 3 then printToAll("Please enter a longer search string.", "Yellow") return end @@ -87,50 +105,29 @@ function search() return end - local cardList = allCardsBag.call("getCardsByName", { name = INPUT_BOX, exact = searchExact }) + -- search all objects in bag + local cardList = allCardsBag.call("getCardsByName", { name = inputParameters.value, exact = searchExact }) if cardList == nil or #cardList == 0 then printToAll("No match found.", "Red") return end - -- search all objects in bag - local spawnCount = 0 - for i, card in ipairs(cardList) do - local pos = self.positionToWorld(Vector(0, 0.5 + spawnCount * 0.15, -0.225)) - local rot = self.getRotation() - spawnObjectData({ - data = card.data, - position = pos, - rotation = rot, - }) - if not spawnAll then - return - end - end -end - -function input_func(_, _, input, stillEditing) - if not stillEditing then INPUT_BOX = input end -end - --- toggle spawn mode -function spawnMode() - spawnAll = not spawnAll - - if spawnAll then - self.editButton({ index = 1, label = "Mode: Spawn all matching cards " }) - else - self.editButton({ index = 1, label = "Mode: Spawn first matching card" }) - end -end - --- toggle search mode -function searchMode() - searchExact = not searchExact - - if searchExact then - self.editButton({ index = 2, label = "Mode: Name matches search term" }) - else - self.editButton({ index = 2, label = "Mode: Name contains search term" }) + -- 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, 0, -0.225)) + for _, card in ipairs(cardList) do + pos[2] = pos[2] + 0.1 + spawnObjectData({ + data = card.data, + position = pos, + rotation = rot + }) + if not spawnAll then return end + if i == 100 then + printToAll("Only first 100 results of " .. #cardList .. " total results spawned.", "Yellow") + return + end end end