-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.hs
59 lines (46 loc) · 1.41 KB
/
Sprite.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Sprite (
Sprite(..),
makeSprite,
spritePoints,
selectPoints,
initDragSprite,
dragSprite,
dragSpriteUpdatingPath,
) where
import Graphics.UI.GLUT
import Rectangle
import Util
data Sprite =
Sprite {
spriteRect :: Rectangle,
-- recorded path
spritePath :: [Position],
-- is it being dragged?
sticky :: Bool,
-- is it selected?
selected :: Bool,
-- mouse offset for dragging
offset :: Pos
} deriving (Show, Eq)
makeSprite :: Position -> Sprite
makeSprite (Position x y) =
Sprite (Rectangle (conv x) (conv y) 20 20) [] False False (0,0)
spritePoints :: Sprite -> [Vertex2 GLdouble]
spritePoints s = vertexRect $ spriteRect s
-- calculate a rectangle around the sprite
selectPoints :: Sprite -> [Vertex2 GLdouble]
selectPoints s = vertexRect $ boxAroundRect (spriteRect s) 4.0
-- start dragging a sprite
initDragSprite :: Position -> Sprite -> Sprite
initDragSprite offs s =
s { sticky = True, offset = posOp (-) myPos (posConv offs) }
where
myPos = ( (rectX$spriteRect$s),(rectY$spriteRect$s) )
dragSprite :: Position -> Sprite -> Sprite
dragSprite p s = s{ spriteRect = newRect }
where
newRect :: Rectangle
newRect = (spriteRect s){rectX = newX, rectY = newY}
(newX,newY) = posOp (+) (posConv p) (offset s)
dragSpriteUpdatingPath :: Position -> Sprite -> Sprite
dragSpriteUpdatingPath p s = dragSprite p s { spritePath = spritePath s ++ [p] }