【问题标题】:How to keep colors of previous lines in Rebol/Red如何在 Rebol/Red 中保留前几行的颜色
【发布时间】:2017-10-01 11:02:24
【问题描述】:

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

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

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

【问题讨论】:

  • 这是rebol2吗? (谈论标签)
  • REBOL/view 2.7.8

标签: colors draw rebol red


【解决方案1】:

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 函数传递的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]
]

【讨论】:

    【解决方案2】:

    一个最小的例子是这样的

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

    【讨论】:

    • 你能简单介绍一下engageeffect。此外,添加文本的按钮不起作用。我正在尝试代码:btn "text" [append s/effect/draw ['text 100x100 "Graphics Plot" ]]。 (我有一段时间不能提出新问题了!)。
    • @rnso 关于Face EffectDraw 方言(与SVG 有点兼容)和Event Handling (engage) 的文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多