window_wrapper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**************************************************************************/
  2. /* window_wrapper.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 "window_wrapper.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/progress_dialog.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/panel.h"
  39. #include "scene/gui/popup.h"
  40. #include "scene/main/window.h"
  41. // WindowWrapper
  42. // Capture all shortcut events not handled by other nodes.
  43. class ShortcutBin : public Node {
  44. GDCLASS(ShortcutBin, Node);
  45. virtual void _notification(int what) {
  46. switch (what) {
  47. case NOTIFICATION_READY:
  48. set_process_shortcut_input(true);
  49. break;
  50. }
  51. }
  52. virtual void shortcut_input(const Ref<InputEvent> &p_event) override {
  53. if (!get_window()->is_visible()) {
  54. return;
  55. }
  56. Window *grandparent_window = get_window()->get_parent_visible_window();
  57. ERR_FAIL_NULL(grandparent_window);
  58. if (Object::cast_to<InputEventKey>(p_event.ptr()) || Object::cast_to<InputEventShortcut>(p_event.ptr())) {
  59. // HACK: Propagate the window input to the editor main window to handle global shortcuts.
  60. grandparent_window->push_input(p_event);
  61. if (grandparent_window->is_input_handled()) {
  62. get_viewport()->set_input_as_handled();
  63. }
  64. }
  65. }
  66. };
  67. Rect2 WindowWrapper::_get_default_window_rect() const {
  68. // Assume that the control rect is the desired one for the window.
  69. return wrapped_control->get_screen_rect();
  70. }
  71. Node *WindowWrapper::_get_wrapped_control_parent() const {
  72. if (margins) {
  73. return margins;
  74. }
  75. return window;
  76. }
  77. void WindowWrapper::_set_window_enabled_with_rect(bool p_visible, const Rect2 p_rect) {
  78. ERR_FAIL_NULL(wrapped_control);
  79. if (!is_window_available()) {
  80. return;
  81. }
  82. if (window->is_visible() == p_visible) {
  83. if (p_visible) {
  84. window->grab_focus();
  85. }
  86. return;
  87. }
  88. Node *parent = _get_wrapped_control_parent();
  89. if (wrapped_control->get_parent() != parent) {
  90. // Move the control to the window.
  91. wrapped_control->reparent(parent, false);
  92. _set_window_rect(p_rect);
  93. wrapped_control->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  94. } else if (!p_visible) {
  95. // Remove control from window.
  96. wrapped_control->reparent(this, false);
  97. }
  98. window->set_visible(p_visible);
  99. if (!p_visible) {
  100. emit_signal("window_close_requested");
  101. }
  102. emit_signal("window_visibility_changed", p_visible);
  103. }
  104. void WindowWrapper::_set_window_rect(const Rect2 p_rect) {
  105. // Set the window rect even when the window is maximized to have a good default size
  106. // when the user remove the maximized mode.
  107. window->set_position(p_rect.position);
  108. window->set_size(p_rect.size);
  109. if (EDITOR_GET("interface/multi_window/maximize_window")) {
  110. window->set_mode(Window::MODE_MAXIMIZED);
  111. }
  112. }
  113. void WindowWrapper::_bind_methods() {
  114. ADD_SIGNAL(MethodInfo("window_visibility_changed", PropertyInfo(Variant::BOOL, "visible")));
  115. ADD_SIGNAL(MethodInfo("window_close_requested"));
  116. }
  117. void WindowWrapper::_notification(int p_what) {
  118. if (!is_window_available()) {
  119. return;
  120. }
  121. switch (p_what) {
  122. case NOTIFICATION_VISIBILITY_CHANGED: {
  123. if (get_window_enabled() && is_visible()) {
  124. // Grab the focus when WindowWrapper.set_visible(true) is called
  125. // and the window is showing.
  126. window->grab_focus();
  127. }
  128. } break;
  129. case NOTIFICATION_READY: {
  130. set_process_shortcut_input(true);
  131. } break;
  132. case NOTIFICATION_THEME_CHANGED: {
  133. window_background->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("PanelForeground", EditorStringName(EditorStyles)));
  134. } break;
  135. }
  136. }
  137. void WindowWrapper::shortcut_input(const Ref<InputEvent> &p_event) {
  138. if (enable_shortcut.is_valid() && enable_shortcut->matches_event(p_event)) {
  139. set_window_enabled(true);
  140. }
  141. }
  142. void WindowWrapper::set_wrapped_control(Control *p_control, const Ref<Shortcut> &p_enable_shortcut) {
  143. ERR_FAIL_NULL(p_control);
  144. ERR_FAIL_COND(wrapped_control);
  145. wrapped_control = p_control;
  146. enable_shortcut = p_enable_shortcut;
  147. add_child(p_control);
  148. }
  149. Control *WindowWrapper::get_wrapped_control() const {
  150. return wrapped_control;
  151. }
  152. Control *WindowWrapper::release_wrapped_control() {
  153. set_window_enabled(false);
  154. if (wrapped_control) {
  155. Control *old_wrapped = wrapped_control;
  156. wrapped_control->get_parent()->remove_child(wrapped_control);
  157. wrapped_control = nullptr;
  158. return old_wrapped;
  159. }
  160. return nullptr;
  161. }
  162. bool WindowWrapper::is_window_available() const {
  163. return window != nullptr;
  164. }
  165. bool WindowWrapper::get_window_enabled() const {
  166. return is_window_available() ? window->is_visible() : false;
  167. }
  168. void WindowWrapper::set_window_enabled(bool p_enabled) {
  169. _set_window_enabled_with_rect(p_enabled, _get_default_window_rect());
  170. }
  171. Rect2i WindowWrapper::get_window_rect() const {
  172. ERR_FAIL_COND_V(!get_window_enabled(), Rect2i());
  173. return Rect2i(window->get_position(), window->get_size());
  174. }
  175. int WindowWrapper::get_window_screen() const {
  176. ERR_FAIL_COND_V(!get_window_enabled(), -1);
  177. return window->get_current_screen();
  178. }
  179. void WindowWrapper::restore_window(const Rect2i &p_rect, int p_screen) {
  180. ERR_FAIL_COND(!is_window_available());
  181. ERR_FAIL_INDEX(p_screen, DisplayServer::get_singleton()->get_screen_count());
  182. _set_window_enabled_with_rect(true, p_rect);
  183. window->set_current_screen(p_screen);
  184. }
  185. void WindowWrapper::restore_window_from_saved_position(const Rect2 p_window_rect, int p_screen, const Rect2 p_screen_rect) {
  186. ERR_FAIL_COND(!is_window_available());
  187. Rect2 window_rect = p_window_rect;
  188. int screen = p_screen;
  189. Rect2 restored_screen_rect = p_screen_rect;
  190. if (screen < 0 || screen >= DisplayServer::get_singleton()->get_screen_count()) {
  191. // Fallback to the main window screen if the saved screen is not available.
  192. screen = get_window()->get_window_id();
  193. }
  194. Rect2i real_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  195. if (restored_screen_rect == Rect2i()) {
  196. // Fallback to the target screen rect.
  197. restored_screen_rect = real_screen_rect;
  198. }
  199. if (window_rect == Rect2i()) {
  200. // Fallback to a standard rect.
  201. window_rect = Rect2i(restored_screen_rect.position + restored_screen_rect.size / 4, restored_screen_rect.size / 2);
  202. }
  203. // Adjust the window rect size in case the resolution changes.
  204. Vector2 screen_ratio = Vector2(real_screen_rect.size) / Vector2(restored_screen_rect.size);
  205. // The screen positioning may change, so remove the original screen position.
  206. window_rect.position -= restored_screen_rect.position;
  207. window_rect = Rect2i(window_rect.position * screen_ratio, window_rect.size * screen_ratio);
  208. window_rect.position += real_screen_rect.position;
  209. // All good, restore the window.
  210. window->set_current_screen(p_screen);
  211. if (window->is_visible()) {
  212. _set_window_rect(window_rect);
  213. } else {
  214. _set_window_enabled_with_rect(true, window_rect);
  215. }
  216. }
  217. void WindowWrapper::enable_window_on_screen(int p_screen, bool p_auto_scale) {
  218. int current_screen = Object::cast_to<Window>(get_viewport())->get_current_screen();
  219. int screen = p_screen < 0 ? current_screen : p_screen;
  220. bool auto_scale = p_auto_scale && !EDITOR_GET("interface/multi_window/maximize_window");
  221. if (auto_scale && current_screen != screen) {
  222. Rect2 control_rect = _get_default_window_rect();
  223. Rect2i source_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(current_screen);
  224. Rect2i dest_screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  225. // Adjust the window rect size in case the resolution changes.
  226. Vector2 screen_ratio = Vector2(source_screen_rect.size) / Vector2(dest_screen_rect.size);
  227. // The screen positioning may change, so remove the original screen position.
  228. control_rect.position -= source_screen_rect.position;
  229. control_rect = Rect2i(control_rect.position * screen_ratio, control_rect.size * screen_ratio);
  230. control_rect.position += dest_screen_rect.position;
  231. restore_window(control_rect, p_screen);
  232. } else {
  233. window->set_current_screen(p_screen);
  234. set_window_enabled(true);
  235. }
  236. }
  237. void WindowWrapper::set_window_title(const String &p_title) {
  238. if (!is_window_available()) {
  239. return;
  240. }
  241. window->set_title(p_title);
  242. }
  243. void WindowWrapper::set_margins_enabled(bool p_enabled) {
  244. if (!is_window_available()) {
  245. return;
  246. }
  247. if (!p_enabled && margins) {
  248. margins->queue_free();
  249. margins = nullptr;
  250. } else if (p_enabled && !margins) {
  251. Size2 borders = Size2(4, 4) * EDSCALE;
  252. margins = memnew(MarginContainer);
  253. margins->add_theme_constant_override("margin_right", borders.width);
  254. margins->add_theme_constant_override("margin_top", borders.height);
  255. margins->add_theme_constant_override("margin_left", borders.width);
  256. margins->add_theme_constant_override("margin_bottom", borders.height);
  257. window->add_child(margins);
  258. margins->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  259. }
  260. }
  261. WindowWrapper::WindowWrapper() {
  262. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  263. return;
  264. }
  265. window = memnew(Window);
  266. window->set_wrap_controls(true);
  267. add_child(window);
  268. window->hide();
  269. window->connect("close_requested", callable_mp(this, &WindowWrapper::set_window_enabled).bind(false));
  270. ShortcutBin *capturer = memnew(ShortcutBin);
  271. window->add_child(capturer);
  272. window_background = memnew(Panel);
  273. window_background->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  274. window->add_child(window_background);
  275. ProgressDialog::get_singleton()->add_host_window(window);
  276. }
  277. // ScreenSelect
  278. void ScreenSelect::_build_advanced_menu() {
  279. // Clear old screen list.
  280. while (screen_list->get_child_count(false) > 0) {
  281. Node *child = screen_list->get_child(0);
  282. screen_list->remove_child(child);
  283. child->queue_free();
  284. }
  285. // Populate screen list.
  286. const real_t height = real_t(get_theme_font_size(SceneStringName(font_size))) * 1.5;
  287. int current_screen = get_window()->get_current_screen();
  288. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  289. Button *button = memnew(Button);
  290. Size2 screen_size = Size2(DisplayServer::get_singleton()->screen_get_size(i));
  291. Size2 button_size = Size2(height * (screen_size.x / screen_size.y), height);
  292. button->set_custom_minimum_size(button_size);
  293. screen_list->add_child(button);
  294. button->set_text(itos(i));
  295. button->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  296. button->set_tooltip_text(vformat(TTR("Make this panel floating in the screen %d."), i));
  297. if (i == current_screen) {
  298. Color accent_color = get_theme_color("accent_color", EditorStringName(Editor));
  299. button->add_theme_color_override(SceneStringName(font_color), accent_color);
  300. }
  301. button->connect(SceneStringName(pressed), callable_mp(this, &ScreenSelect::_emit_screen_signal).bind(i));
  302. button->connect(SceneStringName(pressed), callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  303. button->connect(SceneStringName(pressed), callable_mp(static_cast<Window *>(popup), &Popup::hide));
  304. }
  305. }
  306. void ScreenSelect::_emit_screen_signal(int p_screen_idx) {
  307. if (!is_disabled()) {
  308. emit_signal("request_open_in_screen", p_screen_idx);
  309. }
  310. }
  311. void ScreenSelect::_bind_methods() {
  312. ADD_SIGNAL(MethodInfo("request_open_in_screen", PropertyInfo(Variant::INT, "screen")));
  313. }
  314. void ScreenSelect::_notification(int p_what) {
  315. switch (p_what) {
  316. case NOTIFICATION_READY: {
  317. connect(SceneStringName(gui_input), callable_mp(this, &ScreenSelect::_handle_mouse_shortcut));
  318. } break;
  319. case NOTIFICATION_THEME_CHANGED: {
  320. set_button_icon(get_editor_theme_icon("MakeFloating"));
  321. const real_t popup_height = real_t(get_theme_font_size(SceneStringName(font_size))) * 2.0;
  322. popup->set_min_size(Size2(0, popup_height * 3));
  323. } break;
  324. }
  325. }
  326. void ScreenSelect::_handle_mouse_shortcut(const Ref<InputEvent> &p_event) {
  327. const Ref<InputEventMouseButton> mouse_button = p_event;
  328. if (mouse_button.is_valid()) {
  329. if (mouse_button->is_pressed() && mouse_button->get_button_index() == MouseButton::LEFT) {
  330. _emit_screen_signal(get_window()->get_current_screen());
  331. accept_event();
  332. }
  333. }
  334. }
  335. void ScreenSelect::_show_popup() {
  336. // Adapted from /scene/gui/menu_button.cpp::show_popup
  337. if (!get_viewport()) {
  338. return;
  339. }
  340. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  341. popup->set_size(Size2(size.width, 0));
  342. Point2 gp = get_screen_position();
  343. gp.y += size.y;
  344. if (is_layout_rtl()) {
  345. gp.x += size.width - popup->get_size().width;
  346. }
  347. popup->set_position(gp);
  348. popup->popup();
  349. }
  350. void ScreenSelect::pressed() {
  351. if (popup->is_visible()) {
  352. popup->hide();
  353. return;
  354. }
  355. _build_advanced_menu();
  356. _show_popup();
  357. }
  358. ScreenSelect::ScreenSelect() {
  359. set_button_mask(MouseButtonMask::RIGHT);
  360. set_flat(true);
  361. set_toggle_mode(true);
  362. set_focus_mode(FOCUS_NONE);
  363. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  364. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  365. set_disabled(true);
  366. set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  367. } else {
  368. set_tooltip_text(TTR("Make this panel floating.\nRight-click to open the screen selector."));
  369. }
  370. // Create the popup.
  371. const Size2 borders = Size2(4, 4) * EDSCALE;
  372. popup = memnew(PopupPanel);
  373. popup->connect("popup_hide", callable_mp(static_cast<BaseButton *>(this), &ScreenSelect::set_pressed).bind(false));
  374. add_child(popup);
  375. MarginContainer *popup_root = memnew(MarginContainer);
  376. popup_root->add_theme_constant_override("margin_right", borders.width);
  377. popup_root->add_theme_constant_override("margin_top", borders.height);
  378. popup_root->add_theme_constant_override("margin_left", borders.width);
  379. popup_root->add_theme_constant_override("margin_bottom", borders.height);
  380. popup->add_child(popup_root);
  381. VBoxContainer *vb = memnew(VBoxContainer);
  382. vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  383. popup_root->add_child(vb);
  384. Label *description = memnew(Label(TTR("Select Screen")));
  385. description->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  386. vb->add_child(description);
  387. screen_list = memnew(HBoxContainer);
  388. screen_list->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  389. vb->add_child(screen_list);
  390. popup_root->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  391. }