Something went wrong. Try again.
General purpose modules for Roblox development. [Read-only Codeberg mirror]
Something went wrong. Try again.
9.3 kB · 248 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249--!optimize 2--!strictlocal module = {}
local TextService = game:GetService("TextService")
local X_ALIGNMENT_MAP = { [Enum.TextXAlignment.Left] = Enum.HorizontalAlignment.Left, [Enum.TextXAlignment.Center] = Enum.HorizontalAlignment.Center, [Enum.TextXAlignment.Right] = Enum.HorizontalAlignment.Right,}local Y_ALIGNMENT_MAP = { [Enum.TextYAlignment.Top] = Enum.VerticalAlignment.Top, [Enum.TextYAlignment.Center] = Enum.VerticalAlignment.Center, [Enum.TextYAlignment.Bottom] = Enum.VerticalAlignment.Bottom,}
-- Creates a <code>Frame</code> from a <code>TextLabel</code> for easy manipulation of letters.-- Intended to be used with <code>InsertLetter(listFrame: Frame, letter: string, index: number): Frame?</code>function module.CreateFrameFromLabel(labelTemplate: TextLabel): Frame local originalText = labelTemplate.Text local originalSize = labelTemplate.Size local originalPosition = labelTemplate.Position local originalAnchorPoint = labelTemplate.AnchorPoint local originalRotation = labelTemplate.Rotation local originalName = labelTemplate.Name local originalBackgroundColor3 = labelTemplate.BackgroundColor3 local originalBackgroundTransparency = labelTemplate.BackgroundTransparency local originalXAlignment = labelTemplate.TextXAlignment local originalYAlignment = labelTemplate.TextYAlignment
local listFrame = Instance.new("Frame") listFrame.Name = originalName listFrame.Size = originalSize listFrame.Position = originalPosition listFrame.AnchorPoint = originalAnchorPoint listFrame.Rotation = originalRotation listFrame.BorderSizePixel = 0 listFrame.BackgroundTransparency = 1 listFrame.ClipsDescendants = true listFrame:SetAttribute("OriginalBackgroundColor3", originalBackgroundColor3) listFrame:SetAttribute("OriginalBackgroundTransparency", originalBackgroundTransparency) listFrame:SetAttribute("OriginalText", originalText)
local layout = Instance.new("UIListLayout") layout.Name = "LetterLayout" layout.FillDirection = Enum.FillDirection.Horizontal layout.Wraps = true layout.HorizontalAlignment = X_ALIGNMENT_MAP[originalXAlignment] or Enum.HorizontalAlignment.Left layout.VerticalAlignment = Y_ALIGNMENT_MAP[originalYAlignment] or Enum.VerticalAlignment.Top layout.SortOrder = Enum.SortOrder.LayoutOrder layout.Parent = listFrame
return listFrameend
-- Transforms a <code>TextLabel</code> into a <code>Frame</code> for easy manipulation of letters.-- Letter index is stored in <code>LayoutOrder</code> for each <code>Frame</code>.-- Stores necessary style information as attributes on the returned <code>Frame</code>.function module.TransformText(labelToTransform: TextLabel): Frame local originalText = labelToTransform.Text local originalParent = labelToTransform.Parent local originalFontFace = labelToTransform.FontFace local originalTextSize = labelToTransform.TextSize local originalTextColor = labelToTransform.TextColor3 local originalTextTransparency = labelToTransform.TextTransparency local originalTextStrokeColor = labelToTransform.TextStrokeColor3 local originalTextStrokeTransparency = labelToTransform.TextStrokeTransparency local originalXAlignment = labelToTransform.TextXAlignment local originalBackgroundColor3 = labelToTransform.BackgroundColor3 local originalBackgroundTransparency = labelToTransform.BackgroundTransparency
local listFrame = module.CreateFrameFromLabel(labelToTransform) listFrame.BackgroundColor3 = originalBackgroundColor3 listFrame.BackgroundTransparency = originalBackgroundTransparency
listFrame:SetAttribute("LetterFontFace", originalFontFace) listFrame:SetAttribute("LetterTextSize", originalTextSize) listFrame:SetAttribute("LetterTextColor3", originalTextColor) listFrame:SetAttribute("LetterTextTransparency", originalTextTransparency) listFrame:SetAttribute("LetterTextStrokeColor3", originalTextStrokeColor) listFrame:SetAttribute("LetterTextStrokeTransparency", originalTextStrokeTransparency) listFrame:SetAttribute("LetterTextXAlignment", originalXAlignment)
local spaceParams = Instance.new("GetTextBoundsParams") spaceParams.Font = originalFontFace spaceParams.Size = originalTextSize spaceParams.Text = " " local success, spaceSize: Vector2 = pcall(function() return TextService:GetTextBoundsAsync(spaceParams) end) if success then listFrame:SetAttribute("SpaceSizeX", spaceSize.X) listFrame:SetAttribute("SpaceSizeY", spaceSize.Y) else listFrame:SetAttribute("SpaceSizeX", 10) listFrame:SetAttribute("SpaceSizeY", 10) end
local charParams = Instance.new("GetTextBoundsParams") charParams.Font = originalFontFace charParams.Size = originalTextSize
local textLength = utf8.len(originalText) :: number local childrenToParent: { Frame } = table.create(textLength) :: { Frame } local childIndex = 0 for char in string.gmatch(originalText, ".") do local charSize: Vector2 local isWhitespace = string.match(char, "%s")
if isWhitespace then charSize = spaceSize else charParams.Text = char charSize = TextService:GetTextBoundsAsync(charParams) end
local textContainer = Instance.new("Frame") textContainer.Name = char textContainer.BackgroundTransparency = 1 textContainer.BorderSizePixel = 0 textContainer.Size = UDim2.fromOffset(charSize.X, charSize.Y) textContainer.LayoutOrder = childIndex
if not isWhitespace then local charLabel = Instance.new("TextLabel") charLabel.Name = "CharLabel" charLabel.FontFace = originalFontFace charLabel.TextSize = originalTextSize charLabel.TextColor3 = originalTextColor charLabel.TextTransparency = originalTextTransparency charLabel.TextStrokeColor3 = originalTextStrokeColor charLabel.TextStrokeTransparency = originalTextStrokeTransparency charLabel.BackgroundTransparency = 1 charLabel.Size = UDim2.fromScale(1, 1) charLabel.Text = char charLabel.TextXAlignment = originalXAlignment charLabel.TextYAlignment = Enum.TextYAlignment.Center charLabel.Parent = textContainer end
childIndex += 1 childrenToParent[childIndex] = textContainer end
for i = 1, childIndex do childrenToParent[i].Parent = listFrame end
table.clear(childrenToParent)
listFrame.Parent = originalParent labelToTransform:Destroy()
return listFrameend
-- Inserts a new letter (<code>Frame</code>) into the <code>listFrame</code> at the specified index.-- Adjusts <code>LayoutOrder</code> of subsequent letters.-- Returns the newly created letter <code>Frame</code> or <code>nil</code> if attributes are missing.function module.InsertLetter(listFrame: Frame, letter: string, index: number): Frame? local fontFace = listFrame:GetAttribute("LetterFontFace") :: Font? local textSize = listFrame:GetAttribute("LetterTextSize") :: number? local textColor = listFrame:GetAttribute("LetterTextColor3") :: Color3? local textTransparency = listFrame:GetAttribute("LetterTextTransparency") :: number? local textStrokeColor = listFrame:GetAttribute("LetterTextStrokeColor3") :: Color3? local textStrokeTransparency = listFrame:GetAttribute("LetterTextStrokeTransparency") :: number? local textXAlignment = listFrame:GetAttribute("LetterTextXAlignment") :: Enum.TextXAlignment? local spaceSizeX = listFrame:GetAttribute("SpaceSizeX") :: number? local spaceSizeY = listFrame:GetAttribute("SpaceSizeY") :: number?
if not ( fontFace and textSize and textColor and textTransparency and textStrokeColor and textStrokeTransparency and textXAlignment and spaceSizeX and spaceSizeY ) then warn("Cannot InsertLetter: listFrame is missing required style attributes.") return nil end
local spaceSize = Vector2.new(spaceSizeX, spaceSizeY)
local currentLetters: { Frame } = {} for _, child in ipairs(listFrame:GetChildren()) do if child:IsA("Frame") then table.insert(currentLetters, child) end end
index = math.clamp(index, 0, #currentLetters)
for _, childFrame in ipairs(currentLetters) do if childFrame.LayoutOrder >= index then childFrame.LayoutOrder += 1 end end
local charSize: Vector2 local isWhitespace = string.match(letter, "%s")
if isWhitespace then charSize = spaceSize else local charParams = Instance.new("GetTextBoundsParams") charParams.Font = fontFace charParams.Size = textSize charParams.Text = letter charSize = TextService:GetTextBoundsAsync(charParams) end
local textContainer = Instance.new("Frame") textContainer.Name = letter textContainer.BackgroundTransparency = 1 textContainer.BorderSizePixel = 0 textContainer.Size = UDim2.fromOffset(charSize.X, charSize.Y) textContainer.LayoutOrder = index
if not isWhitespace then local charLabel = Instance.new("TextLabel") charLabel.Name = "CharLabel" charLabel.FontFace = fontFace charLabel.TextSize = textSize charLabel.TextColor3 = textColor charLabel.TextTransparency = textTransparency charLabel.TextStrokeColor3 = textStrokeColor charLabel.TextStrokeTransparency = textStrokeTransparency charLabel.BackgroundTransparency = 1 charLabel.Size = UDim2.fromScale(1, 1) charLabel.Text = letter charLabel.TextXAlignment = textXAlignment charLabel.TextYAlignment = Enum.TextYAlignment.Center charLabel.Parent = textContainer end
textContainer.Parent = listFrame
return textContainerend
return module