diff --git a/eid_api.lua b/eid_api.lua index a0eb1570..aebc8f15 100644 --- a/eid_api.lua +++ b/eid_api.lua @@ -257,7 +257,7 @@ end -- 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) +function EID:filterIconMarkup(text, textPosX, textPosY) local spriteTable = {} for word in string.gmatch(text, "{{.-}}") do local textposition = string.find(text, word) @@ -269,7 +269,7 @@ function EID:filterMarkup(text, textPosX, textPosY) return text, spriteTable end ---renders a list of given inline sprite objects returned by the "EID:filterMarkup()" function +--renders a list of given inline sprite objects returned by the "EID:filterIconMarkup()" 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 @@ -328,10 +328,39 @@ function EID:filterColorMarkup(text, baseKColor) end end - table.insert(textPartsTable,{string.sub(text, lastPosition), lastColor,0}) + table.insert(textPartsTable,{string.sub(text, lastPosition), lastColor, 0}) return textPartsTable end +-- Fits a given string to a specific width +-- returns the string as a table of lines +function EID:fitTextToWidth(str, textboxWidth) + local formatedLines = {} + local curLength = 0 + local text = "" + for word in string.gmatch(str, "([^%s]+)") do + local colorFiltered = EID:filterColorMarkup(word, EID:getTextColor()) + local filteredWord = "" + for _,filtered in ipairs(colorFiltered) do + filteredWord = filteredWord..filtered[1] + end + local strFiltered, spriteTable = EID:filterIconMarkup(filteredWord, 0, 0) + local wordLength = EID:getStrWidth(strFiltered) + + if curLength + wordLength <= textboxWidth or curLength < 12 then + text = text .. word .. " " + curLength = curLength + wordLength + else + print(text) + table.insert(formatedLines, text) + text = word .. " " + curLength = wordLength + end + end + table.insert(formatedLines, text) + return formatedLines +end + -- Renders a given string using the EID Custom font. This will also apply any markup and render icons -- needs to be called in a render Callback -- args: string, Vector(int, int), Vector(float,float), KColor obj, bool @@ -342,9 +371,9 @@ function EID:renderString(str, position, scale, kcolor) local textPartsTable = EID:filterColorMarkup(str, kcolor) local offsetX = 0 for i, textPart in ipairs(textPartsTable) do - local strFiltered, spriteTable = EID:filterMarkup(textPart[1], position.X, position.Y) + local strFiltered, spriteTable = EID:filterIconMarkup(textPart[1], position.X, position.Y) EID:renderInlineIcons(spriteTable, position.X, position.Y) - EID.font:DrawStringScaledUTF8(strFiltered, position.X+offsetX, position.Y, scale.X, scale.Y, textPart[2], 0, false) + EID.font:DrawStringScaledUTF8(strFiltered, position.X + offsetX, position.Y, scale.X, scale.Y, textPart[2], 0, false) offsetX = offsetX + textPart[3] end return textPartsTable[#textPartsTable][2] diff --git a/eid_data.lua b/eid_data.lua index 4236ca38..b504548e 100644 --- a/eid_data.lua +++ b/eid_data.lua @@ -163,7 +163,7 @@ EID.InlineColors ={ return EID:interpolateColors(colors[primaryColorIndex], colors[secondaryColorIndex], (animTime%(colorFractions+1))/colorFractions) end, -- Text will blink frequently - ["ColorBlink"] = function (color) + ["ColorBlink"] = function (color) local maxAnimTime = 40 local animTime = Game():GetFrameCount() % maxAnimTime color = EID:copyKColor(color) or EID:getTextColor() @@ -175,7 +175,7 @@ EID.InlineColors ={ return color end, -- Text will fade in and out - ["ColorFade"] = function (color) + ["ColorFade"] = function (color) local maxAnimTime = 30 local animTime = Game():GetFrameCount() % (maxAnimTime+10) color = EID:copyKColor(color) or EID:getTextColor() diff --git a/eid_debugging.lua b/eid_debugging.lua index 40fa0ac1..639bb5d6 100644 --- a/eid_debugging.lua +++ b/eid_debugging.lua @@ -46,6 +46,9 @@ __eidItemTransformations[2] = "Some Test Transformation" ------ Test: Add all transformations to spoon bender ------ __eidItemTransformations[3] = "1,2,3,4,5,6,7,8,9,10,11,12,13" +------ Test: Description with colors ------ +__eidItemDescriptions[3] = "Some {{ColorRed}}long ass Test string with a lot of Words that sound interesting#With{{ColorBlink}} cool {{CR}}{{ColorRed}}C{{ColorGreen}}o{{ColorBlue}}l{{ColorYellow}}o{{ColorOrange}}r{{ColorPink}}s{{ColorWhite}} and a high amount of nifty {{ColorRainbow}}Swag " + local function onDebugRender(t) for i, v in ipairs(charsToDebug) do local pos = Vector(EIDConfig["XPosition"], EIDConfig["YPosition"] - 15 + ((i - 1) * 14)) diff --git a/main.lua b/main.lua index 858ea0f1..c1955674 100644 --- a/main.lua +++ b/main.lua @@ -178,27 +178,8 @@ end function printBulletPoints(description, padding) local textboxWidth = tonumber(EIDConfig["TextboxWidth"]) * 4 description = EID:replaceShortMarkupStrings(description) - for line in string.gmatch(description, "([^#]+)") do - line = string.gsub(line, "([^%s])({{)", function(a,b) return a.." "..b end) -- add space between icons, if not already existing - local formatedLines = {} - local text = "" - local curLength = 0 - for word in string.gmatch(line, "([^%s]+)") do - local wordLength = EID:getStrWidth(word) - if EID:getIcon(word) ~= EID.InlineIcons["ERROR"] then - wordLength = EID:getIcon(word)[3] - end - if curLength + wordLength <= textboxWidth or curLength < 12 then - text = text .. word .. " " - curLength = curLength + wordLength - else - table.insert(formatedLines, text) - text = word .. " " - curLength = wordLength - end - end - table.insert(formatedLines, text) + local formatedLines = EID:fitTextToWidth(line, textboxWidth) local textColor = EID:getTextColor() for i, lineToPrint in ipairs(formatedLines) do -- render bulletpoint