Comments (5)
@mohanr I'm afraid I'm struggling to understand exactly what your question is, but I'm going to interpret it as "how do I integrate gloss
events with reactive-banana
". If that's not question, let me know and I'll try again! Here is one suggestion:
main :: IO ()
main = do
-- Create an AddHandler that notifies us when gloss Events occur
(eventAddHandler, notifyEvent) <- newAddHandler
-- Create another AddHandler for when we want rendering to happen
(renderAddHandler, render) <- newAddHandler
picRef <- newIORef blank
actuate =<< compile (game picRef eventAddHandler renderAddHandler)
Gloss.playIO
(InWindow "Functional Reactive" (320, 240) (800, 200))
white
30
()
(\() -> render >> readIORef picRef)
(\ev _ -> notifyEvent ev)
(\_ () -> pure ())
game :: IORef Picture -> AddHandler Gloss.Event -> AddHandler () -> MomentIO ()
game picRef eventAddHandler renderAddHandler = do
onEvent <- fromAddHandler eventAddHandler
onSample <- fromAddHandler sampleAddHandler
-- Here we accumulate gloss events into a new world satte
worldState <- accumB .. ..
-- Whenever 'onRender' fires
reactimate $ writeIORef picRef . drawWorld <$> worldState <@ onRender
from reactive-banana.
I'm not really familiar with Gloss, so I can't give a good answer. Your code is a bit confusing, e.g. makeNewEvent
is defined, but not used. Could you try to distill a minimal example that does not work but should?
It looks like the AddHandler
in sources
is used by networkDescriptor
to create the network, but the arguments Gloss.playIO
use the other AddHandler
named eventHandler
that was created before — the two are talking past each other.
from reactive-banana.
This pattern wasn't clear. Hope it is right. This works.
This is just trapping the keys and restricting. Not sure if there can be multiple networkDescriptor
functions for each key.
The other way is to restrict the keys inside the networkDescriptor
.
picRef ← newIORef blank
network <- compile $ networkDescriptor picRef eventHandler
actuate network
let handleEvent e@(EventKey k Down _ _) = case k of
(SpecialKey KeySpace) -> event e
_ -> return ()
handleEvent e = return ()
Gloss.playIO
(InWindow "Functional Reactive" (320, 240) (800, 200))
white
30
()
(\() -> readIORef picRef)
(\ ev () -> handleEvent ev)
(\_ () -> pure ())
from reactive-banana.
@mohanr Was my response above helpful? Can we close this issue, or are you still stuck?
from reactive-banana.
game picRef eventAddHandler renderAddHandler = do
onEvent <- fromAddHandler eventAddHandler
onSample <- fromAddHandler sampleAddHandler
-- Here we accumulate gloss events into a new world state. 'accumB' means that I just draw a new state ?
worldState <- accumB .. ..
-- Whenever 'onRender' fires. Could you explain what 'onRender' here means ?
reactimate $ writeIORef picRef . drawWorld <$> worldState <@ onRender
I have these 'inlined' questions as now I do this. Am I complicating this ? It works though but the new Picture is just very simple.
networkDescriptor :: IORef Picture -> EventSource() -> MomentIO ()
networkDescriptor lastFrame displayGlossEvent = do
glossEvent <- fromAddHandler (addHandler displayGlossEvent )
reactimate $ putStrLn . showValue <$> glossEvent
picture <- liftMoment (handleKeys displayGlossEvent )
changes picture >>= reactimate' . fmap (fmap (writeIORef lastFrame))
valueBLater picture >>= liftIO . writeIORef lastFrame
showValue value = "Value is " ++ show value
handleKeys :: EventSource () -> Moment (Behavior Picture)
handleKeys glossEvent = do
let picture = drawBoard
return $ pure picture
from reactive-banana.
Related Issues (20)
- `reactive-banana` can't be compiled on GHC 9.2 HOT 2
- I created a Matrix channel HOT 8
- An interesting `Applicative`-ish way of combining `Event`s HOT 4
- Add `MonadMoment` instances for all monad transformers in `transformers` HOT 1
- Release 1.3.0.0 HOT 3
- Join Stackage HOT 4
- `Behavior` constantly re-evaluating when caching is expected HOT 4
- Unexpected memory growth HOT 6
- Event handler not seeing early events. HOT 12
- `Behavior`s can hold onto more memory than they need to HOT 2
- Memory leak with dynamic behavior switching HOT 16
- Idea: add a combinator to track when a behavior is a different value HOT 14
- Call `unregister` for garbage collected events made by `AddHandler`. HOT 9
- Discrepancy in model and implementation HOT 15
- Optimize representation of Latch HOT 1
- Not compatible with these-1.2 (disallowed) HOT 2
- Simultaneous events can have undefined behavior within execute HOT 2
- source-code formatter? HOT 2
- Accept deepseq-1.5 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactive-banana.