From a7201c3deb4d6ab6ee7939a36451b2aebe4a2cee Mon Sep 17 00:00:00 2001 From: Steel <48765827+CodeSteel@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:11:01 -0500 Subject: [PATCH 1/2] feat: text_entry enhancements --- lua/pixelui/elements/cl_text_entry.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/pixelui/elements/cl_text_entry.lua b/lua/pixelui/elements/cl_text_entry.lua index 006c622..99be223 100644 --- a/lua/pixelui/elements/cl_text_entry.lua +++ b/lua/pixelui/elements/cl_text_entry.lua @@ -39,12 +39,13 @@ end function PANEL:Paint(w, h) if not self:IsEnabled() then PIXEL.DrawRoundedBox(PIXEL.Scale(4), 0, 0, w, h, self.DisabledCol) - PIXEL.DrawSimpleText("Disabled", "UI.TextEntry", PIXEL.Scale(4), h / 2, PIXEL.Colors.SecondaryText, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) + PIXEL.DrawSimpleText("Disabled", self:GetFont(), PIXEL.Scale(4), h / 2, PIXEL.Colors.SecondaryText, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) return end if self:GetValue() == "" then - PIXEL.DrawSimpleText(self:GetPlaceholderText() or "", "UI.TextEntry", PIXEL.Scale(10), h / 2, self.PlaceholderTextCol, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) + local placeholderY = self:IsMultiline() and draw.GetFontHeight(self:GetFont()) or h / 2 + PIXEL.DrawSimpleText(self:GetPlaceholderText() or "", self:GetFont(), PIXEL.Scale(10), placeholderY, self.PlaceholderTextCol, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER) end local outlineThickness = PIXEL.Scale(1) @@ -97,6 +98,9 @@ function PANEL:SetTabbingDisabled(disabled) self.TextEntry:SetTabbingDisabled(di function PANEL:GetPlaceholderText() return self.TextEntry:GetPlaceholderText() end function PANEL:SetPlaceholderText(text) self.TextEntry:SetPlaceholderText(text) end +function PANEL:GetFont() return self.TextEntry:GetFont() end +function PANEL:SetFont(font) self.TextEntry:SetFontInternal(font) end + function PANEL:GetInt() return self.TextEntry:GetInt() end function PANEL:GetFloat() return self.TextEntry:GetFloat() end From 720cf0ef2979385de19728e542bba1720efc3cc6 Mon Sep 17 00:00:00 2001 From: Steel <48765827+CodeSteel@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:31:34 -0500 Subject: [PATCH 2/2] feat: add text-wrap format --- lua/pixelui/core/sh_formatting.lua | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lua/pixelui/core/sh_formatting.lua b/lua/pixelui/core/sh_formatting.lua index 7bdfd05..e8c2090 100644 --- a/lua/pixelui/core/sh_formatting.lua +++ b/lua/pixelui/core/sh_formatting.lua @@ -91,3 +91,42 @@ function PIXEL.FormatTime(time) return format("%im %is", m, s) end + +function PIXEL.TextWrap(str, font, maxWidth) + if (!text) then + return "" + end + + local totalWidth = 0 + + surface.SetFont(font) + + local spaceWidth = surface.GetTextSize(' ') + str = str:gsub("(%s?[%S]+)", function(word) + local char = string.sub(word, 1, 1) + if char == "\n" or char == "\t" then + totalWidth = 0 + end + + local wordlen = surface.GetTextSize(word) + totalWidth = totalWidth + wordlen + + if wordlen >= maxWidth then + local splitWord, splitPoint = charWrap(word, maxWidth - (totalWidth - wordlen), maxWidth) + totalWidth = splitPoint + return splitWord + elseif totalWidth < maxWidth then + return word + end + + if char == ' ' then + totalWidth = wordlen - spaceWidth + return '\n' .. string.sub(word, 2) + end + + totalWidth = wordlen + return '\n' .. word + end) + + return str +end