为模型和GUI IO(Wx)分离状态:堆栈还是FRP?

时间:2013-04-02 07:59:14

标签: haskell monads state-monad wxhaskell reactive-banana

对于我的图表工具,我想保持核心模型的代码与GUI隔离。

在以下示例中,“状态”与vDiag传递,Tvar。这是wx中的设计决策。现在,对于我的图表工具,我希望将核心模型“存储”在fgl图形中(其中包含复杂类型),并且wx将只给出一个视图;在这个例子中说,绘画时读取访问的点列表,以及点击,拖动等时写入的一些函数。我首先想到了一些Monad堆栈,但即使将一个StateT和来自wx的IO结合起来也看起来并不重要,因为io动作遍布回调代码(点击,绘画等等)。感觉就像堆栈底部的IO不再适合了。

那么如何通过STate,或者不是要走的路? (我直觉这是一个经典。这是RFP如何开始的?)

(在代码中,点击时绘制红色圆圈,点列表在Tvar vDiag中传递。我已标记“--fgl”,其中等效状态访问者会去。并设置一个基本的fgl测试图访问器来说明。我想放入一个状态) (我原本试图在不使用FRP的情况下试一试 - 了解问题,但我想我可能已经打过它了; - )

module Main where

import Graphics.UI.WX hiding (empty)
import Data.Graph.Inductive

main
  = start ballsFrame 

ballsFrame
  = do 


  vDiag  <- varCreate [] 
  --gDiag  <- initg -- fgl
  frame  <- frame    [text := "Demo"]
  p <- panel frame []
  file   <- menuPane [text := "&File"]
  quit   <- menuQuit file [on command := close frame]

  set frame [text:= "testing", menuBar := [file] ]
  set p [on click := drawBins vDiag p , on paint := paintDiag vDiag ] 
                -- fgl pass the var around
  return ()  
    where
    drawBins  d ppanel pt = 
                do varUpdate d  (pt:) 
                    -- addpoint f g -- fgl : insert a point
                   repaint ppanel


    -- paint the balls
    paintDiag vdiag dc view
      = do  balls <- varGet vdiag  -- getPointsFromGraph 
        -- fgl : change to get the list of points
            set dc [brushColor := red, brushKind := BrushSolid] 
            mapM_ (drawDiag dc)  balls

    drawDiag dc pt
      = circle dc pt 10 []

-- basic fgl test graph accessors  I would like to put in a State and replace vDiag

initg:: Gr Point  String
initg = mkGraph [(1,pt 10 10),(2,pt 30 30)] [(1,2,"truc"), (2,1,"revtruc")]

getPointsFromGraph :: Graph gr => gr b b1 -> [b]
getPointsFromGraph g = map snd $ labNodes g
-- getPointsFromGraph initg = [Point {pointX = 10, pointY = 10},Point {pointX = 30, pointY = 30}]

addpoint :: DynGraph gr => a -> gr a b -> gr a b
addpoint p g = -- add a point p into graph p
               insNode (4,p) g

0 个答案:

没有答案