言語ゲーム

とあるエンジニアが嘘ばかり書く日記

Twitter: @propella

レイアウトの自作

モルフの中にさらにサブモルフを追加する際。レイアウトを指定するとサブモフルを自動整列させる事が出来る。自動整列の仕方を決めるにはレイアウトを作成する。

シンプルな例

'From Squeak3.5 of ''11 April 2003'' [latest update: #5180] on 27 July 2003 at 2:29:21 pm'!
LayoutPolicy subclass: #GLayout
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'GGame-SmaCC'!
!GLayout commentStamp: 'tak 7/27/2003 13:25' prior: 0!
sample

m
m _ EllipseMorph new extent: 400 @ 100. m openInHand. m layoutPolicy: GLayout new. 3 timesRepeat: [m addMorph: EllipseMorph new]. ! !GLayout methodsFor: 'layout' stamp: 'tak 7/27/2003 14:23'! layout: aMorph in: newBounds "Compute the layout for the given morph based on the new bounds" | size y spacing subWidth | size _ aMorph submorphs size. size = 1 ifTrue: [aMorph submorphs first center: aMorph center. ^ self]. "Calculate the total width of submorphs" subWidth _ aMorph submorphs inject: 0 into: [:subTotal :next | subTotal + next width]. subWidth < aMorph width ifTrue: [spacing _ aMorph width - subWidth / (size - 1)] ifFalse: [aMorph width: subWidth. spacing _ 0]. y _ newBounds center y. aMorph submorphs inject: aMorph left into: [:posX :morph | morph top: (y - (morph height / 2)) asInteger. morph left: posX asInteger. posX + morph width + spacing]! !

LayoutPolicy を継承し、layout:in: を実装する事によってレイアウトを作成する事ができる。layout:in: で行う事は、ある範囲を与えられた時に指定したモルフの位置を調整する事だ。座標に分数を使うとエラーが出てしまうので注意。