Jean-David Moisan 6 gadi atpakaļ
vecāks
revīzija
5d30e4ec30

BIN
Images/Icon.png


+ 42 - 0
Source/ActionKeyboard.cs

@@ -0,0 +1,42 @@
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Checks various keyboard conditions for a specific key.
+    /// </summary>
+    public class ActionKeyboard {
+        //constructors
+        public ActionKeyboard(Keys iNeedKey) {
+            _needKey = iNeedKey;
+        }
+
+        //public functions
+        public bool Pressed() {
+            return Pressed(_needKey) && InputHelper.IsActive;
+        }
+        public bool Holding() {
+            return Holding(_needKey) && InputHelper.IsActive;
+        }
+        public bool HoldingOnly() {
+            return HoldingOnly(_needKey) && InputHelper.IsActive;
+        }
+        public bool Released() {
+            return Released(_needKey) && InputHelper.IsActive;
+        }
+        public static bool Pressed(Keys key) {
+            return InputHelper.NewKeyboard.IsKeyDown(key) && InputHelper.OldKeyboard.IsKeyUp(key);
+        }
+        public static bool Holding(Keys key) {
+            return InputHelper.NewKeyboard.IsKeyDown(key);
+        }
+        public static bool HoldingOnly(Keys key) {
+            return InputHelper.NewKeyboard.IsKeyDown(key) && InputHelper.OldKeyboard.IsKeyDown(key);
+        }
+        public static bool Released(Keys key) {
+            return InputHelper.NewKeyboard.IsKeyUp(key) && InputHelper.OldKeyboard.IsKeyDown(key);
+        }
+
+        //private vars
+        private Keys _needKey;
+    }
+}

+ 71 - 0
Source/ActionKeyboardComposite.cs

@@ -0,0 +1,71 @@
+using System.Collections.Generic;
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Combines ActionKeyboardSet in order to trigger when either one is true.
+    /// </summary>
+    public class ActionKeyboardComposite {
+        //constructors
+        public ActionKeyboardComposite() {
+            _actionSets = new List<ActionKeyboardSet>();
+        }
+        public ActionKeyboardComposite(List<ActionKeyboardSet> actionSets) {
+            _actionSets = actionSets;
+        }
+
+        //public functions
+        public ActionKeyboardSet AddSet(Keys key) {
+            ActionKeyboardSet newSet = new ActionKeyboardSet().AddNeed(key);
+            AddSet(newSet);
+            return newSet;
+        }
+        public ActionKeyboardComposite AddSet(ActionKeyboardSet aks) {
+            _actionSets.Add(aks);
+            return this;
+        }
+        public bool Pressed() {
+            bool pressed = false;
+            foreach (ActionKeyboardSet aks in _actionSets) {
+                pressed = aks.Pressed();
+                if (pressed) {
+                    break;
+                }
+            }
+            return pressed;
+        }
+        public bool Holding() {
+            bool holding = false;
+            foreach (ActionKeyboardSet aks in _actionSets) {
+                holding = aks.Holding();
+                if (holding) {
+                    break;
+                }
+            }
+            return holding;
+        }
+        public bool HoldingOnly() {
+            bool holdingOnly = false;
+            foreach (ActionKeyboardSet aks in _actionSets) {
+                holdingOnly = aks.HoldingOnly();
+                if (holdingOnly) {
+                    break;
+                }
+            }
+            return holdingOnly;
+        }
+        public bool Released() {
+            bool released = false;
+            foreach (ActionKeyboardSet aks in _actionSets) {
+                released = aks.Released();
+                if (released) {
+                    break;
+                }
+            }
+            return released;
+        }
+
+        //private vars
+        private List<ActionKeyboardSet> _actionSets;
+    }
+}

+ 129 - 0
Source/ActionKeyboardSet.cs

