touch_actions_panel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_settings.h"
  33. #include "editor/editor_string_names.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. } break;
  45. case NOTIFICATION_VISIBILITY_CHANGED: {
  46. set_process_input(is_visible_in_tree());
  47. } break;
  48. case NOTIFICATION_THEME_CHANGED: {
  49. drag_handle->set_texture(get_editor_theme_icon(SNAME("DragHandle")));
  50. layout_toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Orientation")));
  51. lock_panel_button->set_button_icon(get_editor_theme_icon(SNAME("Lock")));
  52. save_button->set_button_icon(get_editor_theme_icon(SNAME("Save")));
  53. delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  54. undo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));
  55. redo_button->set_button_icon(get_editor_theme_icon(SNAME("Redo")));
  56. cut_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCut")));
  57. copy_button->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  58. paste_button->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
  59. } break;
  60. }
  61. }
  62. void TouchActionsPanel::input(const Ref<InputEvent> &event) {
  63. if (ctrl_btn_pressed) {
  64. event->call(SNAME("set_ctrl_pressed"), true);
  65. }
  66. if (shift_btn_pressed) {
  67. event->call(SNAME("set_shift_pressed"), true);
  68. }
  69. if (alt_btn_pressed) {
  70. event->call(SNAME("set_alt_pressed"), true);
  71. }
  72. }
  73. void TouchActionsPanel::_hardware_keyboard_connected(bool p_connected) {
  74. set_visible(!p_connected);
  75. }
  76. void TouchActionsPanel::_simulate_editor_shortcut(const String &p_shortcut_name) {
  77. Ref<Shortcut> shortcut = ED_GET_SHORTCUT(p_shortcut_name);
  78. if (shortcut.is_valid() && !shortcut->get_events().is_empty()) {
  79. Ref<InputEventKey> event = shortcut->get_events()[0];
  80. if (event.is_valid()) {
  81. event->set_pressed(true);
  82. Input::get_singleton()->parse_input_event(event);
  83. }
  84. }
  85. }
  86. void TouchActionsPanel::_simulate_key_press(Key p_keycode) {
  87. Ref<InputEventKey> event;
  88. event.instantiate();
  89. event->set_keycode(p_keycode);
  90. event->set_pressed(true);
  91. Input::get_singleton()->parse_input_event(event);
  92. }
  93. void TouchActionsPanel::_on_modifier_button_toggled(bool p_pressed, int p_modifier) {
  94. switch ((Modifier)p_modifier) {
  95. case MODIFIER_CTRL:
  96. ctrl_btn_pressed = p_pressed;
  97. break;
  98. case MODIFIER_SHIFT:
  99. shift_btn_pressed = p_pressed;
  100. break;
  101. case MODIFIER_ALT:
  102. alt_btn_pressed = p_pressed;
  103. break;
  104. }
  105. }
  106. Button *TouchActionsPanel::_add_new_action_button(const String &p_shortcut, const String &p_name, Key p_keycode) {
  107. Button *action_button = memnew(Button);
  108. action_button->set_theme_type_variation("FlatMenuButton");
  109. action_button->set_accessibility_name(p_name);
  110. action_button->set_focus_mode(FOCUS_NONE);
  111. action_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  112. if (p_keycode == Key::NONE) {
  113. action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_editor_shortcut).bind(p_shortcut));
  114. } else {
  115. action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_key_press).bind(p_keycode));
  116. }
  117. box->add_child(action_button);
  118. return action_button;
  119. }
  120. void TouchActionsPanel::_add_new_modifier_button(Modifier p_modifier) {
  121. String text;
  122. switch (p_modifier) {
  123. case MODIFIER_CTRL:
  124. text = "Ctrl";
  125. break;
  126. case MODIFIER_SHIFT:
  127. text = "Shift";
  128. break;
  129. case MODIFIER_ALT:
  130. text = "Alt";
  131. break;
  132. }
  133. Button *toggle_button = memnew(Button);
  134. toggle_button->set_text(text);
  135. toggle_button->set_toggle_mode(true);
  136. toggle_button->set_theme_type_variation("FlatMenuButton");
  137. toggle_button->set_accessibility_name(text);
  138. toggle_button->set_focus_mode(FOCUS_NONE);
  139. toggle_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_on_modifier_button_toggled).bind((int)p_modifier));
  140. box->add_child(toggle_button);
  141. }
  142. void TouchActionsPanel::_on_drag_handle_gui_input(const Ref<InputEvent> &p_event) {
  143. if (lock_panel_position) {
  144. return;
  145. }
  146. Ref<InputEventMouseButton> mouse_button_event = p_event;
  147. if (mouse_button_event.is_valid() && mouse_button_event->get_button_index() == MouseButton::LEFT) {
  148. if (mouse_button_event->is_pressed()) {
  149. dragging = true;
  150. drag_offset = mouse_button_event->get_position();
  151. } else {
  152. if (dragging) {
  153. dragging = false;
  154. EditorSettings::get_singleton()->set("_touch_actions_panel_position", get_position());
  155. EditorSettings::get_singleton()->save();
  156. }
  157. }
  158. }
  159. Ref<InputEventMouseMotion> mouse_motion_event = p_event;
  160. if (dragging && mouse_motion_event.is_valid()) {
  161. Vector2 new_position = get_position() + mouse_motion_event->get_relative();
  162. const float margin = 25.0;
  163. Vector2 parent_size = get_parent_area_size();
  164. Vector2 panel_size = get_size();
  165. new_position = new_position.clamp(Vector2(margin, margin), parent_size - panel_size - Vector2(margin, margin));
  166. set_position(new_position);
  167. }
  168. }
  169. void TouchActionsPanel::_switch_layout() {
  170. box->set_vertical(!box->is_vertical());
  171. reset_size();
  172. queue_redraw();
  173. EditorSettings::get_singleton()->set("_touch_actions_panel_vertical_layout", box->is_vertical());
  174. EditorSettings::get_singleton()->save();
  175. }
  176. void TouchActionsPanel::_lock_panel_toggled(bool p_pressed) {
  177. lock_panel_position = p_pressed;
  178. layout_toggle_button->set_disabled(p_pressed);
  179. }
  180. TouchActionsPanel::TouchActionsPanel() {
  181. Ref<StyleBoxFlat> panel_style;
  182. panel_style.instantiate();
  183. panel_style->set_bg_color(Color(0.1, 0.1, 0.1, 1));
  184. panel_style->set_border_color(Color(0.3, 0.3, 0.3, 1));
  185. panel_style->set_border_width_all(3);
  186. panel_style->set_corner_radius_all(10);
  187. panel_style->set_content_margin_all(12);
  188. add_theme_style_override(SceneStringName(panel), panel_style);
  189. set_position(EDITOR_DEF("_touch_actions_panel_position", Point2(480, 480))); // Dropped it here for no good reason — users can move it anyway.
  190. box = memnew(BoxContainer);
  191. box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  192. box->add_theme_constant_override("separation", 20);
  193. box->set_vertical(EDITOR_DEF("_touch_actions_panel_vertical_layout", false));
  194. add_child(box);
  195. drag_handle = memnew(TextureRect);
  196. drag_handle->set_custom_minimum_size(Size2(40, 40));
  197. drag_handle->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  198. drag_handle->connect(SceneStringName(gui_input), callable_mp(this, &TouchActionsPanel::_on_drag_handle_gui_input));
  199. box->add_child(drag_handle);
  200. layout_toggle_button = memnew(Button);
  201. layout_toggle_button->set_theme_type_variation("FlatMenuButton");
  202. layout_toggle_button->set_accessibility_name(TTRC("Switch Layout"));
  203. layout_toggle_button->set_focus_mode(FOCUS_NONE);
  204. layout_toggle_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  205. layout_toggle_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_layout));
  206. box->add_child(layout_toggle_button);
  207. lock_panel_button = memnew(Button);
  208. lock_panel_button->set_toggle_mode(true);
  209. lock_panel_button->set_theme_type_variation("FlatMenuButton");
  210. lock_panel_button->set_accessibility_name(TTRC("Lock Panel"));
  211. lock_panel_button->set_focus_mode(FOCUS_NONE);
  212. lock_panel_button->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  213. lock_panel_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_lock_panel_toggled));
  214. box->add_child(lock_panel_button);
  215. ColorRect *separator = memnew(ColorRect);
  216. separator->set_color(Color(0.5, 0.5, 0.5));
  217. separator->set_custom_minimum_size(Size2(2, 2));
  218. box->add_child(separator);
  219. // Add action buttons.
  220. save_button = _add_new_action_button("editor/save_scene", TTRC("Save"));
  221. delete_button = _add_new_action_button("", TTRC("Delete"), Key::KEY_DELETE);
  222. undo_button = _add_new_action_button("ui_undo", TTRC("Undo"));
  223. redo_button = _add_new_action_button("ui_redo", TTRC("Redo"));
  224. cut_button = _add_new_action_button("ui_cut", TTRC("Cut"));
  225. copy_button = _add_new_action_button("ui_copy", TTRC("Copy"));
  226. paste_button = _add_new_action_button("ui_paste", TTRC("Paste"));
  227. _add_new_modifier_button(MODIFIER_CTRL);
  228. _add_new_modifier_button(MODIFIER_SHIFT);
  229. _add_new_modifier_button(MODIFIER_ALT);
  230. }