mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[ion] Keyboard: don't detect a key down when the mouse is too far away
from a key
This commit is contained in:
committed by
EmilieNumworks
parent
4c41b1699d
commit
1c0f6a7913
@@ -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);
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user