From d6804f95fbb6df77ee4b4c0ab383e43732989a81 Mon Sep 17 00:00:00 2001 From: nek0 Date: Mon, 4 May 2020 11:00:49 +0200 Subject: [PATCH] update screenmode toggle function --- examples/example01/Main.hs | 2 +- src/Affection/Util.hs | 46 ++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/examples/example01/Main.hs b/examples/example01/Main.hs index 167fd6f..760de35 100644 --- a/examples/example01/Main.hs +++ b/examples/example01/Main.hs @@ -103,7 +103,7 @@ pre ud = do toggleFullScreen :: KeyboardMessage -> Affection () toggleFullScreen (MsgKeyboardEvent _ _ SDL.Pressed False sym) - | SDL.keysymKeycode sym == SDL.KeycodeF11 = toggleScreen + | SDL.keysymKeycode sym == SDL.KeycodeF11 = toggleScreen 0 | otherwise = return () toggleFullScreen _ = return () diff --git a/src/Affection/Util.hs b/src/Affection/Util.hs index 04d68ff..d0c5e0b 100644 --- a/src/Affection/Util.hs +++ b/src/Affection/Util.hs @@ -11,6 +11,7 @@ import qualified Graphics.Rendering.OpenGL as GL import System.Clock import Data.String (fromString) +import Data.List (find) import Control.Monad.State @@ -35,24 +36,35 @@ getDelta = gets deltaTime -- | Toggle the Screen mode between 'SDL.Windowed' and 'SDL.FullscreenDesktop'. -- Pauses the Engine in the process. -toggleScreen :: Affection () -toggleScreen = do +toggleScreen :: Word -> Affection () +toggleScreen windowIdent = do ad <- get - newMode <- case screenMode ad of - SDL.Windowed -> do - mapM_ (flip SDL.setWindowMode SDL.FullscreenDesktop . (\(_,y,_) -> y)) (drawWindows ad) - return SDL.FullscreenDesktop - SDL.FullscreenDesktop -> do - mapM_ (flip SDL.setWindowMode SDL.Windowed . (\(_,y,_) -> y)) (drawWindows ad) - return SDL.Windowed - x -> do - liftIO $ logIO Warn ("Unexpected Screen mode: " <> fromString (show x)) - return x - now <- liftIO $ getTime Monotonic - put ad - { sysTime = now - , screenMode = newMode - } + let mwindow = find (\(ident, _, _) -> ident == windowIdent) (drawWindows ad) + case mwindow of + Just (ident, window, mode) -> do + newMode <- case mode of + SDL.FullscreenDesktop -> do + SDL.setWindowMode window SDL.Windowed + return SDL.Windowed + SDL.Windowed -> do + SDL.setWindowMode window SDL.FullscreenDesktop + return SDL.FullscreenDesktop + x -> do + liftIO $ logIO Warn ("Unexpected window mode: " <> fromString (show x)) + return x + now <- liftIO $ getTime Monotonic + put ad + { sysTime = now + , drawWindows = map + (\e@(lid, win, mod) -> + if lid == ident + then (lid, win, newMode) + else e + ) + (drawWindows ad) + } + Nothing -> do + liftIO $ logIO Warn ("No window found with ident " <> fromString (show windowIdent)) -- | Fit the GL Viewport to Window size fitViewport