module MessageWindow ( MessageWindow, pause_msgwin, clear_msgwin, write_msg ) where import Color import Curses import Window -- Position is the coordinate of the lower right-hand corner. newtype MessageWindow w = MW w -- Allow basic access to the underlying Window instance (Window w) => Window (MessageWindow w) where new_window topl botr = do win <- new_window topl botr return (MW win) window_size (MW w) = window_size w refresh_window (MW w) = refresh_window w destroy_window (MW w) = destroy_window w touch_window (MW w) = touch_window w clear_window (MW w) = clear_window w move_cursor (MW w) = move_cursor w get_cursor_position (MW w) = get_cursor_position w prompt :: String prompt = " -->>" -- Prompt, wait for input, then wipe the message window. -- place_cursor is a function that puts the cursor where it should -- be while waiting for input. (This is usually the player's position -- on the map.) pause_msgwin :: (TextWindow w) => MessageWindow w -> IO () -> IO () pause_msgwin mw@(MW win) place_cursor = do write_string win Text prompt refresh_window win place_cursor get_key clear_msgwin mw place_cursor -- The player has seen the messages, so it's okay to wipe them. clear_msgwin :: (Window w) => MessageWindow w -> IO () -> IO () clear_msgwin (MW win) place_cursor = do (x, y) <- get_cursor_position win if x == 0 && y == 0 then return () -- Avoid a lot of IO actions else do clear_window win move_cursor win (0, 0) refresh_window win place_cursor -- Write a string to the message window. If the string won't fit, then -- prompt and pause before making room for it. -- This function assumes that the string won't be longer than one line. write_msg :: (TextWindow w) => MessageWindow w -> IO () -> String -> IO () write_msg mw@(MW win) place_cursor msg = do let (maxx, maxy) = window_size win (x, y) <- get_cursor_position win let msgstr = if x == 0 then msg else (' ' : msg) if y == maxy && x + length msgstr + length prompt > maxx then pause_msgwin mw place_cursor else return () -- FIXME: word-wrap write_string win Text msgstr refresh_window win place_cursor