020
イメージ・オブジェクトに Drag and Drop
バージョン2.9 から変わった新しいスクリプト
Web用 revlet には使えません(バージョン4.0現在)。
dragEnter, dragDrop, acceptDrop, dragData
formattedWidth, formattedHeight
private function, private command
set the acceptDrop to true
put the dragData["files"] into tFileList
put the formattedWidth of image 1 into tWidth
put the formattedHeight of image 1 into tHeight
ドラッグ&ドロップが使えるのはイメージだけではないけれど、イメージの考え方を取得すれば、後はほとんど同じです。カード上にイメージ・オブジェクトをひとつを作り、その上にイメージ・ファイルをデスクトップからドラッグ&ドロップしてみてください。何度やっても、何の変化もありません。
revTalk では、まずオブジェクトがドラッグ&ドロップを受け入れるかどうかのプロパティ「acceptDrop」と、そのためのハンドラー「dragEnter」が必要です。
イメージ・オブジェクトに以下を書き込みます。実際のスタックではカード内の方が良いかもしれませんが、ここではイメージ・オブジェクト内に全てのスクリプトを書き込みます。
on dragEnter
set the acceptDrop to true
pass dragEnter
end dragEnter
ここで同じように、イメージ・ファイルをデスクトップからドラッグ&ドロップしてみてください。今度はイメージ・オブジェクトの上で、カーソルが ドラッグ&ドロップ用に変わります。それ以上の事は何も起こりません。
次に必要なハンドラー「dragDrop」と、プロパティ「dragData["files"] 」を
同じイメージ・オブジェクト内に書き込んで
on dragDrop
put the dragData["files"]
end dragDrop
イメージ・ファイルをデスクトップからドラッグ&ドロップすると、今度は
/Users/kenjikojima/Desktop/dragTest.jpg
のような、ファイルパスを得る事ができます。まあ、これで筋書きは読めた事でしょう。このファイルパスに従って、目的のイメージ・オブジェクトに、イメージ・ファイルをセットします。
ハンドラー「dragDrop」のスクリプトを書き換えます。
on dragDrop
put the dragData["files"] into tFileList
if tFileList is not empty then
set the fileName of me to (line 1 of tFileList)
else
beep
exit to top -- ハンドラーから抜ける
end if
end dragDrop
スペル違いがなけれどば、イメージ・ファイルが目的のイメージ・オブジェクトにセットされます。
ですが、ここで、問題がひとつ起こります。もしイメージ・オブジェクトがロックされていない状態だと、イメージ・ファイルの元々のサイズで、カード上にイメージが表示されてしまいます。またはイメージ・オブジェクトがロックされている状態では、正しいイメージのプロポーションで表示されません。
その前に、上で「line 1 of tFileList」としている事を説明しておきます。the dragData["files"] は、ひとつのファイルだけでなく、複数のファイルのファイルパスを return をデリミタとして、得る事ができます。もしドラッグ&ドロップでファイルを3つオブジェクト上に運んでいたら、3行のファイルパスを得る事ができます。後は必要に応じて、ファイルを割り振るなり、answer メッセージを出すということになりますね。
イメージサイズの調整は、プロパティ「formattedWidth」と「formattedHeight」で、セットされたイメージ・オブジェクトの、オリジナルのイメージ・ファイルの幅と高さを知る事ができますから、ヨコまたはタテのサイズの最大値で統一したい場合、
private function adjustImageSIze pMaxSize -- pMaxSIze はヨコ、タテどちらかの最大サイズ
put the formattedWidth of me into tWidth
put the formattedHeight of me into tHeight
if tWidth >= tHeight then
-- オリジナル・イメージはヨコ位置か正方形
return pMaxSize & comma & tHeight * pMaxSize div tWidth
else
-- オリジナル・イメージはタテ位置
return tWidth * pMaxSize div tHeight & comma & pMaxSize
end if
end adjustImageSIze
で、オリジナル・イメージの比例に合わせた、ヨコ、タテのサイズを得る事ができますから、
以下がサイズ調整のスクリプトです。
get adjustImageSIze(400) -- 最大サイズを 400 とした
set the width of me to item 1 of it
set the Height of me to item 2 of it
-- カードの中央にイメージを置く
set the loc of me to the width of this cd div 2 & comma & the height of this cd div 2
「adjustImageSIze」を「private function」としているのは、revTalk では同じオブジェクト内(コントロール内のローカル)で使われるファンクションを、「private function」とすることができ、これによってスクリプトの処理スピードが早くなります。同じように「private command」と言う使い方もできます。
private command someLocalCommand
-- do something
end someLocalCommand
private function someLocalFunction
-- do something
end someLocalFunction
-- 以下がイメージ・オブジェクト内に書き込んだ、通しのスクリプトです。
-- 実際には、イメージ以外のファイルがドロップされた場合のスクリプト等も必要です。
on dragEnter
set the acceptDrop to true
pass dragEnter
end dragEnter
on dragDrop
put the dragData["files"] into tFileList
if tFileList is not empty then
set the fileName of me to (line 1 of tFileList)
else
beep
exit to top
end if
get adjustImageSIze(400)
set the width of me to item 1 of it
set the Height of me to item 2 of it
set the loc of me to the width of this cd div 2 & comma & the height of this cd div 2
end dragDrop
private function adjustImageSIze pMaxSize
put the formattedWidth of me into tWidth
put the formattedHeight of me into tHeight
if tWidth >= tHeight then
return pMaxSize & comma & tHeight * pMaxSize div tWidth
else
return tWidth * pMaxSize div tHeight & comma & pMaxSize
end if
end adjustImageSIze