various changes to search

This commit is contained in:
Buurazu authored and Wofsauge committed 2024-06-03 18:39:46 +02:00
1 parent 9fb43bd836
commit b62acf9b76
1 file changed
+52 -24
+52 -24
View File
@@ -4,12 +4,30 @@ EID.BoCSLockMode = 0
local game = Game() local game = Game()
local locked = false local locked = false
local indexCharMapping = " ??????'????,-./0123456789?;?????abcdefghijklmnopqrstuvwxyz" local indexCharMapping = [[ ??????'????,-./0123456789?;?=???abcdefghijklmnopqrstuvwxyz[\]??]]
local shiftCharMapping = [[ ??????"????<_>?)!@#$%^&*(?:?+???ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}??]]
local searchValue = "" local searchValue = ""
local searchInputEnabled = false local searchInputEnabled = false
local lastBackspaceTrigger = 0 local lastBackspaceTrigger = 0
local backspaceStep = 0 local backspaceStep = 0
-- source: https://stackoverflow.com/questions/65714534/how-do-i-remove-certain-special-characters-from-a-string-in-lua
-- This list is incomplete, add more to it as necessary
local function sanitizeName(name)
local accented = {
['ß'] = 'ss', ['à'] = 'a', ['á'] = 'a', ['â'] = 'a', ['ã'] = 'a', ['å'] = 'a' , ['ä'] = 'ae', ['æ'] = 'ae',
['ç'] = 'c', ['è'] = 'e', ['é'] = 'e', ['ê'] = 'e', ['ë'] = 'e', ['ì'] = 'i', ['í'] = 'i', ['î'] = 'i', ['ï'] = 'i',
['ð'] = 'dh', ['ñ'] = 'n', ['ò'] = 'o', ['ó'] = 'o', ['ô'] = 'o', ['õ'] = 'o', ['ø'] = 'o', ['ö'] = 'oe',
['ù'] = 'u', ['ú'] = 'u', ['û'] = 'u', ['ü'] = 'ue', ['ý'] = 'y', ['ÿ'] = 'y', ['þ'] = 'th'
}
local sanitized = name:lower()
-- Replace some non-ASCII characters:
for fancy, plain in pairs(accented) do
sanitized = sanitized:gsub(fancy, plain)
end
return sanitized
end
--- Returns the current search query --- Returns the current search query
-- @param newValue new search query -- @param newValue new search query
@@ -44,8 +62,6 @@ function EID:BoCSSetLocked(newState, force)
if newState then if newState then
EID:BoCSSetSearchInputEnabled(false, true) EID:BoCSSetSearchInputEnabled(false, true)
else
EID:BoCSSetSearchValue("")
end end
end end
@@ -63,10 +79,6 @@ function EID:BoCSSetSearchInputEnabled(newState, force)
else else
EID:RemoveCallback(ModCallbacks.MC_INPUT_ACTION, EID.BoCSBlockInputAction) EID:RemoveCallback(ModCallbacks.MC_INPUT_ACTION, EID.BoCSBlockInputAction)
searchInputEnabled = newState searchInputEnabled = newState
if EID.BoCSLockMode == 0 and not EID:BoCSGetLocked() then
EID:BoCSSetSearchValue("")
end
end end
end end
@@ -83,23 +95,24 @@ end
--- Returns true if the item name is matched --- Returns true if the item name is matched
-- @returns boolean -- @returns boolean
function EID:BoCSCheckItemName(itemName) function EID:BoCSCheckItemName(itemName, englishName)
if searchValue == "" then if searchValue == "" then
return true return true
end end
return string.find(string.lower(itemName), string.lower(searchValue)) -- Check the localized name and English name for matching our search string (accents removed)
return string.find(sanitizeName(itemName), sanitizeName(searchValue), 1, true)
or string.find(sanitizeName(englishName), sanitizeName(searchValue), 1, true)
end end
local function handleSearchInput() local function handleSearchInput()
if ( if not searchInputEnabled or (EID.BoCSLockMode == 0 and EID:BoCSGetLocked()) then
not searchInputEnabled return
or ( end
EID.BoCSLockMode == 0
and if Input.IsButtonTriggered(Keyboard.KEY_ESCAPE, EID.bagPlayer.ControllerIndex, true) then
EID:BoCSGetLocked() EID:BoCSSetSearchInputEnabled(false)
) EID:BoCSSetSearchValue("")
) then
return return
end end
@@ -109,10 +122,12 @@ local function handleSearchInput()
-- Keep in mind that this only works because the respective -- Keep in mind that this only works because the respective
-- enum values are integers -- enum values are integers
for i=Keyboard.KEY_SPACE,Keyboard.KEY_Z do for i=Keyboard.KEY_SPACE,Keyboard.KEY_RIGHT_BRACKET do
index = index + 1 index = index + 1
if Input.IsButtonTriggered(i, EID.bagPlayer.ControllerIndex, true) then if Input.IsButtonTriggered(i, EID.bagPlayer.ControllerIndex, true) then
local toAppend = string.upper(indexCharMapping:sub(index, index)) local toAppend = indexCharMapping:sub(index, index)
local isShiftPressed = (Input.IsButtonPressed(Keyboard.KEY_LEFT_SHIFT, EID.bagPlayer.ControllerIndex, true) or Input.IsButtonPressed(Keyboard.KEY_RIGHT_SHIFT, EID.bagPlayer.ControllerIndex, true))
if isShiftPressed then toAppend = shiftCharMapping:sub(index, index) end
newValue = newValue .. toAppend newValue = newValue .. toAppend
hasLetterInput = true hasLetterInput = true
@@ -138,9 +153,9 @@ local function handleSearchInput()
if Input.IsButtonPressed(Keyboard.KEY_BACKSPACE, EID.bagPlayer.ControllerIndex, true) and ( if Input.IsButtonPressed(Keyboard.KEY_BACKSPACE, EID.bagPlayer.ControllerIndex, true) and (
backspaceStep == 0 backspaceStep == 0
or or
(backspaceStep == 1 and currentFrame - lastBackspaceTrigger > 15) -- we delay the second deletion a little bit more (backspaceStep == 1 and currentFrame - lastBackspaceTrigger >= 15) -- we delay the second deletion a little bit more
or or
(backspaceStep >= 2 and currentFrame - lastBackspaceTrigger > 3) (backspaceStep >= 2 and currentFrame - lastBackspaceTrigger >= 1)
) then ) then
local isControlPressed = ( local isControlPressed = (
Input.IsButtonPressed(Keyboard.KEY_LEFT_CONTROL, EID.bagPlayer.ControllerIndex, true) Input.IsButtonPressed(Keyboard.KEY_LEFT_CONTROL, EID.bagPlayer.ControllerIndex, true)
@@ -170,7 +185,20 @@ function EID:BoCSHandleInput()
if EID.bagPlayer == nil then if EID.bagPlayer == nil then
return return
end end
if ModConfigMenu and ModConfigMenu.IsVisible then
return
end
-- Clear the previous search when Enter is pressed
-- TODO: make this a modifiable hotkey
if Input.IsButtonTriggered(Keyboard.KEY_ENTER, EID.bagPlayer.ControllerIndex, true) then
if not searchInputEnabled then EID:BoCSSetSearchValue("") end
end
-- Start a new search when Enter is pressed
-- TODO: make this a modifiable hotkey
-- by default, both are Enter, but for instance clearing could be Backspace and then you could modify a search term with Enter
if Input.IsButtonTriggered(Keyboard.KEY_ENTER, EID.bagPlayer.ControllerIndex, true) then if Input.IsButtonTriggered(Keyboard.KEY_ENTER, EID.bagPlayer.ControllerIndex, true) then
EID:BoCSSetSearchInputEnabled(not searchInputEnabled) EID:BoCSSetSearchInputEnabled(not searchInputEnabled)
return return