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

时间:2017-10-01 11:02:24

标签: colors draw 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]
]

但是,当我选择一种颜色时,区域中的所有线条都会改变颜色。如何修改以使早期的线保持相同的颜色?

2 个答案:

答案 0 :(得分:4)

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
]

在这里,我们拥有管理绘图设置所需的一切:

图片 - 图纸本身(在绘制方言中)。

颜色 - 当前的笔颜色。

使用 - 一种更改当前颜色并将其应用于绘图的功能。

重置 - 清除图纸。

画布

我们的画布将是一个简单的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操作上启动新行外,这应该按照示例中的参与功能工作。

操作

我们的'清除'按钮使用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]
]

答案 1 :(得分:1)

最小的例子如下所示

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