Vector Drawing: Put an Arrow endpoint on your arc
Posted: Thu Sep 22, 2016 1:43 am
I have a project that draws a circular progress bar using gdiplus. It dawned on me today that with the Vector Drawing library at our disposal, we no longer have to dip into the gdiplus bare metal anymore, we can do most things with native PB. However, to my acute disappointment, there is no flag for StrokePath() that will draw your stroke ending in an arrow pointer. Gdiplus itself has this nice command: GdipSetPenEndCap(*pen1, #LineCapArrowAnchor) but there is no corresponding capability in StrokePath(). So - I worked out how to do it manually (with actually not that much trouble). Here is the result:
Code: Select all
; Vector Drawing Demo: Draw an endpoint on a partial circle
Enumeration image
#Base
#Final
EndEnumeration
Declare RenderProgressBar(void)
If CreateImage(#Base, 200, 200, 24, RGB(255,255,255))
If StartDrawing(ImageOutput(#Base))
For j=0 To 180 Step 20
For i=0 To 180 Step 20
Box(i,j,10,10,RGB(220,220,220))
Box(i+10,j+10,10,10,RGB(220,220,220))
Next
Next
Box(40,40,120,120,RGB(0,0,0))
Box(41,41,118,118,RGB(220,220,220))
Box(50,50,100,100,RGB(0,0,0))
Box(51,51,98,98,RGB(255,255,255))
StopDrawing()
EndIf
EndIf
If OpenWindow(0, 0, 0, 200, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CopyImage(#Base, #Final)
ImageGadget(0, 0, 0, 200, 200, ImageID(#Final))
tid = CreateThread(@RenderProgressBar(), 0)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
Procedure RenderProgressBar(void)
Delay(500)
progress.d = -90.0 ; To start at the top
While progress < 270.0
CopyImage(#Base, #Final)
If StartVectorDrawing(ImageVectorOutput(#Final))
VectorSourceLinearGradient(0,0,200,200)
VectorSourceGradientColor(RGBA(255, 0, 0, 255), 0.0)
VectorSourceGradientColor(RGBA(255, 255, 0, 255), 1.0)
; partial circle
AddPathCircle(100, 100, 90, -90, progress)
; Save needed values before clearing the path
angle.d = PathPointAngle(PathLength())+90
x.d=PathCursorX()
y.d=PathCursorY()
StrokePath(10)
; Draw endcap arrow using saved coordinates
If progress < 269
RotateCoordinates(x,y,angle)
MovePathCursor(x-10,y)
AddPathLine(x,y-20)
AddPathLine(x+10,y)
ClosePath()
FillPath()
EndIf
StopVectorDrawing()
progress + 0.1
EndIf
SetGadgetState(0, ImageID(#Final))
Delay(1)
Wend
Delay(200)
SetGadgetState(0, ImageID(#Base))
EndProcedure