gdritter repos vkdraw / 9d56f67
Switched rendering to Cairo Getty Ritter 7 years ago
2 changed file(s) with 42 addition(s) and 16 deletion(s). Collapse all Expand all
1717 default-extensions: OverloadedStrings,
1818 ScopedTypeVariables
1919 ghc-options: -Wall
20 build-depends: base >=4.7 && <4.9, gtk
20 build-depends: base >=4.7 && <4.9, gtk, cairo
2121 default-language: Haskell2010
11 module Main where
22
33 import Data.IORef
4 import Graphics.UI.Gtk.Cairo
45 import Graphics.UI.Gtk hiding (eventButton)
56 import Graphics.UI.Gtk.Gdk.Events(eventX,eventY,eventButton)
6 import Graphics.UI.Gtk.Gdk.GC
7 import qualified Graphics.Rendering.Cairo as C
78
89 data ApplicationState = ApplicationState
910 { apLines :: [[(Int, Int)]]
1314 rnd :: Double -> Int
1415 rnd x = ((floor x) `div` 10) * 10
1516
17 dLine :: [(Int, Int)] -> C.Render ()
18 dLine [] = return ()
19 dLine [(x,y)] = do
20 C.moveTo (fromIntegral x) (fromIntegral y)
21 C.lineTo (fromIntegral x) (fromIntegral y)
22 C.stroke
23 dLine ((x,y):rs) = do
24 C.moveTo (fromIntegral x) (fromIntegral y)
25 sequence_ [ C.lineTo (fromIntegral x') (fromIntegral y')
26 | (x', y') <- rs
27 ]
28 C.stroke
29
30 dPoints :: [(Int, Int)] -> C.Render ()
31 dPoints ps =
32 sequence_ [ C.arc (fromIntegral x) (fromIntegral y) 2.0 0.0 (2 * pi) >>
33 C.stroke
34 | (x, y) <- ps
35 ]
36
1637 render :: WidgetClass d => d -> ApplicationState -> IO ()
1738 render d state = do
1839 dw <- widgetGetDrawWindow d
19 gc <- gcNewWithValues dw newGCValues { foreground = Color 0x9999 0x9999 0x9999 }
2040 (w, h) <- drawableGetSize dw
21 drawRectangle dw gc True 0 0 w h
22 gcSetValues gc (newGCValues { foreground = Color 0 0 0 })
23 mapM_ (drawPoint dw gc) [ (x * 10, y * 10)
24 | x <- [0..w `div` 10]
25 , y <- [0..h `div` 10]
26 ]
27 mapM_ (drawLines dw gc) (apLines state)
28 case apCursor state of
29 Nothing -> return ()
30 Just (x, y) -> do
31 gcSetValues gc (newGCValues { foreground = Color 0x9999 0 0 })
32 drawLine dw gc (x-5,y) (x+5,y)
33 drawLine dw gc (x,y-5) (x,y+5)
41 renderWithDrawable dw $ do
42 C.setSourceRGB 1.0 1.0 1.0
43 C.rectangle 0 0 (fromIntegral w) (fromIntegral h)
44 C.fill
45 C.setSourceRGB 0.0 0.0 0.0
46 mapM_ dLine (apLines state)
47 case apLines state of
48 [] -> return ()
49 (x:xs) -> do
50 mapM_ dPoints xs
51 C.setSourceRGB 0.0 1.0 1.0
52 dPoints x
53 C.setSourceRGB 1.0 0.0 0.0
54 case apCursor state of
55 Nothing -> return ()
56 Just (x, y) -> do
57 C.arc (fromIntegral x) (fromIntegral y) 5.0 0.0 (2 * pi)
58 C.stroke
59 return ()
3460
3561 main :: IO ()
3662 main = do