gdritter repos vkdraw / d12679a
Saves temporary files to VK images Getty Ritter 8 years ago
2 changed file(s) with 27 addition(s) and 9 deletion(s). Collapse all Expand all
11 module Main where
22
3 import Data.IORef
4 import Graphics.UI.Gtk.Cairo
5 import Graphics.UI.Gtk hiding (eventButton)
6 import Graphics.UI.Gtk.Gdk.Events(eventX,eventY,eventButton)
3 import qualified Data.ByteString.Lazy as BS
4 import Data.IORef
5 import Graphics.UI.Gtk.Cairo
6 import Graphics.UI.Gtk hiding (eventButton)
7 import Graphics.UI.Gtk.Gdk.Events(eventX,eventY,eventButton)
78 import qualified Graphics.Rendering.Cairo as C
9 import qualified Image.VK as VK
810
911 data ApplicationState = ApplicationState
1012 { apLines :: [[(Int, Int)]]
1113 , apCursor :: Maybe (Int, Int)
14 , apSize :: (Int,Int)
15 , apGrid :: Maybe (Int, Int)
1216 } deriving (Eq, Show)
1317
1418 rnd :: Double -> Int
6367 _ <- initGUI
6468 window <- windowNew
6569 da <- drawingAreaNew
66 set window [ windowDefaultWidth := 200
67 , windowDefaultHeight := 200
70 stRef <- newIORef (ApplicationState [[]] Nothing (800, 800) Nothing)
71 set window [ windowDefaultWidth := 800
72 , windowDefaultHeight := 800
6873 , containerChild := da
6974 , containerBorderWidth := 10
7075 ]
71 stRef <- newIORef (ApplicationState [[]] Nothing)
7276 _ <- onMotionNotify da True $ \ event -> do
7377 st <- readIORef stRef
7478 let (x, y) = (rnd (eventX event), rnd (eventY event))
9094 writeIORef stRef st'
9195 render da st'
9296 return True
93 _ <- window `onDestroy` mainQuit
97 _ <- window `onDestroy` (saveFile stRef >> mainQuit)
9498 widgetShowAll window
9599 mainGUI
100
101 saveFile :: IORef ApplicationState -> IO ()
102 saveFile ref = do
103 st <- readIORef ref
104 let (w, h) = apSize st
105 let bs = VK.encode $ VK.VKImage
106 { VK.vkLines = [ [ (fromIntegral x, fromIntegral y)
107 | (x, y) <- line
108 ]
109 | line <- apLines st
110 ]
111 , VK.vkMeta = VK.VKMeta (fromIntegral w, fromIntegral h)
112 }
113 BS.writeFile "/tmp/tmp.vk" bs
1717 default-extensions: OverloadedStrings,
1818 ScopedTypeVariables
1919 ghc-options: -Wall
20 build-depends: base >=4.7 && <4.9, gtk, cairo
20 build-depends: base >=4.7 && <4.9, gtk, cairo, bytestring, image-vk
2121 default-language: Haskell2010