Skip to content

Commit

Permalink
Merge branch 'experimental/animations'
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdaugherty committed Jan 3, 2025
2 parents 36fc157 + ebf255e commit 304e9e5
Show file tree
Hide file tree
Showing 6 changed files with 907 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Feature Overview
* Progress bar widget
* Simple dialog box widget
* Border-drawing widgets (put borders around or in between things)
* Animation support
* Generic scrollable viewports and viewport scroll bars
* General-purpose layout control combinators
* Extensible widget-building API
Expand Down
24 changes: 23 additions & 1 deletion brick.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ library
hs-source-dirs: src
exposed-modules:
Brick
Brick.Animation
Brick.AttrMap
Brick.BChan
Brick.BorderMap
Expand Down Expand Up @@ -100,6 +101,7 @@ library
Brick.Widgets.Table
Data.IMap
other-modules:
Brick.Animation.Clock
Brick.Types.Common
Brick.Types.TH
Brick.Types.EventM
Expand Down Expand Up @@ -128,7 +130,10 @@ library
deepseq >= 1.3 && < 1.6,
unix-compat,
bytestring,
word-wrap >= 0.2
word-wrap >= 0.2,
unordered-containers,
hashable,
time

executable brick-custom-keybinding-demo
if !flag(demos)
Expand Down Expand Up @@ -455,6 +460,23 @@ executable brick-list-vi-demo
mtl,
vector

executable brick-animation-demo
if !flag(demos)
Buildable: False
hs-source-dirs: programs
ghc-options: -threaded -Wall -Wcompat -O2
default-language: Haskell2010
main-is: AnimationDemo.hs
build-depends: base,
brick,
vty,
vty-crossplatform,
containers,
text,
microlens-platform,
stm,
mtl

executable brick-custom-event-demo
if !flag(demos)
Buildable: False
Expand Down
8 changes: 8 additions & 0 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,14 @@ When creating new widgets, if you would like ``joinBorders`` and
so by consulting the ``ctxDynBorders`` field of the rendering context
before writing to your ``Result``'s ``borders`` field.

Animations
==========

Brick provides animation support in ``Brick.Animation``. See the Haddock
documentation in that module for a complete explanation of the API; see
``programs/AnimationDemo.hs`` (``brick-animation-demo``) for a working
example.

The Rendering Cache
===================

Expand Down
223 changes: 223 additions & 0 deletions programs/AnimationDemo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RankNTypes #-}
module Main where

import Control.Monad (void)
import Lens.Micro.Platform
import Data.List (intersperse)
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import qualified Data.Map as M
import qualified Graphics.Vty as V
import Graphics.Vty.CrossPlatform (mkVty)

import Brick.BChan
import Brick.Util (fg)
import Brick.Main (App(..), neverShowCursor, customMain, halt)
import Brick.AttrMap (AttrName, AttrMap, attrMap, attrName)
import Brick.Types (Widget, EventM, BrickEvent(..), Location(..))
import Brick.Widgets.Border (border)
import Brick.Widgets.Center (center)
import Brick.Widgets.Core ((<+>), str, vBox, hBox, hLimit, vLimit, translateBy, withDefAttr)
import qualified Brick.Animation as A

data CustomEvent =
AnimationUpdate (EventM () St ())
-- ^ The state update constructor required by the animation API

data St =
St { _stAnimationManager :: A.AnimationManager St CustomEvent ()
-- ^ The animation manager that will run all of our animations
, _animation1 :: Maybe (A.Animation St ())
, _animation2 :: Maybe (A.Animation St ())
, _animation3 :: Maybe (A.Animation St ())
, _clickAnimations :: M.Map Location (A.Animation St ())
-- ^ The various fields for storing animation states. For mouse
-- animations, we store animations for each screen location that
-- was clicked.
}

makeLenses ''St

drawUI :: St -> [Widget ()]
drawUI st = drawClickAnimations st <> [drawAnimations st]

drawClickAnimations :: St -> [Widget ()]
drawClickAnimations st =
drawClickAnimation st <$> M.toList (st^.clickAnimations)

drawClickAnimation :: St -> (Location, A.Animation St ()) -> Widget ()
drawClickAnimation st (l, a) =
translateBy l $
A.renderAnimation (const $ str " ") st (Just a)

drawAnimations :: St -> Widget ()
drawAnimations st =
let animStatus label key a =
str (label <> ": ") <+>
maybe (str "Not running") (const $ str "Running") a <+>
str (" (Press " <> key <> " to toggle)")
statusMessages = statusMessage <$> zip [(0::Int)..] animations
statusMessage (i, (c, config)) =
animStatus ("Animation #" <> (show $ i + 1)) [c]
(st^.(animationTarget config))
animationDrawings = hBox $ intersperse (str " ") $
drawSingleAnimation <$> animations
drawSingleAnimation (_, config) =
A.renderAnimation (const $ str " ") st (st^.(animationTarget config))
in vBox [ str "Click and drag the mouse or press keys to start animations."
, str " "
, vBox statusMessages
, animationDrawings
]

