locked
take an object and move the mouse RRS feed

  • Question

  • Good evening,

    I wonder if someone can give mea code example, to move an object (image) with the mouse.One can as if it were apuzzle.There is a form on the screen and I with my mouse selects(by clicking) to move to an other part ofthe screen. Andthen, to start with other forms.

    Thank you.

    absolu



    • Edited by absolu Sunday, February 19, 2012 9:14 PM
    Sunday, February 19, 2012 9:13 PM

Answers

  • We ned to detect mouse down to start the movement and detect if the image is selected, mouse movement during the movement and mouse up to finish the movement.

    Simple example:

    objectX = 100
    objectY = 100
    objectW = 50
    objectH = 20
    object = Shapes.AddRectangle(objectW,objectH)
    Shapes.Move(object,objectX,objectY)
    
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    
    objectMoving = 0
    
    While ("True")
      If (objectMoving = 1) Then
        objectX = GraphicsWindow.MouseX - objectW/2 'Top left point of shape
        objectY = GraphicsWindow.MouseY - objectH/2
        Shapes.Move(object,objectX,objectY)
      EndIf
      Program.Delay(50)
    EndWhile
    
    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      If (xM >= objectX And xM <= objectX+objectW And yM >= objectY And yM <= objectY+objectH) Then
        objectMoving = 1
      EndIf
    EndSub
    
    Sub OnMouseUp
      objectMoving = 0
    EndSub

    Sunday, February 19, 2012 9:41 PM
  • absolu, yes there are several ways to just select one boc depending on what you want to happen, the simplest might just be select one box with the lowest index, perhaps:

    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      For i = 1 To numObject
        If (xM >= objectX[i] And xM <= objectX[i]+objectW[i] And yM >= objectY[i] And yM <= objectY[i]+objectH[i]) Then
          objectMoving[i] = 1
          i = numObject
        EndIf
      EndFor
    EndSub
    

    Tuesday, February 21, 2012 6:40 PM
  • Mainly the following:

    1] image width heights

    objectW[1] = ImageList.GetWidthOfImage(img+ "NEZ.png")
    objectH[1] = ImageList.GetHeightOfImage(img+ "NEZ.png")
    
    objectW[2] = ImageList.GetWidthOfImage(img+ "BASE YEUX 1.png")
    objectH[2] = ImageList.GetHeightOfImage(img+ "BASE YEUX 1.png")
    

    2] To move both images someing like this, import FSB980-0

    Wednesday, February 22, 2012 8:25 PM

