Skip to content

Commit 15d57a3

Browse files
committed
[Draft] Add touch support to TextEdit
1 parent 2a553e7 commit 15d57a3

9 files changed

Lines changed: 299 additions & 83 deletions

File tree

doc/classes/TextEdit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@
702702
Returns the current version of the [TextEdit]. The version is a count of recorded operations by the undo/redo history.
703703
</description>
704704
</method>
705+
<method name="get_virtual_keyboard_show_on_double_tap" qualifiers="const">
706+
<return type="bool" />
707+
<description>
708+
</description>
709+
</method>
705710
<method name="get_visible_line_count" qualifiers="const">
706711
<return type="int" />
707712
<description>
@@ -1252,6 +1257,12 @@
12521257
Provide custom tooltip text. The callback method must take the following args: [code]hovered_word: String[/code].
12531258
</description>
12541259
</method>
1260+
<method name="set_virtual_keyboard_show_on_double_tap">
1261+
<return type="void" />
1262+
<param index="0" name="show_on_double_tap" type="bool" />
1263+
<description>
1264+
</description>
1265+
</method>
12551266
<method name="skip_selection_for_next_occurrence">
12561267
<return type="void" />
12571268
<description>

editor/gui/code_editor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,14 +951,12 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
951951
}
952952
}
953953

954-
#ifndef ANDROID_ENABLED
955954
Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
956955
if (magnify_gesture.is_valid()) {
957956
_zoom_to(zoom_factor * std::pow(magnify_gesture->get_factor(), 0.25f));
958957
accept_event();
959958
return;
960959
}
961-
#endif
962960

963961
Ref<InputEventKey> k = p_event;
964962

@@ -1887,7 +1885,7 @@ void CodeTextEditor::_zoom_out() {
18871885
}
18881886

