レイアウトはプログラムで構築されたときにどのようにREBOLのイベントに対処するには?
質問
私はどのように私は今、ユーザーが画像をクリックしたときにイベントをトリガすることができますか?
、動的に写真ギャラリーを構築する方法を知っています私は、レイアウト内ではなく外部の機能を従事したくない:それは可能です。
?わからない私は明確なので、私に教えています。
解決
はとても簡単なトリックは、あなたが望むアクションとスタイルを設定し、必要に応じて顔を生成するとき、それはあなたが設定したユーザー・データの値を確認することです。
lay: [
across
style my-box box [print [face/user-data face/color]]
]
repeat i 4 [
repeat j 4 [
repend lay [
'my-box 50x50 get random/only [red green blue yellow]
'user-data to pair! reduce [i j]
]
]
append lay 'return
]
view layout lay
他のヒント
、最高のは、レイアウトのコードを最小化するスタイルを作成することです。 だから、スタイルは、すでにあなたが望むアクション用に設定されます。
画像ギャラリーのために、私は私の要件に親指のスタイルを作成することになります。
しかし、あなたは「シンプルなもののために、FUNCに係合使用する必要はありません!何が必要なの右/左クリック取り扱いの場合は、1つまたは2アクションブロックが十分であり、はるかに簡単ます。
ここでは完全な例である:
Rebol [
title: "Basic image gallery"
]
thumb-size: 100x100 ; size of the thumbnails
thumbs-per-row: 6 ; number of thumbs per row
; Here is the actions I want to use when clicking/right clicking the image face.
; It's just a block: it's the 'layout function that will make it a function
; with 'face and 'value arguments
thumb-action: [
; left click: show full size image
view/new layout [
origin 2 space 2
vh3 form (either any [file? face/user-data url? face/user-data text? face/user-data] [face/user-data] ["An image without reference"])
image face/image
]
]
thumb-alt-action: [
; right click: open up the folder/web site where the file is
switch/default type?/word face/user-data [
file! [call/shell join "explorer " to-local-file first split-path face/user-data]
url! [browse face/user-data]
] [alert rejoin ["Can't do anything with " type? face/user-data " type of value ! Sorry."]]
]
; Our styles for the gallery
gallery-styles: probe stylize [
; Here is a style for the thumbnails, with the actions wanted
thumb: image thumb-size effect [aspect] thumb-action thumb-alt-action
]
; Some samples images
imgs: [
; This paths are for a typical Windows7 installation
%/c/windows/web/wallpaper/nature/img1.jpg
%/c/windows/web/wallpaper/nature/img2.jpg
%/c/windows/web/wallpaper/nature/img3.jpg
%/c/windows/web/wallpaper/nature/img4.jpg
; URLs as examples
http://www.rebol.com/graphics/reb-logo.gif
http://www.rebol.com/graphics/ref-card.jpg
]
; Base for your gallery layout
gallery-lay: copy [
origin 2 space 2 across
styles gallery-styles
vh2 "Image gallery"
]
; Builds the final layout
count: 0
foreach img imgs [
; This for handling only a defined number of thumbs per row
if 0 = (count // thumbs-per-row) [append gallery-lay 'return]
count: count + 1
; Here you add the layout code for the current image
append gallery-lay compose [thumb (img) user-data (img)]
]
; Here we are: the result
view layout gallery-lay
これを行うには多くの方法があります。あなたは絵のギャラリーを構築する方法を指定しませんが、私はあなたがIMAGEスタイルのレイアウトを構築し、そのレイアウトを表示していることを仮定しています。
あなたが各画像に特定の物事を行うには自由にしたいようにそれはまた聞こえるので、私はあなたがおそらくIMAGE由来の別々のスタイルを構築示唆しています。あなたはこのようにそれを行うことができます:
stylize/master [
image: image with [
feel: make feel [
engage: func [face act event] [
; do my custom engage function
]
]
]
]
レイアウト前にコードを入れてください。このように、あなたは、レイアウトブロックの外の画像の行動のための複雑なコードを維持することができます。あなたはこのように動作すると、スタイルがグローバルに変更されます。
また、単に名前を変更することによって、新しいスタイルを作成することができます:
stylize/master [
image2: image with [
...
]
]
あなたのレイアウトでIMAGE2を使用することができますしながら、IMAGEは、放置されます。
なぜスタイライズ/ MASTER?私は習慣のうち、スタイライズ/ MASTERを使用するので、私は、レイアウト内の特定のスタイルのリストを指定する必要はありませんし、私は各レイアウトのためのコードの行をオフに剃ることができます。
のは、それを再度試してみましょう。
lay: [
across
style my-box box [print [face/user-data face/color]]
]
repeat i 4 [
repeat j 4 [
repend lay [
'my-box 50x50 get random/only [red green blue yellow]
'user-data to pair! reduce [i j]
]
]
append lay 'return
]
view layout lay