Drag-to-select on CanvasGadget

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Drag-to-select on CanvasGadget

Post by WilliamL »

I needed to be able to select a portion of an image so I used the CanvasGadget to do it entirely in PureBasic (should run on all platforms). You just click and drag and a box is drawn with transparency so you can still see the picture as you drag.

Code: Select all

EnableExplicit

Structure copylen
    x.i
    x1.i
    y.i
    y1.i
    efid.i
    image.i
EndStructure
Global Copy.copylen

Define x,y,event
Define w=200 , h=200
If OpenWindow(0, 0, 0,w,h, "CanvasGadget selection tool", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    Copy\efid=CanvasGadget(#PB_Any, 10, 10,w-20,h-20)
    Copy\image=CreateImage(#PB_Any,w,h)
    ;draw an image to show - from Box in Help
    If Copy\image<>0
        If StartDrawing(ImageOutput(Copy\image)) 
            y = 0
            For x = 0 To 95 Step 10
                Box(x, y, 200-2*x, 200-2*y, RGB(Random(255), Random(255), Random(255)))
                y + 10
            Next x
            StopDrawing()
        EndIf
    EndIf

    ;put image into Canvas gadget
    If StartDrawing(CanvasOutput(Copy\efid)) 
        DrawImage(ImageID(Copy\image),0,0)
        StopDrawing()
    EndIf
    
    Repeat
      Event = WaitWindowEvent()   
      If Event = #PB_Event_Gadget 
          If EventGadget() = Copy\efid
              If EventType()=#PB_EventType_LeftButtonUp
                  Copy\x1 = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseX)
                  Copy\y1 = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseY)
                    Debug "mouse up "+Str(Copy\x1)+" "+Str(Copy\y1)
                  If StartDrawing(CanvasOutput(Copy\efid))
                      DrawingMode(#PB_2DDrawing_AlphaChannel)
                      DrawAlphaImage(ImageID(Copy\image),0,0,255)
                      StopDrawing()
                  EndIf
                    Debug "Points are:"+Str(Copy\x)+"x"+Str(Copy\y)+" & "+Str(Copy\x1)+"x"+Str(Copy\y1)
                    Debug "or "+Str(Abs(Copy\x-Copy\x1))+"x"+Str(Abs(Copy\y-Copy\y1))
                  Copy\x=0 ; setting to zero resets the selection process!
              EndIf
              If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(Copy\efid, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
                  If #PB_Canvas_LeftButton=GetGadgetAttribute(Copy\efid, #PB_Canvas_Buttons)
                      If Copy\x=0
                          Copy\x = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseX)
                          Copy\y = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseY)
                      EndIf
                        Debug "Mouse down "+Str(Copy\x)+" "+Str(Copy\y)
                      Copy\x1 = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseX)
                      Copy\y1 = GetGadgetAttribute(Copy\efid, #PB_Canvas_MouseY)
                      If StartDrawing(CanvasOutput(Copy\efid))
                          DrawingMode(#PB_2DDrawing_AlphaChannel)
                          DrawAlphaImage(ImageID(Copy\image),0,0,255)
                          Box(Copy\x,Copy\y,(Copy\x1-Copy\x),(Copy\y1-Copy\y), RGBA(200,200,200,200))
                          StopDrawing()
                      EndIf
                  EndIf
               EndIf
          EndIf
      EndIf    
    Until Event = #PB_Event_CloseWindow
  EndIf
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1