Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/touch_actions_panel.cpp
9898 views
1
/**************************************************************************/
2
/* touch_actions_panel.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_actions_panel.h"
32
33
#include "core/input/input.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/settings/editor_settings.h"
36
#include "scene/gui/box_container.h"
37
#include "scene/gui/button.h"
38
#include "scene/gui/color_rect.h"
39
#include "scene/gui/texture_rect.h"
40
#include "scene/resources/style_box_flat.h"
41
42
void TouchActionsPanel::_notification(int p_what) {
43
switch (p_what) {
44
case NOTIFICATION_ENTER_TREE: {
45
DisplayServer::get_singleton()->set_hardware_keyboard_connection_change_callback(callable_mp(this, &TouchActionsPanel::_hardware_keyboard_connected));
46
_hardware_keyboard_connected(DisplayServer::get_singleton()->has_hardware_keyboard());
47
if (!is_floating) {
48
get_parent()->move_child(this, embedded_panel_index);
49
}
50
} break;
51
case NOTIFICATION_VISIBILITY_CHANGED: {
52
set_process_input(is_visible_in_tree());
53
} break;
54
case NOTIFICATION_THEME_CHANGED: {
55
if (is_floating) {
56
drag_handle->set_texture(get_editor_theme_icon(SNAME("DragHandle")));
57
layout_toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Orientation")));
58
lock_panel_button->set_button_icon(get_editor_theme_icon(SNAME("Lock")));
59
} else {
60
if (embedded_panel_index == 1) {
61
panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));
62
} else {
63
panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));
64
}
65
}
66
save_button->set_button_icon(get_editor_theme_icon(SNAME("Save")));
67
delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
68
undo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));
69
redo_button->set_button_icon(get_editor_theme_icon(SNAME("Redo")));
70
cut_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCut")));
71
copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
72
paste_button->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
73
} break;
74
}
75
}
76
77
void TouchActionsPanel::input(const Ref<InputEvent> &event) {
78
if (ctrl_btn_pressed) {
79
event->call(SNAME("set_ctrl_pressed"), true);
80
}
81
82
if (shift_btn_pressed) {
83
event->call(SNAME("set_shift_pressed"), true);
84
}
85
86
if (alt_btn_pressed) {
87
event->call(SNAME("set_alt_pressed"), true);
88
}
89
}
90
91
void TouchActionsPanel::_hardware_keyboard_connected(bool p_connected) {
92
set_visible(!p_connected);
93
}
94
95
void TouchActionsPanel::_simulate_editor_shortcut(const String &p_shortcut_name) {
96
Ref<Shortcut> shortcut = ED_GET_SHORTCUT(p_shortcut_name);
97
98
if (shortcut.is_valid() && !shortcut->get_events().is_empty()) {
99
Ref<InputEventKey> event = shortcut->get_events()[0];
100
if (event.is_valid()) {
101
event->set_pressed(true);
102
Input::get_singleton()->parse_input_event(event);
103
}
104
}
105
}
106
107
void TouchActionsPanel::_simulate_key_press(Key p_keycode) {
108
Ref<InputEventKey> event;
109
event.instantiate();
110
event->set_keycode(p_keycode);
111
event->set_pressed(true);
112
Input::get_singleton()->parse_input_event(event);
113
}
114
115
void TouchActionsPanel::_on_modifier_button_toggled(bool p_pressed, int p_modifier) {
116
switch ((Modifier)p_modifier) {
117
case MODIFIER_CTRL:
118
ctrl_btn_pressed = p_pressed;
119
break;
120
case MODIFIER_SHIFT:
121
shift_btn_pressed = p_pressed;
122
break;
123
case MODIFIER_ALT:
124
alt_btn_pressed = p_pressed;
125
break;
126
}
127
}
128
129
Button *TouchActionsPanel::_add_new_action_button(const String &p_shortcut, const String &p_name, Key p_keycode) {
130
Button *action_button = memnew(Button);
131
action_button->set_theme_type_variation("FlatMenuButton");
132
action_button->set_accessibility_name(p_name);
133
action_button->set_focus_mode(FOCUS_ACCESSIBILITY);
134
action_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
135
if (p_keycode == Key::NONE) {
136
action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_editor_shortcut).bind(p_shortcut));
137
} else {
138
action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_key_press).bind(p_keycode));
139
}
140
box->add_child(action_button);
141
return action_button;
142
}
143
144
void TouchActionsPanel::_add_new_modifier_button(Modifier p_modifier) {
145
String text;
146
switch (p_modifier) {
147
case MODIFIER_CTRL:
148
text = "Ctrl";
149
break;
150
case MODIFIER_SHIFT:
151
text = "Shift";
152
break;
153
case MODIFIER_ALT:
154
text = "Alt";
155
break;
156
}
157
Button *toggle_button = memnew(Button);
158
toggle_button->set_text(text);
159
toggle_button->set_toggle_mode(true);
160
toggle_button->set_theme_type_variation("FlatMenuButton");
161
toggle_button->set_accessibility_name(text);
162
toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);
163
toggle_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_on_modifier_button_toggled).bind((int)p_modifier));
164
box->add_child(toggle_button);
165
}
166
167
void TouchActionsPanel::_on_drag_handle_gui_input(const Ref<InputEvent> &p_event) {
168
if (locked_panel) {
169
return;
170
}
171
Ref<InputEventMouseButton> mouse_button_event = p_event;
172
if (mouse_button_event.is_valid() && mouse_button_event->get_button_index() == MouseButton::LEFT) {
173
if (mouse_button_event->is_pressed()) {
174
dragging = true;
175
drag_offset = mouse_button_event->get_position();
176
} else {
177
if (dragging) {
178
dragging = false;
179
EditorSettings::get_singleton()->set("_touch_actions_panel_position", get_position());
180
EditorSettings::get_singleton()->save();
181
}
182
}
183
}
184
185
Ref<InputEventMouseMotion> mouse_motion_event = p_event;
186
if (dragging && mouse_motion_event.is_valid()) {
187
Vector2 new_position = get_position() + mouse_motion_event->get_relative();
188
const float margin = 25.0;
189
Vector2 parent_size = get_parent_area_size();
190
Vector2 panel_size = get_size();
191
new_position = new_position.clamp(Vector2(margin, margin), parent_size - panel_size - Vector2(margin, margin));
192
set_position(new_position);
193
}
194
}
195
196
void TouchActionsPanel::_switch_layout() {
197
box->set_vertical(!box->is_vertical());
198
reset_size();
199
queue_redraw();
200
EditorSettings::get_singleton()->set("_touch_actions_panel_vertical_layout", box->is_vertical());
201
EditorSettings::get_singleton()->save();
202
}
203
204
void TouchActionsPanel::_lock_panel_toggled(bool p_pressed) {
205
locked_panel = p_pressed;
206
layout_toggle_button->set_visible(!p_pressed);
207
drag_handle->set_visible(!p_pressed);
208
reset_size();
209
queue_redraw();
210
}
211
212
void TouchActionsPanel::_switch_embedded_panel_side() {
213
if (embedded_panel_index == 0) {
214
embedded_panel_index = 1;
215
panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));
216
} else {
217
embedded_panel_index = 0;
218
panel_pos_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));
219
}
220
get_parent()->move_child(this, embedded_panel_index); // Parent is a hbox with only two children -- TouchActionsPanel and main Editor UI.
221
EditorSettings::get_singleton()->set("_touch_actions_panel_embed_index", embedded_panel_index);
222
EditorSettings::get_singleton()->save();
223
}
224
225
TouchActionsPanel::TouchActionsPanel() {
226
int panel_mode = EDITOR_GET("interface/touchscreen/touch_actions_panel");
227
is_floating = panel_mode == 2;
228
229
if (is_floating) {
230
Ref<StyleBoxFlat> panel_style;
231
panel_style.instantiate();
232
panel_style->set_bg_color(Color(0.1, 0.1, 0.1, 1));
233
panel_style->set_border_color(Color(0.3, 0.3, 0.3, 1));
234
panel_style->set_border_width_all(3);
235
panel_style->set_corner_radius_all(10);
236
panel_style->set_content_margin_all(12);
237
add_theme_style_override(SceneStringName(panel), panel_style);
238
239
set_position(EDITOR_DEF("_touch_actions_panel_position", Point2(480, 480))); // Dropped it here for no good reason — users can move it anyway.
240
}
241
242
box = memnew(BoxContainer);
243
box->add_theme_constant_override("separation", 20);
244
if (is_floating) {
245
box->set_vertical(EDITOR_DEF("_touch_actions_panel_vertical_layout", false));
246
} else {
247
box->set_vertical(true);
248
}
249
add_child(box);
250
251
if (is_floating) {
252
drag_handle = memnew(TextureRect);
253
drag_handle->set_custom_minimum_size(Size2(40, 40));
254
drag_handle->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
255
drag_handle->connect(SceneStringName(gui_input), callable_mp(this, &TouchActionsPanel::_on_drag_handle_gui_input));
256
box->add_child(drag_handle);
257
258
layout_toggle_button = memnew(Button);
259
layout_toggle_button->set_theme_type_variation("FlatMenuButton");
260
layout_toggle_button->set_accessibility_name(TTRC("Switch Layout"));
261
layout_toggle_button->set_focus_mode(FOCUS_ACCESSIBILITY);
262
layout_toggle_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
263
layout_toggle_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_layout));
264
box->add_child(layout_toggle_button);
265
266
lock_panel_button = memnew(Button);
267
lock_panel_button->set_toggle_mode(true);
268
lock_panel_button->set_theme_type_variation("FlatMenuButton");
269
lock_panel_button->set_accessibility_name(TTRC("Lock Panel"));
270
lock_panel_button->set_focus_mode(FOCUS_ACCESSIBILITY);
271
lock_panel_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
272
lock_panel_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_lock_panel_toggled));
273
box->add_child(lock_panel_button);
274
} else {
275
panel_pos_button = memnew(Button);
276
panel_pos_button->set_theme_type_variation("FlatMenuButton");
277
panel_pos_button->set_accessibility_name(TTRC("Switch Embedded Panel Position"));
278
panel_pos_button->set_focus_mode(FOCUS_ACCESSIBILITY);
279
panel_pos_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
280
panel_pos_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_embedded_panel_side));
281
box->add_child(panel_pos_button);
282
283
embedded_panel_index = EDITOR_DEF("_touch_actions_panel_embed_index", 0);
284
}
285
286
ColorRect *separator = memnew(ColorRect);
287
separator->set_color(Color(0.5, 0.5, 0.5));
288
separator->set_custom_minimum_size(Size2(2, 2));
289
box->add_child(separator);
290
291
// Add action buttons.
292
save_button = _add_new_action_button("editor/save_scene", TTRC("Save"));
293
delete_button = _add_new_action_button("", TTRC("Delete"), Key::KEY_DELETE);
294
undo_button = _add_new_action_button("ui_undo", TTRC("Undo"));
295
redo_button = _add_new_action_button("ui_redo", TTRC("Redo"));
296
cut_button = _add_new_action_button("ui_cut", TTRC("Cut"));
297
copy_button = _add_new_action_button("ui_copy", TTRC("Copy"));
298
paste_button = _add_new_action_button("ui_paste", TTRC("Paste"));
299
300
_add_new_modifier_button(MODIFIER_CTRL);
301
_add_new_modifier_button(MODIFIER_SHIFT);
302
_add_new_modifier_button(MODIFIER_ALT);
303
}
304
305