General purpose modules for Roblox development. [Read-only Codeberg mirror]
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

seperate into a few functions

+168 -27
+168 -27
Modules/TextTransform.luau
··· 1 1 --!optimize 2 2 2 --!strict 3 + local module = {} 4 + 3 5 local TextService = game:GetService("TextService") 4 - local module = {} 5 6 6 7 local X_ALIGNMENT_MAP = { 7 8 [Enum.TextXAlignment.Left] = Enum.HorizontalAlignment.Left, ··· 14 15 [Enum.TextYAlignment.Bottom] = Enum.VerticalAlignment.Bottom, 15 16 } 16 17 17 - -- Transforms a <code>TextLabel</code> into a <code>Frame</code> with many children for easy manipulation of letters. Letter index is stored in <code>LayoutOrder</code> for each <code>Frame</code>. 18 - function module.TransformText(labelToTransform: TextLabel): Frame 19 - local originalText = labelToTransform.Text 20 - local originalSize = labelToTransform.Size 21 - local originalPosition = labelToTransform.Position 22 - local originalAnchorPoint = labelToTransform.AnchorPoint 23 - local originalRotation = labelToTransform.Rotation 24 - local originalParent = labelToTransform.Parent 25 - local originalName = labelToTransform.Name 26 - local originalFontFace = labelToTransform.FontFace 27 - local originalTextSize = labelToTransform.TextSize 28 - local originalTextColor = labelToTransform.TextColor3 29 - local originalTextTransparency = labelToTransform.TextTransparency 30 - local originalTextStrokeColor = labelToTransform.TextStrokeColor3 31 - local originalTextStrokeTransparency = 32 - labelToTransform.TextStrokeTransparency 33 - local originalXAlignment = labelToTransform.TextXAlignment 34 - local originalYAlignment = labelToTransform.TextYAlignment 35 - local originalBackgroundColor3 = labelToTransform.BackgroundColor3 36 - local originalBackgroundTransparency = labelToTransform.BackgroundTransparency 18 + -- Creates a <code>Frame</code> from a <code>TextLabel</code> for easy manipulation of letters. 19 + -- Intended to be used with <code>InsertLetter(listFrame: Frame, letter: string, index: number): Frame?</code> 20 + function module.CreateFrameFromLabel(labelTemplate: TextLabel): Frame 21 + local originalText = labelTemplate.Text 22 + local originalSize = labelTemplate.Size 23 + local originalPosition = labelTemplate.Position 24 + local originalAnchorPoint = labelTemplate.AnchorPoint 25 + local originalRotation = labelTemplate.Rotation 26 + local originalName = labelTemplate.Name 27 + local originalBackgroundColor3 = labelTemplate.BackgroundColor3 28 + local originalBackgroundTransparency = labelTemplate.BackgroundTransparency 29 + local originalXAlignment = labelTemplate.TextXAlignment 30 + local originalYAlignment = labelTemplate.TextYAlignment 37 31 38 32 local listFrame = Instance.new("Frame") 39 33 listFrame.Name = originalName ··· 44 38 listFrame.BorderSizePixel = 0 45 39 listFrame.BackgroundTransparency = 1 46 40 listFrame.ClipsDescendants = true 47 - listFrame.BackgroundColor3 = originalBackgroundColor3 48 - listFrame.BackgroundTransparency = originalBackgroundTransparency 41 + listFrame:SetAttribute("OriginalBackgroundColor3", originalBackgroundColor3) 42 + listFrame:SetAttribute( 43 + "OriginalBackgroundTransparency", 44 + originalBackgroundTransparency 45 + ) 46 + listFrame:SetAttribute("OriginalText", originalText) 49 47 50 48 local layout = Instance.new("UIListLayout") 49 + layout.Name = "LetterLayout" 51 50 layout.FillDirection = Enum.FillDirection.Horizontal 52 51 layout.Wraps = true 53 52 layout.HorizontalAlignment = ··· 57 56 layout.SortOrder = Enum.SortOrder.LayoutOrder 58 57 layout.Parent = listFrame 59 58 59 + return listFrame 60 + end 61 + 62 + -- Transforms a <code>TextLabel</code> into a <code>Frame</code> for easy manipulation of letters. 63 + -- Letter index is stored in <code>LayoutOrder</code> for each <code>Frame</code>. 64 + -- Stores necessary style information as attributes on the returned <code>Frame</code>. 65 + function module.TransformText(labelToTransform: TextLabel): Frame 66 + local originalText = labelToTransform.Text 67 + local originalParent = labelToTransform.Parent 68 + local originalFontFace = labelToTransform.FontFace 69 + local originalTextSize = labelToTransform.TextSize 70 + local originalTextColor = labelToTransform.TextColor3 71 + local originalTextTransparency = labelToTransform.TextTransparency 72 + local originalTextStrokeColor = labelToTransform.TextStrokeColor3 73 + local originalTextStrokeTransparency = 74 + labelToTransform.TextStrokeTransparency 75 + local originalXAlignment = labelToTransform.TextXAlignment 76 + local originalBackgroundColor3 = labelToTransform.BackgroundColor3 77 + local originalBackgroundTransparency = labelToTransform.BackgroundTransparency 78 + 79 + local listFrame = module.CreateFrameFromLabel(labelToTransform) 80 + listFrame.BackgroundColor3 = originalBackgroundColor3 81 + listFrame.BackgroundTransparency = originalBackgroundTransparency 82 + 83 + listFrame:SetAttribute("LetterFontFace", originalFontFace) 84 + listFrame:SetAttribute("LetterTextSize", originalTextSize) 85 + listFrame:SetAttribute("LetterTextColor3", originalTextColor) 86 + listFrame:SetAttribute("LetterTextTransparency", originalTextTransparency) 87 + listFrame:SetAttribute("LetterTextStrokeColor3", originalTextStrokeColor) 88 + listFrame:SetAttribute( 89 + "LetterTextStrokeTransparency", 90 + originalTextStrokeTransparency 91 + ) 92 + listFrame:SetAttribute("LetterTextXAlignment", originalXAlignment) 93 + 60 94 local spaceParams = Instance.new("GetTextBoundsParams") 61 95 spaceParams.Font = originalFontFace 62 96 spaceParams.Size = originalTextSize 63 97 spaceParams.Text = " " 64 - local spaceSize: Vector2 = TextService:GetTextBoundsAsync(spaceParams) 98 + local success, spaceSize: Vector2 = pcall(function() 99 + return TextService:GetTextBoundsAsync(spaceParams) 100 + end) 101 + if success then 102 + listFrame:SetAttribute("SpaceSizeX", spaceSize.X) 103 + listFrame:SetAttribute("SpaceSizeY", spaceSize.Y) 104 + else 105 + listFrame:SetAttribute("SpaceSizeX", 10) 106 + listFrame:SetAttribute("SpaceSizeY", 10) 107 + end 65 108 66 109 local charParams = Instance.new("GetTextBoundsParams") 67 110 charParams.Font = originalFontFace 68 111 charParams.Size = originalTextSize 69 112 70 - local textLength = utf8.len(originalText) 71 - local childrenToParent = table.create(textLength) 113 + local textLength = utf8.len(originalText) :: number 114 + local childrenToParent: { Frame } = table.create(textLength) :: { Frame } 72 115 local childIndex = 0 73 - 74 116 for char in string.gmatch(originalText, ".") do 75 117 local charSize: Vector2 76 118 local isWhitespace = string.match(char, "%s") ··· 91 133 92 134 if not isWhitespace then 93 135 local charLabel = Instance.new("TextLabel") 136 + charLabel.Name = "CharLabel" 94 137 charLabel.FontFace = originalFontFace 95 138 charLabel.TextSize = originalTextSize 96 139 charLabel.TextColor3 = originalTextColor ··· 120 163 labelToTransform:Destroy() 121 164 122 165 return listFrame 166 + end 167 + 168 + -- Inserts a new letter (<code>Frame</code>) into the <code>listFrame</code> at the specified index. 169 + -- Adjusts <code>LayoutOrder</code> of subsequent letters. 170 + -- Returns the newly created letter <code>Frame</code> or <code>nil</code> if attributes are missing. 171 + function module.InsertLetter( 172 + listFrame: Frame, 173 + letter: string, 174 + index: number 175 + ): Frame? 176 + local fontFace = listFrame:GetAttribute("LetterFontFace") :: Font? 177 + local textSize = listFrame:GetAttribute("LetterTextSize") :: number? 178 + local textColor = listFrame:GetAttribute("LetterTextColor3") :: Color3? 179 + local textTransparency = 180 + listFrame:GetAttribute("LetterTextTransparency") :: number? 181 + local textStrokeColor = 182 + listFrame:GetAttribute("LetterTextStrokeColor3") :: Color3? 183 + local textStrokeTransparency = 184 + listFrame:GetAttribute("LetterTextStrokeTransparency") :: number? 185 + local textXAlignment = 186 + listFrame:GetAttribute("LetterTextXAlignment") :: Enum.TextXAlignment? 187 + local spaceSizeX = listFrame:GetAttribute("SpaceSizeX") :: number? 188 + local spaceSizeY = listFrame:GetAttribute("SpaceSizeY") :: number? 189 + 190 + if not ( 191 + fontFace 192 + and textSize 193 + and textColor 194 + and textTransparency 195 + and textStrokeColor 196 + and textStrokeTransparency 197 + and textXAlignment 198 + and spaceSizeX 199 + and spaceSizeY 200 + ) then 201 + warn( 202 + "Cannot InsertLetter: listFrame is missing required style attributes." 203 + ) 204 + return nil 205 + end 206 + 207 + local spaceSize = Vector2.new(spaceSizeX, spaceSizeY) 208 + 209 + local currentLetters: { Frame } = {} 210 + for _, child in ipairs(listFrame:GetChildren()) do 211 + if child:IsA("Frame") then 212 + table.insert(currentLetters, child) 213 + end 214 + end 215 + 216 + index = math.clamp(index, 0, #currentLetters) 217 + 218 + for _, childFrame in ipairs(currentLetters) do 219 + if childFrame.LayoutOrder >= index then 220 + childFrame.LayoutOrder += 1 221 + end 222 + end 223 + 224 + local charSize: Vector2 225 + local isWhitespace = string.match(letter, "%s") 226 + 227 + if isWhitespace then 228 + charSize = spaceSize 229 + else 230 + local charParams = Instance.new("GetTextBoundsParams") 231 + charParams.Font = fontFace 232 + charParams.Size = textSize 233 + charParams.Text = letter 234 + charSize = TextService:GetTextBoundsAsync(charParams) 235 + end 236 + 237 + local textContainer = Instance.new("Frame") 238 + textContainer.Name = letter 239 + textContainer.BackgroundTransparency = 1 240 + textContainer.BorderSizePixel = 0 241 + textContainer.Size = UDim2.fromOffset(charSize.X, charSize.Y) 242 + textContainer.LayoutOrder = index 243 + 244 + if not isWhitespace then 245 + local charLabel = Instance.new("TextLabel") 246 + charLabel.Name = "CharLabel" 247 + charLabel.FontFace = fontFace 248 + charLabel.TextSize = textSize 249 + charLabel.TextColor3 = textColor 250 + charLabel.TextTransparency = textTransparency 251 + charLabel.TextStrokeColor3 = textStrokeColor 252 + charLabel.TextStrokeTransparency = textStrokeTransparency 253 + charLabel.BackgroundTransparency = 1 254 + charLabel.Size = UDim2.fromScale(1, 1) 255 + charLabel.Text = letter 256 + charLabel.TextXAlignment = textXAlignment 257 + charLabel.TextYAlignment = Enum.TextYAlignment.Center 258 + charLabel.Parent = textContainer 259 + end 260 + 261 + textContainer.Parent = listFrame 262 + 263 + return textContainer 123 264 end 124 265 125 266 return module