2022-02-20 13:09:39 +01:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Message;
|
2022-06-05 01:38:42 +02:00
|
|
|
import java.util.Arrays;
|
2022-02-20 13:09:39 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage pointers (fingers) on the screen and long presses.
|
|
|
|
* Call back to IPointerEventHandler.
|
|
|
|
*/
|
|
|
|
public final class Pointers implements Handler.Callback
|
|
|
|
{
|
|
|
|
private Handler _keyrepeat_handler;
|
|
|
|
private ArrayList<Pointer> _ptrs = new ArrayList<Pointer>();
|
|
|
|
private IPointerEventHandler _handler;
|
|
|
|
private Config _config;
|
|
|
|
|
|
|
|
public Pointers(IPointerEventHandler h, Config c)
|
|
|
|
{
|
|
|
|
_keyrepeat_handler = new Handler(this);
|
|
|
|
_handler = h;
|
|
|
|
_config = c;
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Return the list of modifiers currently activated. */
|
|
|
|
public Modifiers getModifiers()
|
2022-04-30 23:36:17 +02:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
return getModifiers(false);
|
2022-04-30 23:36:17 +02:00
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** When [skip_latched] is true, don't take flags of latched keys into account. */
|
|
|
|
private Modifiers getModifiers(boolean skip_latched)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
int n_ptrs = _ptrs.size();
|
|
|
|
KeyValue.Modifier[] mods = new KeyValue.Modifier[n_ptrs];
|
|
|
|
int n_mods = 0;
|
|
|
|
for (int i = 0; i < n_ptrs; i++)
|
2022-04-30 23:36:17 +02:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
Pointer p = _ptrs.get(i);
|
2022-06-05 19:30:53 +02:00
|
|
|
if (p.value != null && p.value.getKind() == KeyValue.Kind.Modifier
|
|
|
|
&& !(skip_latched && p.pointerId == -1
|
|
|
|
&& (p.flags & KeyValue.FLAG_LOCKED) == 0))
|
|
|
|
mods[n_mods++] = p.value.getModifier();
|
2022-04-30 23:36:17 +02:00
|
|
|
}
|
2022-06-05 19:30:53 +02:00
|
|
|
return Modifiers.ofArray(mods, n_mods);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
_ptrs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isKeyDown(KeyboardData.Key k)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
if (p.key == k)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These flags can be different:
|
|
|
|
* FLAG_LOCK Removed when the key is locked
|
|
|
|
* FLAG_LOCKED Added when the key is locked
|
|
|
|
* FLAG_LATCH Removed when the key is latched (released but not consumed yet)
|
|
|
|
* Returns [-1] if not found.
|
|
|
|
*/
|
|
|
|
public int getKeyFlags(KeyValue kv)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
2022-06-06 00:23:45 +02:00
|
|
|
if (p.value != null && p.value.equals(kv))
|
2022-02-20 13:09:39 +01:00
|
|
|
return p.flags;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:02:48 +02:00
|
|
|
/** Fake pointers are latched and not lockable. */
|
2022-10-23 21:34:05 +02:00
|
|
|
public void add_fake_pointer(KeyValue kv, KeyboardData.Key key, boolean locked)
|
2022-07-24 20:02:48 +02:00
|
|
|
{
|
2022-11-06 19:49:30 +01:00
|
|
|
if (getLatched(key, kv) != null)
|
|
|
|
return; // Already latched, don't add an other pointer.
|
2022-07-24 20:02:48 +02:00
|
|
|
Pointer ptr = new Pointer(-1, key, kv, 0.f, 0.f, Modifiers.EMPTY);
|
2022-10-23 21:34:05 +02:00
|
|
|
ptr.flags &= ~KeyValue.FLAG_LATCH;
|
|
|
|
ptr.flags |= KeyValue.FLAG_LOCK | KeyValue.FLAG_FAKE_PTR;
|
|
|
|
if (locked)
|
|
|
|
ptr.flags = (ptr.flags & ~KeyValue.FLAG_LOCK) | KeyValue.FLAG_LOCKED;
|
2022-07-24 20:02:48 +02:00
|
|
|
_ptrs.add(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove_fake_pointer(KeyValue kv, KeyboardData.Key key)
|
|
|
|
{
|
|
|
|
Pointer ptr = getLatched(key, kv);
|
2022-07-30 18:14:05 +02:00
|
|
|
if (ptr != null && (ptr.flags & KeyValue.FLAG_FAKE_PTR) != 0)
|
2022-07-24 20:02:48 +02:00
|
|
|
removePtr(ptr);
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
// Receiving events
|
|
|
|
|
|
|
|
public void onTouchUp(int pointerId)
|
|
|
|
{
|
|
|
|
Pointer ptr = getPtr(pointerId);
|
|
|
|
if (ptr == null)
|
|
|
|
return;
|
|
|
|
stopKeyRepeat(ptr);
|
2022-03-19 15:39:20 +01:00
|
|
|
Pointer latched = getLatched(ptr);
|
2022-02-20 13:09:39 +01:00
|
|
|
if (latched != null) // Already latched
|
|
|
|
{
|
|
|
|
removePtr(ptr); // Remove dupplicate
|
2022-07-24 23:55:00 +02:00
|
|
|
if ((latched.flags & KeyValue.FLAG_LOCK) != 0) // Toggle lockable key
|
|
|
|
lockPointer(latched, false);
|
2022-02-20 13:09:39 +01:00
|
|
|
else // Otherwise, unlatch
|
|
|
|
{
|
|
|
|
removePtr(latched);
|
2022-06-05 01:38:42 +02:00
|
|
|
_handler.onPointerUp(ptr.value, ptr.modifiers);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((ptr.flags & KeyValue.FLAG_LATCH) != 0)
|
|
|
|
{
|
|
|
|
ptr.flags &= ~KeyValue.FLAG_LATCH;
|
|
|
|
ptr.pointerId = -1; // Latch
|
2022-07-24 23:55:00 +02:00
|
|
|
_handler.onPointerFlagsChanged(false);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clearLatched();
|
|
|
|
removePtr(ptr);
|
2022-06-05 01:38:42 +02:00
|
|
|
_handler.onPointerUp(ptr.value, ptr.modifiers);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:44:02 +01:00
|
|
|
public void onTouchCancel(int pointerId)
|
|
|
|
{
|
|
|
|
Pointer ptr = getPtr(pointerId);
|
|
|
|
if (ptr == null)
|
|
|
|
return;
|
|
|
|
stopKeyRepeat(ptr);
|
|
|
|
removePtr(ptr);
|
2022-07-24 23:55:00 +02:00
|
|
|
_handler.onPointerFlagsChanged(true);
|
2022-03-15 20:44:02 +01:00
|
|
|
}
|
|
|
|
|
2022-04-30 23:36:17 +02:00
|
|
|
/* Whether an other pointer is down on a non-special key. */
|
|
|
|
private boolean isOtherPointerDown()
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
if (p.pointerId != -1 && (p.flags & KeyValue.FLAG_SPECIAL) == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public void onTouchDown(float x, float y, int pointerId, KeyboardData.Key key)
|
|
|
|
{
|
2022-03-17 11:24:33 +01:00
|
|
|
// Ignore new presses while a modulated key is active. On some devices,
|
|
|
|
// ghost touch events can happen while the pointer travels on top of other
|
|
|
|
// keys.
|
|
|
|
if (isModulatedKeyPressed())
|
|
|
|
return;
|
2022-06-05 01:38:42 +02:00
|
|
|
// Don't take latched modifiers into account if an other key is pressed.
|
|
|
|
// The other key already "own" the latched modifiers and will clear them.
|
|
|
|
Modifiers mods = getModifiers(isOtherPointerDown());
|
2022-06-24 20:26:27 +02:00
|
|
|
KeyValue value = handleKV(key.key0, mods);
|
2022-06-05 01:38:42 +02:00
|
|
|
Pointer ptr = new Pointer(pointerId, key, value, x, y, mods);
|
2022-02-20 13:09:39 +01:00
|
|
|
_ptrs.add(ptr);
|
2022-07-24 23:55:00 +02:00
|
|
|
startKeyRepeat(ptr);
|
2022-05-08 16:53:33 +02:00
|
|
|
_handler.onPointerDown(false);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 16:38:44 +02:00
|
|
|
/*
|
|
|
|
* Get the KeyValue at the given direction. In case of swipe (!= 0), get the
|
|
|
|
* nearest KeyValue that is not key0.
|
|
|
|
* Take care of applying [_handler.onPointerSwipe] to the selected key, this
|
|
|
|
* must be done at the same time to be sure to treat removed keys correctly.
|
|
|
|
* Return [null] if no key could be found in the given direction or if the
|
|
|
|
* selected key didn't change.
|
|
|
|
*/
|
|
|
|
private KeyValue getKeyAtDirection(Pointer ptr, int direction)
|
|
|
|
{
|
|
|
|
if (direction == 0)
|
2022-06-24 20:26:27 +02:00
|
|
|
return handleKV(ptr.key.key0, ptr.modifiers);
|
2022-05-08 16:38:44 +02:00
|
|
|
KeyValue k;
|
2022-05-08 16:56:34 +02:00
|
|
|
for (int i = 0; i > -3; i = (~i>>31) - i)
|
2022-05-08 16:38:44 +02:00
|
|
|
{
|
2022-07-03 22:58:02 +02:00
|
|
|
int d = (direction + i + 8 - 1) % 8 + 1;
|
2022-05-08 16:38:44 +02:00
|
|
|
// Don't make the difference between a key that doesn't exist and a key
|
|
|
|
// that is removed by [_handler]. Triggers side effects.
|
2022-06-05 01:38:42 +02:00
|
|
|
k = _handler.modifyKey(ptr.key.getAtDirection(d), ptr.modifiers);
|
2022-05-08 16:38:44 +02:00
|
|
|
if (k != null)
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-24 20:26:27 +02:00
|
|
|
private KeyValue handleKV(KeyboardData.Corner c, Modifiers modifiers)
|
|
|
|
{
|
|
|
|
if (c == null)
|
|
|
|
return null;
|
|
|
|
return _handler.modifyKey(c.kv, modifiers);
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public void onTouchMove(float x, float y, int pointerId)
|
|
|
|
{
|
|
|
|
Pointer ptr = getPtr(pointerId);
|
|
|
|
if (ptr == null)
|
|
|
|
return;
|
2022-05-06 18:38:43 +02:00
|
|
|
|
|
|
|
// The position in a IME windows is clampled to view.
|
|
|
|
// For a better up swipe behaviour, set the y position to a negative value when clamped.
|
|
|
|
if (y == 0.0) y = -400;
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
float dx = x - ptr.downX;
|
|
|
|
float dy = y - ptr.downY;
|
|
|
|
float dist = Math.abs(dx) + Math.abs(dy);
|
|
|
|
ptr.ptrDist = dist;
|
2022-05-07 00:08:20 +02:00
|
|
|
|
|
|
|
int direction;
|
2022-02-20 13:09:39 +01:00
|
|
|
if (dist < _config.swipe_dist_px)
|
|
|
|
{
|
2022-05-07 00:08:20 +02:00
|
|
|
direction = 0;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
2022-05-07 00:08:20 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// One of the 8 directions:
|
|
|
|
// |\2|3/|
|
|
|
|
// |1\|/4|
|
|
|
|
// |-----|
|
2022-05-08 01:18:53 +02:00
|
|
|
// |8/|\5|
|
|
|
|
// |/7|6\|
|
2022-05-07 00:08:20 +02:00
|
|
|
direction = 1;
|
|
|
|
if (dx > 0) direction += 2;
|
|
|
|
if (dx > Math.abs(dy) || (dx < 0 && dx > -Math.abs(dy))) direction += 1;
|
2022-05-08 01:18:53 +02:00
|
|
|
if (dy > 0) direction = 9 - direction;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
2022-05-07 00:08:20 +02:00
|
|
|
|
2022-05-08 16:38:44 +02:00
|
|
|
if (direction != ptr.selected_direction)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-05-08 16:38:44 +02:00
|
|
|
ptr.selected_direction = direction;
|
|
|
|
KeyValue newValue = getKeyAtDirection(ptr, direction);
|
2022-06-06 00:23:45 +02:00
|
|
|
if (newValue != null && (ptr.value == null || !newValue.equals(ptr.value)))
|
2022-02-21 00:24:57 +01:00
|
|
|
{
|
2022-03-19 15:39:20 +01:00
|
|
|
int old_flags = ptr.flags;
|
|
|
|
ptr.value = newValue;
|
2022-06-05 17:26:34 +02:00
|
|
|
ptr.flags = newValue.getFlags();
|
2022-02-21 00:24:57 +01:00
|
|
|
// Keep the keyrepeat going between modulated keys.
|
2022-06-05 17:26:34 +02:00
|
|
|
if ((old_flags & ptr.flags & KeyValue.FLAG_PRECISE_REPEAT) == 0)
|
2022-03-19 15:39:20 +01:00
|
|
|
{
|
|
|
|
stopKeyRepeat(ptr);
|
2022-07-24 23:55:00 +02:00
|
|
|
startKeyRepeat(ptr);
|
2022-03-19 15:39:20 +01:00
|
|
|
}
|
2022-05-08 16:53:33 +02:00
|
|
|
_handler.onPointerDown(true);
|
2022-02-21 00:24:57 +01:00
|
|
|
}
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pointers management
|
|
|
|
|
|
|
|
private Pointer getPtr(int pointerId)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
if (p.pointerId == pointerId)
|
|
|
|
return p;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removePtr(Pointer ptr)
|
|
|
|
{
|
|
|
|
_ptrs.remove(ptr);
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:39:20 +01:00
|
|
|
private Pointer getLatched(Pointer target)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-07-24 20:02:48 +02:00
|
|
|
return getLatched(target.key, target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Pointer getLatched(KeyboardData.Key k, KeyValue v)
|
|
|
|
{
|
2022-05-01 00:11:52 +02:00
|
|
|
if (v == null)
|
|
|
|
return null;
|
2022-02-20 13:09:39 +01:00
|
|
|
for (Pointer p : _ptrs)
|
2022-06-06 00:23:45 +02:00
|
|
|
if (p.key == k && p.pointerId == -1 && p.value != null && p.value.equals(v))
|
2022-02-20 13:09:39 +01:00
|
|
|
return p;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clearLatched()
|
|
|
|
{
|
|
|
|
for (int i = _ptrs.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
Pointer ptr = _ptrs.get(i);
|
|
|
|
// Latched and not locked, remove
|
|
|
|
if (ptr.pointerId == -1 && (ptr.flags & KeyValue.FLAG_LOCKED) == 0)
|
|
|
|
_ptrs.remove(i);
|
2022-07-24 23:55:00 +02:00
|
|
|
// Not latched but pressed, don't latch once released and stop long press.
|
2022-02-20 13:09:39 +01:00
|
|
|
else if ((ptr.flags & KeyValue.FLAG_LATCH) != 0)
|
|
|
|
ptr.flags &= ~KeyValue.FLAG_LATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:55:00 +02:00
|
|
|
/** Make a pointer into the locked state. */
|
|
|
|
private void lockPointer(Pointer ptr, boolean shouldVibrate)
|
|
|
|
{
|
|
|
|
ptr.flags = (ptr.flags & ~KeyValue.FLAG_LOCK) | KeyValue.FLAG_LOCKED;
|
|
|
|
_handler.onPointerFlagsChanged(shouldVibrate);
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:24:33 +01:00
|
|
|
private boolean isModulatedKeyPressed()
|
|
|
|
{
|
|
|
|
for (Pointer ptr : _ptrs)
|
|
|
|
{
|
|
|
|
if ((ptr.flags & KeyValue.FLAG_PRECISE_REPEAT) != 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
// Key repeat
|
|
|
|
|
|
|
|
/** Message from [_keyrepeat_handler]. */
|
|
|
|
@Override
|
|
|
|
public boolean handleMessage(Message msg)
|
|
|
|
{
|
|
|
|
for (Pointer ptr : _ptrs)
|
|
|
|
{
|
|
|
|
if (ptr.timeoutWhat == msg.what)
|
|
|
|
{
|
2022-07-24 23:55:00 +02:00
|
|
|
if (handleKeyRepeat(ptr))
|
|
|
|
_keyrepeat_handler.sendEmptyMessageDelayed(msg.what, nextRepeatInterval(ptr));
|
|
|
|
else
|
|
|
|
ptr.timeoutWhat = -1;
|
|
|
|
return true;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-24 23:55:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long nextRepeatInterval(Pointer ptr)
|
|
|
|
{
|
|
|
|
long t = _config.longPressInterval;
|
|
|
|
if (_config.preciseRepeat && (ptr.flags & KeyValue.FLAG_PRECISE_REPEAT) != 0)
|
|
|
|
{
|
|
|
|
// Modulate repeat interval depending on the distance of the pointer
|
|
|
|
t = (long)((float)t * 2.f / modulatePreciseRepeat(ptr));
|
|
|
|
}
|
|
|
|
return t;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int uniqueTimeoutWhat = 0;
|
|
|
|
|
|
|
|
private void startKeyRepeat(Pointer ptr)
|
|
|
|
{
|
2022-07-24 23:55:00 +02:00
|
|
|
if (ptr.value == null)
|
|
|
|
return;
|
2022-02-20 13:09:39 +01:00
|
|
|
int what = (uniqueTimeoutWhat++);
|
|
|
|
ptr.timeoutWhat = what;
|
2022-02-22 19:46:41 +01:00
|
|
|
long timeout = _config.longPressTimeout;
|
|
|
|
// Faster repeat timeout for modulated keys
|
|
|
|
if ((ptr.flags & KeyValue.FLAG_PRECISE_REPEAT) != 0)
|
|
|
|
timeout /= 2;
|
|
|
|
_keyrepeat_handler.sendEmptyMessageDelayed(what, timeout);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void stopKeyRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
if (ptr.timeoutWhat != -1)
|
|
|
|
{
|
|
|
|
_keyrepeat_handler.removeMessages(ptr.timeoutWhat);
|
|
|
|
ptr.timeoutWhat = -1;
|
2022-02-21 00:24:57 +01:00
|
|
|
ptr.repeatingPtrDist = -1.f;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:55:00 +02:00
|
|
|
/** A pointer is repeating. Returns [true] if repeat should continue. */
|
|
|
|
private boolean handleKeyRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
// Long press toggle lock on modifiers
|
|
|
|
if ((ptr.flags & KeyValue.FLAG_LATCH) != 0)
|
|
|
|
{
|
|
|
|
lockPointer(ptr, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Stop repeating: Latched key, special keys
|
|
|
|
if (ptr.pointerId == -1 || (ptr.flags & KeyValue.FLAG_SPECIAL) != 0)
|
|
|
|
return false;
|
|
|
|
_handler.onPointerHold(ptr.value, ptr.modifiers);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:24:57 +01:00
|
|
|
private float modulatePreciseRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
if (ptr.repeatingPtrDist < 0.f)
|
|
|
|
ptr.repeatingPtrDist = ptr.ptrDist; // First repeat
|
|
|
|
if (ptr.ptrDist > ptr.repeatingPtrDist * 2.f)
|
|
|
|
ptr.repeatingPtrDist = ptr.ptrDist / 2.f; // Large swipe, move the middle point
|
|
|
|
float left = ptr.repeatingPtrDist / 2.f;
|
|
|
|
float accel = (ptr.ptrDist - left) / (ptr.repeatingPtrDist - left);
|
2022-02-22 19:46:41 +01:00
|
|
|
return Math.min(8.f, Math.max(0.1f, accel));
|
2022-02-21 00:24:57 +01:00
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
private static final class Pointer
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
|
|
|
/** -1 when latched. */
|
|
|
|
public int pointerId;
|
2022-05-07 00:08:20 +02:00
|
|
|
/** The Key pressed by this Pointer */
|
2022-03-19 15:39:20 +01:00
|
|
|
public final KeyboardData.Key key;
|
2022-05-08 16:38:44 +02:00
|
|
|
/** Current direction. */
|
|
|
|
public int selected_direction;
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Selected value with [modifiers] applied. */
|
2022-02-20 13:09:39 +01:00
|
|
|
public KeyValue value;
|
|
|
|
public float downX;
|
|
|
|
public float downY;
|
|
|
|
/** Distance of the pointer to the initial press. */
|
|
|
|
public float ptrDist;
|
2022-04-30 23:17:20 +02:00
|
|
|
/** Modifier flags at the time the key was pressed. */
|
2022-06-05 01:38:42 +02:00
|
|
|
public Modifiers modifiers;
|
2022-04-30 23:17:20 +02:00
|
|
|
/** Flags of the value. Latch, lock and locked flags are updated. */
|
2022-02-20 13:09:39 +01:00
|
|
|
public int flags;
|
|
|
|
/** Identify timeout messages. */
|
|
|
|
public int timeoutWhat;
|
2022-02-21 00:24:57 +01:00
|
|
|
/** ptrDist at the first repeat, -1 otherwise. */
|
|
|
|
public float repeatingPtrDist;
|
2022-02-20 13:09:39 +01:00
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
|
|
|
pointerId = p;
|
|
|
|
key = k;
|
2022-05-08 16:38:44 +02:00
|
|
|
selected_direction = 0;
|
2022-02-20 13:09:39 +01:00
|
|
|
value = v;
|
|
|
|
downX = x;
|
|
|
|
downY = y;
|
|
|
|
ptrDist = 0.f;
|
2022-06-05 01:38:42 +02:00
|
|
|
modifiers = m;
|
2022-06-05 17:26:34 +02:00
|
|
|
flags = (v == null) ? 0 : v.getFlags();
|
2022-02-20 13:09:39 +01:00
|
|
|
timeoutWhat = -1;
|
2022-02-21 00:24:57 +01:00
|
|
|
repeatingPtrDist = -1.f;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Represent modifiers currently activated.
|
|
|
|
Sorted in the order they should be evaluated. */
|
|
|
|
public static final class Modifiers
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
private final KeyValue.Modifier[] _mods;
|
2022-06-05 01:38:42 +02:00
|
|
|
private final int _size;
|
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
private Modifiers(KeyValue.Modifier[] m, int s)
|
|
|
|
{
|
|
|
|
_mods = m; _size = s;
|
|
|
|
}
|
2022-06-05 01:38:42 +02:00
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
public KeyValue.Modifier get(int i) { return _mods[_size - 1 - i]; }
|
2022-06-05 01:38:42 +02:00
|
|
|
public int size() { return _size; }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() { return Arrays.hashCode(_mods); }
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
{
|
|
|
|
return Arrays.equals(_mods, ((Modifiers)obj)._mods);
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
public static final Modifiers EMPTY =
|
|
|
|
new Modifiers(new KeyValue.Modifier[0], 0);
|
2022-06-05 01:38:42 +02:00
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
protected static Modifiers ofArray(KeyValue.Modifier[] mods, int size)
|
2022-06-05 01:38:42 +02:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
// Sort and remove duplicates and nulls.
|
2022-06-05 01:38:42 +02:00
|
|
|
if (size > 1)
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
Arrays.sort(mods, 0, size);
|
2022-06-05 01:38:42 +02:00
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
KeyValue.Modifier m = mods[i];
|
|
|
|
if (m != null && (i + 1 >= size || m != mods[i + 1]))
|
2022-06-05 01:38:42 +02:00
|
|
|
{
|
|
|
|
mods[j] = m;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = j;
|
|
|
|
}
|
|
|
|
return new Modifiers(mods, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public interface IPointerEventHandler
|
|
|
|
{
|
2022-05-08 16:53:33 +02:00
|
|
|
/** Key can be modified or removed by returning [null]. */
|
2022-06-05 01:38:42 +02:00
|
|
|
public KeyValue modifyKey(KeyValue k, Modifiers flags);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** A key is pressed. [getModifiers()] is uptodate. Might be called after a
|
2022-05-08 16:53:33 +02:00
|
|
|
press or a swipe to a different value. */
|
|
|
|
public void onPointerDown(boolean isSwipe);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2022-05-08 16:53:33 +02:00
|
|
|
/** Key is released. [k] is the key that was returned by
|
|
|
|
[modifySelectedKey] or [modifySelectedKey]. */
|
2022-06-05 01:38:42 +02:00
|
|
|
public void onPointerUp(KeyValue k, Modifiers flags);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
|
|
|
/** Flags changed because latched or locked keys or cancelled pointers. */
|
2022-07-24 23:55:00 +02:00
|
|
|
public void onPointerFlagsChanged(boolean shouldVibrate);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
|
|
|
/** Key is repeating. */
|
2022-06-05 01:38:42 +02:00
|
|
|
public void onPointerHold(KeyValue k, Modifiers flags);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|