言語ゲーム

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

Twitter: @propella

ThingLab で計算機を作る

http://d.hatena.ne.jp/propella/20030805#p1 の続き

http://www.2share.com/thinglab/ThingLab%20-%20Chapter%202.html
を教科書に、 Second Example をみながら。
この ThingLab は壊れているらしく、テキストどおりに行きません。特に入力機能を直すには、

'From Squeak 2.3 of January 14, 1999 on 6 August 2003 at 11:05:00 am'!!TextEditor methodsFor: 'tool protocol' stamp: 'tak 8/6/2003 10:26'!positionAttacher: position offset: offset
    "if the current attacher should merge with a part in the selection,
        start up its text editor."
    | i textView |
    i _ 1.
    [selectionPath == nil and: [i <= stickyParts size]] whileTrue:
        [((stickyParts at: i) enclosingFrame containsPoint: attacher computedLocation)
          ifTrue:
            [selectionPath _ stickyPaths at: i.
            selectionPart _ stickyParts at: i.
            stickyParts _ stickyParts copyWithout: selectionPart.
            stickyPaths _ stickyPaths copyWithout: selectionPath]
          ifFalse: [i _ i + 1]].
    selectionPath == nil ifTrue: [^self].
    "temporary kludge - ask the edited thing what sort of beast it is, and open an editor
     accordingly.  This should instead be done by sending it an 'edit' message."
    (selectionPart isKindOf: TextThing) ifTrue:
        ["start up the text editor in the sticky part of the selection"
        editedView copyFromBuffer.
        textView _ PluggableTextView new on: self text: #text accept: #acceptText:from: readSelection: nil  menu: #textMenu.
        textView controller paragraph textStyle alignment: selectionPart alignment.
        textView borderWidth: 2.
        textView window: (selectionPart frame translateBy: editedView insetDisplayBox origin).
        textView display.
        textView controller startUp.
        textView release.
        ^self].
    (selectionPart isKindOf: BitImage) ifTrue:
        [selectionPart form edit.
        ^self].
    selectionPart inspect! !

を file in します。

  • 摂氏から華氏への変換器を作る。
    • クラス TemperatureConverter を作成(第1ペイン右クリック - Define new thing - TemperatureConverter と入力)
    • TemperatureConverter - picture - insert - Times で掛け算を挿入
    • 挿入する時、右(出力)ノード、本体、左ノードの順でクリックして位置決め。(エラーが出るが何とか無視しながら)
    • 同じく insert - Plus で足し算を挿入
    • 挿入時掛け算の出力と足し算の入力をくっつくようにしておきますが、後で marge - Node を使ってくっつけても構いません。
    • 追加した演算子は、3つの Node の複合体になっています。教科書とは違って、Lead という要素はありません。
    • 数値を入れるまでは演算が出来ないというようなエラーが出ますが、今は無視
    • insert - Constraint で二つの定数ボックスをそれぞれ * と + にくっつける
    • edit - TextThing を押してから、教科書の Figure 2.15 のように定数に数値を入れます。
    • 教科書ではここで、この部品を元に別のクラスを作る事になっているのですが、早く動作確認をしたいので計算機を完成させてしまいます。
    • insert - Printer で二つのテキストボックスを両端にくっつけます。これで完成。
    • 例えば edit - TextThing で左端のボックスに100を入れると、右に 212.0 と出ます。逆に、右に100 と入れると 37.777 と反対の計算も出来てしまいます。この逆向きの計算がスゴイ!
    • 教科書の Figure 2.18 のように、テキストボックスの変わりに温度計をつけるには、insert - Themometer を使います。
    • 温度計を動かすのは move - Point を使います。
    • ここで、Constant や Printer を編集するのがなぜ TextThing で、Themometer を動かすのが Point なのか理解できなかったり覚えられないと思いますが、その時は、試しに edit - Constant 等としてみます。
    • するとインスペクタというツールが開き、Constant が Printer で出来ている事が分かります(printer を押すと a Printer と書いてある)。さらに Printer の文字を右クリックして inspect を選ぶと、Printer は TextThing で出来ている事が分かります。