@@ -14,18 +14,22 @@ defaultHeadIndex = 23
14
14
defaultNumCells = 14
15
15
16
16
main = do initializePage (defaultHeadIndex, defaultNumCells)
17
- (draw, rando, gener, clicks, keys) <- mkSources
18
- wireButton draw sDrawButton (cullErrors >> readInputState)
17
+
18
+ -- init and connect reactive event-sources
19
+ (draw, rando, gener, clicks, keys) <- mkSources
20
+ wireButton draw sDrawButton (cullErrors
21
+ >> readInputState)
19
22
wireButton rando sRandomButton (cullErrors
20
23
>> mkCanvas
21
- >> mkRandomInput
24
+ >> mkRandomInput
22
25
>> readInputState)
23
- wireButton
24
- gener
25
- sCellGen
26
- (cullErrors >> generateCells >> readInputState)
27
- wireClicks clicks
26
+ wireButton gener sCellGen (cullErrors
27
+ >> generateCells
28
+ >> readInputState)
29
+ wireClicks clicks -- does nothing for now
28
30
wireKeys keys readInputState
31
+
32
+ -- build and "actuate" the reactive event network
29
33
n <- compile (mkNetwork (draw, rando, gener, clicks, keys))
30
34
actuate n
31
35
@@ -36,6 +40,8 @@ generateCells =
36
40
Right (start,size) -> initializePage (start,size)
37
41
Left err -> printHighError err
38
42
43
+ {- Tests number of cells and starting index to make sure they're valid.
44
+ Readability as Ints was already checked for in JS.hs code -}
39
45
checkGenInfo :: (Int ,Int ) -> Either String (Int ,Int )
40
46
checkGenInfo (i,s)
41
47
| i < 0 = Left " Starting Index cannot be negative"
@@ -48,50 +54,76 @@ initializePage (start,size) =
48
54
>> mkCanvas
49
55
50
56
wireClicks _ = return () -- maybe implement this later?
57
+
58
+ -- Events responding to keypresses in the memory editing area
51
59
wireKeys (addHandler, fire) f = do let handler _ = f >>= fire
52
60
box <- sLowerControls
53
61
keyup handler def box
54
62
63
+ -- convenience function for wiring all the buttons
55
64
wireButton (addHandler, fire) button f = do
56
65
let handler _ = f >>= fire
57
66
b <- button
58
67
click handler def b
59
68
69
+ -- sources are in the IO monad, so we have to do this?
60
70
mkSources = do a <- newAddHandler
61
71
b <- newAddHandler
62
72
c <- newAddHandler
63
73
d <- newAddHandler
64
74
e <- newAddHandler
65
75
return (a,b,c,d,e)
66
76
77
+ -- convenience
67
78
addHandler = fst
68
79
fire = snd
69
80
70
- mkNetwork (drawSource, randomSource, genSource, clickSource, keySource) = do
81
+ {- And now the fun stuff, describes the reactive "signal graph" of
82
+ events and behaviors, mainly used here to control when cells
83
+ are highlighted to show that they have been edited -}
84
+ mkNetwork ( drawSource
85
+ , randomSource
86
+ , genSource
87
+ , clickSource
88
+ , keySource ) = do
89
+
71
90
eDraws <- fromAddHandler (addHandler drawSource)
72
91
eRandoms <- fromAddHandler (addHandler randomSource)
73
92
eGens <- fromAddHandler (addHandler genSource)
74
93
eKeys <- fromAddHandler (addHandler keySource)
75
- let eResets = eRandoms `union` eGens
94
+
95
+ let -- some useful collections of event-sources
96
+ eResets = eRandoms `union` eGens
76
97
eInputs = eRandoms `union` eGens `union` eKeys
77
- -- bInputState :: Behavior t InputState
78
- bInputState = stepper (emptyInput 5 20 ) eInputs
79
- eDrawnInputState = bInputState <@ eDraws
80
- bLastInputState = stepper (emptyInput 5 20 )
81
- (eDrawnInputState `union` eResets)
98
+
99
+ -- convenience for processing LastInputStates
100
+ bNothing :: Behavior t (Maybe InputState )
101
+ bNothing = pure Nothing
102
+ -- clicking 'draw' should only count if it actually draws
103
+ bTest :: Behavior t (InputState -> Maybe InputState )
104
+ bTest = pure (\ inState -> case parseInput inState of
105
+ Left _ -> Nothing
106
+ Right _ -> Just inState)
107
+
108
+ bInputState = stepper (emptyInput 5 20 ) eInputs
109
+ bLastInputState =
110
+ stepper Nothing
111
+ ((bTest <@> eDraws) `union` (bNothing <@ eResets))
82
112
bDirty = mismatches <$> bInputState <*> bLastInputState
113
+
83
114
cIn <- changes bInputState
84
115
cLIn <- changes bLastInputState
85
116
cDirty <- changes bDirty
86
117
87
-
88
- -- reactimate' $ fmap (\is -> process is) eISChanged
89
- -- reactimate' <$> (fmap (fmap process) eISChanged)
118
+ -- Draw the list!
90
119
reactimate (fmap (\ a -> mkCanvas >> process a) eDraws)
91
- reactimate' $ fmap (\ d -> mark d >> print (show d) >> return () ) <$> cDirty
120
+ -- Mark the "dirty" edited cells (or unmark them if clean)
121
+ reactimate' (fmap (\ d -> mark d >> return () ) <$> cDirty)
122
+
123
+ -- (These are for debugging purposes and print only to the console)
92
124
reactimate' $ fmap (\ d -> print (" InputState: " ++ show d)) <$> cIn
93
125
reactimate' $ fmap (\ d -> print (" LastState: " ++ show d)) <$> cLIn
94
- -- reactimate (fmap (\_ -> fmap process bInputState) eDraws)
126
+
95
127
96
128
process :: InputState -> IO ()
97
129
process = displayOutput . fmap mkLayout . parseInput
0 commit comments