touch_actions_panel.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_THEME_CHANGED: {
  46. drag_handle->set_texture(get_editor_theme_icon(SNAME("DragHandle")));
  47. layout_toggle_button->set_button_icon(get_editor_theme_icon(SNAME("Orientation")));
  48. lock_panel_button->set_button_icon(get_editor_theme_icon(SNAME("Lock")));
  49. save_button->set_button_icon(get_editor_theme_icon(SNAME("Save")));
  50. delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  51. undo_button->set_button_icon(get_editor_theme_icon(SNAME("UndoRedo")));
  52. redo_button->set_button_icon(get_editor_theme_icon(SNAME("Redo")));
  53. } break;
  54. }
  55. }
  56. void TouchActionsPanel::_hardware_keyboard_connected(bool p_connected) {
  57. set_visible(!p_connected);
  58. }
  59. void TouchActionsPanel::_simulate_editor_shortcut(const String &p_shortcut_name) {
  60. Ref<Shortcut> shortcut = ED_GET_SHORTCUT(p_shortcut_name);
  61. if (shortcut.is_valid() && !shortcut->get_events().is_empty()) {
  62. Ref<InputEventKey> event = shortcut->get_events()[0];
  63. if (event.is_valid()) {
  64. event->set_pressed(true);
  65. Input::get_singleton()->parse_input_event(event);
  66. }
  67. }
  68. }
  69. void TouchActionsPanel::_simulate_key_press(Key p_keycode) {
  70. Ref<InputEventKey> event;
  71. event.instantiate();
  72. event->set_keycode(p_keycode);
  73. event->set_pressed(true);
  74. Input::get_singleton()->parse_input_event(event);
  75. }
  76. Button *TouchActionsPanel::_add_new_action_button(const String &p_shortcut, Key p_keycode) {
  77. Button *action_button = memnew(Button);
  78. action_button->set_focus_mode(Control::FOCUS_NONE);
  79. action_button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  80. action_button->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  81. if (p_keycode == Key::NONE) {
  82. action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_editor_shortcut).bind(p_shortcut));
  83. } else {
  84. action_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_simulate_key_press).bind(p_keycode));
  85. }
  86. box->add_child(action_button);
  87. return action_button;
  88. }
  89. void TouchActionsPanel::_on_drag_handle_gui_input(const Ref<InputEvent> &p_event) {
  90. if (lock_panel_position) {
  91. return;
  92. }
  93. Ref<InputEventMouseButton> mouse_button_event = p_event;
  94. if (mouse_button_event.is_valid() && mouse_button_event->get_button_index() == MouseButton::LEFT) {
  95. if (mouse_button_event->is_pressed()) {
  96. dragging = true;
  97. drag_offset = mouse_button_event->get_position();
  98. } else {
  99. dragging = false;
  100. }
  101. }
  102. Ref<InputEventMouseMotion> mouse_motion_event = p_event;
  103. if (dragging && mouse_motion_event.is_valid()) {
  104. Vector2 new_position = get_position() + mouse_motion_event->get_relative();
  105. const float margin = 25.0;
  106. Vector2 parent_size = get_parent_area_size();
  107. Vector2 panel_size = get_size();
  108. new_position = new_position.clamp(Vector2(margin, margin), parent_size - panel_size - Vector2(margin, margin));
  109. set_position(new_position);
  110. }
  111. }
  112. void TouchActionsPanel::_switch_layout() {
  113. box->set_vertical(!box->is_vertical());
  114. reset_size();
  115. }
  116. void TouchActionsPanel::_lock_panel_toggled(bool p_pressed) {
  117. lock_panel_position = p_pressed;
  118. layout_toggle_button->set_disabled(p_pressed);
  119. }
  120. TouchActionsPanel::TouchActionsPanel() {
  121. Ref<StyleBoxFlat> panel_style;
  122. panel_style.instantiate();
  123. panel_style->set_bg_color(Color(0.1, 0.1, 0.1, 1));
  124. panel_style->set_border_color(Color(0.3, 0.3, 0.3, 1));
  125. panel_style->set_border_width_all(3);
  126. panel_style->set_corner_radius_all(10);
  127. panel_style->set_content_margin_all(12);
  128. add_theme_style_override(SceneStringName(panel), panel_style);
  129. set_anchors_and_offsets_preset(Control::PRESET_CENTER_BOTTOM, Control::PRESET_MODE_MINSIZE, 80);
  130. box = memnew(BoxContainer);
  131. box->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  132. box->add_theme_constant_override("separation", 15);
  133. add_child(box);
  134. drag_handle = memnew(TextureRect);
  135. drag_handle->set_custom_minimum_size(Size2(40, 40));
  136. drag_handle->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  137. drag_handle->connect(SceneStringName(gui_input), callable_mp(this, &TouchActionsPanel::_on_drag_handle_gui_input));
  138. box->add_child(drag_handle);
  139. layout_toggle_button = memnew(Button);
  140. layout_toggle_button->set_focus_mode(Control::FOCUS_NONE);
  141. layout_toggle_button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  142. layout_toggle_button->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  143. layout_toggle_button->connect(SceneStringName(pressed), callable_mp(this, &TouchActionsPanel::_switch_layout));
  144. box->add_child(layout_toggle_button);
  145. lock_panel_button = memnew(Button);
  146. lock_panel_button->set_toggle_mode(true);
  147. lock_panel_button->set_focus_mode(Control::FOCUS_NONE);
  148. lock_panel_button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  149. lock_panel_button->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  150. lock_panel_button->connect(SceneStringName(toggled), callable_mp(this, &TouchActionsPanel::_lock_panel_toggled));
  151. box->add_child(lock_panel_button);
  152. ColorRect *separator = memnew(ColorRect);
  153. separator->set_color(Color(0.5, 0.5, 0.5));
  154. separator->set_custom_minimum_size(Size2(2, 2));
  155. box->add_child(separator);
  156. // Add action buttons.
  157. save_button = _add_new_action_button("editor/save_scene");
  158. delete_button = _add_new_action_button("", Key::KEY_DELETE);
  159. undo_button = _add_new_action_button("ui_undo");
  160. redo_button = _add_new_action_button("ui_redo");
  161. }