B4J Code Snippet Scale nodes on mouseover

A bit of code that can add a nice effect if you have a form to fill in.
As the mouse enters the control, it scales it up so it's easier to enter things into a textfield for example.
The controls still work as normal even when scaled.
As the mouse leaves, it returns to the original size.

Using the scroll wheel while the mouse is over the rootpane, will cause all the controls to either scale up (mouse wheel up/forward), or scale down (mouse wheel down/back).
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim tf As TextField
    Dim b As Button
    Dim scale As Double = 1.0
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
    b.Initialize("")
    b.Text = "hello"
    b.Id="b"
    tf.Initialize("")
    tf.id="tf"
    MainForm.RootPane.AddNode(tf,200,60,-1,-1)
    MainForm.RootPane.AddNode(b,200,100,-1,-1)
    'try scroll wheel on mainform
    setHandler(MainForm.RootPane,"setOnScroll","scroll")
    ' try moving mouse over nodes in mainform
    ' when mouse over textfield try typing in it
    ' the button will work too when scaled.
    ' just put nodes in here that you want to scale when mouse goes over them
    ' The textfield get transated to the right when scaled by how much it overlaps the left
    '  side of the mainform if the scale is larger, so left side is still visible to type in.
    MouseOver(Array As Node(tf,b))
End Sub
Sub MouseOver(n() As Node)
    For Each n1 As Node In n
        setHandler(n1,"setOnMouseEntered","mouseIn")
        setHandler(n1,"setOnMouseExited","mouseOut")
    Next
End Sub
Sub setHandler(ob As JavaObject,eventName As String,handlerName As String)
ob.RunMethod(eventName, Array(ob.CreateEventFromUI("javafx.event.EventHandler",handlerName,True)))
End Sub
Sub mouseIn_Event(m As String,args() As Object)
    asJO(Sender).RunMethod("setScaleX",Array(3.0))
    asJO(Sender).RunMethod("setScaleY",Array(3.0))
    MainForm.Title =  asJO(Sender).RunMethod("getId",Null)
    If asJO(Sender).RunMethodJO("getBoundsInParent",Null).RunMethod("getMinX",Null) <0 Then
        asJO(Sender).RunMethodJO("setTranslateX",Array(- asJO(Sender).RunMethodJO("getBoundsInParent",Null).RunMethod("getMinX",Null)))
    End If
    asJO(Sender).RunMethod("toFront",Null)
End Sub
Sub mouseOut_Event(m As String,args() As Object)
    asJO(Sender).RunMethod("setScaleX",Array(scale))
    asJO(Sender).RunMethod("setScaleY",Array(scale))
    asJO(Sender).RunMethod("toBack",Null)
    asJO(Sender).RunMethodJO("setTranslateX",Array(0.0))
    MainForm.Title = ""
End Sub
Sub scroll_Event(MethodName As String, Args() As Object)
    Dim jo As JavaObject
    jo.InitializeStatic("javafx.scene.input.ScrollEvent")
    jo = Args(0)
    If jo.RunMethod("getDeltaY",Null) > 0 And scale <= 2  Then
        scale = scale + 0.05
    End If
    If jo.RunMethod("getDeltaY",Null) < 0 And scale >= 0.5  Then
        scale = scale - 0.05
    End If
    For Each n As Node In MainForm.RootPane.GetAllViewsRecursive
        asJO(n).RunMethod("setScaleX",Array(scale))
        asJO(n).RunMethod("setScaleY",Array(scale))
    Next
    asJO(MainForm.RootPane).RunMethod("requestLayout",Null)
End Sub
Sub asJO(o As JavaObject)As JavaObject
    Return o
End Sub
 
Top