summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2015-12-17 16:52:55 -0500
committerAndrew Cady <d@jerkface.net>2015-12-17 16:52:55 -0500
commit4c1e311e6b267eacee2b1a240024d9210827538b (patch)
tree81323e583e7485c9746c610a8d9213edf4681cc1
parent666a7d84a17dbf93af63cb5d41c084568086ae43 (diff)
Clean up axis.hs lint errors, formatting, etc.
(No semantic changes.)
-rw-r--r--axis-of-eval.cabal2
-rw-r--r--axis.hs343
2 files changed, 169 insertions, 176 deletions
diff --git a/axis-of-eval.cabal b/axis-of-eval.cabal
index f09386a..da7a2a8 100644
--- a/axis-of-eval.cabal
+++ b/axis-of-eval.cabal
@@ -18,7 +18,7 @@ executable axis
18 default-language: Haskell2010 18 default-language: Haskell2010
19 hs-source-dirs: . 19 hs-source-dirs: .
20 build-depends: 20 build-depends:
21 base, time, SDL, SDL-ttf, SDL-gfx, containers, haskore, alsa-seq, alsa-core 21 base, time, SDL, SDL-ttf, SDL-gfx, containers, haskore, alsa-seq, alsa-core, base-prelude
22 main-is: axis.hs 22 main-is: axis.hs
23 other-modules: AlsaSeq 23 other-modules: AlsaSeq
24 24
diff --git a/axis.hs b/axis.hs
index c784865..b093414 100644
--- a/axis.hs
+++ b/axis.hs
@@ -1,6 +1,6 @@
1{-# LANGUAGE NondecreasingIndentation #-}
2{-# LANGUAGE StandaloneDeriving #-} 1{-# LANGUAGE StandaloneDeriving #-}
3import Prelude hiding ((.), id, null, filter) 2import Prelude ()
3import BasePrelude
4import Data.Time.Clock 4import Data.Time.Clock
5import Control.Monad 5import Control.Monad
6import qualified Graphics.UI.SDL as SDL 6import qualified Graphics.UI.SDL as SDL
@@ -19,7 +19,7 @@ import qualified Sound.ALSA.Sequencer.Event as Event
19import qualified Graphics.UI.SDL.Utilities as SDL.Util 19import qualified Graphics.UI.SDL.Utilities as SDL.Util
20import qualified Data.Map as Map 20import qualified Data.Map as Map
21 21
22smartShowPitch p = showPitch p -- TODO: use flat for Eb, Bb; use unicode flat/sharp chars 22smartShowPitch = showPitch -- TODO: use flat for Eb, Bb; use unicode flat/sharp chars
23 23
24_USE_HEXAGONS = True 24_USE_HEXAGONS = True
25_LABEL_WHILE_PLAYING = True 25_LABEL_WHILE_PLAYING = True
@@ -33,10 +33,10 @@ _AXIS_BOTTOMLEFT_PITCH = _AXIS_TOPLEFT_PITCH - (_AXIS_ROWS * 7)
33_AXIS_TOPRIGHT_PITCH = _AXIS_TOPLEFT_PITCH + _AXIS_UNIQUE_COLS `div` 2 33_AXIS_TOPRIGHT_PITCH = _AXIS_TOPLEFT_PITCH + _AXIS_UNIQUE_COLS `div` 2
34 34
35--_KEY_BORDER_COLOR = (SDL.Color 0 0 255) 35--_KEY_BORDER_COLOR = (SDL.Color 0 0 255)
36_KEY_BORDER_COLOR = (SDL.Color 0 0 0) 36_KEY_BORDER_COLOR = SDL.Color 0 0 0
37_KEY_ON_COLOR = (SDL.Color 0xAA 0x00 0xFF) 37_KEY_ON_COLOR = SDL.Color 0xAA 0x00 0xFF
38_KB_BG_COLOR = (SDL.Color 0 0 0) 38_KB_BG_COLOR = SDL.Color 0 0 0
39_KEY_TEXT_COLOR = (SDL.Color 128 128 0) 39_KEY_TEXT_COLOR = SDL.Color 128 128 0
40 40
41_KEY_BORDER_COLOR_PIXEL = colorToPixel _KEY_BORDER_COLOR 41_KEY_BORDER_COLOR_PIXEL = colorToPixel _KEY_BORDER_COLOR
42_KB_BG_COLOR_PIXEL = colorToPixel _KB_BG_COLOR 42_KB_BG_COLOR_PIXEL = colorToPixel _KB_BG_COLOR
@@ -61,24 +61,24 @@ http://en.wikipedia.org/wiki/Enhanced_Graphics_Adapter
6115 – bright white (#FFFFFF) 111111 63 6115 – bright white (#FFFFFF) 111111 63
62-} 62-}
63 63
64_CGA = [ (SDL.Color 0x00 0x00 0x00), --black 64_CGA = [ SDL.Color 0x00 0x00 0x00, --black
65 (SDL.Color 0x00 0x00 0xAA), --blue 65 SDL.Color 0x00 0x00 0xAA, --blue
66 (SDL.Color 0x00 0xAA 0x00), --green 66 SDL.Color 0x00 0xAA 0x00, --green
67 (SDL.Color 0x00 0xAA 0xAA), --cyan 67 SDL.Color 0x00 0xAA 0xAA, --cyan
68 (SDL.Color 0xAA 0x00 0x00), --red 68 SDL.Color 0xAA 0x00 0x00, --red
69 (SDL.Color 0xAA 0x00 0xAA), --magenta 69 SDL.Color 0xAA 0x00 0xAA, --magenta
70 (SDL.Color 0xAA 0x55 0x00), --brown 70 SDL.Color 0xAA 0x55 0x00, --brown
71 (SDL.Color 0xAA 0xAA 0xAA), --white / light gray 71 SDL.Color 0xAA 0xAA 0xAA, --white / light gray
72 (SDL.Color 0x55 0x55 0x55), --dark gray / bright black 72 SDL.Color 0x55 0x55 0x55, --dark gray / bright black
73 (SDL.Color 0x55 0x55 0xFF), --bright blue 73 SDL.Color 0x55 0x55 0xFF, --bright blue
74 (SDL.Color 0x55 0xFF 0x55), --bright green 74 SDL.Color 0x55 0xFF 0x55, --bright green
75 (SDL.Color 0x55 0xFF 0xFF), --bright cyan 75 SDL.Color 0x55 0xFF 0xFF, --bright cyan
76 (SDL.Color 0xFF 0x55 0x55), --bright red 76 SDL.Color 0xFF 0x55 0x55, --bright red
77 (SDL.Color 0xFF 0x55 0xFF), --bright magenta 77 SDL.Color 0xFF 0x55 0xFF, --bright magenta
78 (SDL.Color 0xFF 0xFF 0x55), --bright yellow 78 SDL.Color 0xFF 0xFF 0x55, --bright yellow
79 (SDL.Color 0xFF 0xFF 0xFF)] --bright white 79 SDL.Color 0xFF 0xFF 0xFF] --bright white
80 80
81_CHAN_TO_COLOR = _KEY_ON_COLOR : (tail _CGA) 81_CHAN_TO_COLOR = _KEY_ON_COLOR : tail _CGA
82 82
83_drawHexircle f v x y s c = 83_drawHexircle f v x y s c =
84 if _USE_HEXAGONS 84 if _USE_HEXAGONS
@@ -97,42 +97,42 @@ inMajorC pclass = not (pclass == 1 || pclass == 3 || pclass == 6 || pclass == 8
97 97
98pitchToColor p = 98pitchToColor p =
99 case p `mod` 12 of 99 case p `mod` 12 of
100 2 -> (SDL.Color 0xC0 0xC0 0xFF) -- D 100 2 -> SDL.Color 0xC0 0xC0 0xFF -- D
101 8 -> (SDL.Color 0x33 0x33 0x66) -- G# 101 8 -> SDL.Color 0x33 0x33 0x66 -- G#
102 x | inMajorC x -> (SDL.Color 0xE0 0xE0 0xE0) -- _CGA !! 7 102 x | inMajorC x -> SDL.Color 0xE0 0xE0 0xE0 -- _CGA !! 7
103 _ -> _CGA !! 8 103 _ -> _CGA !! 8
104 104
105smartDrawKeys :: (Enum a, Integral a1, Integral a3, Integral a2, Num a) => a -> Bool -> Set.Set (Event.Channel, Event.Pitch) -> Set.Set (Event.Channel, Event.Pitch) -> SDL.Surface -> SDL.TTF.Font -> [(a2, a3)] -> a1 -> IO () 105smartDrawKeys :: (Enum a, Integral a1, Integral a3, Integral a2, Num a) => a -> Bool -> Set.Set (Event.Channel, Event.Pitch) -> Set.Set (Event.Channel, Event.Pitch) -> SDL.Surface -> SDL.TTF.Font -> [(a2, a3)] -> a1 -> IO ()
106smartDrawKeys colsRepeat reallyErase beforeKeys_ nowKeys_ videoSurface font axis_key_locations axis_key_size = do 106smartDrawKeys colsRepeat reallyErase beforeKeys_ nowKeys_ videoSurface font axis_key_locations axis_key_size = do
107 let 107 let
108 chanfilter = Set.filter (\ (c, _) -> c /= (Event.Channel 9)) 108 chanfilter = Set.filter (\ (c, _) -> c /= Event.Channel 9)
109 beforeKeys = chanfilter beforeKeys_ 109 beforeKeys = chanfilter beforeKeys_
110 nowKeys = chanfilter nowKeys_ 110 nowKeys = chanfilter nowKeys_
111 111
112 changedPitches = Set.map (\ (_, p) -> p) $ Set.union (Set.difference nowKeys beforeKeys) (Set.difference beforeKeys nowKeys) 112 changedPitches = Set.map snd $ Set.union (Set.difference nowKeys beforeKeys) (Set.difference beforeKeys nowKeys)
113 playingNowChans n = Set.map (\ (c, _) -> c) $ Set.filter (\ (_, p) -> p == n) nowKeys 113 playingNowChans n = Set.map fst $ Set.filter (\ (_, p) -> p == n) nowKeys
114 actions = Set.toList $ Set.map (\p -> (p, Set.toList $ playingNowChans p)) changedPitches 114 actions = Set.toList $ Set.map (\p -> (p, Set.toList $ playingNowChans p)) changedPitches
115 115
116 forM_ actions $ \ (n, chans) -> do 116 forM_ actions $ \ (n, chans) -> do
117 let text = smartShowPitch (unPitch n) 117 let text = smartShowPitch (unPitch n)
118 pitch = unPitch n 118 pitch = unPitch n
119 indices = elemIndices pitch $ pitchIndex colsRepeat 119 indices = elemIndices pitch $ pitchIndex colsRepeat
120 off = length chans == 0 120 off = null chans
121 forM_ indices $ \idx -> do 121 forM_ indices $ \idx -> do
122 let showLabel = (not reallyErase) && (_LABEL_ALL_KEYS || (_LABEL_WHILE_PLAYING && (not off))) 122 let showLabel = not reallyErase && (_LABEL_ALL_KEYS || (_LABEL_WHILE_PLAYING && not off))
123 drawKey idx videoSurface font axis_key_locations axis_key_size 123 drawKey idx videoSurface font axis_key_locations axis_key_size
124 (if reallyErase then _KB_BG_COLOR else pitchToColor pitch) 124 (if reallyErase then _KB_BG_COLOR else pitchToColor pitch)
125 (if showLabel then (Just text) else Nothing) 125 (if showLabel then Just text else Nothing)
126 chans 126 chans
127 127
128allKeysOff colsRepeat reallyErase videoSurface font axis_key_locations axis_key_size = do 128allKeysOff colsRepeat reallyErase videoSurface font axis_key_locations axis_key_size = do
129 let indices = [0 .. length axis_key_locations - 1] 129 let indices = [0 .. length axis_key_locations - 1]
130 showLabel = (not reallyErase) && _LABEL_ALL_KEYS 130 showLabel = not reallyErase && _LABEL_ALL_KEYS
131 forM_ indices $ \idx -> do 131 forM_ indices $ \idx -> do
132 let pitch = pitchIndex colsRepeat !! idx 132 let pitch = pitchIndex colsRepeat !! idx
133 bgColor = if reallyErase then _KB_BG_COLOR else pitchToColor pitch 133 bgColor = if reallyErase then _KB_BG_COLOR else pitchToColor pitch
134 text = smartShowPitch pitch 134 text = smartShowPitch pitch
135 label = (if showLabel then (Just text) else Nothing) 135 label = if showLabel then Just text else Nothing
136 drawKey idx videoSurface font axis_key_locations axis_key_size bgColor label [] 136 drawKey idx videoSurface font axis_key_locations axis_key_size bgColor label []
137 137
138-- OK, what we need to do now... 138-- OK, what we need to do now...
@@ -155,16 +155,16 @@ drawKey idx videoSurface font axis_key_locations axis_key_size fillColor text ch
155 drawFilledHexircle videoSurface (fromIntegral x) (fromIntegral y) (fromIntegral axis_key_size) (colorToPixel fillColor) 155 drawFilledHexircle videoSurface (fromIntegral x) (fromIntegral y) (fromIntegral axis_key_size) (colorToPixel fillColor)
156 let len = length channels 156 let len = length channels
157 let channels' = sort channels 157 let channels' = sort channels
158 Control.Monad.when (len /= 0) $ do 158 Control.Monad.when (len /= 0) $
159 forM_ [0 .. len - 1] $ \i -> do 159 forM_ [0 .. len - 1] $ \i -> do
160 let (x', y') = if len == 1 then (0, 0) 160 let (x', y') = if len == 1 then (0, 0)
161 else (d * cos(2*pi/lenf * ifi), d * sin(2*pi/lenf * ifi)) 161 else (d * cos(2*pi/lenf * ifi), d * sin(2*pi/lenf * ifi))
162 ifi = fromIntegral i 162 ifi = fromIntegral i
163 lenf = fromIntegral len 163 lenf = fromIntegral len
164 d = (fromIntegral axis_key_size) / 4 :: Float 164 d = fromIntegral axis_key_size / 4 :: Float
165 r' = (fromIntegral axis_key_size) / 2 :: Float 165 r' = fromIntegral axis_key_size / 2 :: Float
166 x'' = (round x') + (fromIntegral x) 166 x'' = round x' + fromIntegral x
167 y'' = (round y') + (fromIntegral y) 167 y'' = round y' + fromIntegral y
168 chan = channels' !! i 168 chan = channels' !! i
169 color = _CHAN_TO_COLOR !! fromIntegral(unChannel chan) 169 color = _CHAN_TO_COLOR !! fromIntegral(unChannel chan)
170 SDL.Primitive.filledCircle videoSurface x'' y'' (round r') (colorToPixel color) 170 SDL.Primitive.filledCircle videoSurface x'' y'' (round r') (colorToPixel color)
@@ -174,7 +174,7 @@ drawKey idx videoSurface font axis_key_locations axis_key_size fillColor text ch
174 _ -> return () 174 _ -> return ()
175 175
176fi = fromIntegral 176fi = fromIntegral
177rgbColor r g b = SDL.Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. (fi 255)) 177rgbColor r g b = SDL.Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. fi 255)
178 178
179chooseFontSize h w = 30 * d `div` 1024 where d = min h w 179chooseFontSize h w = 30 * d `div` 1024 where d = min h w
180 180
@@ -190,120 +190,114 @@ firstDigitDown k = if Set.null digitsDown then Nothing else Just $ (-48 +) $ fro
190 190
191main = 191main =
192 withAlsaInit $ \h public private q publicAddr privateAddr -> do 192 withAlsaInit $ \h public private q publicAddr privateAddr -> do
193 cmdlineAlsaConnect h public -- fail early if bad command lines 193 cmdlineAlsaConnect h public -- fail early if bad command lines
194 194
195 SDL.withInit [SDL.InitVideo, SDL.InitTimer, SDL.InitJoystick] $ do 195 SDL.withInit [SDL.InitVideo, SDL.InitTimer, SDL.InitJoystick] $ do
196 info <- SDL.getVideoInfo 196 info <- SDL.getVideoInfo
197 let sWidth = SDL.videoInfoWidth info 197 let sWidth = SDL.videoInfoWidth info
198 sHeight = SDL.videoInfoHeight info 198 sHeight = SDL.videoInfoHeight info
199 warpMouse = do 199 warpMouse = do
200 _ <- SDL.warpMouse (fromIntegral (sWidth `div` 2)) (fromIntegral (sHeight `div` 2)) 200 _ <- SDL.warpMouse (fromIntegral (sWidth `div` 2)) (fromIntegral (sHeight `div` 2))
201 return () 201 return ()
202 setVideoMode w h = SDL.setVideoMode w h 32 [SDL.HWSurface, SDL.Resizable, SDL.DoubleBuf] 202 setVideoMode w h = SDL.setVideoMode w h 32 [SDL.HWSurface, SDL.Resizable, SDL.DoubleBuf]
203 _ <- setVideoMode sWidth sHeight 203 _ <- setVideoMode sWidth sHeight
204 204
205 _ <- SDL.TTF.init 205 _ <- SDL.TTF.init
206 font <- SDL.TTF.openFont "LiberationMono-Bold.ttf" (chooseFontSize sWidth sHeight) 206 font <- SDL.TTF.openFont "LiberationMono-Bold.ttf" (chooseFontSize sWidth sHeight)
207--_ <- SDL.showCursor False 207 -- _ <- SDL.showCursor False _ <- SDL.grabInput True warpMouse _ <- SDL.setRelativeMouseMode True --
208--_ <- SDL.grabInput True 208 -- SDL2. Should I use it? using the pixelFormat methods gives the wrong color, with both the real
209--warpMouse 209 -- pixelFormat or the faked one, so fuck it. See colorToPixel let pixelFormat =
210 -- _ <- SDL.setRelativeMouseMode True -- SDL2. Should I use it? 210 -- SDL.surfaceGetPixelFormat videoSurface pixelFormat <- SDL.surfaceGetPixelFormat <$>
211 211 -- SDL.createRGBSurfaceEndian [] 1 1 24
212 -- using the pixelFormat methods gives the wrong color, with both the 212 putStrLn "Initialized."
213 -- real pixelFormat or the faked one, so fuck it. See colorToPixel 213
214--let pixelFormat = SDL.surfaceGetPixelFormat videoSurface 214 let parseAlsa keysDown = parseAlsaEvents h keysDown (forwardNoteEvent h q publicAddr)
215--pixelFormat <- SDL.surfaceGetPixelFormat <$> SDL.createRGBSurfaceEndian [] 1 1 24 215 let loop state midiKeysDown keysDown resolution font = do
216 216 let LoopState firstLoop colsRepeat = state
217 putStrLn "Initialized." 217
218 218 (keysDown', resolution') <- parseSDLEvents keysDown resolution
219 let parseAlsa keysDown = parseAlsaEvents h keysDown (forwardNoteEvent h q publicAddr) 219 midiKeysDown' <- parseAlsa midiKeysDown
220 let loop state midiKeysDown keysDown resolution font = do 220 let colsRepeat' =
221 let (LoopState firstLoop colsRepeat) = state 221 case firstDigitDown keysDown' of
222 222 Nothing -> colsRepeat
223 (keysDown', resolution') <- parseSDLEvents keysDown resolution 223 (Just 0) -> colsRepeat
224 midiKeysDown' <- parseAlsa midiKeysDown 224 (Just n) -> n
225 let colsRepeat' = case firstDigitDown keysDown' of Nothing -> colsRepeat; (Just 0) -> colsRepeat; (Just n) -> n; 225
226 226 let restartVideo = resolution' /= resolution || colsRepeat' /= colsRepeat
227 let restartVideo = resolution' /= resolution || colsRepeat' /= colsRepeat 227
228 228 when restartVideo $ do
229 Control.Monad.when restartVideo $ do 229 let (w, h) = resolution'
230 let (w, h) = resolution' 230 _ <- setVideoMode w h
231 _ <- setVideoMode w h 231 return ()
232 return () 232
233 233 let (w, h) = resolution'
234 let (w, h) = resolution' 234 fontSize = chooseFontSize w h
235 fontSize = chooseFontSize w h 235 font' <- if restartVideo
236 font' <- (if (restartVideo) then SDL.TTF.openFont "LiberationMono-Bold.ttf" fontSize else return font) 236 then SDL.TTF.openFont "LiberationMono-Bold.ttf" fontSize
237 237 else return font
238 videoSurface <- SDL.getVideoSurface 238
239 videoClipRect <- SDL.getClipRect videoSurface 239 videoSurface <- SDL.getVideoSurface
240 let (axis_key_size, axis_key_locations) = getKeyLocations colsRepeat' videoClipRect 240 videoClipRect <- SDL.getClipRect videoSurface
241 241 let (axis_key_size, axis_key_locations) = getKeyLocations colsRepeat' videoClipRect
242 let _ALL_PITCHES = Set.fromList $ map (\p -> (Event.Channel 1, Event.Pitch p)) [_AXIS_BOTTOMLEFT_PITCH .. _AXIS_TOPRIGHT_PITCH] 242
243 keysOFF really = allKeysOff colsRepeat' really videoSurface font' axis_key_locations axis_key_size 243 let _ALL_PITCHES = Set.fromList $ map (\p -> (Event.Channel 1, Event.Pitch p)) [_AXIS_BOTTOMLEFT_PITCH .. _AXIS_TOPRIGHT_PITCH]
244 allKeysOFF = keysOFF False 244 keysOFF really = allKeysOff colsRepeat' really videoSurface font' axis_key_locations axis_key_size
245 allKeysReallyOFF = keysOFF True 245 allKeysOFF = keysOFF False
246 246 allKeysReallyOFF = keysOFF True
247 Control.Monad.when(firstLoop) allKeysOFF 247
248 248 when firstLoop allKeysOFF
249-- Control.Monad.when (x /= x' && x' /= "") $ do 249
250-- textBand videoSurface videoClipRect (SDL.Rect 0 0 0 70) font x' 250 -- when (x /= x' && x' /= "") $ do
251-- return () 251 -- textBand videoSurface videoClipRect (SDL.Rect 0 0 0 70) font x'
252 252 -- return ()
253 let 253 let chanfilter = Set.filter (\(c, _) -> c /= Event.Channel 9)
254 chanfilter = Set.filter (\ (c, _) -> c /= (Event.Channel 9)) 254 beforeKeys = chanfilter midiKeysDown
255 beforeKeys = chanfilter midiKeysDown 255 nowKeys = chanfilter midiKeysDown'
256 nowKeys = chanfilter midiKeysDown' 256
257 257 changedPitches = Set.map snd $ Set.union (Set.difference nowKeys beforeKeys) (Set.difference beforeKeys nowKeys)
258 changedPitches = Set.map (\ (_, p) -> p) $ Set.union (Set.difference nowKeys beforeKeys) (Set.difference beforeKeys nowKeys) 258 playingNowChans n = Set.map fst $ Set.filter (\(_, p) -> p == n) nowKeys
259 playingNowChans n = Set.map (\ (c, _) -> c) $ Set.filter (\ (_, p) -> p == n) nowKeys 259 actions = Set.toList $ Set.map (\p -> (p, Set.toList $ playingNowChans p)) changedPitches
260 actions = Set.toList $ Set.map (\p -> (p, Set.toList $ playingNowChans p)) changedPitches 260 chanPitches = Map.fromListWith (++) $ map (\(c, p) -> (c, [p])) $ Set.toList nowKeys
261 chanPitches = Map.fromListWith (++) $ map (\ (c, p) -> (c, [p])) $ Set.toList nowKeys 261
262 262 when (midiKeysDown' /= midiKeysDown) $ do
263 Control.Monad.when (midiKeysDown' /= midiKeysDown) $ do 263 -- let chord = showChord midiKeysDown'
264-- let chord = showChord midiKeysDown' 264 -- let chord = show $ pitchList midiKeysDown'
265-- let chord = show $ pitchList midiKeysDown' 265 -- let chord = show $ map (\i->elemIndices i pitchIndex) $ pitchList midiKeysDown'
266-- let chord = show $ map (\i->elemIndices i pitchIndex) $ pitchList midiKeysDown' 266 -- textBand videoSurface videoClipRect (SDL.Rect 0 70 0 70) font chord
267-- textBand videoSurface videoClipRect (SDL.Rect 0 70 0 70) font chord 267 smartDrawKeys colsRepeat' False midiKeysDown midiKeysDown' videoSurface font' axis_key_locations axis_key_size
268 268
269 smartDrawKeys colsRepeat' False midiKeysDown midiKeysDown' videoSurface font' axis_key_locations axis_key_size 269 when restartVideo $ do
270 return () 270 allKeysOFF
271 271 smartDrawKeys colsRepeat' False Set.empty midiKeysDown' videoSurface font' axis_key_locations axis_key_size
272 Control.Monad.when (restartVideo) $ do 272
273 allKeysOFF 273 when (keysDown' /= keysDown) $ do
274 smartDrawKeys colsRepeat' False Set.empty midiKeysDown' videoSurface font' axis_key_locations axis_key_size 274 when (keyDown SDL.SDLK_BACKSPACE keysDown') allKeysOFF
275 return () 275 when (keyDown SDL.SDLK_c keysDown') allKeysReallyOFF
276 276
277 Control.Monad.when (keysDown' /= keysDown) $ do 277 -- Control.Monad.when (keysDown' /= keysDown) $ do
278 Control.Monad.when (keyDown SDL.SDLK_BACKSPACE keysDown') allKeysOFF 278 -- let chord = unwords $ map (\k -> drop 5 (show k)) $ Set.toList keysDown'
279 Control.Monad.when (keyDown SDL.SDLK_c keysDown') allKeysReallyOFF 279 -- textBand videoSurface videoClipRect (SDL.Rect 0 210 0 70) font chord
280 280 -- textBand videoSurface videoClipRect (SDL.Rect 0 280 0 70) font $
281-- Control.Monad.when (keysDown' /= keysDown) $ do 281 -- if keyDown SDL.SDLK_a keysDown' then "arpeggiate" else ""
282-- let chord = unwords $ map (\k -> drop 5 (show k)) $ Set.toList keysDown' 282 -- return ()
283-- textBand videoSurface videoClipRect (SDL.Rect 0 210 0 70) font chord 283 -- Control.Monad.when(False) $ do
284-- textBand videoSurface videoClipRect (SDL.Rect 0 280 0 70) font $ 284 --
285-- if keyDown SDL.SDLK_a keysDown' then "arpeggiate" else "" 285 -- mouse <- SDL.getRelativeMouseState
286-- return () 286 -- let (x, y, button) = mouse
287 287 -- let text = ((show x) ++ " " ++ (show y) ++ " " ++ (show button))
288-- Control.Monad.when(False) $ do 288 -- textBand videoSurface videoClipRect (SDL.Rect 0 140 0 70) font text
289-- 289 -- Control.Monad.when (x /= 0 || y /= 0) warpMouse
290-- mouse <- SDL.getRelativeMouseState 290
291-- let (x, y, button) = mouse 291 _ <- SDL.flip videoSurface
292-- let text = ((show x) ++ " " ++ (show y) ++ " " ++ (show button)) 292 let framerate = 30
293-- textBand videoSurface videoClipRect (SDL.Rect 0 140 0 70) font text 293 let delay = 1000 `div` framerate -- TODO: subtract delta
294-- Control.Monad.when (x /= 0 || y /= 0) warpMouse 294 SDL.delay delay
295 295 unless (keyDown SDL.SDLK_ESCAPE keysDown) $
296 _ <- SDL.flip videoSurface 296 loop (LoopState False colsRepeat') midiKeysDown' keysDown' resolution' font'
297 297
298 let framerate = 30 298 loop (LoopState True _AXIS_COLS_REPEAT) Set.empty Set.empty (sWidth, sHeight) font
299 let delay = 1000 `div` framerate -- TODO: subtract delta 299
300 SDL.delay (delay) 300zipzip ls = if null (head ls) then [] else map head ls : zipzip (map tail ls)
301 Control.Monad.when (not $ keyDown SDL.SDLK_ESCAPE keysDown) $
302 loop (LoopState False colsRepeat') midiKeysDown' keysDown' resolution' font'
303
304 loop (LoopState True _AXIS_COLS_REPEAT) Set.empty Set.empty (sWidth, sHeight) font
305
306zipzip ls = if (head ls) == [] then [] else (map head ls) : (zipzip $ map tail ls)
307 301
308drawHexagonSDL = _drawHexagonSDL False 302drawHexagonSDL = _drawHexagonSDL False
309drawFilledHexagonSDL = _drawHexagonSDL True 303drawFilledHexagonSDL = _drawHexagonSDL True
@@ -312,7 +306,7 @@ _drawHexagonSDL filled videoSurface centerx centery radius pixel = do
312 let r = fromIntegral radius 306 let r = fromIntegral radius
313 let points = map (\(x, y) -> (centerx + x, centery + y)) $ 307 let points = map (\(x, y) -> (centerx + x, centery + y)) $
314 map (\(x, y) -> (round x, round y)) $ 308 map (\(x, y) -> (round x, round y)) $
315 map (\i -> (r * cos(pi/3 * (i)), r * sin(pi/3 * (i)))) $ map fromIntegral [0 .. 5] 309 map (\i -> (r * cos(pi/3 * (i)), r * sin(pi/3 * (i)))) [0 .. 5]
316 (if not filled then SDL.Primitive.polygon else SDL.Primitive.filledPolygon) videoSurface points pixel 310 (if not filled then SDL.Primitive.polygon else SDL.Primitive.filledPolygon) videoSurface points pixel
317 311
318centerText videoSurface x y font fgColor bgColor text = do 312centerText videoSurface x y font fgColor bgColor text = do
@@ -324,17 +318,17 @@ centerText videoSurface x y font fgColor bgColor text = do
324 _ <- SDL.blitSurface fontSurface (Just fontClipRect) videoSurface (Just (SDL.Rect (fromIntegral(x) - w `div` 2) (fromIntegral(y) - h `div` 2) w h)) 318 _ <- SDL.blitSurface fontSurface (Just fontClipRect) videoSurface (Just (SDL.Rect (fromIntegral(x) - w `div` 2) (fromIntegral(y) - h `div` 2) w h))
325 return () 319 return ()
326 320
327pitchIndex colsRepeat = concat $ map (\x -> unique) [0 .. colsRepeat - 1] 321pitchIndex colsRepeat = concatMap (const unique) [0 .. colsRepeat - 1]
328 where 322 where
329 toprow = map (\i -> if (i `mod` 2) == 0 then (_AXIS_TOPLEFT_PITCH + i `div` 2) else (_AXIS_TOPLEFT_PITCH - 3 + i `div` 2)) [0 .. _AXIS_UNIQUE_COLS - 1] 323 toprow = map (\i -> if i `mod` 2 == 0 then _AXIS_TOPLEFT_PITCH + i `div` 2 else _AXIS_TOPLEFT_PITCH - 3 + i `div` 2) [0 .. _AXIS_UNIQUE_COLS - 1]
330 colfrom top = map (+ top) $ map (* (-7)) [0 .. _AXIS_ROWS - 1] 324 colfrom top = map ((+ top) . (* (-7))) [0 .. _AXIS_ROWS - 1]
331 unique = concat $ map colfrom toprow 325 unique = concatMap colfrom toprow
332 326
333getKeyLocations colsRepeat (SDL.Rect offx offy totalw totalh) = 327getKeyLocations colsRepeat (SDL.Rect offx offy totalw totalh) =
334 let (key_height, key_width, xys) = getKeyLocationsAbs colsRepeat 328 let (key_height, key_width, xys) = getKeyLocationsAbs colsRepeat
335 329
336 screenw = fromIntegral(totalw) 330 screenw = fromIntegral totalw
337 screenh = fromIntegral(totalh) 331 screenh = fromIntegral totalh
338 332
339 kb_rows = length xys 333 kb_rows = length xys
340 kb_cols = length (head xys) 334 kb_cols = length (head xys)
@@ -358,29 +352,28 @@ getKeyLocations colsRepeat (SDL.Rect offx offy totalw totalh) =
358 (floor(kw/2 * 15/16), map (\(x, y) -> (floor(scale * x + centerx + kw/2), floor(scale * y + centery + kh/2))) $ concat $ zipzip xys) 352 (floor(kw/2 * 15/16), map (\(x, y) -> (floor(scale * x + centerx + kw/2), floor(scale * y + centery + kh/2))) $ concat $ zipzip xys)
359 353
360getKeyLocationsAbs colsRepeat = 354getKeyLocationsAbs colsRepeat =
361 let kb_rows = (fromIntegral _AXIS_ROWS) :: Double 355 let kb_rows = fromIntegral _AXIS_ROWS :: Double
362 kb_cols = (fromIntegral colsRepeat) * (fromIntegral _AXIS_UNIQUE_COLS) :: Double 356 kb_cols = fromIntegral colsRepeat * fromIntegral _AXIS_UNIQUE_COLS :: Double
363 -- the edges of the hexagon are equal in length to its "radius" 357 -- the edges of the hexagon are equal in length to its "radius"
364 -- if the radius is 1, then horizontal movement is 1.5 and vertical movement is sqrt(3) from one center to the next 358 -- if the radius is 1, then horizontal movement is 1.5 and vertical movement is sqrt(3) from one center to the next
365 -- or else it is 2*sqrt(3) to move down 359 -- or else it is 2*sqrt(3) to move down
366 360
367 kw = 1 :: Double 361 kw = 1 :: Double
368 kh = kw/2 * sqrt(3) -- hexagon ratio 362 kh = kw/2 * sqrt 3 -- hexagon ratio
369 363
370 xys = 364 xys =
371 map (\y -> map (\i -> 365 map ((\y -> map (\i ->
372 366
373 let repetition = i `div` fromIntegral(_AXIS_UNIQUE_COLS) 367 let repetition = i `div` fromIntegral _AXIS_UNIQUE_COLS
374 odd = 1 == i `mod` 2 368 odd = 1 == i `mod` 2
375 dropBy = if odd then kh / 2 + kh * fromInteger(repetition `div` 2) 369 dropBy = if odd then kh / 2 + kh * fromInteger (repetition `div` 2)
376 else kh * fromInteger((repetition + 1) `div` 2) 370 else kh * fromInteger ((repetition + 1) `div` 2)
377 in 371 in
378 ( 372 (
379 fromInteger(i) * kw * 3 / 4, 373 fromInteger i * kw * 3 / 4,
380 y + dropBy 374 y + dropBy
381 )) [0 .. round(kb_cols) - 1]) $ 375 )) [0 .. round kb_cols - 1]) . (\i -> kh * fromIntegral i))
382 map (\i -> kh * fromIntegral(i)) 376 [0..round kb_rows - 1]
383 [0..round(kb_rows) - 1]
384 in 377 in
385 (kh, kw, xys) 378 (kh, kw, xys)
386 379
@@ -407,6 +400,6 @@ parseSDLEvents keysDown others = do
407 _ -> parseSDLEvents keysDown others 400 _ -> parseSDLEvents keysDown others
408 401
409keyDown :: SDL.Keysym.SDLKey -> Set.Set SDL.Keysym.SDLKey -> Bool 402keyDown :: SDL.Keysym.SDLKey -> Set.Set SDL.Keysym.SDLKey -> Bool
410keyDown k s = Set.member k s 403keyDown = Set.member
411 404
412deriving instance Ord SDL.Keysym 405deriving instance Ord SDL.Keysym