diff options
author | ClxS <slxxls92@gmail.com> | 2016-03-12 22:36:30 +0000 |
---|---|---|
committer | ClxS <slxxls92@gmail.com> | 2016-03-12 22:36:30 +0000 |
commit | 78609647e9e5ed6310942eaf104f495353e328e3 (patch) | |
tree | 278bf90be1aff05e623c17e3cebc37709b20d898 /StardewModdingAPI/Inheritance/SGame.cs | |
parent | bc84a66a6fd2dd3b334eaf8405ef8b4fc6b4acc0 (diff) | |
parent | d367b0a7bceeac210ee7336477f98559b6122d13 (diff) | |
download | SMAPI-78609647e9e5ed6310942eaf104f495353e328e3.tar.gz SMAPI-78609647e9e5ed6310942eaf104f495353e328e3.tar.bz2 SMAPI-78609647e9e5ed6310942eaf104f495353e328e3.zip |
Updated release branch to 0.37.3
Diffstat (limited to 'StardewModdingAPI/Inheritance/SGame.cs')
-rw-r--r-- | StardewModdingAPI/Inheritance/SGame.cs | 159 |
1 files changed, 155 insertions, 4 deletions
diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index c7b07c43..735cd58a 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -43,6 +43,113 @@ 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 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> buttons = new List<Buttons>();
+ 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);
+ List<Buttons> buttons = new List<Buttons>();
+ 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);
+ 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> buttons = new List<Buttons>();
+ 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; }
@@ -115,6 +222,9 @@ namespace StardewModdingAPI.Inheritance Log.Verbose("XNA Initialize");
ModItems = new Dictionary<Int32, SObject>();
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();
}
@@ -143,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)
@@ -233,11 +347,47 @@ 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);
+
+ for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
+ {
+ Buttons[] buttons = GetFramePressedButtons(i);
+ foreach (Buttons b in buttons)
+ {
+ 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++)
+ {
+ foreach (Buttons b in GetFrameReleasedButtons(i))
+ {
+ 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);
+ }
+ }
+ }
+
+
if (KStateNow != KStatePrior)
{
Events.ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow);
@@ -315,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)
|