locked
Drag and drop performance troubleshooting RRS feed

  • Question

  • Hi, I try to make a very simple click a shape, drag and drop it within windows graphic. But the program still in-perfect.

    My problem is: if I drag the shape too slow or too fast, windows will error like this, which is I'm not capable to troubleshoot (I don't know about hardware performance)

    My Source code:

    a = Shapes.AddEllipse(100,100)

    GraphicsWindow.MouseMove = mousemove

    Sub mousemove
      If GraphicsWindow.MouseX >= Shapes.GetLeft(a) then
        If GraphicsWindow.MouseX <= Shapes.GetLeft(a) + 100 then
          If GraphicsWindow.MouseY >= Shapes.GetTop(a) Then
            If GraphicsWindow.MouseY <= Shapes.GetTop(a) + 100 then
      While Mouse.IsLeftButtonDown
        Shapes.move(a, GraphicsWindow.MouseX - 50, GraphicsWindow.MouseY - 50)
      Endwhile
    EndIf
    EndIf
    EndIf
    endif
    endsub

    Can you help to enhance my program?


    Sunday, February 2, 2014 6:07 AM

Answers

  • GraphicsWindow.MouseMove event is the heaviest of them all. Each tiny mouse movement triggers a callback to it!

    So you gotta transfer your also heavy code there to somewhere else. Most you could leave there is a flag that the event occurred!

    Another tip is rather than keep asking to the Shapes what's its current position w/ methods GetLeft() & GetTop(), merely declare 2 variables to keep track of those 2 coordinates!

    Anyways, I've made my own solution! It's a little complex; but if you wanna take a look... here it comes:

    TWF500-0

    '================================================='
    ' Drag Object Example (v1.02)
    ' by GoToLoop (2014/Feb)
    '
    ' TWF500-0
    '
    ' http://social.msdn.microsoft.com/Forums/en-US
    ' /2d5ec679-25e5-4d98-986a-c662da26ad89
    ' /drag-and-drop-performance-troubleshooting
    '================================================='

    '__________________________________________________________'
    ' Init Graphics Window:
    GW = 640
    GH = 480

    GraphicsWindow.Title = "Drag Object Example"
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Width = GW
    GraphicsWindow.Height = GH
    GraphicsWindow.BackgroundColor = "White"

    ' Init Sprite Properties:
    DIAM = 150
    RAD = DIAM/2

    x = (GW - DIAM)/2
    y = (GH - DIAM)/2

    ' Create Sprite Shape:
    GraphicsWindow.PenWidth = 4
    GraphicsWindow.PenColor = "Black"
    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

    sprite = Shapes.AddEllipse(DIAM, DIAM)
    Shapes.Move(sprite  x, y)

    ' Activate Mouse Events:
    GraphicsWindow.MouseDown = MouseClickedEvent
    GraphicsWindow.MouseUp = MouseReleasedEvent
    '__________________________________________________________'

    ' Main Loop:
    '__________________________________________________________'
    Loop:
    mx = GraphicsWindow.MouseX - RAD
    my = GraphicsWindow.MouseY - RAD

    If isDown And isHovering Then
      DragSpriteToPointerLocation()
    EndIf

    Program.Delay(10)
    Goto Loop
    '__________________________________________________________'

    ' Asynchronous Event Callbacks:
    '__________________________________________________________'
    Sub MouseClickedEvent
      CheckPointerOverSpriteLocation()
      isDown = "True"
    EndSub
    '__________________________________________________________'
    Sub MouseReleasedEvent
      isDown= "False"
    EndSub
    '__________________________________________________________'

    ' Sprite Subroutines:
    '__________________________________________________________'
    Sub CheckPointerOverSpriteLocation
      If Math.Abs(mx - x) < RAD And Math.Abs(my - y) < RAD Then
        isHovering = "True"
      Else
        isHovering = "False"
      EndIf
    EndSub
    '__________________________________________________________'
    Sub  DragSpriteToPointerLocation
      x = mx
      y = my
      Shapes.Move(sprite  x, y)
    EndSub
    '__________________________________________________________'

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Sunday, February 2, 2014 9:12 AM
    • Proposed as answer by litdev Sunday, February 2, 2014 5:56 PM
    • Marked as answer by Totardo_Tobing Tuesday, February 4, 2014 7:27 AM
    Sunday, February 2, 2014 8:45 AM
    Answerer

