Haskell,单击鼠标更改绘图颜色

发布于 2024-09-30 22:36:08 字数 1044 浏览 0 评论 0原文

我想制作一个 haskell 程序,在窗口中绘制一些形状。当我在窗口内单击时,形状的颜色应该改变。

我想出了这个:

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        colorRef <- newIORef Green
        let 
            loop0 = do
                        color <- readIORef colorRef
                        e <- getWindowEvent w
                        case e of
                            Button {pt=pt, isDown=isDown}
                                | isDown && color == Green -> writeIORef colorRef Red
                                | isDown && color == Red -> writeIORef colorRef Green
                            _ -> return ()
                        color <- readIORef colorRef
                        drawInWindow w (withColor color (polyline points))
                        loop0

        color <- readIORef colorRef
        drawInWindow w (withColor color (polyline points))
        loop0

它有点管用。 问题是,我认为窗口事件几乎总是被触发,因此所有内容都一直被绘制,这使得速度变慢。 我怎样才能做到只有在注册点击时才更改绘图?

I want to make a haskell program where some shape is drawn in a window. When I click inside the window the color of the shape should change.

I have come up with this:

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        colorRef <- newIORef Green
        let 
            loop0 = do
                        color <- readIORef colorRef
                        e <- getWindowEvent w
                        case e of
                            Button {pt=pt, isDown=isDown}
                                | isDown && color == Green -> writeIORef colorRef Red
                                | isDown && color == Red -> writeIORef colorRef Green
                            _ -> return ()
                        color <- readIORef colorRef
                        drawInWindow w (withColor color (polyline points))
                        loop0

        color <- readIORef colorRef
        drawInWindow w (withColor color (polyline points))
        loop0

It kinda works.
The problem is, that I think that a window event is triggered almost all the time, so everything is drawn all the time which makes it slow.
How could I make it so, that I only change the drawing when a click is registered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2024-10-07 22:36:08

首先,getWindowEvent 将阻塞直到下一个事件发生,因此所有内容都只在事件上绘制。如果您认为窗口事件触发得太频繁,那么您可以将事件打印到标准输出以找出触发了哪个事件并忽略它(例如,跳过除 Button 事件之外的所有事件的绘制)。

顺便说一句:你不需要 IORef,你可以通过循环传递当前颜色。

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        let 
            loop0 color = do
                        e <- getWindowEvent w
                        let newColor = case e of
                                         Button {pt=pt, isDown=isDown}
                                           | isDown && color == Green -> Red
                                           | isDown && color == Red -> Green
                                         _ -> color
                        when (newColor != color) (drawInWindow w (withColor color (polyline points)))
                        loop0 color

        let color = Red
        drawInWindow w (withColor color (polyline points))
        loop0 color

(代码没有经过编译器测试,所以......)

First of all, getWindowEvent will block until the next event occurs, so everything is drawn only on event. If you think that a window event is triggered too often, then you can print events to the stdout to figure out what event is triggered and just ignore it (e.g. skip drawing on all the events except Button event).

BTW: you don't IORef, you can just pass the current color through the loop.

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        let 
            loop0 color = do
                        e <- getWindowEvent w
                        let newColor = case e of
                                         Button {pt=pt, isDown=isDown}
                                           | isDown && color == Green -> Red
                                           | isDown && color == Red -> Green
                                         _ -> color
                        when (newColor != color) (drawInWindow w (withColor color (polyline points)))
                        loop0 color

        let color = Red
        drawInWindow w (withColor color (polyline points))
        loop0 color

(The code is not tested with the compiler, so...)

木格 2024-10-07 22:36:08

感谢您的回答。
我根据我对代码应该做什么的理解稍微修改了代码。

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        let 
            loop0 color = do
                        e <- getWindowEvent w
                        let newColor = case e of
                                         Button {pt=pt, isDown=isDown}
                                           | isDown && color == Green -> Red
                                           | isDown && color == Red -> Green
                                         _ -> color
                        when (newColor /= color) (drawInWindow w (withColor newColor (polyline points)))
                        loop0 newColor
        let color = Green
        drawInWindow w (withColor color (polyline points))
        loop0 color

但结果有点粗略。有时颜色会立即改变,有时则需要很长时间。我相信这可能是一些更新问题,因为当我关闭窗口时,我看到在窗口消失之前发生了颜色更改。
有什么想法吗?

Thanks for the answer.
I modified the code slightly according to my understanding of what it should do.

testDemo points =
runGraphics $
    do 
        w <- openWindow "Test" (480, 550)
        let 
            loop0 color = do
                        e <- getWindowEvent w
                        let newColor = case e of
                                         Button {pt=pt, isDown=isDown}
                                           | isDown && color == Green -> Red
                                           | isDown && color == Red -> Green
                                         _ -> color
                        when (newColor /= color) (drawInWindow w (withColor newColor (polyline points)))
                        loop0 newColor
        let color = Green
        drawInWindow w (withColor color (polyline points))
        loop0 color

The results are a bit sketchy though. Sometimes the color changes immediately and sometimes it takes a very long time. I believe it could be some update issue, because when I close a window I see an issued color-change happen just before the window is gone.
Any ideas?

夏尔 2024-10-07 22:36:08

如果我在绘制新东西之前调用clearWindow,这会有所帮助。我真的不明白为什么。它可能会安排重绘窗口吗?
很高兴知道,但总的来说,问题现在已经解决了。

It helps if I call clearWindow before I draw the new stuff. I don't really understand why. Does it schedule a redraw of the window maybe?
Would be nice to know, but in general the problem is solved for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文