packages feed

dear-imgui-impl-glfw-1.0.0: src/DearImGui/Impl/GLFW.hs

{-| "DearImGui.Raw.Impl.GLFW" over the @GLFW-b@ 'GLFW.Window' and
'GLFW.Monitor' handles.
-}
module DearImGui.Impl.GLFW
  ( windowPtr
  , monitorPtr
  , initForOpenGL
  , initForVulkan
  , initForOther
  , withInitForOpenGL
  , withInitForVulkan
  , withInitForOther
  , installCallbacks
  , restoreCallbacks
  , getContentScaleForWindow
  , getContentScaleForMonitor
  , setCallbacksChainForAllWindows
  , Raw.newFrame
  , Raw.shutdown
  , Raw.sleep
  ) where

import Bindings.GLFW (C'GLFWmonitor, C'GLFWwindow)
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Data.Text (Text)
import Foreign.C.Types (CBool)
import Foreign.Marshal.Utils (fromBool, toBool)
import Foreign.Ptr (Ptr, castPtr)
import Graphics.UI.GLFW qualified as GLFW
import Graphics.UI.GLFW.C qualified as GLFWC

import DearImGui.Impl.Init (withInitOrFail)
import DearImGui.Raw.Impl.GLFW qualified as Raw
import DearImGui.Raw.Impl.GLFW.GLFWmonitor (GLFWmonitor)
import DearImGui.Raw.Impl.GLFW.GLFWmonitor qualified as RawMonitor
import DearImGui.Raw.Impl.GLFW.GLFWwindow (GLFWwindow)
import DearImGui.Raw.Impl.GLFW.GLFWwindow qualified as RawWindow

windowPtr :: GLFW.Window -> Ptr GLFWwindow
windowPtr = castPtr . GLFWC.toC @(Ptr C'GLFWwindow)

monitorPtr :: GLFW.Monitor -> Ptr GLFWmonitor
monitorPtr = castPtr . GLFWC.toC @(Ptr C'GLFWmonitor)

initForOpenGL :: GLFW.Window -> Bool -> IO Bool
initForOpenGL = initWith RawWindow.initForOpenGL

initForVulkan :: GLFW.Window -> Bool -> IO Bool
initForVulkan = initWith RawWindow.initForVulkan

initForOther :: GLFW.Window -> Bool -> IO Bool
initForOther = initWith RawWindow.initForOther

withInitForOpenGL :: (MonadUnliftIO m) => GLFW.Window -> Bool -> m a -> m a
withInitForOpenGL = withInitWith "ImGui_ImplGlfw_InitForOpenGL" initForOpenGL

withInitForVulkan :: (MonadUnliftIO m) => GLFW.Window -> Bool -> m a -> m a
withInitForVulkan = withInitWith "ImGui_ImplGlfw_InitForVulkan" initForVulkan

withInitForOther :: (MonadUnliftIO m) => GLFW.Window -> Bool -> m a -> m a
withInitForOther = withInitWith "ImGui_ImplGlfw_InitForOther" initForOther

installCallbacks :: GLFW.Window -> IO ()
installCallbacks = RawWindow.installCallbacks . windowPtr

restoreCallbacks :: GLFW.Window -> IO ()
restoreCallbacks = RawWindow.restoreCallbacks . windowPtr

getContentScaleForWindow :: GLFW.Window -> IO Float
getContentScaleForWindow = RawWindow.getContentScaleForWindow . windowPtr

getContentScaleForMonitor :: GLFW.Monitor -> IO Float
getContentScaleForMonitor = RawMonitor.getContentScaleForMonitor . monitorPtr

setCallbacksChainForAllWindows :: Bool -> IO ()
setCallbacksChainForAllWindows = Raw.setCallbacksChainForAllWindows . fromBool

initWith :: (Ptr GLFWwindow -> CBool -> IO CBool) -> GLFW.Window -> Bool -> IO Bool
initWith rawInit window installCallbacksToo =
  toBool <$> rawInit (windowPtr window) (fromBool installCallbacksToo)

withInitWith :: (MonadUnliftIO m) => Text -> (GLFW.Window -> Bool -> IO Bool) -> GLFW.Window -> Bool -> m a -> m a
withInitWith name doInit window installCallbacksToo =
  withInitOrFail name (doInit window installCallbacksToo) Raw.shutdown