Переглянути джерело

Bugfix: Jump button could be missed on high frequency monitors (#913)

Jorrit Rouwe 1 рік тому
батько
коміт
68d94370d7

+ 2 - 9
Samples/Tests/Character/CharacterBaseTest.cpp

@@ -548,15 +548,8 @@ void CharacterBaseTest::ProcessInput(const ProcessInputParams &inParams)
 	mControlInput = rotation * mControlInput;
 
 	// Check actions
-	mJump = false;
-	mSwitchStance = false;
-	for (int key = inParams.mKeyboard->GetFirstKey(); key != 0; key = inParams.mKeyboard->GetNextKey())
-	{
-		if (key == DIK_RSHIFT)
-			mSwitchStance = true;
-		else if (key == DIK_RCONTROL)
-			mJump = true;
-	}
+	mJump = inParams.mKeyboard->IsKeyPressedAndTriggered(DIK_RCONTROL, mWasJump);
+	mSwitchStance = inParams.mKeyboard->IsKeyPressedAndTriggered(DIK_RSHIFT, mWasSwitchStance);
 }
 
 void CharacterBaseTest::PrePhysicsUpdate(const PreUpdateParams &inParams)

+ 2 - 0
Samples/Tests/Character/CharacterBaseTest.h

@@ -120,5 +120,7 @@ private:
 	// Player input
 	Vec3					mControlInput = Vec3::sZero();
 	bool					mJump = false;
+	bool					mWasJump = false;
 	bool					mSwitchStance = false;
+	bool					mWasSwitchStance = false;
 };

+ 1 - 4
Samples/Tests/Character/CharacterSpaceShipTest.cpp

@@ -67,10 +67,7 @@ void CharacterSpaceShipTest::ProcessInput(const ProcessInputParams &inParams)
 	mDesiredVelocity = 0.25f * control_input * cCharacterSpeed + 0.75f * mDesiredVelocity;
 
 	// Check actions
-	mJump = false;
-	for (int key = inParams.mKeyboard->GetFirstKey(); key != 0; key = inParams.mKeyboard->GetNextKey())
-		if (key == DIK_RCONTROL)
-			mJump = true;
+	mJump = inParams.mKeyboard->IsKeyPressedAndTriggered(DIK_RCONTROL, mWasJump);
 }
 
 void CharacterSpaceShipTest::PrePhysicsUpdate(const PreUpdateParams &inParams)

+ 1 - 0
Samples/Tests/Character/CharacterSpaceShipTest.h

@@ -70,4 +70,5 @@ private:
 	// Player input
 	Vec3					mDesiredVelocity = Vec3::sZero();
 	bool					mJump = false;
+	bool					mWasJump = false;
 };

+ 8 - 0
TestFramework/Input/Keyboard.h

@@ -29,6 +29,14 @@ public:
 	bool							IsKeyPressed(int inKey) const		{ return mKeyPressed[inKey] != 0; }
 	bool							IsKeyDoubleClicked(int inKey) const	{ return mKeyDoubleClicked[inKey] != 0; }
 
+	/// Checks if a key is pressed and was not pressed the last time this function was called (state is stored in ioPrevState)
+	bool							IsKeyPressedAndTriggered(int inKey, bool &ioPrevState) const
+	{
+		bool prev_state = ioPrevState;
+		ioPrevState = IsKeyPressed(inKey);
+		return ioPrevState && !prev_state;
+	}
+
 	/// Buffered keyboard input, returns 0 for none or one of the DIK_* constants
 	int								GetFirstKey();
 	int								GetNextKey();