Skip to content

Commit 6dfc04d

Browse files
committed
got edited cells to hightlight with red border
they do it even when there is no diagram drawn yet though, which is not really desirable behaviour (I think?). Anyhow, that will be revisited over the break.
1 parent 6da2b00 commit 6dfc04d

File tree

3 files changed

+91
-12
lines changed

3 files changed

+91
-12
lines changed

src/Demo/JS.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ module Demo.JS ( readInputState
44
, mkRandomInput
55
, sDrawButton
66
, printHighError
7+
, sCellsDiv
8+
, sNumCells
79
, printLowError
810
, cullErrors
11+
, mark
912
, sRandomButton
13+
, sLowerControls
1014
, drawList
1115
, placeValues
1216
, displayOutput
@@ -34,6 +38,7 @@ canvasXPadding = 1 :: Double
3438
canvasYPadding = 1 :: Double
3539
scaleMax = 100 :: Double
3640

41+
sLowerControls = select "#c"
3742
sNumCells = select "#numcells"
3843
sStartHead = select "#starthead"
3944
sCellGen = select "#generatenew"
@@ -52,6 +57,37 @@ sCellNum i = select (pack (template (cellMkName i)))
5257
++ n
5358
++ "\" type=\"text\" name=\"a\" /></div>"
5459

60+
mark :: (Bool, [Int]) -> IO ()
61+
mark (b,is) = markHead b >> unMarkCells >> markCells is
62+
63+
markHead :: Bool -> IO ()
64+
markHead b =
65+
if b
66+
then sHeadInput
67+
>>= setAttr "style" "border-color: red;"
68+
>> return ()
69+
else sHeadInput
70+
>>= setAttr "style" "border-color: black;"
71+
>> return ()
72+
73+
unMarkCells = do start <- pullVal sStartDiv
74+
size <- pullVal sSizeDiv
75+
let f a = select (pack ("#hey" ++ (show a)))
76+
>>= setAttr "style" "border-color: black;"
77+
>> return ()
78+
r i s = if i < s
79+
then f i >> r (i + 1) s
80+
else return ()
81+
r start (start + size)
82+
83+
markCells is = do let r :: [Int] -> IO ()
84+
r (i:is) = f i >> r is
85+
r [] = return ()
86+
f a = select (pack ("#hey" ++ (show a)))
87+
>>= setAttr "style" "border-color: red;"
88+
>> return ()
89+
r is
90+
5591
getGenInfo :: IO (Either String (Int, Int))
5692
getGenInfo =
5793
do start <- fmap unpack (sStartHead >>= getVal)

src/Demo/Links.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Demo.Links ( parseInput
33
, findScale
44
, scaleMap
55
, nmap
6+
, mismatches
67
, mkLayout ) where
78

89
import Text.Read (readMaybe)
910
import System.Random
1011
import Demo.Types
11-
import qualified Data.Map as M (empty, lookup)
12+
import qualified Data.Map as M (Map, empty, lookup)
1213
import qualified Data.List as L (delete, length)
1314

1415
-- Config!
@@ -20,6 +21,21 @@ randomValueRange = ('A','Z')
2021
for later.
2122
-}
2223

