From bfe6537f84fe780197c4554f360f19c3f9f12371 Mon Sep 17 00:00:00 2001 From: ClxS Date: Sun, 6 Mar 2016 19:46:47 +0000 Subject: Added KeyReleased event --- StardewModdingAPI/Inheritance/SGame.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'StardewModdingAPI/Inheritance') diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index c7b07c43..120f4a6e 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -43,6 +43,10 @@ namespace StardewModdingAPI.Inheritance { get { return CurrentlyPressedKeys.Where(x => !PreviouslyPressedKeys.Contains(x)).ToArray(); } } + public Keys[] FrameReleasedKeys + { + get { return PreviouslyPressedKeys.Where(x => !CurrentlyPressedKeys.Contains(x)).ToArray(); } + } public int PreviousGameLocations { get; private set; } public int PreviousLocationObjects { get; private set; } @@ -233,11 +237,15 @@ namespace StardewModdingAPI.Inheritance { KStateNow = Keyboard.GetState(); CurrentlyPressedKeys = KStateNow.GetPressedKeys(); + MStateNow = Mouse.GetState(); foreach (Keys k in FramePressedKeys) Events.ControlEvents.InvokeKeyPressed(k); - + + foreach (Keys k in FrameReleasedKeys) + Events.ControlEvents.InvokeKeyReleased(k); + if (KStateNow != KStatePrior) { Events.ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow); -- cgit From 49090c98fcdc11ca536de42875f50b763e83ce63 Mon Sep 17 00:00:00 2001 From: ClxS Date: Sun, 6 Mar 2016 23:28:32 +0000 Subject: Fixed mod content path not being set correctly. Fixed object draw code. Custom objects can now be placed and show up correctly. --- StardewModdingAPI/Entities/SCharacter.cs | 12 ++++++++++ StardewModdingAPI/Entities/SFarm.cs | 12 ++++++++++ StardewModdingAPI/Entities/SFarmAnimal.cs | 12 ++++++++++ StardewModdingAPI/Entities/SNpc.cs | 12 ++++++++++ StardewModdingAPI/Entities/SPlayer.cs | 37 ++++++++++++++++++++++++++++++ StardewModdingAPI/Inheritance/SObject.cs | 13 ++++++----- StardewModdingAPI/Program.cs | 28 +++++++++++----------- StardewModdingAPI/StardewModdingAPI.csproj | 8 +++++++ TrainerMod/TrainerMod.cs | 4 +--- TrainerMod/TrainerMod.csproj | 6 ++--- 10 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 StardewModdingAPI/Entities/SCharacter.cs create mode 100644 StardewModdingAPI/Entities/SFarm.cs create mode 100644 StardewModdingAPI/Entities/SFarmAnimal.cs create mode 100644 StardewModdingAPI/Entities/SNpc.cs create mode 100644 StardewModdingAPI/Entities/SPlayer.cs (limited to 'StardewModdingAPI/Inheritance') diff --git a/StardewModdingAPI/Entities/SCharacter.cs b/StardewModdingAPI/Entities/SCharacter.cs new file mode 100644 index 00000000..740a6d7f --- /dev/null +++ b/StardewModdingAPI/Entities/SCharacter.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Entities +{ + class SCharacter + { + } +} diff --git a/StardewModdingAPI/Entities/SFarm.cs b/StardewModdingAPI/Entities/SFarm.cs new file mode 100644 index 00000000..5d1647a8 --- /dev/null +++ b/StardewModdingAPI/Entities/SFarm.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Entities +{ + class SFarm + { + } +} diff --git a/StardewModdingAPI/Entities/SFarmAnimal.cs b/StardewModdingAPI/Entities/SFarmAnimal.cs new file mode 100644 index 00000000..0f768f6a --- /dev/null +++ b/StardewModdingAPI/Entities/SFarmAnimal.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Entities +{ + class SFarmAnimal + { + } +} diff --git a/StardewModdingAPI/Entities/SNpc.cs b/StardewModdingAPI/Entities/SNpc.cs new file mode 100644 index 00000000..02242d20 --- /dev/null +++ b/StardewModdingAPI/Entities/SNpc.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Entities +{ + class SNpc + { + } +} diff --git a/StardewModdingAPI/Entities/SPlayer.cs b/StardewModdingAPI/Entities/SPlayer.cs new file mode 100644 index 00000000..ae4e9472 --- /dev/null +++ b/StardewModdingAPI/Entities/SPlayer.cs @@ -0,0 +1,37 @@ +using StardewModdingAPI.Inheritance; +using StardewValley; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Entities +{ + public static class SPlayer + { + public static List AllFarmers + { + get + { + return SGame.getAllFarmers(); + } + } + + public static Farmer CurrentFarmer + { + get + { + return SGame.player; + } + } + + public static GameLocation CurrentFarmerLocation + { + get + { + return SGame.player.currentLocation; + } + } + } +} diff --git a/StardewModdingAPI/Inheritance/SObject.cs b/StardewModdingAPI/Inheritance/SObject.cs index 28254c24..3dcddb9e 100644 --- a/StardewModdingAPI/Inheritance/SObject.cs +++ b/StardewModdingAPI/Inheritance/SObject.cs @@ -65,17 +65,18 @@ namespace StardewModdingAPI.Inheritance } public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1) - { + { if (Texture != null) { - int targSize = Game1.tileSize; - - Vector2 local = Game1.GlobalToLocal(Game1.viewport, new Vector2(x,y)); - Rectangle targ = new Rectangle((int)local.X, (int)local.Y, targSize, targSize); - spriteBatch.Draw(Texture, targ, null, new Color(255, 255, 255, 255f * alpha)); + spriteBatch.Draw(Texture, Game1.GlobalToLocal(Game1.viewport, new Vector2((float)(((x * Game1.tileSize) + (Game1.tileSize / 2)) + ((this.shakeTimer > 0) ? Game1.random.Next(-1, 2) : 0)), (float)(((y * Game1.tileSize) + (Game1.tileSize / 2)) + ((this.shakeTimer > 0) ? Game1.random.Next(-1, 2) : 0)))), new Rectangle?(Game1.currentLocation.getSourceRectForObject(this.ParentSheetIndex)), (Color)(Color.White * alpha), 0f, new Vector2(8f, 8f), (this.scale.Y > 1f) ? this.getScale().Y : ((float)Game1.pixelZoom), this.flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, (this.isPassable() ? ((float)this.getBoundingBox(new Vector2((float)x, (float)y)).Top) : ((float)this.getBoundingBox(new Vector2((float)x, (float)y)).Bottom)) / 10000f); } } + public void drawAsProp(SpriteBatch b) + { + + } + public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1) { Log.Debug("THIS DRAW FUNCTION IS NOT IMPLEMENTED I WANT TO KNOW WHERE IT IS CALLED"); diff --git a/StardewModdingAPI/Program.cs b/StardewModdingAPI/Program.cs index f1ccd936..61770f8e 100644 --- a/StardewModdingAPI/Program.cs +++ b/StardewModdingAPI/Program.cs @@ -89,7 +89,7 @@ namespace StardewModdingAPI //TODO: Have an app.config and put the paths inside it so users can define locations to load mods from _modPaths.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "Mods")); _modPaths.Add(Path.Combine(Constants.ExecutionPath, "Mods")); - _modPaths.Add(Path.Combine(Constants.ExecutionPath, "Mods", "Content")); + _modContentPaths.Add(Path.Combine(Constants.ExecutionPath, "Mods", "Content")); _modContentPaths.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "Mods", "Content")); //Checks that all defined modpaths exist as directories @@ -359,25 +359,25 @@ namespace StardewModdingAPI DebugPixel.SetData(new Color[] { Color.White }); #if DEBUG - Log.Verbose("REGISTERING BASE CUSTOM ITEM"); + StardewModdingAPI.Log.Verbose("REGISTERING BASE CUSTOM ITEM"); SObject so = new SObject(); so.Name = "Mario Block"; so.CategoryName = "SMAPI Test Mod"; so.Description = "It's a block from Mario!\nLoaded in realtime by SMAPI."; - so.Texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, new FileStream(ModContentPaths[0] + "\\Test.png", FileMode.Open)); + so.Texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, new FileStream(_modContentPaths[0] + "\\Test.png", FileMode.Open)); so.IsPassable = true; so.IsPlaceable = true; - Log.Verbose("REGISTERED WITH ID OF: " + SGame.RegisterModItem(so)); - - Log.Verbose("REGISTERING SECOND CUSTOM ITEM"); - SObject so2 = new SObject(); - so2.Name = "Mario Painting"; - so2.CategoryName = "SMAPI Test Mod"; - so2.Description = "It's a painting of a creature from Mario!\nLoaded in realtime by SMAPI."; - so2.Texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, new FileStream(ModContentPaths[0] + "\\PaintingTest.png", FileMode.Open)); - so2.IsPassable = true; - so2.IsPlaceable = true; - Log.Verbose("REGISTERED WITH ID OF: " + SGame.RegisterModItem(so2)); + StardewModdingAPI.Log.Verbose("REGISTERED WITH ID OF: " + SGame.RegisterModItem(so)); + + //StardewModdingAPI.Log.Verbose("REGISTERING SECOND CUSTOM ITEM"); + //SObject so2 = new SObject(); + //so2.Name = "Mario Painting"; + //so2.CategoryName = "SMAPI Test Mod"; + //so2.Description = "It's a painting of a creature from Mario!\nLoaded in realtime by SMAPI."; + //so2.Texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, new FileStream(_modContentPaths[0] + "\\PaintingTest.png", FileMode.Open)); + //so2.IsPassable = true; + //so2.IsPlaceable = true; + //StardewModdingAPI.Log.Verbose("REGISTERED WITH ID OF: " + SGame.RegisterModItem(so2)); Command.CallCommand("load"); #endif diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index 6d70b1c5..5b962d44 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -61,6 +61,9 @@ x86 bin\x86\Debug\ false + + + true x86 @@ -99,6 +102,11 @@ + + + + + diff --git a/TrainerMod/TrainerMod.cs b/TrainerMod/TrainerMod.cs index 9f918ce4..e2482c5d 100644 --- a/TrainerMod/TrainerMod.cs +++ b/TrainerMod/TrainerMod.cs @@ -762,12 +762,10 @@ namespace TrainerMod static void RegisterNewItem(object sender, EventArgsCommand e) { #if DEBUG - Log.Error("Experimental code cannot be run in user mode."); - return; -#endif SObject s = SGame.PullModItemFromDict(0, true); s.Stack = 999; Game1.player.addItemToInventory(s); +#endif } } } diff --git a/TrainerMod/TrainerMod.csproj b/TrainerMod/TrainerMod.csproj index 8753bc1b..3cd42786 100644 --- a/TrainerMod/TrainerMod.csproj +++ b/TrainerMod/TrainerMod.csproj @@ -17,7 +17,7 @@ true full false - bin\Debug\ + ..\StardewModdingAPI\bin\x86\Debug\Mods\ DEBUG;TRACE prompt 4 @@ -41,7 +41,7 @@ False - $(SteamInstallPath)\steamapps\common\Stardew Valley\Stardew Valley.exe + ..\..\..\..\Games\SteamLibrary\steamapps\common\Stardew Valley\Stardew Valley.exe False @@ -53,7 +53,7 @@ - $(SteamInstallPath)\steamapps\common\Stardew Valley\xTile.dll + ..\..\..\..\Games\SteamLibrary\steamapps\common\Stardew Valley\xTile.dll False -- cgit 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/Inheritance') 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 From 0462c9de79a2f1a02e720ac24400d1bf00f74680 Mon Sep 17 00:00:00 2001 From: ClxS Date: Mon, 7 Mar 2016 17:25:51 +0000 Subject: Finished gamepad input events --- StardewModdingAPI/Events/Controls.cs | 12 ++++ StardewModdingAPI/Events/EventArgs.cs | 30 +++++++- StardewModdingAPI/Inheritance/SGame.cs | 108 ++++++++++++++++++++++++++--- StardewModdingAPI/StardewModdingAPI.csproj | 10 +-- 4 files changed, 145 insertions(+), 15 deletions(-) (limited to 'StardewModdingAPI/Inheritance') diff --git a/StardewModdingAPI/Events/Controls.cs b/StardewModdingAPI/Events/Controls.cs index fa344bab..c79c28f6 100644 --- a/StardewModdingAPI/Events/Controls.cs +++ b/StardewModdingAPI/Events/Controls.cs @@ -16,6 +16,8 @@ namespace StardewModdingAPI.Events public static event EventHandler MouseChanged = delegate { }; public static event EventHandler ControllerButtonPressed = delegate { }; public static event EventHandler ControllerButtonReleased = delegate { }; + public static event EventHandler ControllerTriggerPressed = delegate { }; + public static event EventHandler ControllerTriggerReleased = delegate { }; public static void InvokeKeyboardChanged(KeyboardState priorState, KeyboardState newState) { @@ -46,5 +48,15 @@ namespace StardewModdingAPI.Events { ControllerButtonReleased.Invoke(null, new EventArgsControllerButtonReleased(playerIndex, buttons)); } + + public static void InvokeTriggerPressed(PlayerIndex playerIndex, Buttons buttons, float value) + { + ControllerTriggerPressed.Invoke(null, new EventArgsControllerTriggerPressed(playerIndex, buttons, value)); + } + + public static void InvokeTriggerReleased(PlayerIndex playerIndex, Buttons buttons, float value) + { + ControllerTriggerReleased.Invoke(null, new EventArgsControllerTriggerReleased(playerIndex, buttons, value)); + } } } diff --git a/StardewModdingAPI/Events/EventArgs.cs b/StardewModdingAPI/Events/EventArgs.cs index 6c5e1401..ee30b406 100644 --- a/StardewModdingAPI/Events/EventArgs.cs +++ b/StardewModdingAPI/Events/EventArgs.cs @@ -38,7 +38,7 @@ namespace StardewModdingAPI.Events PlayerIndex = playerIndex; ButtonPressed = buttonPressed; } - public PlayerIndex PlayerIndex; + public PlayerIndex PlayerIndex { get; private set; } public Buttons ButtonPressed { get; private set; } } @@ -49,10 +49,36 @@ namespace StardewModdingAPI.Events PlayerIndex = playerIndex; ButtonReleased = buttonReleased; } - public PlayerIndex PlayerIndex; + public PlayerIndex PlayerIndex { get; private set; } public Buttons ButtonReleased { get; private set; } } + public class EventArgsControllerTriggerPressed : EventArgs + { + public EventArgsControllerTriggerPressed(PlayerIndex playerIndex, Buttons buttonPressed, float value) + { + PlayerIndex = playerIndex; + ButtonPressed = buttonPressed; + Value = value; + } + public PlayerIndex PlayerIndex { get; private set; } + public Buttons ButtonPressed { get; private set; } + public float Value { get; private set; } + } + + public class EventArgsControllerTriggerReleased : EventArgs + { + public EventArgsControllerTriggerReleased(PlayerIndex playerIndex, Buttons buttonReleased, float value) + { + PlayerIndex = playerIndex; + ButtonReleased = buttonReleased; + Value = value; + } + public PlayerIndex PlayerIndex { get; private set; } + public Buttons ButtonReleased { get; private set; } + public float Value { 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 8da7c412..cccdff23 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -47,15 +47,56 @@ 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); } - + + private bool WasButtonJustReleased(Buttons button, ButtonState buttonState, PlayerIndex stateIndex) + { + return buttonState == ButtonState.Released && PreviouslyPressedButtons[(int)stateIndex].Contains(button); + } + + private bool WasButtonJustPressed(Buttons button, float value, PlayerIndex stateIndex) + { + return WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); + } + + private bool WasButtonJustReleased(Buttons button, float value, PlayerIndex stateIndex) + { + return WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); + } + + public Buttons[] GetButtonsDown(PlayerIndex index) + { + GamePadState state = GamePad.GetState((PlayerIndex)index); + List buttons = new List(); + if (state.IsConnected) + { + if (state.Buttons.A == ButtonState.Pressed) buttons.Add(Buttons.A); + if (state.Buttons.B == ButtonState.Pressed) buttons.Add(Buttons.B); + if (state.Buttons.Back == ButtonState.Pressed) buttons.Add(Buttons.Back); + if (state.Buttons.BigButton == ButtonState.Pressed) buttons.Add(Buttons.BigButton); + if (state.Buttons.LeftShoulder == ButtonState.Pressed) buttons.Add(Buttons.LeftShoulder); + if (state.Buttons.LeftStick == ButtonState.Pressed) buttons.Add(Buttons.LeftStick); + if (state.Buttons.RightShoulder == ButtonState.Pressed) buttons.Add(Buttons.RightShoulder); + if (state.Buttons.RightStick == ButtonState.Pressed) buttons.Add(Buttons.RightStick); + if (state.Buttons.Start == ButtonState.Pressed) buttons.Add(Buttons.Start); + if (state.Buttons.X == ButtonState.Pressed) buttons.Add(Buttons.X); + if (state.Buttons.Y == ButtonState.Pressed) buttons.Add(Buttons.Y); + if (state.DPad.Up == ButtonState.Pressed) buttons.Add(Buttons.DPadUp); + if (state.DPad.Down == ButtonState.Pressed) buttons.Add(Buttons.DPadDown); + if (state.DPad.Left == ButtonState.Pressed) buttons.Add(Buttons.DPadLeft); + if (state.DPad.Right == ButtonState.Pressed) buttons.Add(Buttons.DPadRight); + if (state.Triggers.Left > 0.2f) buttons.Add(Buttons.LeftTrigger); + if (state.Triggers.Right > 0.2f) buttons.Add(Buttons.RightTrigger); + } + return buttons.ToArray(); + } + public Buttons[] GetFramePressedButtons(PlayerIndex index) { GamePadState state = GamePad.GetState((PlayerIndex)index); @@ -77,11 +118,39 @@ namespace StardewModdingAPI.Inheritance 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); - + if (WasButtonJustPressed(Buttons.LeftTrigger, state.Triggers.Left, index)) buttons.Add(Buttons.LeftTrigger); + if (WasButtonJustPressed(Buttons.RightTrigger, state.Triggers.Right, index)) buttons.Add(Buttons.RightTrigger); } return buttons.ToArray(); } + public Buttons[] GetFrameReleasedButtons(PlayerIndex index) + { + GamePadState state = GamePad.GetState((PlayerIndex)index); + List buttons = new List(); + if (state.IsConnected) + { + if (WasButtonJustReleased(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A); + if (WasButtonJustReleased(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B); + if (WasButtonJustReleased(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back); + if (WasButtonJustReleased(Buttons.BigButton, state.Buttons.BigButton, index)) buttons.Add(Buttons.BigButton); + if (WasButtonJustReleased(Buttons.LeftShoulder, state.Buttons.LeftShoulder, index)) buttons.Add(Buttons.LeftShoulder); + if (WasButtonJustReleased(Buttons.LeftStick, state.Buttons.LeftStick, index)) buttons.Add(Buttons.LeftStick); + if (WasButtonJustReleased(Buttons.RightShoulder, state.Buttons.RightShoulder, index)) buttons.Add(Buttons.RightShoulder); + if (WasButtonJustReleased(Buttons.RightStick, state.Buttons.RightStick, index)) buttons.Add(Buttons.RightStick); + if (WasButtonJustReleased(Buttons.Start, state.Buttons.Start, index)) buttons.Add(Buttons.Start); + if (WasButtonJustReleased(Buttons.X, state.Buttons.X, index)) buttons.Add(Buttons.X); + if (WasButtonJustReleased(Buttons.Y, state.Buttons.Y, index)) buttons.Add(Buttons.Y); + if (WasButtonJustReleased(Buttons.DPadUp, state.DPad.Up, index)) buttons.Add(Buttons.DPadUp); + if (WasButtonJustReleased(Buttons.DPadDown, state.DPad.Down, index)) buttons.Add(Buttons.DPadDown); + if (WasButtonJustReleased(Buttons.DPadLeft, state.DPad.Left, index)) buttons.Add(Buttons.DPadLeft); + if (WasButtonJustReleased(Buttons.DPadRight, state.DPad.Right, index)) buttons.Add(Buttons.DPadRight); + if (WasButtonJustReleased(Buttons.LeftTrigger, state.Triggers.Left, index)) buttons.Add(Buttons.LeftTrigger); + if (WasButtonJustReleased(Buttons.RightTrigger, state.Triggers.Right, index)) buttons.Add(Buttons.RightTrigger); + } + return buttons.ToArray(); + } + public int PreviousGameLocations { get; private set; } public int PreviousLocationObjects { get; private set; } public int PreviousItems_ { get; private set; } @@ -153,6 +222,9 @@ namespace StardewModdingAPI.Inheritance Log.Verbose("XNA Initialize"); ModItems = new Dictionary(); PreviouslyPressedKeys = new Keys[0]; + PreviouslyPressedButtons = new Buttons[4][]; + for (int i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0]; + base.Initialize(); Events.GameEvents.InvokeInitialize(); } @@ -181,6 +253,10 @@ namespace StardewModdingAPI.Inheritance Events.GameEvents.InvokeUpdateTick(); PreviouslyPressedKeys = CurrentlyPressedKeys; + for(PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + { + PreviouslyPressedButtons[(int)i] = GetButtonsDown(i); + } } protected override void Draw(GameTime gameTime) @@ -282,18 +358,32 @@ namespace StardewModdingAPI.Inheritance for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { - foreach(Buttons b in GetFramePressedButtons(i)) + Buttons[] buttons = GetFramePressedButtons(i); + foreach (Buttons b in buttons) { - Events.ControlEvents.InvokeButtonPressed(i, b); + if(b == Buttons.LeftTrigger || b == Buttons.RightTrigger) + { + Events.ControlEvents.InvokeTriggerPressed(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); + } + else + { + Events.ControlEvents.InvokeButtonPressed(i, b); + } } } for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { - GamePadState state = GamePad.GetState(i); - if (state.IsConnected) + foreach (Buttons b in GetFrameReleasedButtons(i)) { - // TODO: Process state + if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger) + { + Events.ControlEvents.InvokeTriggerReleased(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); + } + else + { + Events.ControlEvents.InvokeButtonReleased(i, b); + } } } diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index c563174a..c2da6299 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -78,8 +78,9 @@ - - D:\Games\steamapps\common\Stardew Valley\Stardew Valley.exe + + False + ..\..\..\..\Games\SteamLibrary\steamapps\common\Stardew Valley\Stardew Valley.exe @@ -90,8 +91,9 @@ - - D:\Games\steamapps\common\Stardew Valley\xTile.dll + + False + ..\..\..\..\Games\SteamLibrary\steamapps\common\Stardew Valley\xTile.dll -- cgit From 626452834fed7f15e20f2ecc0d4103be69621193 Mon Sep 17 00:00:00 2001 From: James Finlay Date: Mon, 7 Mar 2016 21:47:52 -0800 Subject: Perf improvements - The original '+=' of the GetHash method was taking ~10% of CPU usage for the game. This should improve performance considerably. - The next largest CPU usage we care about is the 'GetHash' method that gets called very often. Pulling the objects.GetHash() out will reduce hits on the method. --- StardewModdingAPI/Extensions.cs | 6 +++--- StardewModdingAPI/Inheritance/SGame.cs | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'StardewModdingAPI/Inheritance') diff --git a/StardewModdingAPI/Extensions.cs b/StardewModdingAPI/Extensions.cs index 7e849f12..c504f470 100644 --- a/StardewModdingAPI/Extensions.cs +++ b/StardewModdingAPI/Extensions.cs @@ -53,12 +53,12 @@ namespace StardewModdingAPI public static int GetHash(this IEnumerable enumerable) { - string s = string.Empty; + int hash = 0; foreach (var v in enumerable) { - s += v.GetHashCode().ToString(); + hash ^= v.GetHashCode(); } - return s.GetHashCode(); + return hash; } } } \ No newline at end of file diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index cccdff23..735cd58a 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -465,12 +465,13 @@ namespace StardewModdingAPI.Inheritance { Events.PlayerEvents.InvokeInventoryChanged(player.items, changedItems); PreviousItems = player.items.Where(n => n != null).ToDictionary(n => n, n => n.Stack); - } + } - if(currentLocation != null && PreviousLocationObjects != currentLocation.objects.GetHash()) + var objectHash = currentLocation?.objects?.GetHash(); + if(objectHash != null && PreviousLocationObjects != objectHash) { Events.LocationEvents.InvokeOnNewLocationObject(currentLocation.objects); - PreviousLocationObjects = currentLocation.objects.GetHash(); + PreviousLocationObjects = objectHash ?? -1; } if (timeOfDay != PreviousTimeOfDay) -- cgit