ansi-terminal-game 1.1.1.0 → 1.2.0.0
raw patch · 12 files changed
+103/−55 lines, 12 filesdep ~linebreakPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: linebreak
API changes (from Hackage documentation)
- Terminal.Game: paperPlane :: Plane -> String
+ Terminal.Game: data Hyphenator
+ Terminal.Game: english_GB :: Hyphenator
+ Terminal.Game: english_US :: Hyphenator
+ Terminal.Game: esperanto :: Hyphenator
+ Terminal.Game: french :: Hyphenator
+ Terminal.Game: german_1996 :: Hyphenator
+ Terminal.Game: italian :: Hyphenator
+ Terminal.Game: planePaper :: Plane -> String
+ Terminal.Game: spanish :: Hyphenator
+ Terminal.Game: textBoxHyphen :: Hyphenator -> Width -> Height -> String -> Plane
+ Terminal.Game: textBoxHyphenLiquid :: Hyphenator -> Width -> String -> Plane
- Terminal.Game: box :: Char -> Width -> Height -> Plane
+ Terminal.Game: box :: Width -> Height -> Char -> Plane
- Terminal.Game: textBox :: String -> Width -> Height -> Plane
+ Terminal.Game: textBox :: Width -> Height -> String -> Plane
- Terminal.Game: textBoxLiquid :: String -> Width -> Plane
+ Terminal.Game: textBoxLiquid :: Width -> String -> Plane
- Terminal.Game: type Column = Integer
+ Terminal.Game: type Column = Int
- Terminal.Game: type Height = Integer
+ Terminal.Game: type Height = Int
- Terminal.Game: type Row = Integer
+ Terminal.Game: type Row = Int
- Terminal.Game: type Width = Integer
+ Terminal.Game: type Width = Int
Files
- ansi-terminal-game.cabal +4/−4
- changes.txt +19/−0
- example/Alone.hs +7/−7
- src/Terminal/Game.hs +16/−7
- src/Terminal/Game/Draw.hs +32/−11
- src/Terminal/Game/Layer/Imperative.hs +1/−1
- src/Terminal/Game/Layer/Object/IO.hs +1/−1
- src/Terminal/Game/Layer/Object/Interface.hs +1/−1
- src/Terminal/Game/Layer/Object/Narrate.hs +0/−6
- src/Terminal/Game/Plane.hs +12/−13
- test/Terminal/Game/DrawSpec.hs +8/−2
- test/Terminal/Game/PlaneSpec.hs +2/−2
ansi-terminal-game.cabal view
@@ -1,5 +1,5 @@ name: ansi-terminal-game-version: 1.1.1.0+version: 1.2.0.0 synopsis: sdl-like functions for terminal applications, based on ansi-terminal description: Library which aims to replicate standard 2d game@@ -15,7 +15,7 @@ license-file: LICENSE author: Francesco Ariis maintainer: fa-ml@ariis.it-copyright: © 2017-2020 Francesco Ariis+copyright: © 2017-2021 Francesco Ariis category: Game build-type: Simple extra-source-files: README,@@ -55,7 +55,7 @@ cereal == 0.5.*, clock >= 0.7 && < 0.9, exceptions == 0.10.*,- linebreak == 1.0.*,+ linebreak == 1.1.*, mintty == 0.1.*, mtl == 2.2.*, QuickCheck >= 2.13 && < 2.15,@@ -103,7 +103,7 @@ cereal == 0.5.*, clock >= 0.7 && < 0.9, exceptions == 0.10.*,- linebreak == 1.0.*,+ linebreak == 1.1.*, mtl == 2.2.*, QuickCheck >= 2.13 && < 2.15, random >= 1.1 && < 1.3,
changes.txt view
@@ -1,3 +1,22 @@+1.2.0.0+-------++- Added textBoxHyphe and textBoxHyphenLiquid and a handful of `Hypenator`s.+ This will allow you to have autohyphenation in textboxes. Compare:+ Rimasi un po’ a meditare nel buio Rimasi un po’ a meditare nel buio+ velato appena dal barlume azzurrino velato appena dal barlume azzurrino+ del fornello a gas, su cui sobbol- del fornello a gas, su cui+ liva quieta la pentola. sobbolliva quieta la pentola.+- Switched `Width`, `Height`, `Row`, `Col` from `Integer` to `Int`.+ This is unfortunate, but will make playing with `base` simpler. I will+ switch it back once `Prelude` handles both integers appropriately+ or exports the relevant function. (request by sm)+- Changed signature for `box`, `textBox` and `textBoxLiquid`. Now+ width/height parameters come *before* the character/string. E.g.:+ textBoxLiquid :: String -> Width -> Plane -- this was before+ textBoxLiquid :: Width -> String -> Plane -- this is now+ This felt more ergonomic while writing games.+ 1.1.1.0 -------
example/Alone.hs view
@@ -58,7 +58,7 @@ move _ 'd' = E move m _ = m -pos :: Move -> (Integer, Integer) -> (Integer, Integer)+pos :: Move -> (Width, Height) -> (Width, Height) pos m oldcs | oob newcs = oldcs | otherwise = newcs where@@ -82,12 +82,12 @@ drawFun :: MyState -> Plane drawFun (MyState (r, c) _ _) = blankPlane mw mh &- (1, 1) % box '_' mw mh &- (2, 2) % box ' ' (mw-2) (mh-2) &- (15, 20) % textBox "Tap WASD to move, tap again to stop."- 10 4 &- (20, 60) % textBox "Press Q to quit."- 8 10 # color Blue Vivid &+ (1, 1) % box mw mh '_' &+ (2, 2) % box (mw-2) (mh-2) ' ' &+ (15, 20) % textBox 10 4+ "Tap WASD to move, tap again to stop." &+ (20, 60) % textBox 8 10+ "Press Q to quit." # color Blue Vivid & (r, c) % cell '@' # invert where mh :: Height
src/Terminal/Game.hs view
@@ -21,14 +21,12 @@ -- todo test that handlers are closed in case of errr [test] -- todo invert # invert (doppio) si elimina? [test] [design] -- todo schermo piccolo, si centra verticalmente? [test]--- todo metti in a-t-g [design] la formula «menu, poi fai questo, poi--- ritorni, astrai questo pattern+-- todo metti in a-t-g la formula «menu, poi fai questo, poi+-- ritorni, astrai questo pattern [menu] -- todo timer in contrib o altro modulo?--- todo qptain e jimreed, implement normal movement--- todo [design] javascript backend -- todo controlla caratteri strani su windows, prova con morganw e ibispi--- todo boxratio, blitcenter e blit top left etc in ansi-terminal-game? [u:2]+-- todo boxratio, blit top left etc in ansi-terminal-game? [u:2] -- todo autoresize terminal (or get-term-size?) -- todo ranSelect :: [a] -> m a in a-t-g [design] -- todo random shuffle in atg?@@ -39,6 +37,7 @@ -- todo (da sm) I'm also wondering if I can adjust the frame rate during -- play, to speed things up -- todo (da sm) hot reload for game (see IHP web app) [suggestion]+-- todo (da sm) sound with sox module Terminal.Game ( -- * Running@@ -96,7 +95,7 @@ stringPlaneTrans, makeTransparent, makeOpaque,- paperPlane,+ planePaper, planeSize, -- ** Draw@@ -104,10 +103,19 @@ (%), (&), (#), subPlane, mergePlanes,- cell, word, box, textBox, textBoxLiquid,+ cell, word, box, Color(..), ColorIntensity(..), color, bold, invert, + -- *** Text boxes+ textBox, textBoxLiquid,+ textBoxHyphen, textBoxHyphenLiquid,+ Hyphenator,+ -- | Eurocentric convenience reexports. Check+ -- "Text.Hyphenation.Language" for more languages.+ english_GB, english_US, esperanto,+ french, german_1996, italian, spanish,+ -- *** Declarative drawing (|||), (===), (***), hcat, vcat, @@ -135,6 +143,7 @@ import Terminal.Game.Layer.Object as O import Terminal.Game.Plane import Terminal.Game.Random+import Text.LineBreak -- $threaded -- Good practices for cross-compatibility:
src/Terminal/Game/Draw.hs view
@@ -18,6 +18,7 @@ import qualified Data.List as L import qualified System.Console.ANSI as CA + ----------- -- TYPES -- -----------@@ -130,14 +131,14 @@ ------------- -- | A box of dimensions @w h@.-box :: Char -> Width -> Height -> Plane-box chr w h = seqCellsDim w h cells+box :: Width -> Height -> Char -> Plane+box w h chr = seqCellsDim w h cells where cells = [((r, c), chr) | r <- [1..h], c <- [1..w]] -- | A @1x1@ cell. cell :: Char -> Plane-cell ch = box ch 1 1+cell ch = box 1 1 ch -- | @1xn@ 'Plane' with a word in it. If you need to import multiline -- ASCII art, check 'stringPlane' and 'stringPlaneTrans'.@@ -152,28 +153,48 @@ -- (w, h) = pSize p -- | A text-box. Assumes @' '@s are transparent.-textBox :: String -> Width -> Height -> Plane-textBox cs w h = let pt = textBoxLiquid cs w+textBox :: Width -> Height -> String -> Plane+textBox w h cs = let pt = textBoxLiquid w cs in blankPlane w h F.& (1, 1) % pt -- | Like 'textBox', but tall enough to fit @String@.-textBoxLiquid :: String -> Width -> Plane-textBoxLiquid cs w = transparent+textBoxLiquid :: Width -> String -> Plane+textBoxLiquid w cs = textBoxGeneralLiquid Nothing w cs++-- | As 'textBox', but hypenated. Example:+--+-- @+-- Rimasi un po’ a meditare nel buio Rimasi un po’ a meditare nel buio+-- velato appena dal barlume azzurrino velato appena dal barlume azzurrino+-- del fornello a gas, su cui sobbol- del fornello a gas, su cui+-- liva quieta la pentola. sobbolliva quieta la pentola.+-- @+--+-- Notice how in the left box «sobbolliva» is broken in two. This+-- can be useful and aesthetically pleasing when textboxes are narrow.+textBoxHyphen :: Hyphenator -> Width -> Height -> String -> Plane+textBoxHyphen hp w h cs = let pt = textBoxHyphenLiquid hp w cs+ in blankPlane w h F.& (1, 1) % pt++-- | As 'textBoxLiquid', but hypenated.+textBoxHyphenLiquid :: Hyphenator -> Width -> String -> Plane+textBoxHyphenLiquid h w cs = textBoxGeneralLiquid (Just h) w cs++textBoxGeneralLiquid :: Maybe Hyphenator -> Width -> String -> Plane+textBoxGeneralLiquid mh w cs = transparent where -- hypenathion- hyp = Nothing -- Just english_GB- bf = BreakFormat (fromIntegral w) 4 '-' hyp+ bf = BreakFormat (fromIntegral w) 4 '-' mh hcs = breakStringLn bf cs h = L.genericLength hcs f :: [String] -> [(Coords, Char)] f css = concatMap (uncurry rf) (zip [1..] css)- where rf :: Integer -> String -> [(Coords, Char)]+ where rf :: Int -> String -> [(Coords, Char)] rf cr ln = zip (zip (repeat cr) [1..]) ln out = seqCellsDim w h (f hcs) transparent = makeTransparent ' ' out- ----------------- -- ANCILLARIES --
src/Terminal/Game/Layer/Imperative.hs view
@@ -35,7 +35,7 @@ gInitState :: s, -- ^Initial state of the game. gLogicFunction :: s -> Event -> s, -- ^Logic function. gDrawFunction :: s -> Plane, -- ^Draw function.- gQuitFunction :: s -> Bool -- ^\"Should I quit?\" function.+ gQuitFunction :: s -> Bool -- ^«Should I quit?» function. } -- | Entry point for the game execution, should be called in @main@.
src/Terminal/Game/Layer/Object/IO.hs view
@@ -124,7 +124,7 @@ blitPlane w h mp p = T.liftIO (blitPlaneIO w h mp p) shutdownDisplay = T.liftIO cleanAndExit -displaySizeIO :: IO (Integer, Integer)+displaySizeIO :: IO (Width, Height) displaySizeIO = TS.size >>= \ts -> -- cannot use ansi-terminal, on Windows you get
src/Terminal/Game/Layer/Object/Interface.hs view
@@ -83,7 +83,7 @@ class Monad m => MonadDisplay m where setupDisplay :: m () clearDisplay :: m ()- displaySize :: m (Integer, Integer) -- w, h+ displaySize :: m (Width, Height) -- w, h blitPlane :: Width -> Height -> Maybe Plane -> Plane -> m () shutdownDisplay :: m ()
src/Terminal/Game/Layer/Object/Narrate.hs view
@@ -34,12 +34,6 @@ show e Right r -> return r --- class Monad m => MonadInput m where--- startEvents :: FPS -> m (CC.MVar [Event], [CC.ThreadId])--- pollEvents :: CC.MVar [Event] ->--- m [Event]--- stopEvents :: [CC.ThreadId] -> m ()- instance MonadInput Narrate where startEvents fps = Narrate $ R.ask >>= \e ->
src/Terminal/Game/Plane.hs view
@@ -12,7 +12,6 @@ import Terminal.Game.Character import qualified Data.Array as A -import qualified Data.List as L import qualified Data.List.Split as LS import qualified Data.Tuple as T import qualified GHC.Generics as G @@ -25,13 +24,13 @@ -- | 'Row's and 'Column's are 1-based (top-left position is @1 1@). type Coords = (Row, Column) -type Row = Integer -type Column = Integer +type Row = Int +type Column = Int -- | Expressed in 'Column's. -type Width = Integer +type Width = Int -- | Expressed in 'Row's. -type Height = Integer +type Height = Int type Bold = Bool type Reversed = Bool @@ -167,8 +166,8 @@ -- | A String (@\n@ divided and ended) representing the 'Plane'. Useful -- for debugging/testing purposes. -paperPlane :: Plane -> String -paperPlane p = unlines . LS.chunksOf w . +planePaper :: Plane -> String +planePaper p = unlines . LS.chunksOf w . map cellChar . A.elems $ fromPlane p where w :: Int @@ -194,14 +193,14 @@ where lined = lines t - h :: Integer - h = L.genericLength lined + h :: Int + h = length lined - w :: Integer - w = maximum (map L.genericLength lined) + w :: Int + w = maximum (map length lined) - pad :: Integer -> String -> String - pad mw tl = take (fromIntegral mw) (tl ++ repeat ' ') + pad :: Int -> String -> String + pad mw tl = take mw (tl ++ repeat ' ') padded :: [String] padded = map (pad w) lined
test/Terminal/Game/DrawSpec.hs view
@@ -3,6 +3,7 @@ import Test.Hspec import Terminal.Game.Plane import Terminal.Game.Draw+import Terminal.Game -- language hyphenators spec :: Spec@@ -20,12 +21,17 @@ describe "textBox/textBoxLiquid" $ do let s = "las rana in Spa" w = 6- ps = textBox s w 2- pl = textBoxLiquid s w+ ps = textBox w 2 s+ pl = textBoxLiquid w s it "textBox follows specific size" $ planeSize ps `shouldBe` (6, 2) it "textBoxLiquid fits the whole string" $ planeSize pl `shouldBe` (6, 3)++ describe "textBoxHypen" $ do+ let tbh = textBoxHyphen spanish 8 2 "Con pianito"+ it "hyphens long words" $+ planePaper tbh `shouldSatisfy` (elem '-') describe "***" $ do let a = stringPlane ".\n.\n.\n"
test/Terminal/Game/PlaneSpec.hs view
@@ -11,7 +11,7 @@ spec = do let testPlane = blankPlane 2 2 &- (1,1) % box '.' 2 2 &+ (1,1) % box 2 2 '.' & (1,2) % cell ' ' describe "listPlane" $ do@@ -41,7 +41,7 @@ describe "subPlane" $ do let pa = word "prova" === word "fol" it "cuts out a plane" $- paperPlane (subPlane pa (1, 1) (2, 1)) `shouldBe` "p\nf\n"+ planePaper (subPlane pa (1, 1) (2, 1)) `shouldBe` "p\nf\n" it "does not crash on OOB" $ planeSize (subPlane pa (1, 1) (10, 10)) `shouldBe` (5, 2) it "errs on emptycell" $