@@ -0,0 +1,129 @@
+using System.Collections.Generic;
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Combines ActionKeyboard to make more complex action triggers.
+    /// </summary>
+    public class ActionKeyboardSet {
+        //constructors
+        public ActionKeyboardSet() {
+            _needAction = new List<ActionKeyboard>();
+            _notAction = new List<ActionKeyboard>();
+        }
+        public ActionKeyboardSet(List<ActionKeyboard> needAction, List<ActionKeyboard> notAction) {
+            _needAction = needAction;
+            _notAction = notAction;
+        }
+
+        //public functions
+        public ActionKeyboardSet AddNeed(Keys key) {
+            return AddNeed(new ActionKeyboard(key));
+        }
+        public ActionKeyboardSet AddNeed(ActionKeyboard action) {
+            _needAction.Add(action);
+            return this;
+        }
+        public ActionKeyboardSet AddNot(Keys key) {
+            return AddNot(new ActionKeyboard(key));
+        }
+        public ActionKeyboardSet AddNot(ActionKeyboard action) {
+            _notAction.Add(action);
+            return this;
+        }
+        public bool Pressed() {
+            bool pressed = false;
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionKeyboard ak in _needAction) {
+                pressed = pressed || ak.Pressed();
+                if (pressed) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _needAction) {
+                holding = holding && ak.Holding();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _notAction) {
+                notHolding = notHolding || ak.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return pressed && holding && !notHolding;
+        }
+        public bool Holding() {
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionKeyboard ak in _needAction) {
+                holding = holding && ak.Holding();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _notAction) {
+                notHolding = notHolding || ak.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return holding && !notHolding;
+        }
+        public bool HoldingOnly() {
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionKeyboard ak in _needAction) {
+                holding = holding && ak.HoldingOnly();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _notAction) {
+                notHolding = notHolding || ak.HoldingOnly();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return holding && !notHolding;
+        }
+        public bool Released() {
+            bool released = false;
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionKeyboard ak in _needAction) {
+                released = released || ak.Released();
+                if (released) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _needAction) {
+                holding = holding && (ak.Holding() || ak.Released());
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionKeyboard ak in _notAction) {
+                notHolding = notHolding || ak.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return released && holding && !notHolding;
+        }
+
+        //private vars
+        private List<ActionKeyboard> _needAction;
+        private List<ActionKeyboard> _notAction;
+    }
+}

+ 49 - 0
Source/ActionMouse.cs

@@ -0,0 +1,49 @@
+using System;
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Checks various Mouse conditions for a specific button.
+    /// </summary>
+    public class ActionMouse {
+        //constructors
+        public ActionMouse(Func<MouseState, ButtonState> needButton) {
+            _needButton = needButton;
+        }
+
+        //public functions
+        public bool Pressed() {
+            return Pressed(_needButton) && IsMouseValid(InputHelper.IsActive);
+        }
+        public bool Holding() {
+            return Holding(_needButton) && IsMouseValid(InputHelper.IsActive);
+        }
+        public bool HoldingOnly() {
+            return HoldingOnly(_needButton) && IsMouseValid(InputHelper.IsActive);
+        }
+        public bool Released() {
+            return Released(_needButton) && IsMouseValid(InputHelper.IsActive);
+        }
+        public static bool Pressed(Func<MouseState, ButtonState> button) {
+            return button(InputHelper.NewMouse) == ButtonState.Pressed && button(InputHelper.OldMouse) == ButtonState.Released;
+        }
+        public static bool Holding(Func<MouseState, ButtonState> button) {
+            return button(InputHelper.NewMouse) == ButtonState.Pressed;
+        }
+        public static bool HoldingOnly(Func<MouseState, ButtonState> button) {
+            return button(InputHelper.NewMouse) == ButtonState.Pressed && button(InputHelper.OldMouse) == ButtonState.Pressed;
+        }
+        public static bool Released(Func<MouseState, ButtonState> button) {
+            return button(InputHelper.NewMouse) == ButtonState.Released && button(InputHelper.OldMouse) == ButtonState.Pressed;
+        }
+        public static bool IsMouseValid(bool IsActive) {
+            if (IsActive && InputHelper.NewMouse.X >= 0 && InputHelper.NewMouse.X <= InputHelper.WindowWidth && InputHelper.NewMouse.Y >= 0 && InputHelper.NewMouse.Y <= InputHelper.WindowHeight) {
+                return true;
+            }
+            return false;
+        }
+
+        //private vars
+        private Func<MouseState, ButtonState> _needButton;
+    }
+}

+ 72 - 0
Source/ActionMouseComposite.cs

