[Newbies] SimpleSliderMorph target setting question.
Torsten Bergmann
astares at gmx.de
Fri Nov 21 08:52:07 UTC 2008
Assuming you have a simple class SecondaryCoil
Object subclass: #SecondaryCoil
instanceVariableNames: 'turns'
classVariableNames: ''
poolDictionaries: ''
category: 'MyApp'
with an instance variable "turns" and an accessor methods #turns to get the value:
turns
"Returns the turns value"
^turns
and another method #turns: to set the value of the instance variable:
turns: aNumber
"Sets the turns value"
turns := aNumber
then you can use:
|slider coil|
slider := SimpleSliderMorph new.
coil := SecondaryCoil new.
slider target: coil.
slider actionSelector: #turns:.
slider openInWorld.
coil inspect
which is easier to write using "method chaining" as
|slider coil|
slider := SimpleSliderMorph new.
coil := SecondaryCoil new.
slider target: coil;
actionSelector: #turns:;
openInWorld.
coil inspect
Click on the instance variable in the inspector of your coil instance and change the slider.
The value gets updated. What's the idea: a slider acts on a target and you tell him
to send a message with the given selector name to this target anytime its own value changes.
Since the slider will give the sliders value as an argument to the target the selector
has to be a method accepting one argument.
By default the slider uses values from 0.0 to 1.0 but you can change this
if required:
|slider coil|
slider := SimpleSliderMorph new.
coil := SecondaryCoil new.
slider target: coil;
actionSelector: #turns:;
minVal: 0;
maxVal: 360;
openInWorld.
coil inspect
You can also provide another method on your new class and use #reportTurns: as action selector:
reportTurns: aNumber
"Sets the given turns and reports the value"
self turns: aNumber.
Transcript show: self turns asString.
Since your new object is referenced by the inspector window and the slider (who holds it in an
instance variable "target") it will not "die" instantly. If you close both of them and your new
object instance is not referenced elsewhere it get collected when the garbage collector run.
BTW:
Note that in Smalltalk you typically use just "turns:" as the methods name for accessors.
(setTurns: is more Java style)
If you use the Refactoring browser or the OmniBrowser (included in Damiens dev images)
you can select an instance variable name (in the class creation expression above) and
use the context menu to automatically create the accessor methods for you.
Have fun
Torsten
http://astares.blogspot.com
--
Sensationsangebot nur bis 30.11: GMX FreeDSL - Telefonanschluss + DSL
für nur 16,37 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
More information about the Beginners
mailing list