Haskell,单击鼠标更改绘图颜色
我想制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
getWindowEvent
将阻塞直到下一个事件发生,因此所有内容都只在事件上绘制。如果您认为窗口事件触发得太频繁,那么您可以将事件打印到标准输出以找出触发了哪个事件并忽略它(例如,跳过除 Button 事件之外的所有事件的绘制)。顺便说一句:你不需要 IORef,你可以通过循环传递当前颜色。
(代码没有经过编译器测试,所以......)
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.
(The code is not tested with the compiler, so...)
感谢您的回答。
我根据我对代码应该做什么的理解稍微修改了代码。
但结果有点粗略。有时颜色会立即改变,有时则需要很长时间。我相信这可能是一些更新问题,因为当我关闭窗口时,我看到在窗口消失之前发生了颜色更改。
有什么想法吗?
Thanks for the answer.
I modified the code slightly according to my understanding of what it should do.
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?
如果我在绘制新东西之前调用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.