clip1 :: A.Clip a ()
clip1 = A.newClip_ $ str <$> [".", "o", "O", "^", " "]

clip2 :: A.Clip a ()
clip2 = A.newClip_ $ str <$> ["|", "/", "-", "\\"]

clip3 :: A.Clip a ()
clip3 =
A.newClip_ $
(hLimit 9 . vLimit 9 . border . center) <$>
[ border $ str " "
, border $ vBox $ replicate 3 $ str $ replicate 3 ' '
, border $ vBox $ replicate 5 $ str $ replicate 5 ' '
]

mouseClickClip :: A.Clip a ()
mouseClickClip =
A.newClip_
[ withDefAttr attr6 $ str "0"
, withDefAttr attr5 $ str "O"
, withDefAttr attr4 $ str "o"
, withDefAttr attr3 $ str "*"
, withDefAttr attr2 $ str "~"
, withDefAttr attr1 $ str "."
]

attr6 :: AttrName
attr6 = attrName "attr6"

attr5 :: AttrName
attr5 = attrName "attr5"

attr4 :: AttrName
attr4 = attrName "attr4"

attr3 :: AttrName
attr3 = attrName "attr3"

attr2 :: AttrName
attr2 = attrName "attr2"

attr1 :: AttrName
attr1 = attrName "attr1"

attrs :: AttrMap
attrs =
attrMap V.defAttr
[ (attr6, fg V.white)
, (attr5, fg V.brightYellow)
, (attr4, fg V.brightGreen)
, (attr3, fg V.cyan)
, (attr2, fg V.blue)
, (attr1, fg V.black)
]

-- | Animation settings grouped together for lookup by keystroke.
data AnimationConfig =
AnimationConfig { animationTarget :: Lens' St (Maybe (A.Animation St ()))
, animationClip :: A.Clip St ()
, animationFrameTime :: Integer
, animationMode :: A.RunMode
}

animations :: [(Char, AnimationConfig)]
animations =
[ ('1', AnimationConfig animation1 clip1 1000 A.Loop)
, ('2', AnimationConfig animation2 clip2 100 A.Loop)
, ('3', AnimationConfig animation3 clip3 100 A.Once)
]

-- | Start the animation specified by this config.
startAnimationFromConfig :: AnimationConfig -> EventM () St ()
startAnimationFromConfig config = do
mgr <- use stAnimationManager
A.startAnimation mgr (animationClip config)
(animationFrameTime config)
(animationMode config)
(animationTarget config)

-- | If the animation specified in this config is not running, start it.
-- Otherwise stop it.
toggleAnimationFromConfig :: AnimationConfig -> EventM () St ()
toggleAnimationFromConfig config = do
mgr <- use stAnimationManager
mOld <- use (animationTarget config)
case mOld of
Just a -> A.stopAnimation mgr a
Nothing -> startAnimationFromConfig config

-- | Start a new mouse click animation at the specified location if one
-- is not already running there.
startMouseClickAnimation :: Location -> EventM () St ()
startMouseClickAnimation l = do
mgr <- use stAnimationManager
a <- use (clickAnimations.at l)
case a of
Just {} -> return ()
Nothing -> A.startAnimation mgr mouseClickClip 100 A.Once (clickAnimations.at l)

appEvent :: BrickEvent () CustomEvent -> EventM () St ()
appEvent e = do
case e of
-- A mouse click starts an animation at the click location.
VtyEvent (V.EvMouseDown col row _ _) ->
startMouseClickAnimation (Location (col, row))

-- If we got a character keystroke, see if there is a specific
-- animation mapped to that character and toggle the resulting
-- animation.
VtyEvent (V.EvKey (V.KChar c) [])
| Just aConfig <- lookup c animations ->
toggleAnimationFromConfig aConfig

-- Apply a state update from the animation manager.
AppEvent (AnimationUpdate act) -> act

VtyEvent (V.EvKey V.KEsc []) -> halt

_ -> return ()

theApp :: App St CustomEvent ()
theApp =
App { appDraw = drawUI
, appChooseCursor = neverShowCursor
, appHandleEvent = appEvent
, appStartEvent = return ()
, appAttrMap = const attrs
}

main :: IO ()
main = do
chan <- newBChan 10
mgr <- A.startAnimationManager 50 chan AnimationUpdate

let initialState =
St { _stAnimationManager = mgr
, _animation1 = Nothing
, _animation2 = Nothing
, _animation3 = Nothing
, _clickAnimations = mempty
}
buildVty = do
v <- mkVty V.defaultConfig
V.setMode (V.outputIface v) V.Mouse True
return v

initialVty <- buildVty
void $ customMain initialVty buildVty (Just chan) theApp initialState
Loading

0 comments on commit 304e9e5

Please sign in to comment.