简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM