Skip to content

Commit aedc35f

Browse files
committed
do not complicate the code with nearest keys, we have no corrections
1 parent 8a1b68f commit aedc35f

3 files changed

Lines changed: 12 additions & 124 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.content.res.Resources
66
import android.content.res.TypedArray
77
import android.content.res.XmlResourceParser
88
import android.graphics.drawable.Drawable
9-
import android.util.SparseArray
109
import android.util.TypedValue
1110
import android.util.Xml
1211
import android.view.inputmethod.EditorInfo
@@ -45,14 +44,10 @@ class MyKeyboard {
4544
/** Width of the screen available to fit the keyboard */
4645
private var mDisplayWidth = 0
4746

48-
/** What icon should we show at Enter key */
47+
/** What icon should we show at Enter key */
4948
private var mEnterKeyType = IME_ACTION_NONE
5049

51-
/** Keyboard mode, or zero, if none. */
52-
private var mCellWidth = 0
53-
private var mCellHeight = 0
54-
private var mGridNeighbors: SparseArray<IntArray?>? = null
55-
private var mProximityThreshold = 0
50+
/** Keyboard rows */
5651
private val mRows = ArrayList<Row?>()
5752

5853
companion object {
@@ -61,22 +56,12 @@ class MyKeyboard {
6156
private const val TAG_KEY = "Key"
6257
private const val EDGE_LEFT = 0x01
6358
private const val EDGE_RIGHT = 0x02
64-
private const val EDGE_TOP = 0x04
65-
private const val EDGE_BOTTOM = 0x08
6659
const val KEYCODE_SHIFT = -1
6760
const val KEYCODE_MODE_CHANGE = -2
6861
const val KEYCODE_ENTER = -4
6962
const val KEYCODE_DELETE = -5
7063
const val KEYCODE_SPACE = 32
7164

72-
// Variables for pre-computing nearest keys.
73-
private const val GRID_WIDTH = 10
74-
private const val GRID_HEIGHT = 5
75-
private const val GRID_SIZE = GRID_WIDTH * GRID_HEIGHT
76-
77-
/** Number of key widths from current touch point to search for nearest keys. */
78-
private const val SEARCH_DISTANCE = 1.8f
79-
8065
fun getDimensionOrFraction(a: TypedArray, index: Int, base: Int, defValue: Int): Int {
8166
val value = a.peekValue(index) ?: return defValue
8267
return when (value.type) {
@@ -174,7 +159,7 @@ class MyKeyboard {
174159

175160
/**
176161
* Flags that specify the anchoring to edges of the keyboard for detecting touch events that are just out of the boundary of the key.
177-
* This is a bit mask of [MyKeyboard.EDGE_LEFT], [MyKeyboard.EDGE_RIGHT], [MyKeyboard.EDGE_TOP] and [MyKeyboard.EDGE_BOTTOM].
162+
* This is a bit mask of [MyKeyboard.EDGE_LEFT], [MyKeyboard.EDGE_RIGHT].
178163
*/
179164
private var edgeFlags = 0
180165

@@ -240,24 +225,10 @@ class MyKeyboard {
240225
fun isInside(x: Int, y: Int): Boolean {
241226
val leftEdge = edgeFlags and EDGE_LEFT > 0
242227
val rightEdge = edgeFlags and EDGE_RIGHT > 0
243-
val topEdge = edgeFlags and EDGE_TOP > 0
244-
val bottomEdge = edgeFlags and EDGE_BOTTOM > 0
245228
return ((x >= this.x || leftEdge && x <= this.x + width)
246229
&& (x < this.x + width || rightEdge && x >= this.x)
247-
&& (y >= this.y || topEdge && y <= this.y + height)
248-
&& (y < this.y + height || bottomEdge && y >= this.y))
249-
}
250-
251-
/**
252-
* Returns the square of the distance between the center of the key and the given point.
253-
* @param x the x-coordinate of the point
254-
* @param y the y-coordinate of the point
255-
* @return the square of the distance of the point from the center of the key
256-
*/
257-
fun squaredDistanceFrom(x: Int, y: Int): Int {
258-
val xDist = this.x + width / 2 - x
259-
val yDist = this.y + height / 2 - y
260-
return xDist * xDist + yDist * yDist
230+
&& (y >= this.y && y <= this.y + height)
231+
&& (y < this.y + height && y >= this.y))
261232
}
262233
}
263234

@@ -332,60 +303,6 @@ class MyKeyboard {
332303
return false
333304
}
334305

335-
private fun computeNearestNeighbors() {
336-
// Round-up so we don't have any pixels outside the grid
337-
mCellWidth = (mMinWidth + GRID_WIDTH - 1) / GRID_WIDTH
338-
mCellHeight = (mHeight + GRID_HEIGHT - 1) / GRID_HEIGHT
339-
mGridNeighbors = SparseArray<IntArray?>(GRID_SIZE)
340-
val indices = IntArray(mKeys!!.size)
341-
val gridWidth: Int = GRID_WIDTH * mCellWidth
342-
val gridHeight: Int = GRID_HEIGHT * mCellHeight
343-
var x = 0
344-
while (x < gridWidth) {
345-
var y = 0
346-
while (y < gridHeight) {
347-
var count = 0
348-
for (i in mKeys!!.indices) {
349-
val key = mKeys!![i]
350-
if (key!!.squaredDistanceFrom(x, y) < mProximityThreshold || key.squaredDistanceFrom(
351-
x + mCellWidth - 1, y
352-
) < mProximityThreshold || (key.squaredDistanceFrom(x + mCellWidth - 1, y + mCellHeight - 1)
353-
< mProximityThreshold) || key.squaredDistanceFrom(x, y + mCellHeight - 1) < mProximityThreshold
354-
) {
355-
indices[count++] = i
356-
}
357-
}
358-
359-
val cell = IntArray(count)
360-
System.arraycopy(indices, 0, cell, 0, count)
361-
mGridNeighbors!!.put(y / mCellHeight * GRID_WIDTH + x / mCellWidth, cell)
362-
y += mCellHeight
363-
}
364-
x += mCellWidth
365-
}
366-
}
367-
368-
/**
369-
* Returns the indices of the keys that are closest to the given point.
370-
* @param x the x-coordinate of the point
371-
* @param y the y-coordinate of the point
372-
* @return the array of integer indices for the nearest keys to the given point. If the given point is out of range, then an array of size zero is returned.
373-
*/
374-
fun getNearestKeys(x: Int, y: Int): IntArray {
375-
if (mGridNeighbors == null) {
376-
computeNearestNeighbors()
377-
}
378-
379-
if (x in 0 until mMinWidth && y >= 0 && y < mHeight) {
380-
val index = y / mCellHeight * GRID_WIDTH + x / mCellWidth
381-
if (index < GRID_SIZE) {
382-
return mGridNeighbors!![index]!!
383-
}
384-
}
385-
386-
return IntArray(0)
387-
}
388-
389306
private fun createRowFromXml(res: Resources, parser: XmlResourceParser?): Row {
390307
return Row(res, this, parser)
391308
}
@@ -458,8 +375,6 @@ class MyKeyboard {
458375
mDefaultWidth = getDimensionOrFraction(a, R.styleable.MyKeyboard_keyWidth, mDisplayWidth, mDisplayWidth / 10)
459376
mDefaultHeight = res.getDimension(R.dimen.key_height).toInt()
460377
mDefaultHorizontalGap = getDimensionOrFraction(a, R.styleable.MyKeyboard_horizontalGap, mDisplayWidth, 0)
461-
mProximityThreshold = (mDefaultWidth * SEARCH_DISTANCE).toInt()
462-
mProximityThreshold *= mProximityThreshold // Square it for comparison
463378
a.recycle()
464379
}
465380
}

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -678,41 +678,16 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
678678
}
679679
}
680680

681-
private fun getKeyIndices(x: Int, y: Int): Int {
682-
val keys = mKeys
683-
var primaryIndex = NOT_A_KEY
684-
var closestKey = NOT_A_KEY
685-
var closestKeyDist = mProximityThreshold + 1
686-
val nearestKeyIndices = mKeyboard!!.getNearestKeys(x, y)
687-
val keyCount = nearestKeyIndices.size
688-
689-
for (i in 0 until keyCount) {
690-
val key = keys[nearestKeyIndices[i]]
691-
val dist = 0
692-
val isInside = key.isInside(x, y)
693-
if (isInside) {
694-
primaryIndex = nearestKeyIndices[i]
695-
}
696-
697-
if (isInside && key.code > KEYCODE_SPACE) {
698-
if (dist < closestKeyDist) {
699-
closestKeyDist = dist
700-
closestKey = nearestKeyIndices[i]
701-
}
702-
}
681+
private fun getPressedKeyIndex(x: Int, y: Int): Int {
682+
return mKeys.indexOfFirst {
683+
it.isInside(x, y)
703684
}
704-
705-
if (primaryIndex == NOT_A_KEY) {
706-
primaryIndex = closestKey
707-
}
708-
709-
return primaryIndex
710685
}
711686

712687
private fun detectAndSendKey(index: Int, x: Int, y: Int, eventTime: Long) {
713688
if (index != NOT_A_KEY && index < mKeys.size) {
714689
val key = mKeys[index]
715-
getKeyIndices(x, y)
690+
getPressedKeyIndex(x, y)
716691
mOnKeyboardActionListener!!.onKey(key.code)
717692
mLastTapTime = eventTime
718693
}
@@ -1095,7 +1070,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10951070

10961071
val action = me.actionMasked
10971072
val eventTime = me.eventTime
1098-
val keyIndex = getKeyIndices(touchX, touchY)
1073+
val keyIndex = getPressedKeyIndex(touchX, touchY)
10991074

11001075
// Ignore all motion events until a DOWN.
11011076
if (mAbortKey && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
@@ -1118,7 +1093,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
11181093

11191094
val newPointerX = me.getX(1).toInt()
11201095
val newPointerY = me.getY(1).toInt()
1121-
val secondKeyIndex = getKeyIndices(newPointerX, newPointerY)
1096+
val secondKeyIndex = getPressedKeyIndex(newPointerX, newPointerY)
11221097
showPreview(secondKeyIndex)
11231098
detectAndSendKey(secondKeyIndex, newPointerX, newPointerY, eventTime)
11241099

app/src/main/res/xml/keys_letters_english.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@
118118
app:keyEdgeFlags="left"
119119
app:keyLabel="123"
120120
app:keyWidth="15%p" />
121-
<Key
122-
app:keyLabel=","
123-
app:keyWidth="10%p" />
121+
<Key app:keyLabel="," />
124122
<Key
125123
app:code="32"
126124
app:isRepeatable="true"

0 commit comments

Comments
 (0)