B4J Question Movable and resizable view example

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Maybe is there any ready code to make the pane used for area selection ?
A pane that moveable and resizable by mouse with changing the cursor at edges (arrows) and hand in the center.
 

peacemaker

Expert
Licensed User
Longtime User
Maybe useful for anyone. Not so elegant, but works in B4J (only).
B4X:
#Region Project Attributes
    #MainFormWidth: 500
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label As Label
    Private downx, downy, movex, movey As Float, Resizing As Boolean
    Dim gap As Int = 10

    Private panOrig As Pane
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Private Sub  Label_MousePressed (EventData As MouseEvent)
    'Log("Pressed")
    downx = EventData.x
    downy = EventData.y
End Sub

Private Sub  Label_MouseReleased (EventData As MouseEvent)
    Resizing = False
    'Log("Released")
End Sub

Private Sub  Label_MouseMoved (EventData As MouseEvent)
    If EventData.X > (Label.Width - gap) Or EventData.y > ( Label.Height - gap) Then
        Label.MouseCursor = fx.Cursors.OPEN_HAND
    Else
        Label.MouseCursor = fx.Cursors.MOVE
    End If
End Sub

Private Sub panOrig_MouseDragged (EventData As MouseEvent)
    Dim deltax, deltay As Float
    deltax = EventData.X - movex
    deltay = EventData.y - movey
    'Log("deltax = " & deltax)
    If deltax > 1 Then deltax = 1
    If deltax < -1 Then deltax = -1
    If deltay > 1 Then deltay = 1
    If deltay < -1 Then deltay = -1

    If downx > ( Label.prefWidth - gap) And Not(Resizing) Then
        Resizing = True
    End If
    If Resizing Then
        Label.PrefWidth = Max( Label.prefWidth + deltax, 50)
    Else
        Label.Left =  Label.Left + deltax
    End If
    If  Label.Left < 0 Then  Label.Left = 0
    If  Label.Left +  Label.Width > panOrig.Width Then
        Label.PrefWidth = panOrig.Width -  Label.Left
    End If
 
    If downy > ( Label.Height - gap) And Not(Resizing) Then
        Resizing = True
    End If
    If Resizing Then
        Label.PrefHeight = Max( Label.PrefHeight + deltay, 50)
    Else
        Label.Top =  Label.Top + deltay
    End If
    If  Label.Top < 0 Then  Label.Top = 0
    If  Label.Top +  Label.Height > panOrig.Height Then
        Label.PrefHeight = panOrig.Height -  Label.Top
    End If

    movex = EventData.X
    movey = EventData.y
End Sub

Sub Label_Action
    If Sender Is MenuItem Then
        Dim mi As MenuItem = Sender
        Log(mi.Text & " HERE")
    Else
        Log(Sender & " is not a MenuItem")
    End If
End Sub

1634139603779.png



1634139630505.png

1634139935219.png

1634139954248.png
 

Attachments

  • area_selector.zip
    2.6 KB · Views: 125
Last edited:
Upvote 0
Top