key-state (empty) → 0.0.0
raw patch · 9 files changed
+230/−0 lines, 9 filesdep +basedep +hspecdep +key-statesetup-changed
Dependencies added: base, hspec, key-state
Files
- LICENSE +29/−0
- README.md +3/−0
- Setup.hs +6/−0
- key-state.cabal +51/−0
- library/KeyState.hs +33/−0
- package.yaml +31/−0
- stack.yaml +6/−0
- tests/KeyStateSpec.hs +70/−0
- tests/Main.hs +1/−0
+ LICENSE view
@@ -0,0 +1,29 @@+BSD 3-Clause License++Copyright (c) 2018, Joe Vargas+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++* Neither the name of the copyright holder nor the names of its+ contributors may be used to endorse or promote products derived from+ this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,3 @@+# key-state++Manage key and button states and statuses
+ Setup.hs view
@@ -0,0 +1,6 @@+-- This script is used to build and install your package. Typically you don't+-- need to change it. The Cabal documentation has more information about this+-- file: <https://www.haskell.org/cabal/users-guide/installing-packages.html>.+import qualified Distribution.Simple+main :: IO ()+main = Distribution.Simple.defaultMain
+ key-state.cabal view
@@ -0,0 +1,51 @@+-- This file has been generated from package.yaml by hpack version 0.17.1.+--+-- see: https://github.com/sol/hpack++name: key-state+version: 0.0.0+synopsis: Manage key and button states and statuses+description: Manage key and button states and statuses+category: Game+homepage: https://github.com/jxv/key-state#readme+bug-reports: https://github.com/jxv/key-state/issues+maintainer: Joe Vargas+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ package.yaml+ README.md+ stack.yaml++source-repository head+ type: git+ location: https://github.com/jxv/key-state++library+ hs-source-dirs:+ library+ default-extensions: NamedFieldPuns+ ghc-options: -Wall+ build-depends:+ base >=4.7 && <5+ exposed-modules:+ KeyState+ default-language: Haskell2010++test-suite key-state-tests+ type: exitcode-stdio-1.0+ main-is: Main.hs+ hs-source-dirs:+ tests+ default-extensions: NamedFieldPuns+ ghc-options: -Wall -rtsopts -threaded -with-rtsopts=-N+ build-depends:+ base+ , key-state+ , hspec+ other-modules:+ KeyStateSpec+ default-language: Haskell2010
+ library/KeyState.hs view
@@ -0,0 +1,33 @@+module KeyState where++data KeyStatus+ = KeyStatus'Untouched+ | KeyStatus'Pressed+ | KeyStatus'Held+ | KeyStatus'Released+ deriving (Show, Eq, Ord, Enum)++data KeyState count = KeyState+ { ksStatus :: KeyStatus+ , ksCounter :: Maybe count -- ^ Counter+ } deriving (Show, Eq)++initKeyState :: KeyState count+initKeyState = KeyState KeyStatus'Untouched Nothing++updateKeyState+ :: Num count+ => count -- ^ Counter delta+ -> KeyState count+ -> Bool -- ^ Touched+ -> KeyState count+updateKeyState delta KeyState{ksStatus, ksCounter} True = case ksStatus of+ KeyStatus'Untouched -> KeyState KeyStatus'Pressed Nothing+ KeyStatus'Pressed -> KeyState KeyStatus'Held Nothing+ KeyStatus'Held -> KeyState KeyStatus'Held (Just $ delta + (case ksCounter of Nothing -> 0; Just counter -> counter))+ KeyStatus'Released -> KeyState KeyStatus'Pressed Nothing+updateKeyState delta KeyState{ksStatus, ksCounter} False = case ksStatus of+ KeyStatus'Untouched -> KeyState KeyStatus'Untouched (Just $ delta + (case ksCounter of Nothing -> 0; Just counter -> counter))+ KeyStatus'Pressed -> KeyState KeyStatus'Released Nothing+ KeyStatus'Held -> KeyState KeyStatus'Released Nothing+ KeyStatus'Released -> KeyState KeyStatus'Untouched Nothing
+ package.yaml view
@@ -0,0 +1,31 @@+name: key-state+version: '0.0.0'+category: Game+synopsis: Manage key and button states and statuses+description: Manage key and button states and statuses+maintainer: Joe Vargas+default-extensions:+- NamedFieldPuns+extra-source-files:+- package.yaml+- README.md+- stack.yaml+ghc-options: -Wall+library:+ dependencies:+ - base >=4.7 && <5+ source-dirs: library+tests:+ key-state-tests:+ dependencies:+ - base+ - key-state+ - hspec+ ghc-options:+ - -rtsopts+ - -threaded+ - -with-rtsopts=-N+ main: Main.hs+ source-dirs: tests+github: jxv/key-state+license: BSD3
+ stack.yaml view
@@ -0,0 +1,6 @@+resolver: lts-9.14+packages:+- '.'+extra-deps: []+flags: {}+extra-package-dbs: []
+ tests/KeyStateSpec.hs view
@@ -0,0 +1,70 @@+{-# LANGUAGE LambdaCase #-}+module KeyStateSpec where++import Test.Hspec+import KeyState++spec :: Spec+spec = do+ describe "updateKeyState" $ do+ context "Not touching" $ do+ it "Untouched key state (init)" $ do+ let ks = initKeyState :: KeyState Int+ let expected = KeyState KeyStatus'Untouched (Just 1)+ updateKeyState 1 ks False `shouldBe` expected++ it "Untouched key state" $ do+ let ks = initKeyState { ksCounter = Just 10 } :: KeyState Int+ let expected = KeyState KeyStatus'Untouched (Just 11)+ updateKeyState 1 ks False `shouldBe` expected++ it "Pressed key state (init)" $ do+ let ks = initKeyState { ksStatus = KeyStatus'Pressed } :: KeyState Int+ let expected = KeyState KeyStatus'Released Nothing+ updateKeyState 1 ks False `shouldBe` expected++ it "Pressed key state" $ do+ let ks = KeyState KeyStatus'Pressed (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Released Nothing+ updateKeyState 1 ks False `shouldBe` expected++ it "Held key state" $ do+ let ks = KeyState KeyStatus'Held (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Released Nothing+ updateKeyState 1 ks False `shouldBe` expected++ it "Released key state" $ do+ let ks = KeyState KeyStatus'Released (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Untouched Nothing+ updateKeyState 1 ks False `shouldBe` expected++ context "Touching" $ do+ it "Untouched key state (init)" $ do+ let ks = initKeyState :: KeyState Int+ let expected = KeyState KeyStatus'Pressed Nothing+ updateKeyState 1 ks True `shouldBe` expected++ it "Untouched key state" $ do+ let ks = initKeyState { ksCounter = Just 10 } :: KeyState Int+ let expected = KeyState KeyStatus'Pressed Nothing+ updateKeyState 1 ks True `shouldBe` expected++ it "Pressed key state (init)" $ do+ let ks = initKeyState { ksStatus = KeyStatus'Pressed } :: KeyState Int+ let expected = KeyState KeyStatus'Held Nothing+ updateKeyState 1 ks True `shouldBe` expected++ it "Pressed key state" $ do+ let ks = KeyState KeyStatus'Pressed (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Held Nothing+ updateKeyState 1 ks True `shouldBe` expected++ it "Held key state" $ do+ let ks = KeyState KeyStatus'Held (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Held (Just 11)+ updateKeyState 1 ks True `shouldBe` expected++ it "Released key state" $ do+ let ks = KeyState KeyStatus'Released (Just 10) :: KeyState Int+ let expected = KeyState KeyStatus'Pressed Nothing+ updateKeyState 1 ks True `shouldBe` expected
+ tests/Main.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}