Last active
May 11, 2019 12:51
-
-
Save Phoenix1123/95863a1a903f7d50ec68a2e86e34d5c7 to your computer and use it in GitHub Desktop.
A utility for creating/drawing paragraphs on 2d planes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A utilty for drawing paragraphs on 2d planes (derma, vgui/hud etc..) | |
-- Original file found at https://gist.github.com/Khubajsn/5482298 (2013) | |
-- Modified/fixed/updated by ParaPhoenix/Jabami/PyroPhoe | |
-- Feel free to use/modify/republish | |
--[[ | |
Break up a long string into lines <= the mWidth provided | |
text -> The text to seperate into lines | |
font -> The font you will be using to draw the text | |
mWidth -> The maximum width of each line (GetTextSize(n)) | |
--]] | |
local function toLines(text, font, mWidth) | |
surface.SetFont(font) | |
local buffer = { } | |
local nLines = { } | |
for word in string.gmatch(text, "%S+") do | |
local w,h = surface.GetTextSize(table.concat(buffer, " ").." "..word) | |
if w > mWidth then | |
table.insert(nLines, table.concat(buffer, " ")) | |
buffer = { } | |
end | |
table.insert(buffer, word) | |
end | |
if #buffer > 0 then -- If still words to add. | |
table.insert(nLines, table.concat(buffer, " ")) | |
end | |
return nLines | |
end | |
--[[ | |
Draw a paragraph. Supports DrawSimpleTextOutlined. | |
Text -> The text(to-be-paragraphed) to draw | |
Font -> The font to use | |
mWidth -> The maximum width per line | |
Spacing -> the spacing between lines | |
X/Y -> X/Y for the drawing. Y will +Spacing on each line | |
Color -> The text color | |
AlignX/AlignY -> Text alignment. Required however you can modify this if wanted | |
oWidth -> The outline width --> Not required but if both provided you will get SimpleTextOutlined | |
oColor -> The outline color -^ | |
EDIT: Returns the Y size of the paragraph, so you can put things underneath it by using it as a Y offset. | |
]] | |
local function drawMultiLine(text, font, mWidth, spacing, x, y, color, alignX, alignY, oWidth, oColor) | |
local mLines = toLines(text, font, mWidth) | |
for i,line in pairs(mLines) do | |
if oWidth and oColor then | |
draw.SimpleTextOutlined(line, font, x, y + (i - 1) * spacing, color, alignX, alignY, oWidth, oColor) | |
else | |
draw.SimpleText(line, font, x, y + (i - 1) * spacing, color, alignX, alignY) | |
end | |
end | |
return (#mLines - 1) * spacing | |
-- return #mLines * spacing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment