Script for snap-in movement of widgets (needs GP V4 or higher)

Today i tried to simulate the behaviour of “multi-position” switches with knob or fader widgets…
the script i wrote makes the widget’s position “snap” to the next “fixed-switch-point” if there is no more manual widget movement within a specified time threshold.
That way you can emulate such things as 3, 4, or more way selector switches like they can be found on many plugin’s GUIs.

You need a widget with the ScriptName “w_snapped” (see “advanced” tab in edit mode) and then, within the script, you can specify the number of snap-points (snaps) and the time threshold (waiting_time) value in ms…

The movement of a fader “acting” as a three-way switch looks like this:
position-snapping

The code of the script:

var
w_snapped : widget //handle name of the widget - set in edit/advvanced tab

snaps : Integer = 3 //total number of snap points
waiting_time : double = 20 //time to wait in ms befor switching to snap point

snap_point : Integer //varable for actual snap point number
old_snap_point: Integer //buffering the old snap-point
snap_interval : double = 1.0 / (snaps-1) //jumping range for the widget

widget_pos : double
snapped : boolean = false
move_start : double //the moment the widget started to change

On WidgetValueChanged (knb_val : double) From w_snapped
    snap_point = Round(knb_val / snap_interval)
    move_start = TimeSinceStartup()
    SetTimersRunning(true)
End

On TimerTick(ms : double)
    If ms - move_start > waiting_time and snapped == false Then
        snapped = true
        widget_pos = snap_point * snap_interval
        If snap_point == 0 Then
            widget_pos = 0.0
        End
        
        SetWidgetValue (w_snapped, widget_pos)
        SetTimersRunning(false)
        snapped = false
    End
End

And here is a rackspace which you can import into a gig file to try it:
Snap-Widget.rackspace (22.3 KB)

Maybe it’s useful for some of you guys…
Cheers! :beers:
Erik (aka Schamass) :metal: :sunglasses: :metal:

8 Likes