Draw dialect 是对绘图的累积描述。在您的示例中,您只设置了一次笔颜色,之后的所有线条都继承了所述颜色。当您使用单词mycol 设置笔颜色时,一旦面部显示更新(上面代码中的show s),您的所有线条都会设置为该单词所指的任何颜色。
可以在这里稍微分解一下以了解一些操作:
绘图
让我们用当前颜色在它自己的对象中开始绘制。
drawing: make object! [
image: []
color: black
use: func [new [tuple!]][
append image reduce ['pen color: new]
]
reset: does [
clear image
use color
]
reset
]
这里我们拥有管理绘图设置所需的一切:
image — 绘图本身(在 Draw 方言中)。
color — 当前笔的颜色。
use — 改变当前颜色并将其应用于绘图的函数。
reset — 清除绘图。
画布
我们的画布将是一个包含绘图的简单 BOX 面:
box 1060x600 white
effect reduce ['draw drawing/image]
并且将对通过Engage 函数传递的down 和over 动作做出反应:
feel [
engage: func [face action event] [
switch action [
down [append drawing/image 'line]
over [append drawing/image event/offset show face]
]
]
]
(我已将此处的参与参数更改为它们的全名——在 Rebol/Red 中使用单字母词几乎没有提高效率,也失去了很多表现力)
这应该按照您示例中的参与函数工作,除了在 down 操作上开始新行。
动作
我们的“清除”按钮接合drawing 对象并重置画布(按钮的最旧兄弟):
btn "Clear" 100x50 [
drawing/reset
show first face/parent-face/pane
]
只是为了一点界面糖,我们将使用切换来指示当前颜色。您可以使用 of 关键字在切换之间创建相互关系:
tog of 'color "Red" 100x50 [drawing/use red]
tog of 'color "Blue" 100x50 [drawing/use blue]
tog of 'color "Magenta" 100x50 [drawing/use magenta]
tog of 'color "Green" 100x50 [drawing/use green]
tog of 'color "Yellow" 100x50 [drawing/use yellow]
tog of 'color "Orange" 100x50 [drawing/use orange]
把它放在一起
可以将其封装在脚本中:
Rebol [Title: "Paint"]
drawing: make object! [
image: []
color: black
use: func [new [tuple!]][
append image reduce ['pen color: new]
]
reset: does [
clear image
use color
]
reset
]
view layout [
box 1060x600 white
effect reduce ['draw drawing/image]
feel [
engage: func [face action event] [
switch action [
down [append drawing/image 'line]
over [append drawing/image event/offset show face]
]
]
]
across
btn "Clear" 100x50 [drawing/reset show face/parent-face/pane/1]
tog of 'color "Red" 100x50 [drawing/use red]
tog of 'color "Blue" 100x50 [drawing/use blue]
tog of 'color "Magenta" 100x50 [drawing/use magenta]
tog of 'color "Green" 100x50 [drawing/use green]
tog of 'color "Yellow" 100x50 [drawing/use yellow]
tog of 'color "Orange" 100x50 [drawing/use orange]
btn "Quit" 100x50 [unview]
]