From 4906f8105f9f831b5977d4935f90ef26b8afdafd Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sat, 25 May 2024 21:19:44 +0200 Subject: Circle and round trip gestures (#640) This implements clockwise/anticlockwise circle and round trip gestures inspired by Messagease. The circle gestures start after a small threshold to avoid making the regular swipe too hard to aim. The gestures do: - circle: The center symbol with Shift applied, with a fallback on Fn - round trip: Same as the circle gesture but applied to a side symbol - anticlockwise circle: Nothing currently. It is intended to be made configurable per-layout in the future. The new Gesture class keeps track of what the pointer is doing while it moves on a key. It replaces the 'selected_direction' integer. --- srcs/juloo.keyboard2/Pointers.java | 144 ++++++++++++++++++++++++++++--------- 1 file changed, 110 insertions(+), 34 deletions(-) (limited to 'srcs/juloo.keyboard2/Pointers.java') diff --git a/srcs/juloo.keyboard2/Pointers.java b/srcs/juloo.keyboard2/Pointers.java index 4d6ff9a..8de4832 100644 --- a/srcs/juloo.keyboard2/Pointers.java +++ b/srcs/juloo.keyboard2/Pointers.java @@ -143,6 +143,12 @@ public final class Pointers implements Handler.Callback return; } stopKeyRepeat(ptr); + KeyValue ptr_value = ptr.value; + if (ptr.gesture != null && ptr.gesture.is_in_progress()) + { + // A gesture was in progress + ptr.gesture.pointer_up(); + } Pointer latched = getLatched(ptr); if (latched != null) // Already latched { @@ -152,7 +158,7 @@ public final class Pointers implements Handler.Callback else // Otherwise, unlatch { removePtr(latched); - _handler.onPointerUp(ptr.value, ptr.modifiers); + _handler.onPointerUp(ptr_value, ptr.modifiers); } } else if ((ptr.flags & FLAG_P_LATCHABLE) != 0) @@ -168,7 +174,7 @@ public final class Pointers implements Handler.Callback { clearLatched(); removePtr(ptr); - _handler.onPointerUp(ptr.value, ptr.modifiers); + _handler.onPointerUp(ptr_value, ptr.modifiers); } } @@ -217,18 +223,15 @@ public final class Pointers implements Handler.Callback return k.keys[DIRECTION_TO_INDEX[direction]]; } - /* - * Get the KeyValue at the given direction. In case of swipe (direction != - * null), 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. + /** + * Get the key nearest to [direction] that is not key0. Take care + * of applying [_handler.modifyKey] to the selected key in the same + * operation 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 getNearestKeyAtDirection(Pointer ptr, Integer direction) + private KeyValue getNearestKeyAtDirection(Pointer ptr, int direction) { - if (direction == null) - return _handler.modifyKey(ptr.key.keys[0], ptr.modifiers); KeyValue k; // [i] is [0, -1, 1, -2, 2, ...] for (int i = 0; i > -4; i = (~i>>31) - i) @@ -261,37 +264,59 @@ public final class Pointers implements Handler.Callback float dy = y - ptr.downY; float dist = Math.abs(dx) + Math.abs(dy); - Integer direction; if (dist < _config.swipe_dist_px) { - direction = null; + // Pointer is still on the center. + if (ptr.gesture == null || !ptr.gesture.is_in_progress()) + return; + // Gesture ended + ptr.gesture.moved_to_center(); + ptr.value = apply_gesture(ptr, ptr.gesture.get_gesture()); + ptr.flags = 0; + } else - { + { // Pointer is on a quadrant. // See [getKeyAtDirection()] for the meaning. The starting point on the // circle is the top direction. double a = Math.atan2(dy, dx) + Math.PI; // a is between 0 and 2pi, 0 is pointing to the left // add 12 to align 0 to the top - direction = ((int)(a * 8 / Math.PI) + 12) % 16; - } + int direction = ((int)(a * 8 / Math.PI) + 12) % 16; + if (ptr.gesture == null) + { // Gesture starts + + ptr.gesture = new Gesture(direction); + KeyValue new_value = getNearestKeyAtDirection(ptr, direction); + if (new_value != null) + { // Pointer is swiping into a side key. + + ptr.value = new_value; + ptr.flags = pointer_flags_of_kv(new_value); + // Sliding mode is entered when key5 or key6 is down on a slider key. + if (ptr.key.slider && + (new_value.equals(ptr.key.getKeyValue(5)) + || new_value.equals(ptr.key.getKeyValue(6)))) + { + startSliding(ptr, x); + } + _handler.onPointerDown(new_value, true); + } - if (direction != ptr.selected_direction) - { - ptr.selected_direction = direction; - KeyValue newValue = getNearestKeyAtDirection(ptr, direction); - if (newValue != null && !newValue.equals(ptr.value)) - { - ptr.value = newValue; - ptr.flags = pointer_flags_of_kv(newValue); - // Sliding mode is entered when key5 or key6 is down on a slider key. - if (ptr.key.slider && - (newValue.equals(ptr.key.getKeyValue(5)) - || newValue.equals(ptr.key.getKeyValue(6)))) + } + else if (ptr.gesture.changed_direction(direction)) + { // Gesture changed state + if (!ptr.gesture.is_in_progress()) + { // Gesture ended + stopKeyRepeat(ptr); + _handler.onPointerFlagsChanged(true); + } + else { - startSliding(ptr, x); + ptr.value = apply_gesture(ptr, ptr.gesture.get_gesture()); + restartKeyRepeat(ptr); + ptr.flags = 0; // Special behaviors are ignored during a gesture. } - _handler.onPointerDown(newValue, true); } } } @@ -395,6 +420,12 @@ public final class Pointers implements Handler.Callback } } + private void restartKeyRepeat(Pointer ptr) + { + stopKeyRepeat(ptr); + startKeyRepeat(ptr); + } + /** A pointer is repeating. Returns [true] if repeat should continue. */ private boolean handleKeyRepeat(Pointer ptr) { @@ -447,14 +478,51 @@ public final class Pointers implements Handler.Callback return flags; } + // Gestures + + /** Apply a gesture to the current key. */ + KeyValue apply_gesture(Pointer ptr, Gesture.Name gesture) + { + switch (gesture) + { + case None: + return ptr.value; + case Swipe: + return ptr.value; + case Roundtrip: + return + modify_key_with_extra_modifier( + ptr, + getNearestKeyAtDirection(ptr, ptr.gesture.current_direction()), + KeyValue.Modifier.GESTURE); + case Circle: + return + modify_key_with_extra_modifier(ptr, ptr.key.keys[0], + KeyValue.Modifier.GESTURE); + case Anticircle: + return _handler.modifyKey(ptr.key.keys[0], ptr.modifiers); + } + return ptr.value; // Unreachable + } + + KeyValue modify_key_with_extra_modifier(Pointer ptr, KeyValue kv, + KeyValue.Modifier extra_mod) + { + return + _handler.modifyKey(kv, + ptr.modifiers.with_extra_mod(KeyValue.makeInternalModifier(extra_mod))); + } + + // Pointers + private static final class Pointer { /** -1 when latched. */ public int pointerId; /** The Key pressed by this Pointer */ public final KeyboardData.Key key; - /** Current direction. [null] means not swiping. */ - public Integer selected_direction; + /** Gesture state, see [Gesture]. [null] means the pointer has not moved out of the center region. */ + public Gesture gesture; /** Selected value with [modifiers] applied. */ public KeyValue value; public float downX; @@ -472,7 +540,7 @@ public final class Pointers implements Handler.Callback { pointerId = p; key = k; - selected_direction = null; + gesture = null; value = v; downX = x; downY = y; @@ -602,6 +670,14 @@ public final class Pointers implements Handler.Callback return false; } + /** Return a copy of this object with an extra modifier added. */ + public Modifiers with_extra_mod(KeyValue m) + { + KeyValue[] newmods = Arrays.copyOf(_mods, _size + 1); + newmods[_size] = m; + return ofArray(newmods, newmods.length); + } + /** Returns the activated modifiers that are not in [m2]. */ public Iterator diff(Modifiers m2) { -- cgit v1.2.3