Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rescale subMorph when the container does?

I am exploring Squeak, release 5.2 in MacOS.

I am drawing lines (PolygonMorph) inside a RectangleMorph 'r'. When I translate 'r' the lines translate but when I rescale 'r' the lines do not rescale.

Run the snipped below. Then with the Halo translate and resize the rectangle 'r'. You will see line 'p' get translated but not rescaled.

r := RectangleMorph new.
"[Pharo] r:= Morph.new."
r extent: 500@500.
r openInWindow. 

p := PolygonMorph 
        vertices: {(r left)@(r top). (r right)@(r bottom)}
        color: Color white borderWidth: 2 borderColor: Color white.

r addMorph: p.

How can I get 'p' to rescale ?

bye

like image 770
Nicola Mingotti Avatar asked Jul 30 '19 10:07

Nicola Mingotti


1 Answers

This may not be the complete answer you are looking for, but sharing what I did, might help you to make some progress with your interesting problem.

  1. I'm not a Squeak (or Pharo) user, so I downloaded Squeak from the web.
  2. Pasted your code in a Workspace and executed it.
  3. Reproduced the behavior you observed: resizing the rectangle has no effect on its polygon sub-morph.
  4. Popped up the rectangle menu and saw the 'add halo' item.
  5. Wrote 'add halo' somewhere and right-clicked to bring all methods with that literal.
  6. Found #addHalo: as the associated message to the menu item.
  7. Inserted a halt to debug #addHalo:.
  8. Cmd+clicked to bring the halo and debugged until I found that the resize handle would send addGrow:with: to the morph.
  9. Then I saw that addGrow:with: sends setExtentFromHalo: and that this sends extent:.

My conclusion is that you would need a new subclass of RectangleMorph that resizes all its sub-morphs, proportionally, when it receives setExtentFromHalo:. Something on the lines of

ScalableRectangleMorph >> #setExtentFromHalo: aPoint
  current := self extent.
  super setExtentFromHalo: aPoint.
  scale := self extent - current / current.
  self submorphsDo: [:m | m extent: (m extent * scale) rounded]

Give this a try (I haven't), and let us know how it went.

like image 121
Leandro Caniglia Avatar answered Nov 01 '22 19:11

Leandro Caniglia