Moving an object with mouse on canvas

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Moving an object with mouse on canvas

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Moving an object with mouse on canvas

Post 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
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Moving an object with mouse on canvas

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Moving an object with mouse on canvas

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Moving an object with mouse on canvas

Post by davido »

Here is another example which allows dragging images and zooming.

http://www.purebasic.fr/english/viewtop ... 93#p409193
DE AA EB
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Moving an object with mouse on canvas

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply