Page 1 of 1

Common part of several elements in Canvas

Posted: Thu Mar 18, 2021 10:30 pm
by Johanson
How to select ONLY a triangle when hovering over the common part, and not a triangle and a rectangle at the same time?

Code: Select all

Procedure Paint()
  Protected x.i=GetGadgetAttribute(5, #PB_Canvas_MouseX), y.i=GetGadgetAttribute(5, #PB_Canvas_MouseY)
  Protected kOut.i =RGBA(  0,255, 0, 255)
  Protected kOver.i=RGBA(255,  0, 0, 255)
  If StartVectorDrawing(CanvasVectorOutput(5))
  
    AddPathSegments("M 150 220 L 150 50 L 360 50 L 360 220 Z")
    If IsInsidePath(x,y): VectorSourceColor(kOut): Else: VectorSourceColor(kOver): EndIf: StrokePath(5, #PB_Path_Preserve)
    VectorSourceColor(RGBA(255, 255, 0, 255)): FillPath()
        
    MovePathCursor(40, 160)
    AddPathLine(100, 20)
    AddPathLine(180, 160)
    ClosePath()
    If IsInsidePath(x,y): VectorSourceColor(kOut): Else: VectorSourceColor(kOver): EndIf: StrokePath(5, #PB_Path_Preserve)
    VectorSourceColor(RGBA(255, 255, 0, 255)): FillPath()
    
    StopVectorDrawing()
  EndIf
EndProcedure

If OpenWindow(10, 0,0, 500,480, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(5, 0,0, 500,480)
  BindGadgetEvent(5, @Paint())
  Paint()
  Repeat
    Event = WaitWindowEvent()
    Select WindowEvent()
      Case #PB_Event_Gadget 
        eg=EventGadget()
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  Until false    
EndIf

Re: Common part of several elements in Canvas

Posted: Thu Mar 18, 2021 11:59 pm
by STARGÅTE
In such a case you have to split you Paint procedure in two parts.
One part checks the overlap (from top to bottom)
The second part draws all objects (from bottom to top)

You can use a simple LinkedList:

Code: Select all

Global NewList Paths.s()

AddElement(Paths()) : Paths() = "M 100 100 C 200 100 200 100 200 200 C 100 200 100 200 100 100 Z"
AddElement(Paths()) : Paths() = "M 150 220 L 150 50 L 360 50 L 360 220 Z"
AddElement(Paths()) : Paths() = "M 40 160 L 100 20 L 180 160 Z"


Procedure Paint()
	Protected x.i=GetGadgetAttribute(5, #PB_Canvas_MouseX), y.i=GetGadgetAttribute(5, #PB_Canvas_MouseY)
	Protected kOut.i =RGBA(  0,255, 0, 255)
	Protected kOver.i=RGBA(255,  0, 0, 255)
	Protected *HoveredElement
	If StartVectorDrawing(CanvasVectorOutput(5))
		
		; Inside path check
		*HoveredElement = #Null
		If LastElement(Paths())
			Repeat
				ResetPath()
				AddPathSegments(Paths())
				If IsInsidePath(x,y)
					*HoveredElement = @Paths()
					Break
				EndIf
			Until Not PreviousElement(Paths())
		EndIf
		
		; Drawing
		ForEach Paths()
			ResetPath()
			AddPathSegments(Paths())
			If *HoveredElement  = @Paths()
				VectorSourceColor(kOut)
			Else
				VectorSourceColor(kOver)
			EndIf
			StrokePath(5, #PB_Path_Preserve)
			VectorSourceColor(RGBA(255, 255, 0, 255)): FillPath()
		Next
			
		StopVectorDrawing()
	EndIf
EndProcedure

If OpenWindow(10, 0,0, 500,480, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(5, 0,0, 500,480)
  BindGadgetEvent(5, @Paint())
  Paint()
  Repeat
    Event = WaitWindowEvent()
    Select WindowEvent()
      Case #PB_Event_Gadget
        eg=EventGadget()
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  Until false   
EndIf

Re: Common part of several elements in Canvas

Posted: Fri Mar 19, 2021 12:06 am
by Johanson
STARGÅTE wrote:In such a case you have to split you Paint procedure in two parts.
One part checks the overlap (from top to bottom)
The second part draws all objects (from bottom to top)
Well, I thought so, but I thought that there was a more optimal method.
In any case, thank you very much - it's a good method.

Re: Common part of several elements in Canvas

Posted: Fri Mar 19, 2021 12:45 am
by JHPJHP
Removed; post ignored.

Re: Common part of several elements in Canvas

Posted: Fri Mar 19, 2021 8:26 am
by mestnyi