Created
July 13, 2017 22:07
-
-
Save pepeiborra/f09cdb1b556d8fdb3e95d5ac547cd1d2 to your computer and use it in GitHub Desktop.
Hoodlums meetup 13 Jul 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: lights | |
version: 0.1.0.0 | |
-- synopsis: | |
-- description: | |
license: BSD3 | |
license-file: LICENSE | |
copyright: All Rights Reserved | |
category: Web | |
build-type: Simple | |
extra-source-files: README.md | |
cabal-version: >=1.10 | |
executable lights | |
hs-source-dirs: app | |
main-is: Main.hs | |
ghc-options: -threaded -rtsopts -with-rtsopts=-N | |
build-depends: base | |
, async | |
, containers | |
, threepenny-gui | |
default-language: Haskell2010 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE PartialTypeSignatures#-} | |
import Prelude hiding (lookup) | |
import Control.Concurrent | |
import Control.Concurrent.Async | |
import Control.Monad (void) | |
import Data.List (isPrefixOf) | |
import Data.Maybe | |
import Data.Monoid | |
import Graphics.UI.Threepenny as UI | |
import Graphics.UI.Threepenny.Core hiding (delete) | |
import Text.Printf | |
{----------------------------------------------------------------------------- | |
Main | |
------------------------------------------------------------------------------} | |
main :: IO () | |
main = void run | |
run = do | |
triggerV <- newEmptyMVar | |
async $ startGUI defaultConfig{jsStatic= Just "static"} (setup triggerV) | |
takeMVar triggerV | |
setup :: MVar _ -> Window -> UI _ | |
setup triggerV window = | |
void $ do | |
addStyleSheet window "style.css" | |
t <- timer # set interval 40 | |
start t | |
(timeFunctionEvent, trigger) <- liftIO newEvent | |
liftIO $ putMVar triggerV trigger | |
timeFunctionValue <- accumB (\fn n -> "black") timeFunctionEvent | |
frame <- accumB 0 (const succ <$> tick t) | |
let colors = ["red", "green", "yellow", "blue"] | |
let colorValue n = (\f t -> f t n) <$> timeFunctionValue <*> frame | |
getBody window #+ | |
[ grid | |
[ [ createLight (colorValue (row + col * 10)) | |
| col <- [0..3] | |
] | |
| row <- [0..9] | |
] | |
] | |
where | |
createLight :: Behavior String -> UI Element | |
createLight color = | |
let style = | |
(\v -> | |
[ ("background-color", v) | |
]) <$> | |
color | |
in new # UI.sink UI.style style #. "light" | |
rgb :: Integer -> Integer -> Integer -> String | |
rgb r g b = printf "#%02x%02x%02x" (r `mod` 256) (g `mod` 256) (b `mod` 256) | |
gray x = rgb x x x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment