AGK leaves a lot to be desired when it comes to displaying text. If you want 10 lines of text to display a paragraph, you need 10 text objects. And what if you change your mind about how wide you want the paragraph displayed? This function is designed to simplify that problem.
You do need to estimate how many lines of text you expect to use, so that an array of an appropriate size can be made. When you call
wrapDescription(string, integer) it will take the given string and break it up among the lines to keep each line within the defined width as best it can. It's not 100% perfect, as you may sometimes have the last letter occasionally stick partially beyond the limit. I find this happens infrequently but it is something you should account for. The length of the text on each line is estimated but I feel the outcome is accurate enough for my use. It also wraps based on whole words, so it does not cut a word in half when wrapping.
setVirtualResolution(800,600)
#CONSTANT MAXLINES = 10
#CONSTANT LINESPACING = 40
dim t_description[MAXLINES]
for i = 1 to MAXLINES
t_description[i] = createText("")
setTextSize(t_description[i], LINESPACING)
setTextDepth(t_description[i], 11)
next i
d$ = "A young boy is arrested by the U.S. Secret Service for writing a computer virus and is banned from using a computer until his 18th birthday. Years later, he and his new-found friends discover a plot to unleash a dangerous computer virus, but they must use their computer skills to find the evidence while being pursued by the Secret Service and the evil computer genius behind the virus."
width = 500
wrapDescription(d$, width)
repeat
if getRawMouseRightPressed() = 1
inc width, 40
wrapDescription(d$, width)
endif
if getRawMouseLeftPressed() = 1
dec width, 40
wrapDescription(d$, width)
endif
x = getRawMouseX()
y = getRawMouseY()
positionDescription(x, y, LINESPACING)
drawBox(x, y, x+width, y+MAXLINES*LINESPACING)
sync()
until getRawKeyPressed(27) = 1
end
function positionDescription(x, y, spacing)
for i = 1 to MAXLINES
setTextPosition(t_description[i], x, y+(i-1)*spacing)
next i
endfunction
function wrapDescription(s$, maxWidth#)
setTextString(t_description[1], s$)
w = getTextTotalWidth(t_description[1])
t# = maxWidth# / w
L = len(s$) * t#
r = ceil(w / maxWidth#)
// bounds check for safety
if r > MAXLINES then r = MAXLINES
start = 1
for i = 1 to r
s1$ = mid(s$, start, L)
for k = L to 1 step -1
if mid(s1$, k, 1) = " "
x = L - (L-k)
if i <> r
s1$ = mid(s1$, 1, x)
else
s1$ = mid(s1$, 1, L)
endif
exit
endif
next k
setTextString(t_description[i], s1$)
start = start + len(s1$)
next i
for i = r+1 to MAXLINES
setTextString(t_description[i], "")
next i
endfunction
function drawBox(x1, y1, x2, y2)
drawLine(x1, y1, x2, y1, 255,255,255)
drawLine(x1, y2, x2, y2, 255,255,255)
drawLine(x1, y1, x1, y2, 255,255,255)
drawLine(x2, y1, x2, y2, 255,255,255)
endfunction
"I like offending people, because I think people who get offended should be offended." - Linus Torvalds