Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Type point(x As Float, y As Float)
Type size(width As Float, height As Float)
Private touchedButton, changedButton As Button
Private lastPosition, difference As point
Private buttonOrgSize As size
Private Button1 As Button
Private Pane1 As Pane
End Sub
Public Sub Initialize
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
buttonOrgSize = Createsize(Button1.Width,Button1.Height) 'set the init size of the button
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub Button1_Click
xui.MsgboxAsync("Hello world!", "B4X")
End Sub
Private Sub MainForm_Touch (Action As Int, X As Float, Y As Float)
Select Action
Case 0 'down
touchedButton = checkifbuttonpressed(X,Y,False)
Case 1 'up
If touchedButton.IsInitialized Then
If checkifbuttonpressed(X,Y,True).IsInitialized Then
changedButton = checkifbuttonpressed(X,Y,True)
Dim changebPoint As point = Createpoint(changedButton.Left, changedButton.Top)
changedButton.SetLayoutAnimated(200,lastPosition.x,lastPosition.y,buttonOrgSize.width,buttonOrgSize.height) 'move the changebuton to touchbutton position
touchedButton.SetLayoutAnimated(200,changebPoint.x,changebPoint.y,buttonOrgSize.width,buttonOrgSize.height) 'stick the touched button
Else
touchedButton.SetLayoutAnimated(200,lastPosition.x,lastPosition.y,buttonOrgSize.width,buttonOrgSize.height) 'return touchedbutton to his origin
End If
End If
touchedButton = Null
Case 2 'moving
If touchedButton.IsInitialized Then
touchedButton.Left = X - difference.x
touchedButton.top = Y - difference.y
End If
End Select
End Sub
Private Sub checkifbuttonpressed(x As Float, y As Float, skipTouchedButton As Boolean) As Button
For Each v As Node In Main.MainForm.RootPane.GetAllViewsRecursive
If v Is Button Then
Dim b As Button = v
If x > b.Left And y > b.Top And x < b.Left+b.Width And y < b.Top + b.Height Then
If skipTouchedButton Then
If b = touchedButton Then Continue 'skip tocuhedbutton check
Else
lastPosition = Createpoint(b.Left,b.Top)
End If
BringToFront(b)
Dim scale As Float = 20dip
difference = Createpoint(x-b.Left+scale, y-b.Top+scale)
b.SetLayoutAnimated(50,b.Left-scale,b.Top-scale,b.Width+(scale*2),b.Height+(scale*2))
Return b
End If
End If
Next
Return Null
End Sub
Sub BringToFront(n As Node)
Dim parent As Pane = n.Parent
n.RemoveNodeFromParent
parent.AddNode(n, n.Left, n.Top, n.PrefWidth, n.PrefHeight)
End Sub
Public Sub Createpoint (x As Float, y As Float) As point
Dim t1 As point
t1.Initialize
t1.x = x
t1.y = y
Return t1
End Sub
Public Sub Createsize (width As Float, height As Float) As size
Dim t1 As size
t1.Initialize
t1.width = width
t1.height = height
Return t1
End Sub