SimpleSliderMorph target setting question.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

SimpleSliderMorph target setting question.

Torsten Bergmann
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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: SimpleSliderMorph target setting question.

Jerome Peace
[Newbies] SimpleSliderMorph target setting question.

***
>Tim Patti timpatti at yahoo.com
>Fri Nov 21 01:34:21 UTC 2008
>
>Hi all,  I've been stuck for a couple of days on trying to figure out how to
>properly set the target and setValueSelector of a SimpleSliderMorph.  My
>intended target is an instance of a class that I created in the workspace and
>I assume the setValueSelector is what I use to send the value to a method in
>my instance of the class.
>
See Torsten Bergmann's note for a progammatic way to set things.

For a one off, in the field testing, sort of way...
For classes with morphic representations you can:
grab a slider from the parts bin
Bring up its halo.
Select the red menu.
Make it stay up.
Set the action selector to 'setTurns:'.
Set the maximum and minimum range.
Set truncation on to avoid fractional values.
Sight target to select a target.*
Play!

*(If you are not offered this option,
 then you are using a sqeauk earlier than 3.9
without some essential targeting fixes.
If thats the case Torsten's methods will still work.)

 


>si := SecondaryCoil. "make an instance of the class"
>si setTurns:10."set the turns to 10"
>Transcript show: si getTurns."print the turns"
>
>The above code works in the workspace and transcript, but I cannot figure out
>how to point the SimpleSliderMorph target to my instance si.  Tried all kind
>of combinations with no luck.

In the workspace you can create the slider

mySlider :=
SimpleSliderMorph new openCenteredInWorld .

then
mySlider target: si

alternately from the workspace menu
check the box for
'create textual references to dropped morphs'.
Then drag your slider over and drop it on the window.


>
>I'd like to be able to use the slider to set the turns of the coil in my
>instance of SecondaryCoil.
>
>I have 4 books on Squeak but I must me missing something obvious here.
>
>Tim
>
***



     
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners