Page 1 of 2

Some Tips using VectorDrawing lib.

Posted: Sat Nov 11, 2017 7:17 am
by RASHAD
Removed
See the last post for better tech.

- Load progress
- Busy
- Animate In Out

Re: Some Tips using VectorDrawing lib.

Posted: Sat Nov 11, 2017 7:48 am
by davido
@RASHAD,
More very nice examples. :D
Thank you very much for sharing.

Re: Some Tips using VectorDrawing lib.

Posted: Sat Nov 11, 2017 9:35 pm
by Andre
Very nice, thank you! :mrgreen:

Re: Some Tips using VectorDrawing lib.

Posted: Sat Nov 11, 2017 11:27 pm
by dobro
Another Path (no Vector)

Code: Select all





UseGIFImageDecoder()



Enumeration
		#win
		#bouton
		#Gadget_Text    
		#gadget_g
		#image
EndEnumeration
Restore Sign:
Declare playanimation(*hgif)


; on precharge le Gif se trouvant sur le Net


AnimatedGIF.s = GetTemporaryDirectory() + "_test.gif"

If Not FileSize(AnimatedGIF) > 0
		InitNetwork()
		ReceiveHTTPFile("https://icons8.com/preloaders/preloaders/301/Windows8%20loader.gif", AnimatedGIF)
EndIf