18891887
void CodeTextEditor::_zoom_to(float p_zoom_factor) {
1890-
if (zoom_factor == p_zoom_factor) {
1888+
if (Math::is_equal_approx(zoom_factor, p_zoom_factor)) {
18911889
return;
18921890
}
18931891

@@ -1962,6 +1960,7 @@ CodeTextEditor::CodeTextEditor() {
19621960
text_editor->set_draw_bookmarks_gutter(true);
19631961

19641962
text_editor->set_virtual_keyboard_show_on_focus(false);
1963+
text_editor->set_virtual_keyboard_show_on_double_tap(true);
19651964
text_editor->set_draw_line_numbers(true);
19661965
text_editor->set_highlight_matching_braces_enabled(true);
19671966
text_editor->set_auto_indent_enabled(true);

platform/android/android_input_handler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void AndroidInputHandler::_cancel_all_touch() {
146146
touch.clear();
147147
}
148148

149-
void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) {
149+
void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled) {
150150
if (touch.size()) {
151151
//end all if exist
152152
for (int i = 0; i < touch.size(); i++) {
@@ -156,7 +156,7 @@ void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool
156156
ev->set_pressed(p_pressed);
157157
ev->set_canceled(p_canceled);
158158
ev->set_position(touch[i].pos);
159-
ev->set_double_tap(p_double_tap);
159+
ev->set_double_tap(touch[i].double_tap);
160160
Input::get_singleton()->parse_input_event(ev);
161161
}
162162
}
@@ -167,7 +167,7 @@ void AndroidInputHandler::_release_all_touch() {
167167
touch.clear();
168168
}
169169

170-
void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {
170+
void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points) {
171171
switch (p_event) {
172172
case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
173173
// Release any remaining touches or mouse event
@@ -180,10 +180,11 @@ void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const
180180
touch.write[i].pos = p_points[i].pos;
181181
touch.write[i].pressure = p_points[i].pressure;
182182
touch.write[i].tilt = p_points[i].tilt;
183+
touch.write[i].double_tap = p_points[i].double_tap;
183184
}
184185

185186
//send touch
186-
_parse_all_touch(true, false, p_double_tap);
187+
_parse_all_touch(true, false);
187188

188189
} break;
189190
case AMOTION_EVENT_ACTION_MOVE: { //motion

platform/android/android_input_handler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class AndroidInputHandler {
4343
Point2 pos;
4444
float pressure = 0;
4545
Vector2 tilt;
46+
bool double_tap = false;
4647
};
4748

4849
struct MouseEventInfo {
@@ -90,15 +91,15 @@ class AndroidInputHandler {
9091

9192
void _cancel_mouse_event_info(bool p_source_mouse_relative = false);
9293

93-
void _parse_all_touch(bool p_pressed, bool p_canceled = false, bool p_double_tap = false);
94+
void _parse_all_touch(bool p_pressed, bool p_canceled = false);
9495

9596
void _release_all_touch();
9697

9798
void _cancel_all_touch();
9899

99100
public:
100101
void process_mouse_event(int p_event_action, int p_event_android_buttons_mask, Point2 p_event_pos, Vector2 p_delta, bool p_double_click, bool p_source_mouse_relative, float p_pressure, Vector2 p_tilt);
101-
void process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap);
102+
void process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points);
102103
void process_magnify(Point2 p_pos, float p_factor);
103104
void process_pan(Point2 p_pos, Vector2 p_delta);
104105
void process_joy_event(JoypadEvent p_event);

platform/android/java/lib/src/main/java/org/godotengine/godot/input/GodotGestureHandler.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@
3030

3131
package org.godotengine.godot.input
3232

33-
import android.os.Build
3433
import android.view.GestureDetector.SimpleOnGestureListener
35-
import android.view.InputDevice
3634
import android.view.MotionEvent
3735
import android.view.ScaleGestureDetector
3836
import android.view.ScaleGestureDetector.OnScaleGestureListener
39-
import org.godotengine.godot.GodotLib
4037

4138
/**
4239
* Handles regular and scale gesture input related events for the [GodotView] view.
@@ -51,12 +48,19 @@ internal class GodotGestureHandler(private val inputHandler: GodotInputHandler)
5148
}
5249

5350
/**
54-
* Enable pan and scale gestures
51+
* Enable panning gestures.
5552
*/
56-
var panningAndScalingEnabled = false
53+
var panningEnabled = false
54+
55+
/**
56+
* Enable scaling gestures.
57+
*/
58+
var scalingEnabled = false
5759

5860
var scrollDeadzoneDisabled = false
5961

62+
var longPressEnabled = false
63+
6064
/**
6165
* Enable haptic feedback on long-press right-click
6266
*/
@@ -181,15 +185,22 @@ internal class GodotGestureHandler(private val inputHandler: GodotInputHandler)
181185
override fun onDoubleTapEvent(event: MotionEvent): Boolean {
182186
if (event.actionMasked == MotionEvent.ACTION_UP) {
183187
nextDownIsDoubleTap = false
184-
inputHandler.handleMotionEvent(event)
185-
} else if (event.actionMasked == MotionEvent.ACTION_MOVE && !panningAndScalingEnabled) {
188+
189+
// Long press is restored to its previous value.
190+
inputHandler.gestureDetector.setIsLongpressEnabled(longPressEnabled)
191+
192+
inputHandler.handleMotionEvent(event, event.actionMasked, true)
193+
} else if (event.actionMasked == MotionEvent.ACTION_MOVE && !(scalingEnabled && inputHandler.scaleGestureDetector.isQuickScaleEnabled)) {
186194
inputHandler.handleMotionEvent(event)
187195
}
188196

189197
return true
190198
}
191199

192200
override fun onDoubleTap(event: MotionEvent): Boolean {
201+
// Long press is disabled as it interferes with double tap events.
202+
inputHandler.gestureDetector.setIsLongpressEnabled(false)
203+
193204
nextDownIsDoubleTap = true
194205
return true
195206
}
@@ -214,7 +225,7 @@ internal class GodotGestureHandler(private val inputHandler: GodotInputHandler)
214225

215226
val x = terminusEvent.x
216227
val y = terminusEvent.y
217-
if (terminusEvent.pointerCount >= 2 && panningAndScalingEnabled && !pointerCaptureInProgress && !dragInProgress) {
228+
if (terminusEvent.pointerCount >= 2 && panningEnabled && !pointerCaptureInProgress && !dragInProgress) {
218229
inputHandler.handlePanEvent(x, y, distanceX / 5f, distanceY / 5f)
219230
} else if (!scaleInProgress) {
220231
dragInProgress = true
@@ -226,7 +237,7 @@ internal class GodotGestureHandler(private val inputHandler: GodotInputHandler)
226237
}
227238

228239
override fun onScale(detector: ScaleGestureDetector): Boolean {
229-
if (!panningAndScalingEnabled || pointerCaptureInProgress || dragInProgress) {
240+
if (!scalingEnabled || pointerCaptureInProgress || dragInProgress) {
230241
return false
231242
}
232243

@@ -237,7 +248,7 @@ internal class GodotGestureHandler(private val inputHandler: GodotInputHandler)
237248
}
238249

239250
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
240-
if (!panningAndScalingEnabled || pointerCaptureInProgress || dragInProgress) {
251+
if (!scalingEnabled || pointerCaptureInProgress || dragInProgress) {
241252
return false
242253
}
243254
scaleInProgress = true

platform/android/java/lib/src/main/java/org/godotengine/godot/input/GodotInputHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens
8888
private final Godot godot;
8989
private final InputManager mInputManager;
9090
private final WindowManager windowManager;
91-
private final GestureDetector gestureDetector;
92-
private final ScaleGestureDetector scaleGestureDetector;
91+
final GestureDetector gestureDetector;
92+
final ScaleGestureDetector scaleGestureDetector;
9393
private final GodotGestureHandler godotGestureHandler;
9494

9595
/**
@@ -112,9 +112,12 @@ public GodotInputHandler(Context context, Godot godot) {
112112

113113
this.godotGestureHandler = new GodotGestureHandler(this);
114114
this.gestureDetector = new GestureDetector(context, godotGestureHandler);
115-
this.gestureDetector.setIsLongpressEnabled(false);
115+
enableLongPress(false);
116+
116117
this.scaleGestureDetector = new ScaleGestureDetector(context, godotGestureHandler);
117118
this.scaleGestureDetector.setStylusScaleEnabled(true);
119+
this.scaleGestureDetector.setQuickScaleEnabled(false);
120+
118121
Configuration config = context.getResources().getConfiguration();
119122
hasHardwareKeyboardConfig = config.keyboard != Configuration.KEYBOARD_NOKEYS &&
120123
config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
@@ -125,6 +128,7 @@ public GodotInputHandler(Context context, Godot godot) {
125128
*/
126129
public void enableLongPress(boolean enable) {
127130
this.gestureDetector.setIsLongpressEnabled(enable);
131+
this.godotGestureHandler.setLongPressEnabled(enable);
128132
}
129133

130134
/**
@@ -157,7 +161,8 @@ void performHapticFeedback() {
157161
* Note: This may interfere with multi-touch handling / support.
158162
*/
159163
public void enablePanningAndScalingGestures(boolean enable) {
160-
this.godotGestureHandler.setPanningAndScalingEnabled(enable);
164+
this.godotGestureHandler.setPanningEnabled(enable);
165+
this.godotGestureHandler.setScalingEnabled(enable);
161166
}
162167

163168
/**

platform/android/java_godot_lib_jni.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,11 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchTouchEvent(JN
362362
tp.pos = Point2(p[1], p[2]);
363363
tp.pressure = p[3];
364364
tp.tilt = Vector2(p[4], p[5]);
365+
tp.double_tap = p_double_tap;
365366
points.push_back(tp);
366367
}
367368

368-
input_handler->process_touch_event(ev, pointer, points, p_double_tap);
369+
input_handler->process_touch_event(ev, pointer, points);
369370
}
370371

371372
// Called on the UI thread

0 commit comments

Comments
 (0)