Here’s an updated version of the above code. It splits a string on space boundaries to fit within scrolling box area. Pass the class the string and the x,y,w,h of the scroll area. Also, the font size, font color, and font name can be different for each scroll area. I could have added a lot more options, but this is good enough for now.
viewer.mode=FULLSCREEN
function setup()
textMode(CORNER)
str="Five little monkeys jumping on the bed. One fell off and bumped his head. Mama called the doctor and the doctor said: No more monkeys jumping on the bed! Four little monkeys jumping on the bed. One fell off and bumped his head. Mama called the doctor and the doctor said: No more monkeys jumping on the bed! Three little monkeys jumping on the bed. One fell off and bumped his head. Mama called the doctor and the doctor said: No more monkeys jumping on the bed! Two little monkeys jumping on the bed. One fell off and bumped his head. Mama called the doctor and the doctor said: No more monkeys jumping on the bed! One little monkey jumping on the bed. He fell off and bumped his head. Mama called the doctor and the doctor said: No more monkeys jumping on the bed! Now there's no little monkeys jumping on the bed. They`re all jumping on the sofa instead! "
for z=1,5 do -- create a larger string
str=str..str
end
-- string, x, y, w, h, font size, font color, font name
s1=splitString(str, 20,300,300,400, 10,color(0,255,255),"MarkerFelt-Thin")
s2=splitString(str, 350,400,450,400, 30,color(0,255,0),"SnellRoundhand")
s3=splitString(str, 450,100,300,200, 15,color(255,255,0),"CourierNewPS-ItalicMT")
end
function draw()
background(32, 72, 104)
s1:draw()
s2:draw()
s3:draw()
end
function touched(t)
s1:touched(t)
s2:touched(t)
s3:touched(t)
end
splitString=class()
function splitString:init(str,x,y,width,height,sizeFont,colr,fontName)
self.x=math.max(5,x)
self.y=math.max(5,y)
self.width=math.min(width,WIDTH-self.x-5)
self.height=math.min(height,HEIGHT-self.y-5)
self.sizeFont=sizeFont
self.colr=colr
self.fontName=fontName
self.lineHeight=0
self.line=""
self.dy=0
self.tab={}
self:split(self)
end
function splitString:split()
font(self.fontName)
fontSize(self.sizeFont)
_,self.lineHeight=textSize("A")
local temp={}
for word in str:gmatch("%S+") do
table.insert(temp,word)
end
for a,b in pairs(temp) do
if textSize(self.line)+textSize(b)<self.width then
self.line=self.line..b.." "
else
table.insert(self.tab,self.line)
self.line=b.." "
end
end
if #self.line>0 then
table.insert(self.tab,self.line)
end
end
function splitString:draw()
clip(self.x-5,self.y-5,self.width+10,self.height+10)
stroke(255)
strokeWidth(2)
fill(40)
rect(self.x-5,self.y-5,self.width+10,self.height+10)
fill(self.colr)
font(self.fontName)
fontSize(self.sizeFont)
for a,b in pairs(self.tab) do
text(b,self.x,self.y-a*self.lineHeight+self.dy+self.height)
end
end
function splitString:touched(t)
if t.x>self.x and t.x<self.x+self.width and
t.y>self.y and t.y<self.y+self.height then
if t.state==CHANGED then
self.dy=self.dy+t.deltaY
if self.dy<0 then
self.dy=0
end
end
end
end