On Mon, 30 Dec 2002 12:15:13 -0800 Bill Spight bspight@pacbell.net wrote:
How do you make a Morph that keeps its shape upon resizing? I would have thought that that would be a common thing to do, but I have not found anything in the documentation about how to do that.
Bill,
You could override the #extent: method to do something like:
extent: aPoint
x _ aPoint x. y _ aPoint y. (y * aspectRatio) > x ifTrue: [ x _ y * aspectRatio ] ifFalse: [ y _ x / aspectRatio ]. ^super extent: (x@y) rounded
Where aspectRatio is an instance variable set to the desired x/y ratio.
Cheers, Bob
Dear Bob,
Thanks very much. :-)
Let me see if I understand what is going on.
You could override the #extent: method to do something like:
In resizing the morph is sent an #extent: message. We make sure the new extent has the correct proportions.
extent: aPoint
x _ aPoint x. y _ aPoint y. (y * aspectRatio) > x ifTrue: [ x _ y * aspectRatio ] ifFalse: [ y _ x / aspectRatio ].
Doing it this way makes for the greater extent, which is faster when expanding.
^super extent: (x@y) rounded
x@y is rounded because drawing requires an integral number of pixels.
Sending this message to super avoids the extra processing if it were sent to self. (And also avoids an infinite loop when rounding).
Where aspectRatio is an instance variable set to the desired x/y ratio.
Bueno!
Bill
squeak-dev@lists.squeakfoundation.org