24+
mismatches :: InputState -> InputState -> (Bool,[Int])
25+
mismatches (InSt i1 s1 h1 m1) (InSt i2 s2 h2 m2) =
26+
let headChanged = h1 /= h2
27+
f = (matchIndex m1 m2)
28+
in (headChanged, (foldr
29+
f
30+
[]
31+
[i1 .. (i1 + s1 - 1)] ))
32+
33+
matchIndex :: M.Map Int Cell -> M.Map Int Cell -> Int -> [Int] -> [Int]
34+
matchIndex c b i is = let f m = (fmap snd (M.lookup i m))
35+
in if (f c) == (f b)
36+
then is
37+
else i:is
38+
2339
{- There are two implemented functions for Step:
2440
1. arrow: "we're looking for an arrow next"
2541
2. box: "we're looking for a box next"

src/LinkedListDemo.hs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE CPP, OverloadedStrings #-}
1+
{-# LANGUAGE CPP, OverloadedStrings, ForeignFunctionInterface #-}
22

33
import Reactive.Banana
44
import Reactive.Banana.Frameworks
@@ -14,11 +14,19 @@ defaultHeadIndex = 23
1414
defaultNumCells = 14
1515

1616
main = do initializePage (defaultHeadIndex, defaultNumCells)
17-
(draw, rando, gener) <- mkSources
17+
(draw, rando, gener, clicks, keys) <- mkSources
1818
wireButton draw sDrawButton (cullErrors >> readInputState)
19-
wireButton rando sRandomButton (cullErrors >> mkRandomInput)
20-
wireButton gener sCellGen (cullErrors >> generateCells)
21-
n <- compile (mkNetwork (draw, rando, gener))
19+
wireButton rando sRandomButton (cullErrors
20+
>> mkCanvas
21+
>>mkRandomInput
22+
>> readInputState)
23+
wireButton
24+
gener
25+
sCellGen
26+
(cullErrors >> generateCells >> readInputState)
27+
wireClicks clicks
28+
wireKeys keys readInputState
29+
n <- compile (mkNetwork (draw, rando, gener, clicks, keys))
2230
actuate n
2331

2432
generateCells :: IO ()
@@ -39,7 +47,10 @@ initializePage (start,size) =
3947
>> writeInputState (emptyInput start size)
4048
>> mkCanvas
4149

42-
50+
wireClicks _ = return () -- maybe implement this later?
51+
wireKeys (addHandler, fire) f = do let handler _ = f >>= fire
52+
box <- sLowerControls
53+
keyup handler def box
4354

4455
wireButton (addHandler, fire) button f = do
4556
let handler _ = f >>= fire
@@ -49,21 +60,37 @@ wireButton (addHandler, fire) button f = do
4960
mkSources = do a <- newAddHandler
5061
b <- newAddHandler
5162
c <- newAddHandler
52-
return (a,b,c)
63+
d <- newAddHandler
64+
e <- newAddHandler
65+
return (a,b,c,d,e)
5366

5467
addHandler = fst
5568
fire = snd
5669

57-
mkNetwork (drawSource, randomSource, genSource) = do
70+
mkNetwork (drawSource, randomSource, genSource, clickSource, keySource) = do
5871
eDraws <- fromAddHandler (addHandler drawSource)
5972
eRandoms <- fromAddHandler (addHandler randomSource)
60-
let eInputs = eDraws `union` eRandoms
61-
--bInputState :: Behavior t InputState
73+
eGens <- fromAddHandler (addHandler genSource)
74+
eKeys <- fromAddHandler (addHandler keySource)
75+
let eResets = eRandoms `union` eGens
76+
eInputs = eRandoms `union` eGens `union` eKeys
77+
--bInputState :: Behavior t InputState
6278
bInputState = stepper (emptyInput 5 20) eInputs
63-
79+
eDrawnInputState = bInputState <@ eDraws
80+
bLastInputState = stepper (emptyInput 5 20)
81+
(eDrawnInputState `union` eResets)
82+
bDirty = mismatches <$> bInputState <*> bLastInputState
83+
cIn <- changes bInputState
84+
cLIn <- changes bLastInputState
85+
cDirty <- changes bDirty
86+
87+
6488
--reactimate' $ fmap (\is -> process is) eISChanged
6589
--reactimate' <$> (fmap (fmap process) eISChanged)
6690
reactimate (fmap (\a -> mkCanvas >> process a) eDraws)
91+
reactimate' $ fmap (\d -> mark d >> print (show d) >> return ()) <$> cDirty
92+
reactimate' $ fmap (\d -> print ("InputState: " ++ show d)) <$> cIn
93+
reactimate' $ fmap (\d -> print ("LastState: " ++ show d)) <$> cLIn
6794
--reactimate (fmap (\_ -> fmap process bInputState) eDraws)
6895

6996
process :: InputState -> IO ()

0 commit comments

Comments
 (0)