All replies

  • GraphicsWindow.MouseMove event is the heaviest of them all. Each tiny mouse movement triggers a callback to it!

    So you gotta transfer your also heavy code there to somewhere else. Most you could leave there is a flag that the event occurred!

    Another tip is rather than keep asking to the Shapes what's its current position w/ methods GetLeft() & GetTop(), merely declare 2 variables to keep track of those 2 coordinates!

    Anyways, I've made my own solution! It's a little complex; but if you wanna take a look... here it comes:

    TWF500-0

    '================================================='
    ' Drag Object Example (v1.02)
    ' by GoToLoop (2014/Feb)
    '
    ' TWF500-0
    '
    ' http://social.msdn.microsoft.com/Forums/en-US
    ' /2d5ec679-25e5-4d98-986a-c662da26ad89
    ' /drag-and-drop-performance-troubleshooting
    '================================================='

    '__________________________________________________________'
    ' Init Graphics Window:
    GW = 640
    GH = 480

    GraphicsWindow.Title = "Drag Object Example"
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Width = GW
    GraphicsWindow.Height = GH
    GraphicsWindow.BackgroundColor = "White"

    ' Init Sprite Properties:
    DIAM = 150
    RAD = DIAM/2

    x = (GW - DIAM)/2
    y = (GH - DIAM)/2

    ' Create Sprite Shape:
    GraphicsWindow.PenWidth = 4
    GraphicsWindow.PenColor = "Black"
    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

    sprite = Shapes.AddEllipse(DIAM, DIAM)
    Shapes.Move(sprite  x, y)

    ' Activate Mouse Events:
    GraphicsWindow.MouseDown = MouseClickedEvent
    GraphicsWindow.MouseUp = MouseReleasedEvent
    '__________________________________________________________'

    ' Main Loop:
    '__________________________________________________________'
    Loop:
    mx = GraphicsWindow.MouseX - RAD
    my = GraphicsWindow.MouseY - RAD

    If isDown And isHovering Then
      DragSpriteToPointerLocation()
    EndIf

    Program.Delay(10)
    Goto Loop
    '__________________________________________________________'

    ' Asynchronous Event Callbacks:
    '__________________________________________________________'
    Sub MouseClickedEvent
      CheckPointerOverSpriteLocation()
      isDown = "True"
    EndSub
    '__________________________________________________________'
    Sub MouseReleasedEvent
      isDown= "False"
    EndSub
    '__________________________________________________________'

    ' Sprite Subroutines:
    '__________________________________________________________'
    Sub CheckPointerOverSpriteLocation
      If Math.Abs(mx - x) < RAD And Math.Abs(my - y) < RAD Then
        isHovering = "True"
      Else
        isHovering = "False"
      EndIf
    EndSub
    '__________________________________________________________'
    Sub  DragSpriteToPointerLocation
      x = mx
      y = my
      Shapes.Move(sprite  x, y)
    EndSub
    '__________________________________________________________'

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Sunday, February 2, 2014 9:12 AM
    • Proposed as answer by litdev Sunday, February 2, 2014 5:56 PM
    • Marked as answer by Totardo_Tobing Tuesday, February 4, 2014 7:27 AM
    Sunday, February 2, 2014 8:45 AM
    Answerer
  • Your program is working perfectly. Let me study yours. How you dissecting the problems in your mind?

    Sunday, February 2, 2014 4:48 PM
  • How you dissecting the problems in your mind?

    I don't believe I can deep analyze myself by myself!  LoL

    But if you don't understand some part, don't hold back to ask about it!  :D


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Sunday, February 2, 2014 5:34 PM
    Answerer
  • Yes, your program is kind a too advance for me, let me study it part by part first.

    Tuesday, February 4, 2014 7:23 AM