Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/touch_screen_button.cpp
20851 views
1
/**************************************************************************/
2
/* touch_screen_button.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "touch_screen_button.h"
32
33
#include "core/input/input.h"
34
#include "scene/main/viewport.h"
35
36
void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {
37
if (texture_normal == p_texture) {
38
return;
39
}
40
if (texture_normal.is_valid()) {
41
texture_normal->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
42
}
43
texture_normal = p_texture;
44
if (texture_normal.is_valid()) {
45
texture_normal->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);
46
}
47
queue_redraw();
48
}
49
50
Ref<Texture2D> TouchScreenButton::get_texture_normal() const {
51
return texture_normal;
52
}
53
54
void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {
55
if (texture_pressed == p_texture_pressed) {
56
return;
57
}
58
if (texture_pressed.is_valid()) {
59
texture_pressed->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
60
}
61
texture_pressed = p_texture_pressed;
62
if (texture_pressed.is_valid()) {
63
texture_pressed->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);
64
}
65
queue_redraw();
66
}
67
68
Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {
69
return texture_pressed;
70
}
71
72
void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
73
bitmask = p_bitmask;
74
}
75
76
Ref<BitMap> TouchScreenButton::get_bitmask() const {
77
return bitmask;
78
}
79
80
void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
81
if (shape == p_shape) {
82
return;
83
}
84
if (shape.is_valid()) {
85
shape->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
86
}
87
shape = p_shape;
88
if (shape.is_valid()) {
89
shape->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
90
}
91
queue_redraw();
92
}
93
94
Ref<Shape2D> TouchScreenButton::get_shape() const {
95
return shape;
96
}
97
98
void TouchScreenButton::set_shape_centered(bool p_shape_centered) {
99
shape_centered = p_shape_centered;
100
queue_redraw();
101
}
102
103
bool TouchScreenButton::is_shape_visible() const {
104
return shape_visible;
105
}
106
107
void TouchScreenButton::set_shape_visible(bool p_shape_visible) {
108
shape_visible = p_shape_visible;
109
queue_redraw();
110
}
111
112
bool TouchScreenButton::is_shape_centered() const {
113
return shape_centered;
114
}
115
116
void TouchScreenButton::_accessibility_action_click(const Variant &p_data) {
117
_press(0);
118
_release();
119
}
120
121
void TouchScreenButton::_notification(int p_what) {
122
switch (p_what) {
123
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
124
RID ae = get_accessibility_element();
125
ERR_FAIL_COND(ae.is_null());
126
127
Rect2 dst_rect(Point2(), texture_normal.is_valid() ? texture_normal->get_size() : Size2());
128
129
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
130
131
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &TouchScreenButton::_accessibility_action_click));
132
133
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());
134
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect);
135
} break;
136
137
case NOTIFICATION_DRAW: {
138
if (!is_inside_tree()) {
139
return;
140
}
141
if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
142
return;
143
}
144
145
if (finger_pressed != -1) {
146
if (texture_pressed.is_valid()) {
147
draw_texture(texture_pressed, Point2());
148
} else if (texture_normal.is_valid()) {
149
draw_texture(texture_normal, Point2());
150
}
151
152
} else {
153
if (texture_normal.is_valid()) {
154
draw_texture(texture_normal, Point2());
155
}
156
}
157
158
if (!shape_visible) {
159
return;
160
}
161
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
162
return;
163
}
164
if (shape.is_valid()) {
165
Color draw_col = get_tree()->get_debug_collisions_color();
166
167
Vector2 pos;
168
if (shape_centered && texture_normal.is_valid()) {
169
pos = texture_normal->get_size() * 0.5;
170
}
171
172
draw_set_transform_matrix(get_canvas_transform().translated_local(pos));
173
shape->draw(get_canvas_item(), draw_col);
174
}
175
} break;
176
177
case NOTIFICATION_ENTER_TREE: {
178
if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
179
return;
180
}
181
queue_redraw();
182
183
if (!Engine::get_singleton()->is_editor_hint()) {
184
set_process_input(is_visible_in_tree());
185
}
186
} break;
187
188
case NOTIFICATION_EXIT_TREE: {
189
if (is_pressed()) {
190
_release(true);
191
}
192
} break;
193
194
case NOTIFICATION_VISIBILITY_CHANGED: {
195
if (Engine::get_singleton()->is_editor_hint()) {
196
break;
197
}
198
if (is_visible_in_tree()) {
199
set_process_input(true);
200
} else {
201
set_process_input(false);
202
if (is_pressed()) {
203
_release();
204
}
205
}
206
} break;
207
208
case NOTIFICATION_SUSPENDED:
209
case NOTIFICATION_PAUSED: {
210
if (is_pressed()) {
211
_release();
212
}
213
} break;
214
}
215
}
216
217
bool TouchScreenButton::is_pressed() const {
218
return finger_pressed != -1;
219
}
220
221
void TouchScreenButton::set_action(const String &p_action) {
222
action = p_action;
223
}
224
225
String TouchScreenButton::get_action() const {
226
return action;
227
}
228
229
void TouchScreenButton::input(const Ref<InputEvent> &p_event) {
230
ERR_FAIL_COND(p_event.is_null());
231
232
if (!is_visible_in_tree()) {
233
return;
234
}
235
236
const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
237
238
if (passby_press) {
239
const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
240
241
if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
242
_release();
243
}
244
245
if ((st && st->is_pressed()) || sd) {
246
int index = st ? st->get_index() : sd->get_index();
247
Point2 coord = st ? st->get_position() : sd->get_position();
248
249
if (finger_pressed == -1 || index == finger_pressed) {
250
if (_is_point_inside(coord)) {
251
if (finger_pressed == -1) {
252
_press(index);
253
}
254
} else {
255
if (finger_pressed != -1) {
256
_release();
257
}
258
}
259
}
260
}
261
262
} else {
263
if (st) {
264
if (st->is_pressed()) {
265
const bool can_press = finger_pressed == -1;
266
if (!can_press) {
267
return; //already fingering
268
}
269
270
if (_is_point_inside(st->get_position())) {
271
_press(st->get_index());
272
}
273
} else {
274
if (st->get_index() == finger_pressed) {
275
_release();
276
}
277
}
278
}
279
}
280
}
281
282
bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
283
Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
284
285
bool touched = false;
286
bool check_rect = true;
287
288
if (shape.is_valid()) {
289
check_rect = false;
290
291
Vector2 pos;
292
if (shape_centered && texture_normal.is_valid()) {
293
pos = texture_normal->get_size() * 0.5;
294
}
295
296
touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
297
}
298
299
if (bitmask.is_valid()) {
300
check_rect = false;
301
if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
302
if (bitmask->get_bitv(coord)) {
303
touched = true;
304
}
305
}
306
}
307
308
if (!touched && check_rect) {
309
if (texture_normal.is_valid()) {
310
touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
311
}
312
}
313
314
return touched;
315
}
316
317
void TouchScreenButton::_press(int p_finger_pressed) {
318
finger_pressed = p_finger_pressed;
319
320
if (action != StringName()) {
321
Input::get_singleton()->action_press(action);
322
Ref<InputEventAction> iea;
323
iea.instantiate();
324
iea->set_action(action);
325
iea->set_pressed(true);
326
get_viewport()->push_input(iea, true);
327
}
328
329
emit_signal(SceneStringName(pressed));
330
queue_redraw();
331
}
332
333
void TouchScreenButton::_release(bool p_exiting_tree) {
334
finger_pressed = -1;
335
336
if (action != StringName()) {
337
Input::get_singleton()->action_release(action);
338
if (!p_exiting_tree) {
339
Ref<InputEventAction> iea;
340
iea.instantiate();
341
iea->set_action(action);
342
iea->set_pressed(false);
343
get_viewport()->push_input(iea, true);
344
}
345
}
346
347
if (!p_exiting_tree) {
348
emit_signal(SNAME("released"));
349
queue_redraw();
350
}
351
}
352
353
#ifdef DEBUG_ENABLED
354
Rect2 TouchScreenButton::_edit_get_rect() const {
355
if (texture_normal.is_null()) {
356
return CanvasItem::_edit_get_rect();
357
}
358
359
return Rect2(Size2(), texture_normal->get_size());
360
}
361
362
bool TouchScreenButton::_edit_use_rect() const {
363
return texture_normal.is_valid();
364
}
365
#endif // DEBUG_ENABLED
366
367
Rect2 TouchScreenButton::get_anchorable_rect() const {
368
if (texture_normal.is_null()) {
369
return CanvasItem::get_anchorable_rect();
370
}
371
372
return Rect2(Size2(), texture_normal->get_size());
373
}
374
375
void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
376
visibility = p_mode;
377
queue_redraw();
378
}
379
380
TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
381
return visibility;
382
}
383
384
void TouchScreenButton::set_passby_press(bool p_enable) {
385
passby_press = p_enable;
386
}
387
388
bool TouchScreenButton::is_passby_press_enabled() const {
389
return passby_press;
390
}
391
392
#ifndef DISABLE_DEPRECATED
393
bool TouchScreenButton::_set(const StringName &p_name, const Variant &p_value) {
394
if (p_name == CoreStringName(normal)) { // Compatibility with Godot 3.x.
395
set_texture_normal(p_value);
396
return true;
397
} else if (p_name == SceneStringName(pressed)) { // Compatibility with Godot 3.x.
398
set_texture_pressed(p_value);
399
return true;
400
}
401
return false;
402
}
403
#endif // DISABLE_DEPRECATED
404
405
void TouchScreenButton::_bind_methods() {
406
ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
407
ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
408
409
ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
410
ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
411
412
ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
413
ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
414
415
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
416
ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
417
418
ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
419
ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
420
421
ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
422
ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
423
424
ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
425
ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
426
427
ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
428
ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
429
430
ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
431
ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
432
433
ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
434
435
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture_normal", "get_texture_normal");
436
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture_pressed", "get_texture_pressed");
437
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, BitMap::get_class_static()), "set_bitmask", "get_bitmask");
438
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, Shape2D::get_class_static()), "set_shape", "get_shape");
439
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
440
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
441
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
442
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action", PROPERTY_HINT_INPUT_NAME, "show_builtin,loose_mode"), "set_action", "get_action");
443
ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
444
445
ADD_SIGNAL(MethodInfo("pressed"));
446
ADD_SIGNAL(MethodInfo("released"));
447
448
BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
449
BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
450
}
451
452
TouchScreenButton::TouchScreenButton() {
453
unit_rect.instantiate();
454
unit_rect->set_size(Vector2(1, 1));
455
}
456
457