From 64cd3ecd4fbe59bee2255dce2a69f211d7dea8df Mon Sep 17 00:00:00 2001 From: Wofsauge Date: Sun, 25 Oct 2020 17:22:19 +0100 Subject: [PATCH] Improved inline sprite rendering and improved code readability --- eid_api.lua | 63 ++++++++++++++++++++++++++++++++++++++++------------- main.lua | 47 ++++++--------------------------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/eid_api.lua b/eid_api.lua index 2b7ec5e5..2344b804 100644 --- a/eid_api.lua +++ b/eid_api.lua @@ -98,8 +98,20 @@ function EID:getLastDescribedEntity() end -- returns descriptions from the legacy mod descriptions -function EID:getModDescription(list, id) - return (list) and (list[id]) +function EID:getLegacyModDescription(objTable, id) + if objTable == "collectibles" then + return (__eidItemDescriptions) and (__eidItemDescriptions[id]) + elseif objTable == "trinkets" then + return (__eidTrinketDescriptions) and (__eidTrinketDescriptions[id]) + elseif objTable == "cards" then + return (__eidCardDescriptions) and (__eidCardDescriptions[id]) + elseif objTable == "pills" then + return (__eidPillDescriptions) and (__eidPillDescriptions[id]) + elseif objTable == "transformation" then + return (__eidItemTransformations) and (__eidItemTransformations[id]) + elseif objTable == "custom" then + return (__eidEntityDescriptions) and (__eidEntityDescriptions[id]) + end end -- returns the specified object table in the current language. @@ -112,11 +124,20 @@ end -- falls back to english if the objID isnt available function EID:getDescriptionObj(objTable, objID) local tableEntry = EID.descriptions[EIDConfig["Language"]][objTable][tonumber(objID)] or EID.descriptions["en_us"][objTable][tonumber(objID)] + local description = {} description.ID = tonumber(tableEntry[1]) or objID description.Name = EID:getObjectName(objID, objTable) or objTable - description.Description = tableEntry[4] or tableEntry[3] or "" - description.Transformation = tableEntry[2] or "0" + + local legacyModdedDescription = EID:getLegacyModDescription(objTable, objID) + description.Description = legacyModdedDescription or tableEntry[4] or tableEntry[3] or tableEntry[2] or "MISSING DESCRIPTION" + + local legacyModdedTransformation = nil + if objTable == "collectibles" then + legacyModdedTransformation = EID:getLegacyModDescription("transformation", objID) + end + description.Transformation = legacyModdedTransformation or tableEntry[2] or "0" + return description end @@ -210,24 +231,35 @@ function EID:getStrWidth(str) return EID.font:GetStringWidthUTF8(str) end --- Searches true the given string and replaces Iconplaceholders with icons. --- Returns the string without the placeholders but with an accurate space between lines. +-- Searches thru the given string and replaces Iconplaceholders with icons. +-- Returns 2 values. the string without the placeholders but with an accurate space between lines. and a table of all Inline Sprites function EID:filterMarkup(text, textPosX, textPosY) + local spriteTable = {} for word in string.gmatch(text, "{{.-}}") do local textposition = string.find(text, word) local lookup = EID:getIcon(word) or EID.InlineIcons["ERROR"] - local Xoffset = lookup[5] or 0 - local prefixTextWidth = EID:getStrWidth(string.sub(text, 0, textposition - 1))*EIDConfig["Scale"] - EID.InlineIconSprite:SetFrame(lookup[1], lookup[2]) - EID.InlineIconSprite.Scale = Vector(EIDConfig["Scale"], EIDConfig["Scale"]) - EID.InlineIconSprite:Render( - Vector((textPosX + prefixTextWidth + Xoffset -1), textPosY), + local preceedingTextWidth = EID:getStrWidth(string.sub(text, 0, textposition - 1))*EIDConfig["Scale"] + table.insert(spriteTable,{lookup, preceedingTextWidth}) + text = string.gsub(text, word, EID:generatePlaceholderString(lookup[3]), 1) + end + return text, spriteTable +end + +--renders a list of given inline sprite objects returned by the "EID:filterMarkup()" function +-- Table entry format: {EID.InlineIcons Object, Width of text preceeding the icon} +function EID:renderInlineIcons(spriteTable, posX, posY) + for _,sprite in ipairs(spriteTable) do + local Xoffset = sprite[1][5] or -1 + local Yoffset = sprite[1][6] or 0 + local spriteObj = sprite[1][7] or EID.InlineIconSprite + spriteObj:SetFrame(sprite[1][1], sprite[1][2]) + spriteObj.Scale = Vector(EIDConfig["Scale"], EIDConfig["Scale"]) + spriteObj:Render( + Vector((posX + sprite[2] + Xoffset), posY + Yoffset), Vector(0, 0), Vector(0, 0) ) - text = string.gsub(text, word, EID:generatePlaceholderString(lookup[3]), 1) end - return text end -- Returns the icon used for the bulletpoint. It will look at the first word in the given string. @@ -243,7 +275,8 @@ end -- args: string, Vector(int, int), Vector(float,float), KColor obj, bool function EID:renderString(str, position, scale, kcolor) str = EID:replaceShortMarkupStrings(str) - str = EID:filterMarkup(str, position.X, position.Y) + str, spriteTable = EID:filterMarkup(str, position.X, position.Y) + EID:renderInlineIcons(spriteTable, position.X, position.Y) EID.font:DrawStringScaledUTF8(str, position.X, position.Y, scale.X, scale.Y, kcolor, 0, false) end diff --git a/main.lua b/main.lua index c64c9f05..c50e626b 100644 --- a/main.lua +++ b/main.lua @@ -167,7 +167,7 @@ function printDescription(desc) printBulletPoints(desc.Description, padding) end -function printTrinketDescription(desc, objType) +function printTrinketDescription(desc) local padding = posY() --Display Itemname if EIDConfig["ShowItemName"] then @@ -354,54 +354,22 @@ local function onRender(t) --Handle Trinkets if closest.Variant == PickupVariant.PICKUP_TRINKET then + printTrinketDescription(EID:getDescriptionObj("trinkets",closest.SubType)) --Handle Collectibles - if EID:getModDescription(__eidTrinketDescriptions, closest.SubType) then - printTrinketDescription( - {closest.SubType, EID:getModDescription(__eidTrinketDescriptions, closest.SubType)}, - "trinkets" - ) - elseif closest.SubType <= 128 then - printTrinketDescription(EID:getDescriptionObj("trinkets",closest.SubType), "trinkets") - else - printTrinketDescription({closest.SubType, EID.itemConfig:GetTrinket(closest.SubType).Description}) - end elseif closest.Variant == PickupVariant.PICKUP_COLLECTIBLE then - --Handle Cards & Runes if EID:hasCurseBlind() and EIDConfig["DisableOnCurse"] then renderQuestionMark() return end - if EID:getModDescription(__eidItemDescriptions, closest.SubType) then - local tranformation = "0" - if EID:getModDescription(__eidItemTransformations, closest.SubType) then - tranformation = EID:getModDescription(__eidItemTransformations, closest.SubType) - end - printDescription({closest.SubType, tranformation, EID:getModDescription(__eidItemDescriptions, closest.SubType)}) - elseif closest.SubType <= 552 then - if EID:getModDescription(__eidItemTransformations, closest.SubType) then - printDescription( - { - closest.SubType, - EID:getModDescription(__eidItemTransformations, closest.SubType), - EID:getDescriptionObj("collectibles",closest.SubType)[3] - } - ) - else - printDescription(EID:getDescriptionObj("collectibles",closest.SubType)) - end - else - printDescription({closest.SubType, "", EID.itemConfig:GetCollectible(closest.SubType).Description}) - end + printDescription(EID:getDescriptionObj("collectibles",closest.SubType)) + --Handle Cards & Runes elseif closest.Variant == PickupVariant.PICKUP_TAROTCARD then - --Handle Pills if closest:ToPickup():IsShopItem() and not EIDConfig["DisplayCardInfoShop"] then renderQuestionMark() return end - if EID:getModDescription(__eidCardDescriptions, closest.SubType) then - printTrinketDescription({closest.SubType, EID:getModDescription(__eidCardDescriptions, closest.SubType)}, "cards") - elseif closest.SubType <= 54 then - printTrinketDescription(EID:getDescriptionObj("cards", closest.SubType), "cards") + printTrinketDescription(EID:getDescriptionObj("cards", closest.SubType), "cards") + if closest.SubType <= 54 then CardSprite:Play(tostring(closest.SubType)) CardSprite.Scale = Vector(EIDConfig["Scale"], EIDConfig["Scale"]) CardSprite:Update() @@ -414,9 +382,8 @@ local function onRender(t) Vector(0, 0), Vector(0, 0) ) - else - printTrinketDescription({closest.SubType, EID.itemConfig:GetCard(closest.SubType).Description}) end + --Handle Pills elseif closest.Variant == PickupVariant.PICKUP_PILL then if closest:ToPickup():IsShopItem() and not EIDConfig["DisplayPillInfoShop"] then renderQuestionMark()