Page 1 of 1

Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 6:52 am
by collectordave
Just a simple programme to demonstrate a method of using the mouse to move an object on a canvas. The canvas is needed to pick up the mouse events etc.

Just one object in the programme gets a lot more complicated with multiple objects to chose from etc. Kept programme as simple as possible if anyone can make it simpler please post here.

Code: Select all

EnableExplicit

;Gadget Structure
Structure UsedGadget
  ID.l
  Type.s
  x.l 
  y.l 
  Width.i
  Height.i 
EndStructure
Global MyGadgets.UsedGadget

MyGadgets\x = 45
MyGadgets\y = 25
MyGadgets\Width = 120
MyGadgets\Height= 40

Global frmMain.l,cvsTest.l,GadX.i,GadY.i,ActiveMove.i

Define Event.i,cx.i,cy,i

  frmMain = OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu)
  cvsTest = CanvasGadget(#PB_Any, 0, 0, 600, 400)
  MyGadgets\ID = ButtonGadget(#PB_Any, MyGadgets\x, MyGadgets\y , MyGadgets\Width, MyGadgets\Height, "Test")
  ActiveMove = #False
  
 Macro GadgetHoverCheck(x, y)
  (((Not x < MyGadgets\x) & (Not y< MyGadgets\y)) &(Not x>=(MyGadgets\x+MyGadgets\Width)) & (Not y>=(MyGadgets\y+MyGadgets\Height)))
EndMacro 
  
 Procedure.i HitTest(x, y)
    
    If GadgetHoverCheck(x,y)
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
  
EndProcedure

 Procedure MoveGadget(x,y)
   
  MyGadgets\x = GadgetX(MyGadgets\ID)
  MyGadgets\y = GadgetY(MyGadgets\ID)
  ResizeGadget(MyGadgets\ID, x, y, #PB_Ignore, #PB_Ignore)
  
 EndProcedure
  
Repeat
  
  Event = WaitWindowEvent()
  
  Select event

    Case #PB_Event_Gadget
      
      Select EventGadget()
          
        Case cvsTest
          
          Select EventType ()
            Case #PB_EventType_LeftButtonDown

              ;Get current mouse co-ordinates
              cx = WindowMouseX(frmMain)
              cy = WindowMouseY(frmMain)
              If HitTest(cx,cy)
                gadx = cx - MyGadgets\x
                gady = cy - MyGadgets\y
                ActiveMove = #True
              EndIf

           Case #PB_EventType_LeftButtonUp    

               ActiveMove = #False
               MyGadgets\x = GadgetX(MyGadgets\ID)
               MyGadgets\y = GadgetY(MyGadgets\ID)

           Case #PB_EventType_MouseMove 
             
             If ActiveMove
               ;Get current mouse co-ordinates
               cx = WindowMouseX(frmMain)
               cy = WindowMouseY(frmMain)
               If cx > -1 And cy > -1 
                 MoveGadget(cx - gadx,cy - gady)
               EndIf
              EndIf
             
         EndSelect ;EventType ()
            
            
     EndSelect ;EventGadget()
     
 EndSelect ;Event
 
Until Event = #PB_Event_CloseWindow

Re: Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 7:44 am
by infratec
Hi,

since when it is allowed in PB to place 2 gadgets on top of each other :?:

I always thought (and confirmed by practice) that it is not a good idea.

Bernd

Re: Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 7:54 am
by collectordave
Can you show, with a little code, why it is not a good idea? :D

Using the canvas gadget I have found it quite practical and it works in this case and others I am working on. If there is a better way of doing this without recourse to API calls or OS specific commands please let me know.

Re: Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 8:36 am
by TI-994A
collectordave wrote:Can you show, with a little code, why it is not a good idea?
To the best of my knowledge, overlapped gadgets don't work well in PureBasic. In your example, you'll notice that the button would not respond to clicks.

One workaround to this would be to disable the underlying gadget. This approach works particularly well when using image gadgets as background images. However, if an underlying canvas gadget is disabled, all its events would also be disabled, essentially voiding its use in the first place.

Just some thoughts. :wink:

Re: Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 9:18 am
by davido
Here is another example which allows dragging images and zooming.

http://www.purebasic.fr/english/viewtop ... 93#p409193

Re: Moving an object with mouse on canvas

Posted: Fri Jan 15, 2016 3:17 pm
by collectordave
Hi All

Thanks for the replies. Not trying to use the Button on the canvas, the intention here is not to use the button, just showing one method to use for dragging objects around a canvas. The method works just as well with other gadgets as well, such as images or text.

It is just one more step to a WYSIWYG data report designer and a page layout programme I am working on.

The example above http://www.purebasic.fr/english/viewtop ... 93#p409193 is very similar code. I will look at the zoom part though.