레이아웃이 프로그래밍 방식으로 구축 될 때 레볼 이벤트에 대처하는 방법은 무엇입니까?
문제
사진 갤러리를 동적으로 빌드하는 방법을 알고 있습니다. 사용자가 사진을 클릭 할 때 어떻게 이벤트를 트리거 할 수 있습니까?
나는 레이아웃 내부에 참여 기능을 원하지 않지만 외부 : 가능합니까?
확실하지 않아서 말 해주세요.
해결책
매우 쉬운 트릭은 원하는 액션으로 스타일을 설정하고 필요한 경우 얼굴을 생성 할 때 설정 한 사용자 데이터 값을 보도록하는 것입니다.
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
다른 팁
말했듯이, 가장 좋은 것은 레이아웃에서 코드를 최소화하기 위해 스타일을 만드는 것입니다. 따라서 스타일은 이미 원하는 동작에 대해 구성되었습니다.
사진 갤러리의 경우 요구 사항이있는 엄지 스타일을 만들 것입니다.
그러나 간단한 것들을 위해 'funct를 사용 할 필요는 없습니다! 필요한 것이 왼쪽/마우스 오른쪽 클릭 인 경우 하나 또는 두 개의 동작 블록으로 충분하고 훨씬 간단합니다.
다음은 전체 예입니다.
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
이를 수행하는 방법에는 여러 가지가 있습니다. 사진 갤러리를 만드는 방법을 지정하지는 않지만 이미지 스타일 레이아웃을 구축 한 다음 해당 레이아웃을 표시한다고 가정합니다.
또한 각 이미지로 특정한 일을 할 수있는 자유를 원한 것 같습니다. 이미지에서 파생 된 별도의 스타일을 구축하는 것이 좋습니다. 당신은 다음과 같이 할 수 있습니다.
stylize/master [
image: image with [
feel: make feel [
engage: func [face act event] [
; do my custom engage function
]
]
]
]
레이아웃 전에 코드를 넣으십시오. 이렇게하면 레이아웃 블록 외부에서 이미지의 동작에 대한 복잡한 코드를 유지할 수 있습니다. 이런 식으로 작업하면 스타일이 전 세계적으로 변경됩니다.
이름을 변경하여 새로운 스타일을 만들 수도 있습니다.
stylize/master [
image2: image with [
...
]
]
이미지는 손대지 않으며 레이아웃에서 image2를 사용할 수 있습니다.
왜 스타일/마스터? 나는 습관에서 스타일/마스터를 사용하므로 레이아웃에서 특정 스타일 목록을 지정할 필요가 없으며 각 레이아웃에 대해 코드 라인을 면도 할 수 있습니다.
다시 시도해 봅시다 :
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