touch_actions_panel.cpp 12 KB

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