Page 1 of 1

DrawText() line wrap

Posted: Sat Apr 23, 2005 12:49 pm
by Trond
Does anyone know how to write a procedure which mimics DrawText() except some differences:

DrawTextExt(StringToDraw.s,WordWrap.w)

it should split up lines which are longer than WordWrap (pixels) on hyphens (-) or spaces ( ).

Posted: Sat Apr 23, 2005 11:10 pm
by kenmo
Here you go, just whipped this up.

Code: Select all

InitSprite() : InitKeyboard()
OpenScreen(640,480,16,"WordWrap")

Procedure WrapText(x,y,text.s,width,color)
   FrontColor(Red(color),Green(color),Blue(color)) : Locate(x,y)
   If TextLength(text)<=width
      DrawText(text)
   Else
      limit=0 : Repeat : limit+1 : Until TextLength(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(Left(text,cut)) : wraptext(x,y+16,Right(text,Len(text)-cut),width,color)
   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, and uses default text height (16) as line space. Press ESC to exit."
Repeat
   ClearScreen(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,$00FFFF) : If flash : DrawText("|") : EndIf
   StopDrawing()
   If ElapsedMilliseconds()-flashtime>1000 : flashtime=ElapsedMilliseconds() : flash=1-flash : EndIf
   FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
End
You may have to edit that procedure a bit to integrate it into your project...

Posted: Sun Apr 24, 2005 9:07 am
by Trond
Thanks, incredible!

word wrap

Posted: Tue Jul 18, 2006 1:21 am
by Lostdragon
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,

Posted: Tue Jul 18, 2006 10:31 am
by Michael Vogel
PB4 code did not work - Forum problem with HTML...

Code: Select all

InitSprite() : InitKeyboard() 
OpenScreen(640,480,16,"WordWrap") 

Procedure WrapText(x,y,text.s,width,color) 
   FrontColor(color)
   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+16,Right(text,Len(text)-cut),width,color) 
   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, and uses default text height (16) as line space. Press ESC to exit." 
Repeat 
   ClearScreen(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,$00FFFF) : If flash : DrawText(x,y,"|") : EndIf 
   StopDrawing() 
   If ElapsedMilliseconds()-flashtime>1000 : flashtime=ElapsedMilliseconds() : flash=1-flash : EndIf 
   FlipBuffers() 
Until KeyboardPushed(#PB_Key_Escape) 
End 

Posted: Tue Jul 18, 2006 4:35 pm
by Lostdragon
Oh. Sorry. It is fixed now. You should now be able to copy and paste the source from my original message. :oops: