29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In the updating protocol we need to define a method which will catch the result of the ch<strong>an</strong>ge<br />

message set when a new widget is added to the model. This is the update:with: message. In our<br />

case we merely wish to draw the newly added object in the view:<br />

update: <strong>an</strong>Aspect with: <strong>an</strong><strong>Object</strong><br />

<strong>an</strong>Aspect == #object ifTrue: [<strong>an</strong><strong>Object</strong> displayOn: self graphicsContext].<br />

We now need to define the displayOn: method for the drawing object within a displaying<br />

protocol. This is sent to the drawing view when what is being displayed needs updating (e.g. when part<br />

of the window is uncovered). This method sends the displayOn: message to all the elements<br />

currently held in the nodes list of the drawingModel:<br />

displayOn: aGraphicsContext<br />

self model nodes do: [:node | node displayOn: aGraphicsContext]<br />

Finally we need to extend the class side method in the inst<strong>an</strong>ce creation protocol so that it sends the<br />

message initialize to the newly created inst<strong>an</strong>ce:<br />

new<br />

^super new initialize<br />

29.5.4 The DrawingController class<br />

The DrawingController class is a subclass of the ControllerWithMenu class. This is<br />

because we wish to catch the left mouse button pressed event. This is simplified for the<br />

ControllerWithMenu cla ss as a method called redButtonActivity is called whenever the<br />

left mouse button is pressed.<br />

ControllerWithMenu subclass: #DrawingController<br />

inst<strong>an</strong>ceVariableNames: ''<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'<br />

The class comment for the DrawingController is left as <strong>an</strong> exercise for the reader. Only two<br />

methods are defined for this class: controlLoop <strong><strong>an</strong>d</strong> redButtonActivity. Both methods are<br />

defined in the control defaults protocol. The controlLoop method is called whenever the curso r<br />

enters the area of the window displayed by the DrawingView. This controlLoop method merely<br />

ch<strong>an</strong>ges the type of cursor displayed (to that of a cross) when the user moves the cursor onto the<br />

drawing area. It then sends the message super controlLoop. This a llows the control operations<br />

defined higher up the class hierarchy to be invoked:<br />

controlLoop<br />

"Ch<strong>an</strong>ge the cursor to a cross-hair for drawing."<br />

Cursor crossHair showWhile: [super controlLoop].<br />

Finally the redButtonActivity method defines what should happen when the left mouse<br />

button is pressed. In this case a new graphic object is added to the drawing at the point indicated by the<br />

current cursor position:<br />

redButtonActivity<br />

"Place widget at the current cursor position when left button pressed"<br />

self model addWidgetAt: self sensor cursorPoint.<br />

29.5.5 The DrawingWidget class<br />

The DrawingWidget class is <strong>an</strong> abstract superclass which captures all the elements which are<br />

common to graphic objects which will be displayed within a drawing. The DrawingWidget class (a<br />

subclass of <strong>Object</strong>) defines three inst<strong>an</strong>ce variables origin (the cursor position when the mouse is<br />

clicked), the extent (or size of the object) <strong><strong>an</strong>d</strong> the border:<br />

<strong>Object</strong> subclass: #DrawingWidget<br />

inst<strong>an</strong>ceVariableNames: 'origin extent border '<br />

253

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!