From e863ba459bcbb58451f0141f0d36e5eba154e202 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Fri, 29 Jul 2022 22:49:12 +0200 Subject: [PATCH] Split UI message text to fit on screen --- files/ui.lua | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/files/ui.lua b/files/ui.lua index b122493..584d0c6 100644 --- a/files/ui.lua +++ b/files/ui.lua @@ -18,6 +18,30 @@ require("utilities") -- Loads Noita's utilities from `data/scripts/lib/utilitite -- Code -- ---------- +---Splits the given string to fit inside maxLength. +---@param gui userdata +---@param text string +---@param maxLength number -- In UI pixels. +---@return string[] +local function splitString(gui, text, maxLength) + local splitted = {} + + local first, rest = text, "" + + while first:len() > 0 do + local width, height = GuiGetTextDimensions(gui, first, 1, 2) + if width <= maxLength then + table.insert(splitted, first) + first, rest = rest, "" + else + first, rest = first:sub(1, -2), first:sub(-1, -1) .. rest + end + end + + + return splitted +end + ---Returns unique IDs for the widgets. ---`_ResetID` has to be called every time before the UI is rebuilt. ---@return integer @@ -69,6 +93,8 @@ function UI:_DrawMessages(messages) -- Abort if there is no messages list. if not messages then return end + local screenWidth, screenHeight = GuiGetScreenDimensions(gui) + GuiZSet(gui, 0) -- Unfortunately you can't stack multiple layout containers with the same direction. @@ -92,7 +118,9 @@ function UI:_DrawMessages(messages) if type(message.Lines) == "table" then for _, line in ipairs(message.Lines) do for splitLine in tostring(line):gmatch("[^\n]+") do - GuiText(gui, 0, 0, splitLine) posY = posY + 11 + for _, splitLine in ipairs(splitString(gui, splitLine, screenWidth - 80)) do + GuiText(gui, 0, 0, splitLine) posY = posY + 11 + end end end end