Tutorial T2 – Sliders

Sometimes, it is very useful to interact with the simulation. Sliders can be used for numerous reasons, from controlling coefficients to displaying values in real-time.

```// Tutorial 2 - Sliders

// TUTORIALS ARE DESIGNED TO BE READ AND STEPPED THROUGH IN THE
// APRON SIMULATOR. THEY WILL NOT DO INTERESTING THINGS IF RAN!
// (c) David R W Barr 2011

// This tutorial introduces "sliders". Sliders are horizontal scroll
// bars that can be used to fluidly set parameters inside running
// algorithms.

!include('apron.aps')

// Create and display a register
!create('a', REGISTER, 128, 128)
sim.display(r[a])

// Create a slider - a variable is used as a handle to the slider
// so it can be identified later
handle_to_slider = sim.addSlider('Value', -100, 100, 0, 1.0, 0.1)

// The parameters for slider creation are as follows
// Name
// Minimum Value
// Maximum Value
// Default Value
// Scaling Value
// Accuracy (Minimum Step Size)

// The accuracy value determines how precisely a slider value can be set, a value of 1.0 will
// limit the slider to selecting whole numbers only. A value of 0.5 will limit the selection to
// whole numbers and half fractions, and so on etc... Here it is set to 0.1, which will limit
// the slider value to the nearest 10th.

handle_to_slider2 = sim.addSlider('Weird', -100, 100, 0, 1.0, 0.1)

t = 0 // Define a variable to store the value of a slider

// Try running now in the simulator, and move the value slider

#start
// To read a slider we use the "sim.get" function, passing along the handle
sim.get(handle_to_slider, t)
r[a] = t
sim.show(r[a])

t = t * -1

// We can also set slider positions to visualise a variable
sim.set(handle_to_slider2, t)

jump(#start)

//
// You will have noticed that the handles for the slider get classified as variables,
// which you may not want to appear in your regular variable list. To overcome this
// you could create a type derived from SIM_VARIABLE:
//
// !object('GUI', 'h', 700, 'SIM_VARIABLE')
// !create('value', 'GUI')
// !create('weird', 'GUI')
//