diff --git a/ion/include/ion/keyboard.h b/ion/include/ion/keyboard.h index f2b8042bd..ba056ffd2 100644 --- a/ion/include/ion/keyboard.h +++ b/ion/include/ion/keyboard.h @@ -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); diff --git a/ion/src/simulator/shared/layout.cpp b/ion/src/simulator/shared/layout.cpp index 088e5444c..f930015cc 100644 --- a/ion/src/simulator/shared/layout.cpp +++ b/ion/src/simulator/shared/layout.cpp @@ -1,4 +1,5 @@ #include "layout.h" +#include 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 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; ix; 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]; }