B4J Question How to drag a control to move it to another position

Discussion in 'B4J Questions' started by xulihang, Jun 19, 2019.

  1. xulihang

    xulihang Active Member Licensed User

    Hi there,

    I am making a simple layered image editor. Its major function is to add text layers on images. I am trying to use Label as text layers and use snapshot to export merged images.

    I try to use the mouse to drag a control to a new place.

    I use the mousedragged event, but the dragging process is shaky.

    Code:
    Sub ImageView1_MouseDragged (EventData As MouseEvent)
        
    Dim node As Node
        
    node=Sender
        
    node.Left=EventData.X
        
    node.Top=EventData.Y
        
    node.MouseCursor=fx.Cursors.HAND
    End Sub
     
    Last edited: Jun 19, 2019
  2. xulihang

    xulihang Active Member Licensed User

    The log output shows the position changes. This is why the control will shake. I don't know the reason.

    Code:
    Log("Dragged")
    Log(EventData.X)
    Log(EventData.Y)
    Code:
    Dragged
    48
    89
    Dragged
    348
    106
    Dragged
    46
    91
    Dragged
    348
    107
    Dragged
    45
    94
    Dragged
    348
    108
    Dragged
    45
    96
    Dragged
    347
    111
    Dragged
    45
    97
    Dragged
    347
    113
    release
    45
    97
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Your code is incorrect because X and Y are relative to the node position.

    Code:
    Sub Process_Globals
       
    Private fx As JFX
       
    Private MainForm As Form
       
    Type PositionData (PressedX As Int, PressedY As Int)
       
    Private Button1 As Button
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
       MainForm = Form1
       MainForm.RootPane.LoadLayout(
    "1"'Load the layout file.
       MainForm.Show
    End Sub

    Sub Button1_MouseDragged (EventData As MouseEvent)
       
    Dim view As B4XView = Sender
       
    Dim pd As PositionData = view.Tag
       
    Dim ParentX As Int = EventData.X + view.Left
       
    Dim ParentY As Int = EventData.Y + view.Top
       
    view.Left = ParentX - pd.PressedX
       
    view.Top = ParentY - pd.PressedY
    End Sub

    Sub Button1_MousePressed (EventData As MouseEvent)
       
    Dim view As B4XView = Sender
       
    Dim pd As PositionData
       pd.PressedX = EventData.X
       pd.PressedY = EventData.Y
       
    view.Tag = pd
    End Sub
    [​IMG]
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice