editor_title_bar.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**************************************************************************/
  2. /* editor_title_bar.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 "editor_title_bar.h"
  31. void EditorTitleBar::gui_input(const Ref<InputEvent> &p_event) {
  32. if (!can_move) {
  33. return;
  34. }
  35. Ref<InputEventMouseMotion> mm = p_event;
  36. if (mm.is_valid() && moving) {
  37. if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
  38. Window *w = Object::cast_to<Window>(get_viewport());
  39. if (w) {
  40. Point2 mouse = DisplayServer::get_singleton()->mouse_get_position();
  41. w->set_position(mouse - click_pos);
  42. }
  43. } else {
  44. moving = false;
  45. }
  46. }
  47. Ref<InputEventMouseButton> mb = p_event;
  48. if (mb.is_valid() && has_point(mb->get_position())) {
  49. Window *w = Object::cast_to<Window>(get_viewport());
  50. if (w) {
  51. if (mb->get_button_index() == MouseButton::LEFT) {
  52. if (mb->is_pressed()) {
  53. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_DRAG)) {
  54. DisplayServer::get_singleton()->window_start_drag(w->get_window_id());
  55. } else {
  56. click_pos = DisplayServer::get_singleton()->mouse_get_position() - w->get_position();
  57. moving = true;
  58. }
  59. } else {
  60. moving = false;
  61. }
  62. }
  63. if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click() && mb->is_pressed()) {
  64. if (DisplayServer::get_singleton()->window_maximize_on_title_dbl_click()) {
  65. if (w->get_mode() == Window::MODE_WINDOWED) {
  66. w->set_mode(Window::MODE_MAXIMIZED);
  67. } else if (w->get_mode() == Window::MODE_MAXIMIZED) {
  68. w->set_mode(Window::MODE_WINDOWED);
  69. }
  70. } else if (DisplayServer::get_singleton()->window_minimize_on_title_dbl_click()) {
  71. w->set_mode(Window::MODE_MINIMIZED);
  72. }
  73. moving = false;
  74. }
  75. }
  76. }
  77. }
  78. void EditorTitleBar::set_center_control(Control *p_center_control) {
  79. center_control = p_center_control;
  80. }
  81. Control *EditorTitleBar::get_center_control() const {
  82. return center_control;
  83. }
  84. void EditorTitleBar::_notification(int p_what) {
  85. if (!center_control || p_what != NOTIFICATION_SORT_CHILDREN) {
  86. return;
  87. }
  88. Control *prev = nullptr;
  89. Control *base = nullptr;
  90. Control *next = nullptr;
  91. bool rtl = is_layout_rtl();
  92. int start;
  93. int end;
  94. int delta;
  95. if (rtl) {
  96. start = get_child_count() - 1;
  97. end = -1;
  98. delta = -1;
  99. } else {
  100. start = 0;
  101. end = get_child_count();
  102. delta = +1;
  103. }
  104. for (int i = start; i != end; i += delta) {
  105. Control *c = as_sortable_control(get_child(i));
  106. if (!c) {
  107. continue;
  108. }
  109. if (base) {
  110. next = c;
  111. break;
  112. }
  113. if (c != center_control) {
  114. prev = c;
  115. continue;
  116. }
  117. base = c;
  118. }
  119. if (base && prev && next) {
  120. Size2i title_size = get_size();
  121. Size2i c_size = base->get_combined_minimum_size();
  122. int min_offset = prev->get_position().x + prev->get_combined_minimum_size().x;
  123. int max_offset = next->get_position().x + next->get_size().x - next->get_combined_minimum_size().x - c_size.x;
  124. int offset = (title_size.width - c_size.width) / 2;
  125. offset = CLAMP(offset, min_offset, max_offset);
  126. fit_child_in_rect(prev, Rect2i(prev->get_position().x, 0, offset - prev->get_position().x, title_size.height));
  127. fit_child_in_rect(base, Rect2i(offset, 0, c_size.width, title_size.height));
  128. fit_child_in_rect(next, Rect2i(offset + c_size.width, 0, next->get_position().x + next->get_size().x - (offset + c_size.width), title_size.height));
  129. }
  130. }
  131. void EditorTitleBar::set_can_move_window(bool p_enabled) {
  132. can_move = p_enabled;
  133. set_process_input(can_move);
  134. }
  135. bool EditorTitleBar::get_can_move_window() const {
  136. return can_move;
  137. }