DrawText() line wrap

Just starting out? Need help? Post your questions and find answers here.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

DrawText() line wrap

Post 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 ( ).
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post 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...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Thanks, incredible!
Lostdragon
New User
New User
Posts: 9
Joined: Sun Mar 13, 2005 3:16 am
Location: U.S.A.
Contact:

word wrap

Post 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,
Last edited by Lostdragon on Tue Jul 18, 2006 4:33 pm, edited 2 times in total.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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 
Lostdragon
New User
New User
Posts: 9
Joined: Sun Mar 13, 2005 3:16 am
Location: U.S.A.
Contact:

Post by Lostdragon »

Oh. Sorry. It is fixed now. You should now be able to copy and paste the source from my original message. :oops:
Post Reply