Common part of several elements in Canvas

Just starting out? Need help? Post your questions and find answers here.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Common part of several elements in Canvas

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2288
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Common part of several elements in Canvas

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: Common part of several elements in Canvas

Post 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.
JHPJHP
Addict
Addict
Posts: 2285
Joined: Sat Oct 09, 2010 3:47 am

Re: Common part of several elements in Canvas

Post by JHPJHP »

Removed; post ignored.
Last edited by JHPJHP on Sat Mar 20, 2021 3:55 pm, edited 1 time in total.
Post Reply