summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2014-01-20 19:41:00 -0500
committerAndrew Cady <d@jerkface.net>2014-01-20 19:41:00 -0500
commitd3201069a8b5deb15300601ea6b7d688ff57d1ee (patch)
tree479832e653a2d90b23ca02249548e7de3ef38b88
parentd09c723fe925b5fe92420f3b0b3db7545622870d (diff)
Implement window resizing.
This made the code uglier. Need to introduce the state monad or else actually use netwire.
-rw-r--r--axis.hs80
1 files changed, 50 insertions, 30 deletions
diff --git a/axis.hs b/axis.hs
index 8c30a32..3b3ba8f 100644
--- a/axis.hs
+++ b/axis.hs
@@ -163,50 +163,63 @@ drawKey idx videoSurface font axis_key_locations axis_key_size fillColor text ch
163fi = fromIntegral 163fi = fromIntegral
164rgbColor r g b = SDL.Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. (fi 255)) 164rgbColor r g b = SDL.Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. (fi 255))
165 165
166chooseFontSize h w = 30 * d `div` 1024 where d = min h w
167
166main = 168main =
167 withAlsaInit $ \h public private q publicAddr privateAddr -> do 169 withAlsaInit $ \h public private q publicAddr privateAddr -> do
168 cmdlineAlsaConnect h public -- fail early if bad command lines 170 cmdlineAlsaConnect h public -- fail early if bad command lines
169 171
170 SDL.withInit [SDL.InitVideo, SDL.InitTimer, SDL.InitJoystick] $ do 172 SDL.withInit [SDL.InitVideo, SDL.InitTimer, SDL.InitJoystick] $ do
171 info <- SDL.getVideoInfo 173 info <- SDL.getVideoInfo
172 let width = SDL.videoInfoWidth info 174 let sWidth = SDL.videoInfoWidth info
173 height = SDL.videoInfoHeight info 175 sHeight = SDL.videoInfoHeight info
174 warpMouse = do 176 warpMouse = do
175 _ <- SDL.warpMouse (fromIntegral (width `div` 2)) (fromIntegral (height `div` 2)) 177 _ <- SDL.warpMouse (fromIntegral (sWidth `div` 2)) (fromIntegral (sHeight `div` 2))
176 return () 178 return ()
177--screen <- SDL.setVideoMode width height 32 [SDL.SWSurface, SDL.Fullscreen] 179 _ <- SDL.setVideoMode sWidth sHeight 32 [SDL.SWSurface, SDL.Resizable]
178 screen <- SDL.setVideoMode width height 32 [SDL.SWSurface]
179 180
180 _ <- SDL.TTF.init 181 _ <- SDL.TTF.init
181 font <- SDL.TTF.openFont "LiberationMono-Bold.ttf" 30 182 font <- SDL.TTF.openFont "LiberationMono-Bold.ttf" (chooseFontSize sWidth sHeight)
182 videoSurface <- SDL.getVideoSurface
183 videoClipRect <- SDL.getClipRect videoSurface
184--_ <- SDL.showCursor False 183--_ <- SDL.showCursor False
185--_ <- SDL.grabInput True 184--_ <- SDL.grabInput True
186--warpMouse 185--warpMouse
187 186
187 -- using the pixelFormat methods gives the wrong color, with both the
188 -- real pixelFormat or the faked one, so fuck it. See colorToPixel
188--let pixelFormat = SDL.surfaceGetPixelFormat videoSurface 189--let pixelFormat = SDL.surfaceGetPixelFormat videoSurface
189 pixelFormat <- SDL.surfaceGetPixelFormat <$> SDL.createRGBSurfaceEndian [] 1 1 24 190--pixelFormat <- SDL.surfaceGetPixelFormat <$> SDL.createRGBSurfaceEndian [] 1 1 24
190 -- _ <- SDL.setRelativeMouseMode True -- SDL2. Should I use it? 191 -- _ <- SDL.setRelativeMouseMode True -- SDL2. Should I use it?
191 192
192 let (axis_key_size, axis_key_locations) = getKeyLocations videoClipRect
193
194 let _ALL_PITCHES = Set.fromList $ map (\p -> (Event.Channel 1, Event.Pitch p)) [_AXIS_BOTTOM_PITCH .. _AXIS_TOP_PITCH]
195 keysOFF really = smartDrawKeys really _ALL_PITCHES Set.empty videoSurface font axis_key_locations axis_key_size
196 allKeysOFF = keysOFF False
197 allKeysReallyOFF = keysOFF True
198 allKeysOFF
199
200 putStrLn "Initialized." 193 putStrLn "Initialized."
201 194
202 let parseAlsa keysDown = parseAlsaEvents h keysDown (forwardNoteEvent h q publicAddr) 195 let parseAlsa keysDown = parseAlsaEvents h keysDown (forwardNoteEvent h q publicAddr)
203 let loop midiKeysDown keysDown s w x = do 196 let loop firstLoop midiKeysDown keysDown resolution font s w x = do
204 keysDown' <- parseSDLEvents keysDown 197 (keysDown', resolution') <- parseSDLEvents keysDown resolution
205 midiKeysDown' <- parseAlsa midiKeysDown 198 midiKeysDown' <- parseAlsa midiKeysDown
206 (ds, s') <- stepSession s 199 (ds, s') <- stepSession s
207 (ex, w') <- stepWire w ds (Right x) 200 (ex, w') <- stepWire w ds (Right x)
208 let x' = either (const "") id ex 201 let x' = either (const "") id ex
209 202
203 Control.Monad.when (resolution' /= resolution) $ do
204 let (w, h) = resolution
205 screen <- SDL.setVideoMode w h 32 [SDL.SWSurface, SDL.Resizable]
206 return ()
207
208 let (w, h) = resolution'
209 fontSize = chooseFontSize w h
210 font' <- (if (resolution' /= resolution) then SDL.TTF.openFont "LiberationMono-Bold.ttf" fontSize else return font)
211
212 videoSurface <- SDL.getVideoSurface
213 videoClipRect <- SDL.getClipRect videoSurface
214 let (axis_key_size, axis_key_locations) = getKeyLocations videoClipRect
215
216 let _ALL_PITCHES = Set.fromList $ map (\p -> (Event.Channel 1, Event.Pitch p)) [_AXIS_BOTTOM_PITCH .. _AXIS_TOP_PITCH]
217 keysOFF really = smartDrawKeys really _ALL_PITCHES Set.empty videoSurface font' axis_key_locations axis_key_size
218 allKeysOFF = keysOFF False
219 allKeysReallyOFF = keysOFF True
220
221 Control.Monad.when(firstLoop) allKeysOFF
222
210-- Control.Monad.when (x /= x' && x' /= "") $ do 223-- Control.Monad.when (x /= x' && x' /= "") $ do
211-- textBand videoSurface videoClipRect (SDL.Rect 0 0 0 70) font x' 224-- textBand videoSurface videoClipRect (SDL.Rect 0 0 0 70) font x'
212-- return () 225-- return ()
@@ -217,11 +230,17 @@ main =
217-- let chord = show $ map (\i->elemIndices i pitchIndex) $ pitchList midiKeysDown' 230-- let chord = show $ map (\i->elemIndices i pitchIndex) $ pitchList midiKeysDown'
218-- textBand videoSurface videoClipRect (SDL.Rect 0 70 0 70) font chord 231-- textBand videoSurface videoClipRect (SDL.Rect 0 70 0 70) font chord
219 232
220 smartDrawKeys False midiKeysDown midiKeysDown' videoSurface font axis_key_locations axis_key_size 233 smartDrawKeys False midiKeysDown midiKeysDown' videoSurface font' axis_key_locations axis_key_size
234 return ()
235
236 Control.Monad.when (resolution' /= resolution) $ do
237 allKeysOFF
238 smartDrawKeys False Set.empty midiKeysDown' videoSurface font' axis_key_locations axis_key_size
221 return () 239 return ()
222 240
223 Control.Monad.when (keysDown' /= keysDown && keyDown SDL.SDLK_BACKSPACE keysDown') allKeysOFF 241 Control.Monad.when (keysDown' /= keysDown) $ do
224 Control.Monad.when (keysDown' /= keysDown && keyDown SDL.SDLK_c keysDown') allKeysReallyOFF 242 Control.Monad.when (keyDown SDL.SDLK_BACKSPACE keysDown') allKeysOFF
243 Control.Monad.when (keyDown SDL.SDLK_c keysDown') allKeysReallyOFF
225 244
226-- Control.Monad.when (keysDown' /= keysDown) $ do 245-- Control.Monad.when (keysDown' /= keysDown) $ do
227-- let chord = unwords $ map (\k -> drop 5 (show k)) $ Set.toList keysDown' 246-- let chord = unwords $ map (\k -> drop 5 (show k)) $ Set.toList keysDown'
@@ -244,9 +263,9 @@ main =
244 let delay = 1000 `div` framerate - 1000 * (floor $ dtime ds) 263 let delay = 1000 `div` framerate - 1000 * (floor $ dtime ds)
245 SDL.delay (delay) 264 SDL.delay (delay)
246 Control.Monad.when (not $ keyDown SDL.SDLK_ESCAPE keysDown) $ 265 Control.Monad.when (not $ keyDown SDL.SDLK_ESCAPE keysDown) $
247 loop midiKeysDown' keysDown' s' w' x' 266 loop False midiKeysDown' keysDown' resolution' font' s' w' x'
248 267
249 loop Set.empty Set.empty clockSession_ netwireIsCool "" 268 loop True Set.empty Set.empty (sWidth, sHeight) font clockSession_ netwireIsCool ""
250 269
251zipzip ls = if (head ls) == [] then [] else (map head ls) : (zipzip $ map tail ls) 270zipzip ls = if (head ls) == [] then [] else (map head ls) : (zipzip $ map tail ls)
252 271
@@ -339,14 +358,15 @@ textBand videoSurface videoClipRect (SDL.Rect _ y _ h) font text = do
339 return () 358 return ()
340 return () 359 return ()
341 360
342parseSDLEvents :: Set.Set SDL.SDLKey -> IO (Set.Set SDL.Keysym.SDLKey) 361--parseSDLEvents :: Set.Set SDL.SDLKey -> Set.Set SDL.Event -> IO (Set.Set SDL.Keysym.SDLKey, Set.Set SDL.Event)
343parseSDLEvents keysDown = do 362parseSDLEvents keysDown others = do
344 event <- SDL.pollEvent 363 event <- SDL.pollEvent
345 case event of 364 case event of
346 SDL.NoEvent -> return keysDown 365 SDL.NoEvent -> return (keysDown, others)
347 SDL.KeyDown (SDL.Keysym k _ _) -> parseSDLEvents (Set.insert k keysDown) 366 SDL.KeyDown (SDL.Keysym k _ _) -> parseSDLEvents (Set.insert k keysDown) others
348 SDL.KeyUp (SDL.Keysym k _ _) -> parseSDLEvents (Set.delete k keysDown) 367 SDL.KeyUp (SDL.Keysym k _ _) -> parseSDLEvents (Set.delete k keysDown) others
349 _ -> parseSDLEvents keysDown 368 SDL.VideoResize w h -> parseSDLEvents keysDown (w, h)
369 _ -> parseSDLEvents keysDown others
350 370
351keyDown :: SDL.Keysym.SDLKey -> Set.Set SDL.Keysym.SDLKey -> Bool 371keyDown :: SDL.Keysym.SDLKey -> Set.Set SDL.Keysym.SDLKey -> Bool
352keyDown k s = Set.member k s 372keyDown k s = Set.member k s