Page 1 of 1

Text alignment by special character

Posted: Thu Jul 16, 2009 11:22 am
by einander

Code: Select all


;by einander
;PB 4.31 - Jul 16, 2009

Procedure CharAlign(A$,B$,xAlign=#PB_Any,Y=#PB_Any,ChAlign.s="")
  ; Draw text aligned by special char
  ; xAlign, y, and ChAlign.s needed only for the first text string
  Static xPos,yPos,OldChAlign.s
  If ChAlign=""
    If Len(OldChAlign):  ChAlign=OldChAlign
    Else :  ChAlign=" : " ; default Align Char
    EndIf
  EndIf
  If Y=#PB_Any:Y=yPos:EndIf
  If xAlign=#PB_Any:xAlign=xPos:EndIf
  TS=TextWidth(ChAlign)/2
  DrawText(xAlign-TextWidth(A$)-TS,Y,A$)
  DrawText(xAlign-TS,Y,ChAlign)
  DrawText(xAlign+TS,Y,B$)
  xPos=xAlign
  yPos=Y+TextHeight(A$)
  OldChAlign=ChAlign
  ProcedureReturn yPos
EndProcedure

     ;Test it <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
hwnd=OpenWindow(0, 100, 100,700,500 ,"Text alignment by special character",    #PB_Window_SystemMenu|1)
Wi=WindowWidth(0):He=WindowHeight(0)
_ImGad=ImageGadget(#PB_Any,100,40,0,0,0,#PB_Image_Border)
_Img=CreateImage(#PB_Any,Wi-200,He-100,32)
Font1=FontID(LoadFont(#PB_Any,"arial",12))
Font2=FontID(LoadFont(#PB_Any,"arial",8))
StartDrawing(ImageOutput(_Img))
  Box(0,0,ImageWidth(_Img),ImageHeight(_Img),#White)
  Xcenter=ImageWidth(_Img)/2
  
  CharAlign("Abc def ghijk","123456 7890",Xcenter,30,"    :    ")
  CharAlign("def ghijk","123456 789")
  CharAlign("ghijk","123456 7")
  CharAlign("dddddAbc def ghijk","123456 7890")
  CharAlign("def dddghijk","aaaaa23456 789")
  Y=CharAlign("ddddddghijk","BBB123456 aa7") ;keep last Y for future use
  
  DrawingFont(Font1)
  FrontColor(#Blue)
  CharAlign("Abc def ghijk","123456 7890",Xcenter,Y+30,"@")
  CharAlign("def ghijk","123456 789")
  CharAlign("ghijk","123456 7")
  CharAlign("dddddAbc def ghijk","123456 7890")
  CharAlign("def dddghijk","aaaaa23456 789")
  Y=CharAlign("ddddddghijk","BBB123456 aa7")
  
  DrawingFont(Font2)
  FrontColor(#Red)
  CharAlign("Abc def ghijk","123456 7890",Xcenter,Y+30," = ")
  CharAlign("def ghijk","123456 789")
  CharAlign("ghijk","123456 7")
  CharAlign("dddddAbc def ghijk","123456 7890")
  CharAlign("def dddghijk","aaaaa23456 789")
  Y=CharAlign("ddddddghijk","BBB123456 aa7")
  
StopDrawing()
SetGadgetState(_ImGad,ImageID(_Img))
     
Repeat
  Ev=WaitWindowEvent()
Until Ev=#PB_Event_CloseWindow
End 

Posted: Thu Jul 16, 2009 9:01 pm
by #NULL

Code: Select all

[21:57:47] [ERROR] Line: 42
[21:57:47] [ERROR] LoadFont() can't be called inside a StartDrawing()/StopDrawing() block.
strange, because i'm using pb 431 too.

<edit>
but it works without the debugger, even the different fonts are visible.

thanks for sharing :)

Posted: Thu Jul 16, 2009 9:46 pm
by einander
Thanks Null.
Edited:
LoadFont is now outside StartDrawing()/StopDrawing()