@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Combines ActionMouseSet in order to trigger when either one is true.
+    /// </summary>
+    public class ActionMouseComposite {
+        //constructors
+        public ActionMouseComposite() {
+            _actionSets = new List<ActionMouseSet>();
+        }
+        public ActionMouseComposite(List<ActionMouseSet> actionSets) {
+            _actionSets = actionSets;
+        }
+
+        //public functions
+        public ActionMouseSet AddSet(Func<MouseState, ButtonState> button) {
+            ActionMouseSet newSet = new ActionMouseSet().AddNeed(button);
+            AddSet(newSet);
+            return newSet;
+        }
+        public ActionMouseComposite AddSet(ActionMouseSet ams) {
+            _actionSets.Add(ams);
+            return this;
+        }
+        public bool Pressed() {
+            bool pressed = false;
+            foreach (ActionMouseSet ams in _actionSets) {
+                pressed = ams.Pressed();
+                if (pressed) {
+                    break;
+                }
+            }
+            return pressed;
+        }
+        public bool Holding() {
+            bool holding = false;
+            foreach (ActionMouseSet ams in _actionSets) {
+                holding = ams.Holding();
+                if (holding) {
+                    break;
+                }
+            }
+            return holding;
+        }
+        public bool HoldingOnly() {
+            bool holdingOnly = false;
+            foreach (ActionMouseSet ams in _actionSets) {
+                holdingOnly = ams.HoldingOnly();
+                if (holdingOnly) {
+                    break;
+                }
+            }
+            return holdingOnly;
+        }
+        public bool Released() {
+            bool released = false;
+            foreach (ActionMouseSet ams in _actionSets) {
+                released = ams.Released();
+                if (released) {
+                    break;
+                }
+            }
+            return released;
+        }
+
+        //private vars
+        private List<ActionMouseSet> _actionSets;
+    }
+}

+ 136 - 0
Source/ActionMouseSet.cs

