-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.hs
89 lines (77 loc) · 2.89 KB
/
Menu.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Menu where
import Bullet
import Codec.Picture.Types (PixelRGBA8 (..))
import Data.List (foldl')
import Graphics.Rasterific hiding (V2 (..))
import qualified Graphics.Rasterific as Rasterific
( V2 (..),
)
import Graphics.Rasterific.Texture (uniformTexture)
import Graphics.Text.TrueType (Font)
import SDL
import Space
import Visual
type Header = [(String, PixelRGBA8)]
type ContinueName = String
data Selection = Continue | Quit deriving (Show, Eq)
data Menu = Menu Header ContinueName Selection deriving (Show, Eq)
menuSize :: Size2D
menuSize = V2 540 414
defaultTextColor = PixelRGBA8 0xE6 0xE6 0xE6 255
getMenuImageId :: Header -> ImageId
getMenuImageId header = "menu-" ++ concatMap fst header
getStaticMenuImage :: Font -> Menu -> (ImageId, VectorImage)
getStaticMenuImage font (Menu header continueName _) =
let textSize = PointSize 54
menuLine x y = printTextAt font textSize (Rasterific.V2 x y)
in ( getMenuImageId header,
VectorImage menuSize (backgroundColorAlpha 120) $
withTexture (uniformTexture defaultTextColor) $
do
printTextRanges (Rasterific.V2 90 135) $
map
( \(text, color) ->
TextRange
font
textSize
text
(Just $ uniformTexture color)
)
header
menuLine 144 225 continueName
menuLine 144 315 "quit"
)
drawMenu :: Menu -> [(Rectangle Float, Either VectorImage ImageId)]
drawMenu (Menu header _ selection) =
let selectionPosition = if selection == Continue then 540 else 630
in [ (Rectangle (P $ V2 690 333) menuSize, Right $ getMenuImageId header),
drawBullet $ createBullet (V2 807 selectionPosition) 0 (-1)
]
updateSelection :: [Event] -> Menu -> Menu
updateSelection events (Menu header continueName previous) =
Menu header continueName $ foldl' eventToSelection previous events
eventToSelection :: Selection -> Event -> Selection
eventToSelection fallback (Event _ (JoyAxisEvent axisEventData)) =
let JoyAxisEventData _ axisId axisPos = axisEventData
noiseThreshold = 5000
in if axisId == 1
then
if axisPos < - noiseThreshold
then Continue
else if axisPos > noiseThreshold then Quit else fallback
else fallback
eventToSelection fallback (Event _ (JoyHatEvent hatEventData)) =
let JoyHatEventData _ _ hatPosition = hatEventData
in case hatPosition of
HatUp -> Continue
HatDown -> Quit
_ -> fallback
eventToSelection fallback (Event _ (KeyboardEvent eventData)) =
let KeyboardEventData _ motion _ (Keysym (Scancode code) _ _) = eventData
in if motion == Pressed
then case code of
82 -> Continue
81 -> Quit
_ -> fallback
else fallback
eventToSelection fallback _ = fallback