[ion] Keyboard: don't detect a key down when the mouse is too far away

from a key
This commit is contained in:
Émilie Feral
2020-06-30 12:48:33 +02:00
committed by EmilieNumworks
parent 4c41b1699d
commit 1c0f6a7913
2 changed files with 41 additions and 3 deletions

View File

@@ -43,7 +43,9 @@ public:
}
operator uint64_t() const { return m_bitField; }
void setKey(Key k) {
m_bitField |= (uint64_t)1 << (uint8_t)k;
if (k != Key::None) {
m_bitField |= (uint64_t)1 << (uint8_t)k;
}
}
void clearKey(Key k) {
m_bitField &= ~((uint64_t)1 << (uint8_t)k);

View File

@@ -1,4 +1,5 @@
#include "layout.h"
#include <limits.h>
namespace Ion {
namespace Simulator {
@@ -147,16 +148,51 @@ static void getKeyCenter(int validKeyIndex, SDL_Point * point) {
makeAbsolute(&sKeyCenters[validKeyIndex], point);
}
#if 0
/* TODO: once we use std++14, we can make maxHalfDistanceBetweekAdjacentKeys
* constexpr and use it to compute the k_closenessThreshold. */
static int maxHalfDistanceBetweekAdjacentKeys() {
int maxSquaredDistance = 0;
for (int i=0; i<Keyboard::NumberOfValidKeys-1; i++) {
int minSquaredDistance = INT_MAX;
for (int j=i+1; j<Keyboard::NumberOfValidKeys; j++) {
SDL_Point keyICenter;
SDL_Point keyJCenter;
getKeyCenter(i, &keyICenter);
getKeyCenter(j, &keyJCenter);
int dx = keyICenter.x - keyJCenter.x;
int dy = keyICenter.y - keyJCenter.y;
int squaredDistance = dx*dx + dy*dy;
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
}
}
if (minSquaredDistance > maxSquaredDistance) {
maxSquaredDistance = minSquaredDistance;
}
}
return maxSquaredDistance/4; // return 843
}
#endif
Keyboard::Key keyAt(SDL_Point * p) {
int minSquaredDistance = -1;
int minSquaredDistance = INT_MAX;
Keyboard::Key nearestKey = Keyboard::Key::None;
/* The maximal distance between two adjacent keys is between keys Home and
* OK (index 3 and 7). So we set the threshold to correspond to slightly over
* half the distance between these keys.
* TODO: Looking for the maximal distance between adjacent keys could be done
* constexpr once we use c++14 standard.
* (Cf maxHalfDistanceBetweekAdjacentKeys above) */
constexpr int k_closenessThreshold = 3*843/2; // 3*maxHalfDistanceBetweekAdjacentKeys()/2
for (int i=0; i<Keyboard::NumberOfValidKeys; i++) {
SDL_Point keyCenter;
getKeyCenter(i, &keyCenter);
int dx = keyCenter.x - p->x;
int dy = keyCenter.y - p->y;
int squaredDistance = dx*dx + dy*dy;
if (squaredDistance < minSquaredDistance || minSquaredDistance < 0) {
if (squaredDistance < k_closenessThreshold && squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
nearestKey = Keyboard::ValidKeys[i];
}