4

Is it possible to hide the cursor using Gloss in Haskell? We want to replace the player with the mouse position.

4

1 回答 1

1

Internal这个功能不会被光泽暴露:如果模块被暴露(通过使用例如playWithBackendIO和一些自定义实例Backend来隐藏光标),它会更容易实现。如果你真的想要这个功能,最好fork 项目并添加你想要的功能。


有一些不修改原始包的解决方法:当使用任何...IO函数时,我们可以在传入的函数中执行任意IO操作,这允许我们修改后端的状态。

为简单起见,我将在displayIO这里使用,但这适用于任何模式,例如playIO等。

import Prelude hiding (init)
import Control.Monad
import Data.IORef
import Data.StateVar
import Graphics.Gloss
import Graphics.Gloss.Interface.IO.Display
import qualified Graphics.UI.GLUT.Window as Glut

init :: IO ()
init = Glut.cursor $= Glut.None

render :: IORef Bool -> IO Picture
render doneInit = do
    needsInit <- not <$> readIORef doneInit
    when needsInit $ do
        init
        writeIORef doneInit True
    return $ color white $ circle 30

controllerCallback :: Controller -> IO ()
controllerCallback _ = return ()

main :: IO ()
main = do
    let disp = InWindow "test" (800, 600) (0, 0)
    initVar <- newIORef False
    displayIO disp black (render initVar) controllerCallback

最重要的部分是,它使用 StateVar 的函数Glut.cursor $= Glut.None设置 GLUT 的值并自动更新 glut 上下文。我们只想运行一次,所以我们使用 an来跟踪我们之前是否运行过它。cursor$=IORef

最后,这不适用于 GLFW 后端。光泽使用一个非常旧的 GLFW-b 版本,它不支持与光标模式有关的任何事情更现代的版本可以,但我无法找到获取Window实例的方法,因为 Gloss 只是将其丢弃。

于 2018-12-06T13:55:09.947 回答