@@ -0,0 +1,136 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.Xna.Framework.Input;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Combines ActionMouse to make more complex action triggers.
+    /// </summary>
+    public class ActionMouseSet {
+        //constructors
+        public ActionMouseSet() {
+            _needAction = new List<ActionMouse>();
+            _notAction = new List<ActionMouse>();
+        }
+        public ActionMouseSet(List<ActionMouse> needAction, List<ActionMouse> notAction) {
+            _needAction = needAction;
+            _notAction = notAction;
+        }
+
+        //public functions
+        public ActionMouseSet AddNeed(Func<MouseState, ButtonState> button) {
+            return AddNeed(new ActionMouse(button));
+        }
+        public ActionMouseSet AddNeed(ActionMouse action) {
+            _needAction.Add(action);
+            return this;
+        }
+        public ActionMouseSet AddNot(Func<MouseState, ButtonState> button) {
+            return AddNot(new ActionMouse(button));
+        }
+        public ActionMouseSet AddNot(ActionMouse action) {
+            _notAction.Add(action);
+            return this;
+        }
+        public bool Pressed() {
+            bool pressed = false;
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionMouse am in _needAction) {
+                pressed = pressed || am.Pressed();
+                if (pressed) {
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _needAction) {
+                holding = holding && am.Holding();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _notAction) {
+                notHolding = notHolding || am.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return pressed && holding && !notHolding;
+        }
+        public bool Holding() {
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionMouse am in _needAction) {
+                holding = holding && am.Holding();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _notAction) {
+                notHolding = notHolding || am.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return holding && !notHolding;
+        }
+        public bool HoldingOnly() {
+            bool holding = true;
+            bool notHolding = false;
+
+            foreach (ActionMouse am in _needAction) {
+                holding = holding && am.HoldingOnly();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _notAction) {
+                notHolding = notHolding || am.HoldingOnly();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return holding && !notHolding;
+        }
+        public bool Released() {
+            bool released = false;
+            bool holding = true;
+            bool notHolding = false;
+
+            ActionMouse releasedMouse = null;
+
+            foreach (ActionMouse am in _needAction) {
+                released = released || am.Released();
+                if (released) {
+                    releasedMouse = am;
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _needAction) {
+                if (am == releasedMouse) {
+                    continue;
+                }
+                holding = holding && am.Holding();
+                if (!holding) {
+                    break;
+                }
+            }
+            foreach (ActionMouse am in _notAction) {
+                notHolding = notHolding || am.Holding();
+                if (notHolding) {
+                    break;
+                }
+            }
+
+            return released && holding && !notHolding;
+        }
+
+        //private vars
+        private List<ActionMouse> _needAction;
+        private List<ActionMouse> _notAction;
+    }
+}

+ 22 - 0
Source/Apos.Input.csproj

@@ -0,0 +1,22 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
+    <PackageId>Apos.Input</PackageId>
+    <Title>Apos.Input</Title>
+    <Version>0.1.0</Version>
+    <Description>Input library for MonoGame.</Description>
+    <Authors>Jean-David Moisan</Authors>
+    <Company>Vyne Enterprise Inc.</Company>
+    <Copyright>Copyright 2018</Copyright>
+    <PackageTags>gamedev;monogame;ui</PackageTags>
+    <PackageIconUrl>https://raw.githubusercontent.com/Apostolique/Apos.Input/master/Images/Icon.png</PackageIconUrl>
+    <RepositoryUrl>https://github.com/Apostolique/Apos.Input</RepositoryUrl>
+    <PackageProjectUrl>https://github.com/Apostolique/Apos.Input</PackageProjectUrl>
+    <PackageLicenseExpression>MIT</PackageLicenseExpression>
+    <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
+    <RepositoryType>git</RepositoryType>
+  </PropertyGroup>
+  <ItemGroup>
+    <PackageReference Include="MonoGame.Framework.Portable" PrivateAssets="All" Version="3.6.0.1625" />
+  </ItemGroup>
+</Project>

+ 80 - 0
Source/InputHelper.cs

@@ -0,0 +1,80 @@
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Input.Touch;
+
+namespace Apos.Input {
+    /// <summary>
+    /// Goal: Unorganized helper functions for Apos.Input.
+    /// </summary>
+    public static class InputHelper {
+        //public vars
+        public static Game Game;
+        public static bool IsActive => Game.IsActive;
+        public static GameWindow Window => Game.Window;
+        public static int WindowWidth => Window.ClientBounds.Width;
+        public static int WindowHeight => Window.ClientBounds.Height;
+        public static MouseState OldMouse => _oldMouse;
+        public static MouseState NewMouse => _newMouse;
+        public static KeyboardState OldKeyboard => _oldKeyboard;
+        public static KeyboardState NewKeyboard => _newKeyboard;
+        public static TouchCollection NewTouchCollection => _newTouchCollection;
+        public static TouchPanelCapabilities TouchPanelCapabilities => _touchPanelCapabilities;
+        public static GamePadState[] OldGamePad => _oldGamePad;
+        public static GamePadState[] NewGamePad => _newGamepad;
+        public static GamePadCapabilities[] GamePadCapabilities => _gamePadCapabilities;
+
+        //public functions
+        public static void Update() {
+            if (!_initiated) {
+                Setup();
+            }
+
+            _oldMouse = _newMouse;
+            _oldKeyboard = _newKeyboard;
+            _oldGamePad = _newGamepad;
+
+            _newMouse = Mouse.GetState();
+            _newKeyboard = Keyboard.GetState();
+
+            _newTouchCollection = TouchPanel.GetState();
+            _touchPanelCapabilities = TouchPanel.GetCapabilities();
+
+            _newGamepad = new GamePadState[GamePad.MaximumGamePadCount];
+            _gamePadCapabilities = new GamePadCapabilities[GamePad.MaximumGamePadCount];
+            for (int i = 0; i < GamePad.MaximumGamePadCount; i++) {
+                _newGamepad[i] = GamePad.GetState(i);
+            }
+            for (int i = 0; i < GamePad.MaximumGamePadCount; i++) {
+                _gamePadCapabilities[i] = GamePad.GetCapabilities(i);
+            }
+        }
+
+        //private functions
+        private static void Setup() {
+            _newMouse = Mouse.GetState();
+            _newKeyboard = Keyboard.GetState();
+            TouchPanel.GetCapabilities();
+
+            _newGamepad = new GamePadState[GamePad.MaximumGamePadCount];
+            for (int i = 0; i < GamePad.MaximumGamePadCount; i++) {
+                _newGamepad[i] = GamePad.GetState(i);
+            }
+
+            _newTouchCollection = TouchPanel.GetState();
+
+            _initiated = true;
+        }
+
+        //private vars
+        private static bool _initiated = false;
+        private static MouseState _oldMouse;
+        private static MouseState _newMouse;
+        private static KeyboardState _oldKeyboard;
+        private static KeyboardState _newKeyboard;
+        private static TouchCollection _newTouchCollection;
+        private static TouchPanelCapabilities _touchPanelCapabilities;
+        private static GamePadState[] _oldGamePad;
+        private static GamePadState[] _newGamepad;
+        private static GamePadCapabilities[] _gamePadCapabilities;
+    }
+}