From 71bcfc11dea8c189152a9aa2534c87e1b1486018 Mon Sep 17 00:00:00 2001 From: ClxS Date: Mon, 7 Mar 2016 13:49:45 +0000 Subject: Partially completed events for gamepad input --- StardewModdingAPI/Events/Controls.cs | 15 ++++++++- StardewModdingAPI/Events/EventArgs.cs | 24 +++++++++++++- StardewModdingAPI/Inheritance/SGame.cs | 52 ++++++++++++++++++++++++++++++ StardewModdingAPI/StardewModdingAPI.csproj | 13 +++----- 4 files changed, 93 insertions(+), 11 deletions(-) (limited to 'StardewModdingAPI') diff --git a/StardewModdingAPI/Events/Controls.cs b/StardewModdingAPI/Events/Controls.cs index 8cf0f431..fa344bab 100644 --- a/StardewModdingAPI/Events/Controls.cs +++ b/StardewModdingAPI/Events/Controls.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; using System; using System.Collections.Generic; using System.Linq; @@ -13,6 +14,8 @@ namespace StardewModdingAPI.Events public static event EventHandler KeyPressed = delegate { }; public static event EventHandler KeyReleased = delegate { }; public static event EventHandler MouseChanged = delegate { }; + public static event EventHandler ControllerButtonPressed = delegate { }; + public static event EventHandler ControllerButtonReleased = delegate { }; public static void InvokeKeyboardChanged(KeyboardState priorState, KeyboardState newState) { @@ -33,5 +36,15 @@ namespace StardewModdingAPI.Events { KeyReleased.Invoke(null, new EventArgsKeyPressed(key)); } + + public static void InvokeButtonPressed(PlayerIndex playerIndex, Buttons buttons) + { + ControllerButtonPressed.Invoke(null, new EventArgsControllerButtonPressed(playerIndex, buttons)); + } + + public static void InvokeButtonReleased(PlayerIndex playerIndex, Buttons buttons) + { + ControllerButtonReleased.Invoke(null, new EventArgsControllerButtonReleased(playerIndex, buttons)); + } } } diff --git a/StardewModdingAPI/Events/EventArgs.cs b/StardewModdingAPI/Events/EventArgs.cs index 66d057a7..6c5e1401 100644 --- a/StardewModdingAPI/Events/EventArgs.cs +++ b/StardewModdingAPI/Events/EventArgs.cs @@ -30,7 +30,29 @@ namespace StardewModdingAPI.Events } public Keys KeyPressed { get; private set; } } - + + public class EventArgsControllerButtonPressed : EventArgs + { + public EventArgsControllerButtonPressed(PlayerIndex playerIndex, Buttons buttonPressed) + { + PlayerIndex = playerIndex; + ButtonPressed = buttonPressed; + } + public PlayerIndex PlayerIndex; + public Buttons ButtonPressed { get; private set; } + } + + public class EventArgsControllerButtonReleased : EventArgs + { + public EventArgsControllerButtonReleased(PlayerIndex playerIndex, Buttons buttonReleased) + { + PlayerIndex = playerIndex; + ButtonReleased = buttonReleased; + } + public PlayerIndex PlayerIndex; + public Buttons ButtonReleased { get; private set; } + } + public class EventArgsMouseStateChanged : EventArgs { public EventArgsMouseStateChanged(MouseState priorState, MouseState newState) diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index 120f4a6e..8da7c412 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -48,6 +48,40 @@ namespace StardewModdingAPI.Inheritance get { return PreviouslyPressedKeys.Where(x => !CurrentlyPressedKeys.Contains(x)).ToArray(); } } + public Buttons[][] CurrentlyPressedButtons; + public Buttons[][] PreviouslyPressedButtons; + + private bool WasButtonJustPressed(Buttons button, ButtonState buttonState, PlayerIndex stateIndex) + { + return buttonState == ButtonState.Pressed && !PreviouslyPressedButtons[(int)stateIndex].Contains(button); + } + + public Buttons[] GetFramePressedButtons(PlayerIndex index) + { + GamePadState state = GamePad.GetState((PlayerIndex)index); + List buttons = new List(); + if (state.IsConnected) + { + if (WasButtonJustPressed(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A); + if (WasButtonJustPressed(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B); + if (WasButtonJustPressed(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back); + if (WasButtonJustPressed(Buttons.BigButton, state.Buttons.BigButton, index)) buttons.Add(Buttons.BigButton); + if (WasButtonJustPressed(Buttons.LeftShoulder, state.Buttons.LeftShoulder, index)) buttons.Add(Buttons.LeftShoulder); + if (WasButtonJustPressed(Buttons.LeftStick, state.Buttons.LeftStick, index)) buttons.Add(Buttons.LeftStick); + if (WasButtonJustPressed(Buttons.RightShoulder, state.Buttons.RightShoulder, index)) buttons.Add(Buttons.RightShoulder); + if (WasButtonJustPressed(Buttons.RightStick, state.Buttons.RightStick, index)) buttons.Add(Buttons.RightStick); + if (WasButtonJustPressed(Buttons.Start, state.Buttons.Start, index)) buttons.Add(Buttons.Start); + if (WasButtonJustPressed(Buttons.X, state.Buttons.X, index)) buttons.Add(Buttons.X); + if (WasButtonJustPressed(Buttons.Y, state.Buttons.Y, index)) buttons.Add(Buttons.Y); + if (WasButtonJustPressed(Buttons.DPadUp, state.DPad.Up, index)) buttons.Add(Buttons.DPadUp); + if (WasButtonJustPressed(Buttons.DPadDown, state.DPad.Down, index)) buttons.Add(Buttons.DPadDown); + if (WasButtonJustPressed(Buttons.DPadLeft, state.DPad.Left, index)) buttons.Add(Buttons.DPadLeft); + if (WasButtonJustPressed(Buttons.DPadRight, state.DPad.Right, index)) buttons.Add(Buttons.DPadRight); + + } + return buttons.ToArray(); + } + public int PreviousGameLocations { get; private set; } public int PreviousLocationObjects { get; private set; } public int PreviousItems_ { get; private set; } @@ -246,6 +280,24 @@ namespace StardewModdingAPI.Inheritance foreach (Keys k in FrameReleasedKeys) Events.ControlEvents.InvokeKeyReleased(k); + for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + { + foreach(Buttons b in GetFramePressedButtons(i)) + { + Events.ControlEvents.InvokeButtonPressed(i, b); + } + } + + for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + { + GamePadState state = GamePad.GetState(i); + if (state.IsConnected) + { + // TODO: Process state + } + } + + if (KStateNow != KStatePrior) { Events.ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow); diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index 5b962d44..c563174a 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -78,11 +78,8 @@ - - False - $(SteamInstallPath)\steamapps\common\Stardew Valley\Stardew Valley.exe - False - True + + D:\Games\steamapps\common\Stardew Valley\Stardew Valley.exe @@ -93,10 +90,8 @@ - - False - $(SteamInstallPath)\steamapps\common\Stardew Valley\xTile.dll - True + + D:\Games\steamapps\common\Stardew Valley\xTile.dll -- cgit