All replies

  • We ned to detect mouse down to start the movement and detect if the image is selected, mouse movement during the movement and mouse up to finish the movement.

    Simple example:

    objectX = 100
    objectY = 100
    objectW = 50
    objectH = 20
    object = Shapes.AddRectangle(objectW,objectH)
    Shapes.Move(object,objectX,objectY)
    
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    
    objectMoving = 0
    
    While ("True")
      If (objectMoving = 1) Then
        objectX = GraphicsWindow.MouseX - objectW/2 'Top left point of shape
        objectY = GraphicsWindow.MouseY - objectH/2
        Shapes.Move(object,objectX,objectY)
      EndIf
      Program.Delay(50)
    EndWhile
    
    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      If (xM >= objectX And xM <= objectX+objectW And yM >= objectY And yM <= objectY+objectH) Then
        objectMoving = 1
      EndIf
    EndSub
    
    Sub OnMouseUp
      objectMoving = 0
    EndSub

    Sunday, February 19, 2012 9:41 PM
  • That's exactly right.
    Thank youforthe exampleand explanation

    absolu

    Monday, February 20, 2012 6:27 AM
  • I have adjusted for 2 shapes :

    objectX = 100
    objectY = 100
    objectW = 50
    objectH = 20
    object = Shapes.AddRectangle(objectW,objectH)
    Shapes.Move(object,objectX,objectY)


    objectX2 = 400
    objectY2 = 100
    objectW2 = 50
    objectH2 = 20
    object2 = Shapes.AddRectangle(objectW2,objectH2)
    Shapes.Move(object2,objectX2,objectY2)


    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp

    'GraphicsWindow.MouseDown = OnMouseDown2
    'GraphicsWindow.MouseUp = OnMouseUp2


    objectMoving = 0
    objectMoving2 = 0


    While ("True")
      If (objectMoving = 1) Then
        objectX = GraphicsWindow.MouseX - objectW/2 'Top left point of shape
        objectY = GraphicsWindow.MouseY - objectH/2
        Shapes.Move(object,objectX,objectY)
      EndIf
     
     
      If (objectMoving2 = 1) Then
        objectX2 = GraphicsWindow.MouseX - objectW2/2 'Top left point of shape
        objectY2 = GraphicsWindow.MouseY - objectH2/2
        Shapes.Move(object2,objectX2,objectY2)
      EndIf
     
      Program.Delay(50)
    EndWhile


    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      If (xM >= objectX And xM <= objectX+objectW And yM >= objectY And yM <= objectY+objectH) Then
        objectMoving = 1
      EndIf
    'EndSub


     'Sub OnMouseDown2
       xM2 = GraphicsWindow.MouseX
      YM2 = GraphicsWindow.MouseY
      If (xM2 >= objectX2 And xM2 <= objectX2+objectW2 And yM2 >= objectY2 And yM2 <= objectY2+objectH2) Then
        objectMoving2 = 1
      EndIf
    EndSub


    Sub OnMouseUp
      objectMoving = 0
    'EndSub


    'Sub OnMouseUp2
      objectMoving2 = 0
    EndSub


    absolu

    Monday, February 20, 2012 6:45 PM
  • Perhaps also using arrays, import GDW561.
    Monday, February 20, 2012 6:57 PM
  • Indeed, itis more efficient.
    Itseems there's just one problemofoverlappingelementswhen placedon top of eachother.Looks like weno longer actasa single element.


    absolu

    Tuesday, February 21, 2012 10:47 AM
  • I used Litdev's code in RWJ900 (Jigsaw puzzle) .  see above thread(Post your.... Ⅳ)

    Thanks Litdev.

    Tuesday, February 21, 2012 1:58 PM
    Answerer
  • absolu, yes there are several ways to just select one boc depending on what you want to happen, the simplest might just be select one box with the lowest index, perhaps:

    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      For i = 1 To numObject
        If (xM >= objectX[i] And xM <= objectX[i]+objectW[i] And yM >= objectY[i] And yM <= objectY[i]+objectH[i]) Then
          objectMoving[i] = 1
          i = numObject
        EndIf
      EndFor
    EndSub
    

    Tuesday, February 21, 2012 6:40 PM
  • Yes,thereisperfect^ ^
    I willtrain withthis code.

    Thank you.


    absolu

    Tuesday, February 21, 2012 10:08 PM
  • Another, slightly better version, not shifting and recentereing on mouse and just one moving object by default, DRK156
    Tuesday, February 21, 2012 10:48 PM
  • Hello,

    I try to loadan image.Pngto movethe same waythat the rectangle.
    What Ican notdo isdetermine the dimensionsso that the mousecan catch it.

    'object = Shapes.AddRectangle(objectW,objectH)
    'Shapes.Move(object,objectX,objectY)


    Here'sthe idea :

    PATCH="E:\VISAGE\"
    'PATCH="http://www................................"

    PIF= ImageList.LoadImage(PATCH + "NEZ.png")

    object= Shapes.AddImage(PIF)
    Shapes.Move(object,objectX,objectY)

    objectMoving = 0

    While ("True")
      If (objectMoving = 1) Then
        objectX = GraphicsWindow.MouseX - objectW/2 'Top left point of shape
        objectY = GraphicsWindow.MouseY - objectH/2
        Shapes.Move(object,objectX,objectY)
      Endif

      Program.Delay(50)
    EndWhile

    Sub OnMouseDown
      xM = GraphicsWindow.MouseX
      YM = GraphicsWindow.MouseY
      If (xM >= objectX And xM <= objectX+objectW And yM >= objectY And yM <= objectY+objectH) Then
        objectMoving = 1
      EndIf
    EndSub

    Sub OnMouseUp
      objectMoving = 0
    EndSub

    Thanks for your help ^^


    absolu


    • Edited by absolu Wednesday, February 22, 2012 5:58 PM
    Wednesday, February 22, 2012 5:58 PM
  • Use the ImageList function to get the width and height of an image.

    For i = 1 To numObject
      objectX[i] = 100 + Math.GetRandomNumber(200)
      objectY[i] = 100 + Math.GetRandomNumber(200)
      img = Flickr.GetRandomPicture("car")
      object[i] = Shapes.AddImage(img)
      Shapes.Move(object[i],objectX[i],objectY[i])
      objectW[i] = ImageList.GetWidthOfImage(img)
      objectH[i] = ImageList.GetHeightOfImage(img)
    EndFor
    
    Wednesday, February 22, 2012 6:25 PM
  • Thank youfor the code^ ^
    But for now,I preferworking witha simple codetounderstand the basics.

    Here's whatI try to do,butit does not work :

    FSB980


    I would tryto movethesetwoobjects,likerectangles.

    Can you help me?

    Thank you.


    absolu

    Wednesday, February 22, 2012 8:08 PM
  • Mainly the following:

    1] image width heights

    objectW[1] = ImageList.GetWidthOfImage(img+ "NEZ.png")
    objectH[1] = ImageList.GetHeightOfImage(img+ "NEZ.png")
    
    objectW[2] = ImageList.GetWidthOfImage(img+ "BASE YEUX 1.png")
    objectH[2] = ImageList.GetHeightOfImage(img+ "BASE YEUX 1.png")
    

    2] To move both images someing like this, import FSB980-0

    Wednesday, February 22, 2012 8:25 PM
  • OK,thank you.
    I donot know anything aboutthe function:ImageList.GetWidthOfImage

    Now Iunderstand better.

    Canyou imagine aprinciple ofmagnetism,so thatobjectsare able tofollow suit?
    Always withthe principleof a jigsaw puzzle?

    absolu

    Thursday, February 23, 2012 6:11 AM
  • wecanalsogiverotationsto objects?
    Iguess it'swithshape.rotate.

    The idea is tomove objects withtheright mouseclickand rotationwiththe left click.

    absolu

    Thursday, February 23, 2012 7:12 AM

  • Hello,

    I just made a small program, with your advice. Locally, it works very well, but online it does not work.

    Here's the code:

    FSB980-3


    Is what I missed something?

    absolu

    Thursday, February 23, 2012 2:49 PM
  • A little simple testing with TextWindow.WriteLine and testing online shows that the width and height are 0, probably the ImageList.GetWidthOfImage methods don't work propertly online - in which case your program can still work if you set these values manually.
    Thursday, February 23, 2012 6:46 PM
  • Just out of curiosity, I've decided to check FSB980-3 out.

    In my case, it seems to work perfectly. I can move those 14 objects anywhere.

    Inside this loop:

    For i = 1 To 14
      object[i] = Shapes.AddImage(img[i])
      Shapes.Move(object[i],objectX[i],objectY[i])
      objectW[i] = ImageList.GetWidthOfImage(img[i])
      objectH[i] = ImageList.GetHeightOfImage(img[i]) 
    EndFor

    I've added up -> TextWindow.WriteLine(i + " = " + objectW[i] + " , " + objectH[i])

    And it showed many diff. size values for those pictures.

    Perhaps I just got lucky?

    Friday, February 24, 2012 4:39 AM
    Answerer
  • The problemis that the programonly workslocallybutnot online.
    I triedseveral times,but the objectsdo not moveon the internet.

    Another problemthat I cannot solve, noristhe rotationofobjects.
    Itwould be nice todirect them.Clickto moveleftandright clickto rotate,for example.Perhapssame as usingthe arrow keyscould simplifythe principle.

    Ifanyone has an idea...?I'm interested.Andof course,the idealis toexecutethe programonline,but ifitonly workslocallyitwould already begreat.

    Thank you.

    absolu

    Friday, February 24, 2012 6:34 AM
  • see FSB980-9

    Litdev already said , imagelist.width,height are not available.

    So, added math.abs(xM-......)<=50,  .....

    you can catch imageshape.

    and added rotation code. If you click right button , shape rotates 90,180,270,0,90......degree.

    Unfortunately  online is not available now.

    edit: Now online is available, but it cannot rotate. because if right button clicked,

    "SilverLight(S)" message appears.  How to remove this message? I don't know. 
    edit2:Locally rotation works only once(+90). more consideration is required.

    edit3:Locally continuous rotation OK. FSB980-10.   (but not online yet)



    Friday, February 24, 2012 1:39 PM
    Answerer
  • Oh, sorry, my mistake

    I thought you meant images loaded from the Internet. I haven't figured out you meant your code running from Silverlight *_*


    Friday, February 24, 2012 2:32 PM
    Answerer
  • see FSB980-9

    Litdev already said , imagelist.width,height are not available.

    So, added math.abs(xM-......)<=50,  .....

    you can catch imageshape.

    and added rotation code. If you click right button , shape rotates 90,180,270,0,90......degree.

    Unfortunately  online is not available now.

    edit: Now online is available, but it cannot rotate. because if right button clicked,

    "SilverLight(S)" message appears.  How to remove this message? I don't know. 
    edit2:Locally rotation works only once(+90). more consideration is required.

    edit3:Locally continuous rotation OK. FSB980-10.   (but not online yet)



    Great!

    While waiting to findtherightmouse click, I adjustedthe code with thekeyboardkeys1 and 2.
    So,it works.Just needtheleft mouse buttonis pressedat the same timeyou are doingrotations.

    FSB980-14

    Otherwise,I tried tochange thedetectionof the mouseon the object,but I'm nothappened.
    Math.absfunction, Ido not knowtoo much:IfMath.abs(xM-ObjectX[i])<= 50AndMath.abs(yM-objectY[i])<= 50Then
    Iunderstand how it worksbut I cannot use it.

    I will try tobring upa point(circle)so that weunderstandthat onehasbeenselected


    absolu

    Friday, February 24, 2012 9:02 PM
  • ARGF....*_*
    Rotationwithkeyboard eventsdonot work-_-

    FSB980-16


    absolu

    Friday, February 24, 2012 9:47 PM
  • How about this?  FSB980-18  

    Use "Right"  and "Left" arrow key. 

    Note:Math. abs....    0<=MX  and   MX<=100  equal  math.abs(MX-50)<=50   

     Math.abs(-360)=360 ,math.abs(360)=360  

    Friday, February 24, 2012 11:52 PM
    Answerer
  • Ok

    Actuallythe first programworked.It was a mistakeon my part.

    Theidealwould be to havea small dot(likea circle)thatappearby selectingan object,to understandthat it has beenselected.

    GraphicsWindow.FillEllipse(xM,yM,50,50) ??


    absolu

    Saturday, February 25, 2012 6:29 AM