amaranth/src/Canvas.hs

62 lines
2.2 KiB
Haskell
Raw Normal View History

2022-07-29 16:32:45 +02:00
module Canvas where
2022-07-31 18:58:13 +02:00
import Graphics.Gloss.Data.Picture ( Picture (Text, Blank), pictures, scale, translate, rectangleSolid, circle )
2022-07-31 17:44:14 +02:00
import Graphics.Gloss.Interface.IO.Interact ( Event (EventKey, EventMotion), KeyState (Down, Up), Key (Char, MouseButton), MouseButton (LeftButton) )
2022-07-31 17:44:14 +02:00
type Dimension = (Int, Int)
type Position = (Float, Float)
2022-07-31 18:58:13 +02:00
type TipSize = Float
2022-07-29 16:32:45 +02:00
2022-07-31 15:48:17 +02:00
data Canvas = Canvas {
items :: [Picture]
, mode :: Mode
, drawing :: Bool
2022-07-31 17:44:14 +02:00
, pos :: Position
, tip :: Tip
2022-07-29 16:32:45 +02:00
}
2022-07-31 15:48:17 +02:00
data Mode = Brush | Eraser deriving (Eq, Show)
2022-07-31 18:58:13 +02:00
data Tip = Tip {
shape :: Shape
, size :: TipSize
}
data Shape = Rectangular | Circular
2022-07-31 18:58:13 +02:00
nextPicture :: Canvas -> Picture
nextPicture (Canvas _ Brush True position (Tip Rectangular size)) = uncurry translate position $ rectangleSolid size size
nextPicture (Canvas _ Brush True position (Tip Circular size)) = uncurry translate position $ circle size
nextPicture _ = Blank
windowSize :: Dimension
windowSize = (500, 500)
2022-07-31 18:58:13 +02:00
leftCornerize :: Picture -> Picture
leftCornerize = translate (fromIntegral $ (-(fst windowSize)) `div` 2 + (fst windowSize `div` 10))
(fromIntegral $ (snd windowSize) `div` 2 - (snd windowSize `div` 10))
2022-07-31 18:58:13 +02:00
rightCornerize :: Picture -> Picture
rightCornerize = translate (fromIntegral $ (fst windowSize) `div` 2 - (fst windowSize `div` 5))
2022-07-31 17:44:14 +02:00
(fromIntegral $ (snd windowSize) `div` 2 - (snd windowSize `div` 10))
showInfo :: Canvas -> Picture
showInfo canv = leftCornerize
2022-07-31 18:58:13 +02:00
$ scale 0.15 0.15
2022-07-31 17:44:14 +02:00
$ Text ( show (mode canv) ++ " - " ++ show (pos canv))
2022-07-29 16:32:45 +02:00
render :: Canvas -> Picture
2022-07-31 18:58:13 +02:00
render canv = pictures ( showInfo canv : items canv )
2022-07-29 16:32:45 +02:00
handle :: Event -> Canvas -> Canvas
handle (EventKey (Char 'b') Down _ _ ) canv = canv { mode = Brush }
handle (EventKey (Char 'e') Down _ _ ) canv = canv { mode = Eraser }
2022-07-31 17:44:14 +02:00
handle (EventKey (MouseButton LeftButton) Down _ mouse ) canv = canv { drawing = True }
handle (EventKey (MouseButton LeftButton) Up _ mouse ) canv = canv { drawing = False }
handle (EventMotion (x, y)) canv = canv { pos = (x, y)}
2022-07-31 15:48:17 +02:00
handle _ canv = canv
2022-07-29 16:32:45 +02:00
update :: Float -> Canvas -> Canvas
2022-07-31 18:58:13 +02:00
update _ canv = canv { items = nextPicture canv : items canv }
2022-07-31 17:44:14 +02:00