window_wrapper.cpp 16 KB

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