Code Monkey home page Code Monkey logo

Comments (5)

ocharles avatar ocharles commented on August 17, 2024 1

@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.

HeinrichApfelmus avatar HeinrichApfelmus commented on August 17, 2024

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.

mohanr avatar mohanr commented on August 17, 2024

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.

ocharles avatar ocharles commented on August 17, 2024

@mohanr Was my response above helpful? Can we close this issue, or are you still stuck?

from reactive-banana.

mohanr avatar mohanr commented on August 17, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.