gdritter repos vkdraw / 460c18a
Added grid and some basic keyboard handling Getty Ritter 7 years ago
1 changed file(s) with 38 addition(s) and 5 deletion(s). Collapse all Expand all
1 {-# LANGUAGE LambdaCase #-}
2
13 module Main where
24
35 import qualified Data.ByteString.Lazy as BS
46 import Data.IORef
57 import Graphics.UI.Gtk.Cairo
6 import Graphics.UI.Gtk hiding (eventButton)
7 import Graphics.UI.Gtk.Gdk.Events(eventX,eventY,eventButton)
8 import Graphics.UI.Gtk hiding (eventButton, eventKeyVal)
9 import Graphics.UI.Gtk.Gdk.Events(Event(..),eventX,eventY,eventButton)
810 import qualified Graphics.Rendering.Cairo as C
911 import qualified Image.VK as VK
1012
4244 render d state = do
4345 dw <- widgetGetDrawWindow d
4446 (w, h) <- drawableGetSize dw
47 region <- regionRectangle (Rectangle 0 0 w h)
48 drawWindowBeginPaintRegion dw region
4549 renderWithDrawable dw $ do
4650 C.setSourceRGB 1.0 1.0 1.0
4751 C.rectangle 0 0 (fromIntegral w) (fromIntegral h)
4852 C.fill
53 case apGrid state of
54 Nothing -> return ()
55 Just (xW, yW) -> do
56 C.setSourceRGB 0.8 0.8 0.8
57 C.setLineWidth 2
58 C.setLineCap C.LineCapRound
59 sequence_ [ dot (fromIntegral (x * xW)) (fromIntegral (y * yW))
60 | x <- [0..(w `div` xW)]
61 , y <- [0..(h `div` yW)]
62 ]
4963 C.setSourceRGB 0.0 0.0 0.0
5064 mapM_ dLine (apLines state)
5165 case apLines state of
6074 Just (x, y) -> do
6175 C.arc (fromIntegral x) (fromIntegral y) 5.0 0.0 (2 * pi)
6276 C.stroke
63 return ()
77 drawWindowEndPaint dw
78
79 dot :: Double -> Double -> C.Render ()
80 dot x y = do
81 C.moveTo x y
82 C.lineTo x y
83 C.stroke
6484
6585 main :: IO ()
6686 main = do
6787 _ <- initGUI
6888 window <- windowNew
89 vbox <- vBoxNew False 10
6990 da <- drawingAreaNew
70 stRef <- newIORef (ApplicationState [[]] Nothing (800, 800) Nothing)
91 minibuf <- labelNew (Just ":" :: Maybe String)
92 stRef <- newIORef (ApplicationState [[]] Nothing (800, 800) (Just (10, 10)))
7193 set window [ windowDefaultWidth := 800
7294 , windowDefaultHeight := 800
73 , containerChild := da
95 , containerChild := vbox
7496 , containerBorderWidth := 10
7597 ]
98 boxPackStart vbox da PackGrow 0
99 boxPackStart vbox minibuf PackRepel 0
76100 _ <- onMotionNotify da True $ \ event -> do
77101 st <- readIORef stRef
78102 let (x, y) = (rnd (eventX event), rnd (eventY event))
94118 writeIORef stRef st'
95119 render da st'
96120 return True
121 _ <- onKeyPress window $ \ case
122 Key { eventKeyChar = Just 'c' } -> do
123 modifyIORef stRef (\ st -> st { apLines = [[]] })
124 readIORef stRef >>= render da
125 return True
126 Key { eventKeyChar = Just ':' } -> do
127 labelSetText minibuf ("command" :: String)
128 return True
129 _ -> return True
97130 _ <- window `onDestroy` (saveFile stRef >> mainQuit)
98131 widgetShowAll window
99132 mainGUI