Hi. I am new to this language so this is probably a mess.
I liked the word wrap procedure so I tried to improve it (and update it for PB4). Hopefully there are not too many mistakes.
Edit: I turned off html for this post. Now the code should be copy/paste-able.
Code: Select all
; updated for pb4
; added fontspace to accomodate fonts of any size.
; note that fontspace = pixel size of row you want, not "points"
InitSprite() : InitKeyboard()
OpenScreen(640,480,16,"WordWrap")
Procedure WrapText(x,y,text.s,width,fontspace,red,green,blue) ;Word Wrap example by kenmo updated to pb4, added fontspace to accomodate fonts of any size.
FrontColor(RGB(red,green,blue))
If TextWidth(text)<=width
DrawText(x,y,text)
Else
limit=0
Repeat
limit+1
Until TextWidth(Left(text,limit))>width
cut=limit
Repeat
cut-1
Until Mid(text,cut,1)=" " Or Mid(text,cut,1)="-" Or cut=0
If cut=0
cut=limit-1
EndIf
DrawText(x,y,Left(text,cut))
wraptext(x,y+fontspace,Right(text,Len(text)-cut),width,fontspace,red,green,blue)
EndIf
EndProcedure
input.s="Word Wrap example by kenmo... Type in this box! Automatically cuts at spaces and hyphens. Does NOT cut at vertical limit. Uses programmable text height (16 is value for default font). Press ESC to exit."
Repeat
ClearScreen(RGB(0,0,0))
ExamineKeyboard()
input.s+KeyboardInkey()
If KeyboardReleased(#PB_Key_Back)
input=Left(input,Len(input)-1)
EndIf
StartDrawing(ScreenOutput())
Box(10,10,300,200,$800000)
DrawingMode(5)
Box(10,10,300,200,$FFFFFF)
wraptext(15,15,input,290,16,99,99,99)
If flash
DrawText(x,y,"|")
EndIf
StopDrawing()
If ElapsedMilliseconds()-flashtime>1000
flashtime=ElapsedMilliseconds()
flash=1-flash
EndIf
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
End
I would like to be able to have a vertical limitation (a set number of rows) with a "press space to continue" option to show more text, but for now I do not understand the loop or PB well enough to make that work.
Cheers,