If AnimatedGIF
		UseGIFImageDecoder()
		hGIF = LoadImage(#PB_Any, AnimatedGIF)
		
		If IsImage(hGIF)
				nWidth = ImageWidth(hGIF) : nHeight = ImageHeight(hGIF)
		ENdif
Endif
Global Thread



If OpenWindow(#win, 0, 0, 200, 300, "Signe d'attente.", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ButtonGadget  (#bouton, 10, 10, 200, 20, "Cliquez moi")
		TextGadget(#Gadget_Text, 100, 50, 100, 35, "")
		
		ImageGadget(#Gadget_g, 50, 150, nWidth, nHeight, #Null)
		Thread=CreateThread(@PlayAnimation(), hGIF)
		HideGadget(#Gadget_g, #True) ; cache le gadget
		
		
		Repeat
				Event = WaitWindowEvent()
				Select Event
						Case #PB_Event_Gadget
						Select EventGadget()
								Case #bouton
								HideGadget(#Gadget_g, #False); montre le gadget
								
								; boucle de traitement
								For i=1 to 50000
										compteur_sign=compteur_sign+1
										if compteur_sign=5:compteur_sign=1:Restore Sign :Endif
										Read.s sig.s
										SetGadgetText(#Gadget_Text, sig.s)   
										WindowEvent()
								Next i
								SetGadgetText(#Gadget_Text, "")   
								HideGadget(#Gadget_g, #True) ; cache le gadget
						EndSelect
				EndSelect
		Until Event = #PB_Event_CloseWindow
		If  IsThread(Thread)
				KillThread(Thread)
		Endif
		
		FreeImage(hGIF)
		
EndIf

; ********* Zone Procedures *******************
Procedure PlayAnimation(*hGIF)
;JHPJHP
		FrameCount = ImageFrameCount(*hGIF)
		
		Repeat
				For rtnCount = 0 To FrameCount - 1
						SetImageFrame(*hGIF, rtnCount)
						SetGadgetState(#Gadget_g, ImageID(*hGIF)) : Delay(40)
				Next
		ForEver
EndProcedure

DataSection
		sign:
		Data.s "|o---|"
		Data.s "|-o--|"
		Data.s "|--o-|"
		Data.s "|---o|"
		Data.s "|--o-|"
EndDataSection







Gif here :
https://icons8.com/preloaders/en/free

et bien voilaaaa !!
Image Image

ou bien :
Image Image

ça aussi c'est cool :

Image Image

:)

peut meme faire en 3d
Image Image

du texte:
Image


Image Image



Image

Image

Re: Some Tips using VectorDrawing lib.

Posted: Sun Nov 12, 2017 9:15 pm
by Kwai chang caine
Thanks RASHAD for sharing this usefull examples 8)

Re: Some Tips using VectorDrawing lib.

Posted: Sun Nov 12, 2017 11:07 pm
by RASHAD
Thanks guys

Code: Select all

Global bColor.q
;x = x0 + r*Cos(t)
;y = y0 + r*Sin(t)

Procedure createIMG()
  If CreateImage(0, 120, 120)
    StartVectorDrawing(ImageVectorOutput(0))
      VectorSourceColor(bColor)
      FillVectorOutput()
      alpha = 255    
      For angle = 30 To 361 Step 30
        VectorSourceColor(RGBA(255,0,0,alpha))   
        x = 60 + 50*Cos(Radian(angle))
        y = 60 + 50*Sin(Radian(angle))
        AddPathCircle(x,y, 8)
        FillPath()
        alpha - 15
      Next
    StopVectorDrawing()    
  EndIf
  
EndProcedure
 
LoadFont(0, "Georgia", 8, #PB_Font_Bold)
  
Procedure runCB(par)
  Repeat    
      alpha = 255
      For angle = 361 To 0 Step -15
        If IsGadget(0)
          StartVectorDrawing(CanvasVectorOutput(0))
            DrawVectorImage(ImageID(0), 255)
            MovePathCursor(60, 60)     
            AddPathCircle(60, 60, 60, 15, angle, #PB_Path_Connected)
            VectorSourceColor(bColor)
            FillPath()        
            VectorSourceColor(RGBA(0,0,255,alpha))
            MovePathCursor(40, 50)
            VectorFont(FontID(0), 18)
            DrawVectorText("Busy")                 
            alpha - 15 
          StopVectorDrawing()
          Delay(50)
        EndIf 
      Next    
  ForEver
EndProcedure

  If OpenWindow(0, 0, 0, 400, 400, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SetWindowColor(0,$E4E4E5)
    bColor = GetWindowColor(0)
    If bColor < 0
      StartDrawing(WindowOutput(0))
        bColor = Point(100,100)
      StopDrawing()
    EndIf
    bColor = bColor | $FF000000
    createIMG()
    CanvasGadget(0, 150, 150, 120, 120)
    thread = CreateThread(@runCB(),35)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Quit = 1          

      EndSelect          
    Until Quit = 1
  EndIf

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 4:14 pm
by oO0XX0Oo
Very cool, thanks for all the examples! :D

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 4:45 pm
by Kukulkan
Nice!

But isn't it a problem to draw from threads? In my experience, every threaded access to any gadget created in the main thread lead to strange behavior up to crashes. While it seem to work somehow on Windows, on Linux and Mac it was a guarantee for crashes.

Is the Canvas thread-save in this way?

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 6:02 pm
by RASHAD
Thanks guys
Previous post updated
- Graphics modified little bit
- Added check for existing of the CanvasGadget

@Kukulkan
It should be very safe now
It is just drawing to a gadget created in the main loop(that is all)
Using thread is the best way to avoid stop while moving or resizing the main window and leave the main window free from affected by the required delay time

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 8:57 pm
by skywalk
Hi RASHAD, nice examples.
I do see random crashes within runCB() when closing the window.
Or
Moving the window and then closing it.

The runCB() drawing functions have nowhere to paint if the window closes before the thread ends.
Even adding
Case #PB_Event_CloseWindow
Quit = 1
WaitThread(thread, 6000)
does not always work.

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 9:15 pm
by RASHAD
Hi skywalk
Thanks
That is why I added check for existing of the CanvasGadget() in the last example and it should be added to every example in the first post
Please check the last example again
Waiting for your results

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 9:26 pm
by skywalk
Yes, the error happens with your last example.
Maybe IsGadget(0) is not updated when the app quits?

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 9:40 pm
by RASHAD
I can not replicate the error

Try

Code: Select all

    Repeat
      Select WaitWindowEvent(10)
        Case #PB_Event_CloseWindow
          KillThread(thread)
          Quit = 1          

      EndSelect          
    Until Quit = 1
Or try other different timeout

Or try next snippet to avoid KillThread(thread)

Code: Select all

Global bColor.q,qFlag
Procedure createIMG()
  If CreateImage(0, 120, 120)
    StartVectorDrawing(ImageVectorOutput(0))
      VectorSourceColor(bColor)
      FillVectorOutput()
      alpha = 255    
      For angle = 30 To 361 Step 30
        VectorSourceColor(RGBA(255,0,0,alpha))   
        x = 60 + 50*Cos(Radian(angle))
        y = 60 + 50*Sin(Radian(angle))
        AddPathCircle(x,y, 8)
        FillPath()
        alpha - 15
      Next
    StopVectorDrawing()    
  EndIf
  
EndProcedure
 
LoadFont(0, "Georgia", 8, #PB_Font_Bold)
  
Procedure runCB(par)
  Repeat 
      alpha = 255
      For angle = 361 To 0 Step -15
        If qFlag = 1
          Break
        EndIf   
        StartVectorDrawing(CanvasVectorOutput(0))
          DrawVectorImage(ImageID(0), 255)
          MovePathCursor(60, 60)     
          AddPathCircle(60, 60, 60, 15, angle, #PB_Path_Connected)
          VectorSourceColor(bColor)
          FillPath()        
          VectorSourceColor(RGBA(0,0,255,alpha))
          MovePathCursor(40, 50)
          VectorFont(FontID(0), 18)
          DrawVectorText("Busy")                 
          alpha - 15 
        StopVectorDrawing()
        Delay(50)
      Next    
  ForEver
EndProcedure

  If OpenWindow(0, 0, 0, 400, 400, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SetWindowColor(0,$E4E4E5)
    bColor = GetWindowColor(0)
    If bColor < 0
      StartDrawing(WindowOutput(0))
        bColor = Point(100,100)
      StopDrawing()
    EndIf
    bColor = bColor | $FF000000
    createIMG()
    CanvasGadget(0, 140, 120, 120, 120)
    thread = CreateThread(@runCB(),35)
    Repeat
      Select WaitWindowEvent(1)
        Case #PB_Event_CloseWindow
          qFlag = 1
          Delay(100)
          Quit = 1
          
      EndSelect          
    Until Quit = 1
  EndIf

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 10:33 pm
by skywalk
I had tried waitthread() where you put killthread() and that still had errors.
Monitoring qFlag in the thread works.

Re: Some Tips using VectorDrawing lib.

Posted: Mon Nov 13, 2017 10:38 pm
by RASHAD
@skywalk thanks
The problem is we must use the thread to get the proper functionality
Glad to know that qFlag works
Thanks again