如何在 Rebol/Red 中保留前几行的颜色

How to keep colors of previous lines in Rebol/Red

我正在尝试在修改后的绘图程序中绘制不同颜色的线条:

REBOL [title: "Paint"]
mycol: black
view layout [ size 1100x700
    s: area 1060x600 white feel [
        engage: func [f a e] [
            if a = 'over [append s/effect/draw e/offset  show s]
            if a = 'up [append s/effect/draw 'line]
        ]
    ] effect [draw [pen mycol line]]
    across
    btn "Clear" 100x50 [s/effect/draw: copy [line] show s]
    btn "Red" 100x50 [set [to-word mycol] red]
    btn "blue" 100x50 [set [to-word mycol] blue]
    btn "magenta" 100x50 [set [to-word mycol] magenta]
    btn "green" 100x50 [set [to-word mycol] green]
    btn "yellow" 100x50 [set [to-word mycol] yellow]
    btn "orange" 100x50 [set [to-word mycol] orange]
    btn "Quit" 100x50 [quit]
]

但是,当我select一种颜色时,区域中的所有线条都会改变颜色。我如何修改才能使前面的行保持相同的颜色?

一个最小的例子看起来像这样

view layout [
    s: area white feel [
        engage: func [f a e] [
            if a = 'over [append s/effect/draw e/offset  show s]
            if a = 'up [append s/effect/draw 'line]
        ]
    ] effect [draw [pen blue line]]
    btn "Clear" [s/effect/draw: copy [line] show s]
    btn "Red" [append s/effect/draw [pen red line]]
]

所以你的脚本应该是

view layout [ size 1100x700
    s: area 1060x600 white feel [
        engage: func [f a e] [
            if a = 'over [append s/effect/draw e/offset  show s]
            if a = 'up [append s/effect/draw 'line]
        ]
    ] effect [draw [pen black line]]
    across
    btn "Clear" 100x50 [s/effect/draw: copy [line] show s]
    btn "Red" 100x50 [append s/effect/draw [pen red line]]
    btn "blue" 100x50 [append s/effect/draw [pen blue line]]
    btn "magenta" 100x50 [append s/effect/draw [pen magenta line]]
    btn "green" 100x50 [append s/effect/draw [pen gree line]]
    btn "yellow" 100x50 [append s/effect/draw [pen yellow line]]
    btn "orange" 100x50 [append s/effect/draw [pen orange line]]
    btn "Quit" 100x50 [quit]
]

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 — 改变当前颜色并将其应用于绘图的函数。

重置 — 清除绘图。

Canvas

我们的 canvas 将是一个包含绘图的简单 BOX 面:

box 1060x600 white
effect reduce ['draw drawing/image]

并将对通过 Engage 函数传递的 downover 操作作出反应:

feel [
    engage: func [face action event] [
        switch action [
            down [append drawing/image 'line]
            over [append drawing/image event/offset show face]
        ]
    ]
]

(我已将此处的参与参数更改为它们的全名——Rebol/Red 使用单字母单词几乎没有提高效率并且失去了很多表现力)

除了在 down 操作上开始新行外,这应该按照您示例中的参与功能工作。

操作

我们的 'Clear' 按钮使用 drawing 对象并重置 canvas(按钮的最老兄弟):

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]
]