aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Toaster.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-04-15 22:59:15 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-04-15 22:59:15 +0100
commit7b3aa61462dc5a1135e01beedcbc98efe47ffb13 (patch)
tree7969d4f80f2640f9b51690eb853993131ad48454 /src/Game/Toaster.hs
parent631c611d929ea8fb633fdaa285485b6dbd5db702 (diff)
downloadspace-plat-hs-7b3aa61462dc5a1135e01beedcbc98efe47ffb13.tar.gz
space-plat-hs-7b3aa61462dc5a1135e01beedcbc98efe47ffb13.zip
"Toaster" notifications
To provide feedback to the user when a controller is plugged/unplugged.
Diffstat (limited to 'src/Game/Toaster.hs')
-rw-r--r--src/Game/Toaster.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/Game/Toaster.hs b/src/Game/Toaster.hs
new file mode 100644
index 0000000..e3fbc69
--- /dev/null
+++ b/src/Game/Toaster.hs
@@ -0,0 +1,56 @@
+module Game.Toaster
+ ( Toaster,
+ mkToaster,
+ add,
+ update,
+ render,
+ )
+where
+
+import qualified Game.BitmapFont as BM
+import qualified SDL
+
+toastDelay :: Int
+toastDelay = 128
+
+data Toast = Toast
+ { message :: String,
+ y :: Int,
+ delay :: Int
+ }
+
+data Toaster = Toaster
+ { font :: BM.BitmapFont,
+ gameHeight :: Int,
+ queue :: [String],
+ current :: Maybe Toast
+ }
+
+mkToaster :: BM.BitmapFont -> Int -> IO Toaster
+mkToaster font height = do
+ pure $ Toaster font height [] Nothing
+
+add :: Toaster -> String -> Toaster
+add t message =
+ t {queue = t.queue ++ [message]}
+
+update :: Toaster -> Toaster
+update t = case t.current of
+ Nothing -> case t.queue of
+ [] -> t
+ (next : _) -> t {current = Just $ Toast next t.gameHeight toastDelay, queue = tail t.queue}
+ -- FIXME: magic number
+ Just toast -> t {current = updateToast (t.gameHeight - 14) toast}
+ where
+ updateToast :: Int -> Toast -> Maybe Toast
+ updateToast height toast
+ | toast.y > height && toast.delay > 0 = Just $ toast {y = toast.y - 1}
+ | toast.delay > 0 = Just $ toast {delay = toast.delay - 1}
+ | toast.y < t.gameHeight = Just $ toast {y = toast.y + 1}
+ | otherwise = Nothing
+
+render :: SDL.Renderer -> Toaster -> IO ()
+render renderer t = case t.current of
+ Nothing -> pure ()
+ Just toast -> do
+ BM.renderTextSolid renderer t.font 4 toast.y toast.message