window.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*************************************************************************/
  2. /* window.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "core/os/keyboard.h"
  33. #include "scene/gui/control.h"
  34. #include "scene/resources/dynamic_font.h"
  35. #include "scene/scene_string_names.h"
  36. void Window::set_title(const String &p_title) {
  37. title = p_title;
  38. if (embedder) {
  39. embedder->_sub_window_update(this);
  40. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  41. DisplayServer::get_singleton()->window_set_title(p_title, window_id);
  42. }
  43. }
  44. String Window::get_title() const {
  45. return title;
  46. }
  47. void Window::set_current_screen(int p_screen) {
  48. current_screen = p_screen;
  49. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  50. return;
  51. }
  52. DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
  53. }
  54. int Window::get_current_screen() const {
  55. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  56. current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
  57. }
  58. return current_screen;
  59. }
  60. void Window::set_position(const Point2i &p_position) {
  61. position = p_position;
  62. if (embedder) {
  63. embedder->_sub_window_update(this);
  64. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  65. DisplayServer::get_singleton()->window_set_position(p_position, window_id);
  66. }
  67. }
  68. Point2i Window::get_position() const {
  69. return position;
  70. }
  71. void Window::set_size(const Size2i &p_size) {
  72. size = p_size;
  73. _update_window_size();
  74. }
  75. Size2i Window::get_size() const {
  76. return size;
  77. }
  78. Size2i Window::get_real_size() const {
  79. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  80. return DisplayServer::get_singleton()->window_get_real_size(window_id);
  81. }
  82. return size;
  83. }
  84. void Window::set_max_size(const Size2i &p_max_size) {
  85. max_size = p_max_size;
  86. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  87. DisplayServer::get_singleton()->window_set_max_size(max_size, window_id);
  88. }
  89. _update_window_size();
  90. }
  91. Size2i Window::get_max_size() const {
  92. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  93. max_size = DisplayServer::get_singleton()->window_get_max_size(window_id);
  94. }
  95. return max_size;
  96. }
  97. void Window::set_min_size(const Size2i &p_min_size) {
  98. min_size = p_min_size;
  99. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  100. DisplayServer::get_singleton()->window_set_min_size(min_size, window_id);
  101. }
  102. _update_window_size();
  103. }
  104. Size2i Window::get_min_size() const {
  105. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  106. min_size = DisplayServer::get_singleton()->window_get_min_size(window_id);
  107. }
  108. return min_size;
  109. }
  110. void Window::set_mode(Mode p_mode) {
  111. mode = p_mode;
  112. if (embedder) {
  113. embedder->_sub_window_update(this);
  114. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  115. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
  116. }
  117. }
  118. Window::Mode Window::get_mode() const {
  119. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  120. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  121. }
  122. return mode;
  123. }
  124. void Window::set_flag(Flags p_flag, bool p_enabled) {
  125. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  126. flags[p_flag] = p_enabled;
  127. if (embedder) {
  128. embedder->_sub_window_update(this);
  129. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  130. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  131. }
  132. }
  133. bool Window::get_flag(Flags p_flag) const {
  134. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  135. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  136. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  137. }
  138. return flags[p_flag];
  139. }
  140. bool Window::is_maximize_allowed() const {
  141. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  142. return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
  143. }
  144. return true;
  145. }
  146. void Window::request_attention() {
  147. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  148. DisplayServer::get_singleton()->window_request_attention(window_id);
  149. }
  150. }
  151. void Window::move_to_foreground() {
  152. if (embedder) {
  153. embedder->_sub_window_grab_focus(this);
  154. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  155. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  156. }
  157. }
  158. bool Window::can_draw() const {
  159. if (!is_inside_tree()) {
  160. return false;
  161. }
  162. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  163. return DisplayServer::get_singleton()->window_can_draw(window_id);
  164. }
  165. return visible;
  166. }
  167. void Window::set_ime_active(bool p_active) {
  168. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  169. DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
  170. }
  171. }
  172. void Window::set_ime_position(const Point2i &p_pos) {
  173. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  174. DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
  175. }
  176. }
  177. bool Window::is_embedded() const {
  178. ERR_FAIL_COND_V(!is_inside_tree(), false);
  179. return _get_embedder() != nullptr;
  180. }
  181. void Window::_make_window() {
  182. ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
  183. uint32_t f = 0;
  184. for (int i = 0; i < FLAG_MAX; i++) {
  185. if (flags[i]) {
  186. f |= (1 << i);
  187. }
  188. }
  189. window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), f, Rect2i(position, size));
  190. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  191. DisplayServer::get_singleton()->window_set_current_screen(current_screen, window_id);
  192. DisplayServer::get_singleton()->window_set_max_size(max_size, window_id);
  193. DisplayServer::get_singleton()->window_set_min_size(min_size, window_id);
  194. DisplayServer::get_singleton()->window_set_title(title, window_id);
  195. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  196. _update_window_size();
  197. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  198. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  199. }
  200. for (Set<Window *>::Element *E = transient_children.front(); E; E = E->next()) {
  201. if (E->get()->window_id != DisplayServer::INVALID_WINDOW_ID) {
  202. DisplayServer::get_singleton()->window_set_transient(E->get()->window_id, transient_parent->window_id);
  203. }
  204. }
  205. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  206. }
  207. void Window::_update_from_window() {
  208. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  209. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  210. for (int i = 0; i < FLAG_MAX; i++) {
  211. flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
  212. }
  213. }
  214. void Window::_clear_window() {
  215. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  216. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  217. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  218. }
  219. for (Set<Window *>::Element *E = transient_children.front(); E; E = E->next()) {
  220. if (E->get()->window_id != DisplayServer::INVALID_WINDOW_ID) {
  221. DisplayServer::get_singleton()->window_set_transient(E->get()->window_id, DisplayServer::INVALID_WINDOW_ID);
  222. }
  223. }
  224. _update_from_window();
  225. DisplayServer::get_singleton()->delete_sub_window(window_id);
  226. window_id = DisplayServer::INVALID_WINDOW_ID;
  227. _update_viewport_size();
  228. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  229. }
  230. void Window::_rect_changed_callback(const Rect2i &p_callback) {
  231. //we must always accept this as the truth
  232. if (size == p_callback.size && position == p_callback.position) {
  233. return;
  234. }
  235. position = p_callback.position;
  236. if (size != p_callback.size) {
  237. size = p_callback.size;
  238. _update_viewport_size();
  239. }
  240. }
  241. void Window::_propagate_window_notification(Node *p_node, int p_notification) {
  242. p_node->notification(p_notification);
  243. for (int i = 0; i < p_node->get_child_count(); i++) {
  244. Node *child = p_node->get_child(i);
  245. Window *window = Object::cast_to<Window>(child);
  246. if (window) {
  247. break;
  248. }
  249. _propagate_window_notification(child, p_notification);
  250. }
  251. }
  252. void Window::_event_callback(DisplayServer::WindowEvent p_event) {
  253. switch (p_event) {
  254. case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
  255. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  256. emit_signal("mouse_entered");
  257. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
  258. } break;
  259. case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
  260. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  261. emit_signal("mouse_exited");
  262. } break;
  263. case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
  264. focused = true;
  265. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
  266. emit_signal("focus_entered");
  267. } break;
  268. case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
  269. focused = false;
  270. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  271. emit_signal("focus_exited");
  272. } break;
  273. case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
  274. if (exclusive_child != nullptr) {
  275. break; //has an exclusive child, can't get events until child is closed
  276. }
  277. _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
  278. emit_signal("close_requested");
  279. } break;
  280. case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
  281. _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
  282. emit_signal("go_back_requested");
  283. } break;
  284. case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
  285. _update_viewport_size();
  286. _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
  287. emit_signal("dpi_changed");
  288. } break;
  289. }
  290. }
  291. void Window::show() {
  292. set_visible(true);
  293. }
  294. void Window::hide() {
  295. set_visible(false);
  296. }
  297. void Window::set_visible(bool p_visible) {
  298. if (visible == p_visible) {
  299. return;
  300. }
  301. visible = p_visible;
  302. if (!is_inside_tree()) {
  303. return;
  304. }
  305. if (updating_child_controls) {
  306. _update_child_controls();
  307. }
  308. ERR_FAIL_COND_MSG(get_parent() == nullptr, "Can't change visibility of main window.");
  309. Viewport *embedder_vp = _get_embedder();
  310. if (!embedder_vp) {
  311. if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
  312. _clear_window();
  313. }
  314. if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
  315. _make_window();
  316. _update_window_callbacks();
  317. }
  318. } else {
  319. if (visible) {
  320. embedder = embedder_vp;
  321. embedder->_sub_window_register(this);
  322. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  323. } else {
  324. embedder->_sub_window_remove(this);
  325. embedder = nullptr;
  326. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  327. }
  328. _update_window_size();
  329. }
  330. if (!visible) {
  331. focused = false;
  332. }
  333. notification(NOTIFICATION_VISIBILITY_CHANGED);
  334. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  335. RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
  336. //update transient exclusive
  337. if (transient_parent) {
  338. if (exclusive && visible) {
  339. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  340. transient_parent->exclusive_child = this;
  341. } else {
  342. if (transient_parent->exclusive_child == this) {
  343. transient_parent->exclusive_child = nullptr;
  344. }
  345. }
  346. }
  347. }
  348. void Window::_clear_transient() {
  349. if (transient_parent) {
  350. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  351. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  352. }
  353. transient_parent->transient_children.erase(this);
  354. if (transient_parent->exclusive_child == this) {
  355. transient_parent->exclusive_child = nullptr;
  356. }
  357. transient_parent = nullptr;
  358. }
  359. }
  360. void Window::_make_transient() {
  361. if (!get_parent()) {
  362. //main window, can't be transient
  363. return;
  364. }
  365. //find transient parent
  366. Viewport *vp = get_parent()->get_viewport();
  367. Window *window = nullptr;
  368. while (vp) {
  369. window = Object::cast_to<Window>(vp);
  370. if (window) {
  371. break;
  372. }
  373. if (!vp->get_parent()) {
  374. break;
  375. }
  376. vp = vp->get_parent()->get_viewport();
  377. }
  378. if (window) {
  379. transient_parent = window;
  380. window->transient_children.insert(this);
  381. if (is_inside_tree() && is_visible() && exclusive) {
  382. if (transient_parent->exclusive_child == nullptr) {
  383. transient_parent->exclusive_child = this;
  384. } else if (transient_parent->exclusive_child != this) {
  385. ERR_PRINT("Making child transient exclusive, but parent has another exclusive child");
  386. }
  387. }
  388. }
  389. //see if we can make transient
  390. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  391. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  392. }
  393. }
  394. void Window::set_transient(bool p_transient) {
  395. if (transient == p_transient) {
  396. return;
  397. }
  398. transient = p_transient;
  399. if (!is_inside_tree()) {
  400. return;
  401. }
  402. if (transient) {
  403. _make_transient();
  404. } else {
  405. _clear_transient();
  406. }
  407. }
  408. bool Window::is_transient() const {
  409. return transient;
  410. }
  411. void Window::set_exclusive(bool p_exclusive) {
  412. if (exclusive == p_exclusive) {
  413. return;
  414. }
  415. exclusive = p_exclusive;
  416. if (transient_parent) {
  417. if (p_exclusive && is_inside_tree() && is_visible()) {
  418. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  419. transient_parent->exclusive_child = this;
  420. } else {
  421. if (transient_parent->exclusive_child == this) {
  422. transient_parent->exclusive_child = nullptr;
  423. }
  424. }
  425. }
  426. }
  427. bool Window::is_exclusive() const {
  428. return exclusive;
  429. }
  430. bool Window::is_visible() const {
  431. return visible;
  432. }
  433. void Window::_update_window_size() {
  434. Size2i size_limit;
  435. if (wrap_controls) {
  436. size_limit = get_contents_minimum_size();
  437. }
  438. size_limit.x = MAX(size_limit.x, min_size.x);
  439. size_limit.y = MAX(size_limit.y, min_size.y);
  440. size.x = MAX(size_limit.x, size.x);
  441. size.y = MAX(size_limit.y, size.y);
  442. if (max_size.x > 0 && max_size.x > min_size.x && size.x > max_size.x) {
  443. size.x = max_size.x;
  444. }
  445. if (max_size.y > 0 && max_size.y > min_size.y && size.y > max_size.y) {
  446. size.y = max_size.y;
  447. }
  448. if (embedder) {
  449. embedder->_sub_window_update(this);
  450. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  451. DisplayServer::get_singleton()->window_set_size(size, window_id);
  452. }
  453. //update the viewport
  454. _update_viewport_size();
  455. }
  456. void Window::_update_viewport_size() {
  457. //update the viewport part
  458. Size2i final_size;
  459. Size2i final_size_override;
  460. Rect2i attach_to_screen_rect(Point2i(), size);
  461. Transform2D stretch_transform;
  462. float font_oversampling = 1.0;
  463. if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
  464. stretch_transform = Transform2D();
  465. final_size = size;
  466. } else {
  467. //actual screen video mode
  468. Size2 video_mode = size;
  469. Size2 desired_res = content_scale_size;
  470. Size2 viewport_size;
  471. Size2 screen_size;
  472. float viewport_aspect = desired_res.aspect();
  473. float video_mode_aspect = video_mode.aspect();
  474. if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  475. //same aspect or ignore aspect
  476. viewport_size = desired_res;
  477. screen_size = video_mode;
  478. } else if (viewport_aspect < video_mode_aspect) {
  479. // screen ratio is smaller vertically
  480. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  481. //will stretch horizontally
  482. viewport_size.x = desired_res.y * video_mode_aspect;
  483. viewport_size.y = desired_res.y;
  484. screen_size = video_mode;
  485. } else {
  486. //will need black bars
  487. viewport_size = desired_res;
  488. screen_size.x = video_mode.y * viewport_aspect;
  489. screen_size.y = video_mode.y;
  490. }
  491. } else {
  492. //screen ratio is smaller horizontally
  493. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  494. //will stretch horizontally
  495. viewport_size.x = desired_res.x;
  496. viewport_size.y = desired_res.x / video_mode_aspect;
  497. screen_size = video_mode;
  498. } else {
  499. //will need black bars
  500. viewport_size = desired_res;
  501. screen_size.x = video_mode.x;
  502. screen_size.y = video_mode.x / viewport_aspect;
  503. }
  504. }
  505. screen_size = screen_size.floor();
  506. viewport_size = viewport_size.floor();
  507. Size2 margin;
  508. Size2 offset;
  509. //black bars and margin
  510. if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  511. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  512. //RenderingServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  513. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  514. } else if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  515. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  516. //RenderingServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  517. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  518. } else {
  519. //RenderingServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  520. }
  521. switch (content_scale_mode) {
  522. case CONTENT_SCALE_MODE_DISABLED: {
  523. // Already handled above
  524. //_update_font_oversampling(1.0);
  525. } break;
  526. case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
  527. final_size = screen_size;
  528. final_size_override = viewport_size;
  529. attach_to_screen_rect = Rect2(margin, screen_size);
  530. font_oversampling = screen_size.x / viewport_size.x;
  531. Size2 scale = Vector2(screen_size) / Vector2(final_size_override);
  532. stretch_transform.scale(scale);
  533. } break;
  534. case CONTENT_SCALE_MODE_VIEWPORT: {
  535. final_size = viewport_size;
  536. attach_to_screen_rect = Rect2(margin, screen_size);
  537. } break;
  538. }
  539. }
  540. bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
  541. _set_size(final_size, final_size_override, attach_to_screen_rect, stretch_transform, allocate);
  542. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  543. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
  544. } else {
  545. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
  546. }
  547. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  548. if (!use_font_oversampling) {
  549. font_oversampling = 1.0;
  550. }
  551. if (DynamicFontAtSize::font_oversampling != font_oversampling) {
  552. DynamicFontAtSize::font_oversampling = font_oversampling;
  553. DynamicFont::update_oversampling();
  554. }
  555. }
  556. notification(NOTIFICATION_WM_SIZE_CHANGED);
  557. if (embedder) {
  558. embedder->_sub_window_update(this);
  559. }
  560. }
  561. void Window::_update_window_callbacks() {
  562. DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
  563. DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
  564. DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
  565. DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
  566. DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
  567. }
  568. Viewport *Window::_get_embedder() const {
  569. Viewport *vp = get_parent_viewport();
  570. while (vp) {
  571. if (vp->is_embedding_subwindows()) {
  572. return vp;
  573. }
  574. if (vp->get_parent()) {
  575. vp = vp->get_parent()->get_viewport();
  576. } else {
  577. vp = nullptr;
  578. }
  579. }
  580. return nullptr;
  581. }
  582. void Window::_notification(int p_what) {
  583. if (p_what == NOTIFICATION_ENTER_TREE) {
  584. bool embedded = false;
  585. {
  586. embedder = _get_embedder();
  587. if (embedder) {
  588. embedded = true;
  589. if (!visible) {
  590. embedder = nullptr; //not yet since not visible
  591. }
  592. }
  593. }
  594. if (embedded) {
  595. //create as embedded
  596. if (embedder) {
  597. embedder->_sub_window_register(this);
  598. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  599. _update_window_size();
  600. }
  601. } else {
  602. if (get_parent() == nullptr) {
  603. //it's the root window!
  604. visible = true; //always visible
  605. window_id = DisplayServer::MAIN_WINDOW_ID;
  606. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  607. _update_from_window();
  608. //since this window already exists (created on start), we must update pos and size from it
  609. {
  610. position = DisplayServer::get_singleton()->window_get_position(window_id);
  611. size = DisplayServer::get_singleton()->window_get_size(window_id);
  612. }
  613. _update_viewport_size(); //then feed back to the viewport
  614. _update_window_callbacks();
  615. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  616. } else {
  617. //create
  618. if (visible) {
  619. _make_window();
  620. _update_window_callbacks();
  621. }
  622. }
  623. }
  624. if (transient) {
  625. _make_transient();
  626. }
  627. if (visible) {
  628. notification(NOTIFICATION_VISIBILITY_CHANGED);
  629. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  630. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  631. }
  632. }
  633. if (p_what == NOTIFICATION_READY) {
  634. if (wrap_controls) {
  635. _update_child_controls();
  636. }
  637. }
  638. if (p_what == NOTIFICATION_EXIT_TREE) {
  639. if (transient) {
  640. _clear_transient();
  641. }
  642. if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
  643. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  644. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  645. _update_window_callbacks();
  646. } else {
  647. _clear_window();
  648. }
  649. } else {
  650. if (embedder) {
  651. embedder->_sub_window_remove(this);
  652. embedder = nullptr;
  653. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  654. }
  655. _update_viewport_size(); //called by clear and make, which does not happen here
  656. }
  657. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  658. }
  659. }
  660. void Window::set_content_scale_size(const Size2i &p_size) {
  661. ERR_FAIL_COND(p_size.x < 0);
  662. ERR_FAIL_COND(p_size.y < 0);
  663. content_scale_size = p_size;
  664. _update_viewport_size();
  665. }
  666. Size2i Window::get_content_scale_size() const {
  667. return content_scale_size;
  668. }
  669. void Window::set_content_scale_mode(ContentScaleMode p_mode) {
  670. content_scale_mode = p_mode;
  671. _update_viewport_size();
  672. }
  673. Window::ContentScaleMode Window::get_content_scale_mode() const {
  674. return content_scale_mode;
  675. }
  676. void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
  677. content_scale_aspect = p_aspect;
  678. _update_viewport_size();
  679. }
  680. Window::ContentScaleAspect Window::get_content_scale_aspect() const {
  681. return content_scale_aspect;
  682. }
  683. void Window::set_use_font_oversampling(bool p_oversampling) {
  684. if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
  685. ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
  686. }
  687. use_font_oversampling = p_oversampling;
  688. _update_viewport_size();
  689. }
  690. bool Window::is_using_font_oversampling() const {
  691. return use_font_oversampling;
  692. }
  693. DisplayServer::WindowID Window::get_window_id() const {
  694. return window_id;
  695. }
  696. void Window::set_wrap_controls(bool p_enable) {
  697. wrap_controls = p_enable;
  698. if (wrap_controls) {
  699. child_controls_changed();
  700. }
  701. }
  702. bool Window::is_wrapping_controls() const {
  703. return wrap_controls;
  704. }
  705. Size2 Window::_get_contents_minimum_size() const {
  706. Size2 max;
  707. for (int i = 0; i < get_child_count(); i++) {
  708. Control *c = Object::cast_to<Control>(get_child(i));
  709. if (c) {
  710. Point2i pos = c->get_position();
  711. Size2i min = c->get_combined_minimum_size();
  712. max.x = MAX(pos.x + min.x, max.x);
  713. max.y = MAX(pos.y + min.y, max.y);
  714. }
  715. }
  716. return max;
  717. }
  718. void Window::_update_child_controls() {
  719. if (!updating_child_controls) {
  720. return;
  721. }
  722. _update_window_size();
  723. updating_child_controls = false;
  724. }
  725. void Window::child_controls_changed() {
  726. if (!is_inside_tree() || !visible || updating_child_controls) {
  727. return;
  728. }
  729. updating_child_controls = true;
  730. call_deferred("_update_child_controls");
  731. }
  732. bool Window::_can_consume_input_events() const {
  733. return exclusive_child == nullptr;
  734. }
  735. void Window::_window_input(const Ref<InputEvent> &p_ev) {
  736. if (Engine::get_singleton()->is_editor_hint() && (Object::cast_to<InputEventJoypadButton>(p_ev.ptr()) || Object::cast_to<InputEventJoypadMotion>(*p_ev))) {
  737. return; //avoid joy input on editor
  738. }
  739. if (EngineDebugger::is_active()) {
  740. //quit from game window using F8
  741. Ref<InputEventKey> k = p_ev;
  742. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == KEY_F8) {
  743. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  744. }
  745. }
  746. if (exclusive_child != nullptr) {
  747. Window *focus_target = exclusive_child;
  748. while (focus_target->exclusive_child != nullptr) {
  749. focus_target->grab_focus();
  750. focus_target = focus_target->exclusive_child;
  751. }
  752. focus_target->grab_focus();
  753. if (!is_embedding_subwindows()) { //not embedding, no need for event
  754. return;
  755. }
  756. }
  757. emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);
  758. input(p_ev);
  759. if (!is_input_handled()) {
  760. unhandled_input(p_ev);
  761. }
  762. }
  763. void Window::_window_input_text(const String &p_text) {
  764. input_text(p_text);
  765. }
  766. void Window::_window_drop_files(const Vector<String> &p_files) {
  767. emit_signal("files_dropped", p_files, current_screen);
  768. }
  769. Viewport *Window::get_parent_viewport() const {
  770. if (get_parent()) {
  771. return get_parent()->get_viewport();
  772. } else {
  773. return nullptr;
  774. }
  775. }
  776. Window *Window::get_parent_visible_window() const {
  777. Viewport *vp = get_parent_viewport();
  778. Window *window = nullptr;
  779. while (vp) {
  780. window = Object::cast_to<Window>(vp);
  781. if (window && window->visible) {
  782. break;
  783. }
  784. if (!vp->get_parent()) {
  785. break;
  786. }
  787. vp = vp->get_parent()->get_viewport();
  788. }
  789. return window;
  790. }
  791. void Window::popup_on_parent(const Rect2i &p_parent_rect) {
  792. ERR_FAIL_COND(!is_inside_tree());
  793. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  794. if (!is_embedded()) {
  795. Window *window = get_parent_visible_window();
  796. if (!window) {
  797. popup(p_parent_rect);
  798. } else {
  799. popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
  800. }
  801. } else {
  802. popup(p_parent_rect);
  803. }
  804. }
  805. void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
  806. ERR_FAIL_COND(!is_inside_tree());
  807. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  808. Rect2 parent_rect;
  809. if (is_embedded()) {
  810. parent_rect = get_parent_viewport()->get_visible_rect();
  811. } else {
  812. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  813. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  814. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  815. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  816. }
  817. Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
  818. Rect2i popup_rect;
  819. popup_rect.size = Vector2i(MIN(size_ratio.x, p_size.x), MIN(size_ratio.y, p_size.y));
  820. popup_rect.position = (parent_rect.size - popup_rect.size) / 2;
  821. popup(popup_rect);
  822. }
  823. void Window::popup_centered(const Size2i &p_minsize) {
  824. ERR_FAIL_COND(!is_inside_tree());
  825. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  826. Rect2 parent_rect;
  827. if (is_embedded()) {
  828. parent_rect = get_parent_viewport()->get_visible_rect();
  829. } else {
  830. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  831. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  832. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  833. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  834. }
  835. Rect2i popup_rect;
  836. if (p_minsize == Size2i()) {
  837. popup_rect.size = _get_contents_minimum_size();
  838. } else {
  839. popup_rect.size = p_minsize;
  840. }
  841. popup_rect.position = (parent_rect.size - popup_rect.size) / 2;
  842. popup(popup_rect);
  843. }
  844. void Window::popup_centered_ratio(float p_ratio) {
  845. ERR_FAIL_COND(!is_inside_tree());
  846. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  847. Rect2 parent_rect;
  848. if (is_embedded()) {
  849. parent_rect = get_parent_viewport()->get_visible_rect();
  850. } else {
  851. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  852. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  853. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  854. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  855. }
  856. Rect2i popup_rect;
  857. popup_rect.size = parent_rect.size * p_ratio;
  858. popup_rect.position = (parent_rect.size - popup_rect.size) / 2;
  859. popup(popup_rect);
  860. }
  861. void Window::popup(const Rect2i &p_screen_rect) {
  862. emit_signal("about_to_popup");
  863. if (p_screen_rect != Rect2i()) {
  864. set_position(p_screen_rect.position);
  865. set_size(p_screen_rect.size);
  866. }
  867. Rect2i adjust = _popup_adjust_rect();
  868. if (adjust != Rect2i()) {
  869. set_position(adjust.position);
  870. set_size(adjust.size);
  871. }
  872. int scr = DisplayServer::get_singleton()->get_screen_count();
  873. for (int i = 0; i < scr; i++) {
  874. Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
  875. if (r.has_point(position)) {
  876. current_screen = i;
  877. break;
  878. }
  879. }
  880. set_transient(true);
  881. set_visible(true);
  882. _post_popup();
  883. notification(NOTIFICATION_POST_POPUP);
  884. }
  885. Size2 Window::get_contents_minimum_size() const {
  886. return _get_contents_minimum_size();
  887. }
  888. void Window::grab_focus() {
  889. if (embedder) {
  890. embedder->_sub_window_grab_focus(this);
  891. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  892. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  893. }
  894. }
  895. bool Window::has_focus() const {
  896. return focused;
  897. }
  898. Rect2i Window::get_usable_parent_rect() const {
  899. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  900. Rect2i parent;
  901. if (is_embedded()) {
  902. parent = _get_embedder()->get_visible_rect();
  903. } else {
  904. const Window *w = is_visible() ? this : get_parent_visible_window();
  905. //find a parent that can contain us
  906. ERR_FAIL_COND_V(!w, Rect2());
  907. parent = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
  908. }
  909. return parent;
  910. }
  911. void Window::add_child_notify(Node *p_child) {
  912. Control *child_c = Object::cast_to<Control>(p_child);
  913. if (child_c && child_c->data.theme.is_null() && (theme_owner || theme_owner_window)) {
  914. Control::_propagate_theme_changed(child_c, theme_owner, theme_owner_window); //need to propagate here, since many controls may require setting up stuff
  915. }
  916. Window *child_w = Object::cast_to<Window>(p_child);
  917. if (child_w && child_w->theme.is_null() && (theme_owner || theme_owner_window)) {
  918. Control::_propagate_theme_changed(child_w, theme_owner, theme_owner_window); //need to propagate here, since many controls may require setting up stuff
  919. }
  920. if (is_inside_tree() && wrap_controls) {
  921. child_controls_changed();
  922. }
  923. }
  924. void Window::remove_child_notify(Node *p_child) {
  925. Control *child_c = Object::cast_to<Control>(p_child);
  926. if (child_c && (child_c->data.theme_owner || child_c->data.theme_owner_window) && child_c->data.theme.is_null()) {
  927. Control::_propagate_theme_changed(child_c, nullptr, nullptr);
  928. }
  929. Window *child_w = Object::cast_to<Window>(p_child);
  930. if (child_w && (child_w->theme_owner || child_w->theme_owner_window) && child_w->theme.is_null()) {
  931. Control::_propagate_theme_changed(child_w, nullptr, nullptr);
  932. }
  933. if (is_inside_tree() && wrap_controls) {
  934. child_controls_changed();
  935. }
  936. }
  937. void Window::set_theme(const Ref<Theme> &p_theme) {
  938. if (theme == p_theme) {
  939. return;
  940. }
  941. theme = p_theme;
  942. if (!p_theme.is_null()) {
  943. theme_owner = nullptr;
  944. theme_owner_window = this;
  945. Control::_propagate_theme_changed(this, nullptr, this);
  946. } else {
  947. Control *parent_c = cast_to<Control>(get_parent());
  948. if (parent_c && (parent_c->data.theme_owner || parent_c->data.theme_owner_window)) {
  949. Control::_propagate_theme_changed(this, parent_c->data.theme_owner, parent_c->data.theme_owner_window);
  950. } else {
  951. Window *parent_w = cast_to<Window>(get_parent());
  952. if (parent_w && (parent_w->theme_owner || parent_w->theme_owner_window)) {
  953. Control::_propagate_theme_changed(this, parent_w->theme_owner, parent_w->theme_owner_window);
  954. } else {
  955. Control::_propagate_theme_changed(this, nullptr, nullptr);
  956. }
  957. }
  958. }
  959. }
  960. Ref<Theme> Window::get_theme() const {
  961. return theme;
  962. }
  963. Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_type) const {
  964. StringName type = p_type ? p_type : get_class_name();
  965. return Control::get_icons(theme_owner, theme_owner_window, p_name, type);
  966. }
  967. Ref<Shader> Window::get_theme_shader(const StringName &p_name, const StringName &p_type) const {
  968. StringName type = p_type ? p_type : get_class_name();
  969. return Control::get_shaders(theme_owner, theme_owner_window, p_name, type);
  970. }
  971. Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_type) const {
  972. StringName type = p_type ? p_type : get_class_name();
  973. return Control::get_styleboxs(theme_owner, theme_owner_window, p_name, type);
  974. }
  975. Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_type) const {
  976. StringName type = p_type ? p_type : get_class_name();
  977. return Control::get_fonts(theme_owner, theme_owner_window, p_name, type);
  978. }
  979. Color Window::get_theme_color(const StringName &p_name, const StringName &p_type) const {
  980. StringName type = p_type ? p_type : get_class_name();
  981. return Control::get_colors(theme_owner, theme_owner_window, p_name, type);
  982. }
  983. int Window::get_theme_constant(const StringName &p_name, const StringName &p_type) const {
  984. StringName type = p_type ? p_type : get_class_name();
  985. return Control::get_constants(theme_owner, theme_owner_window, p_name, type);
  986. }
  987. bool Window::has_theme_icon(const StringName &p_name, const StringName &p_type) const {
  988. StringName type = p_type ? p_type : get_class_name();
  989. return Control::has_icons(theme_owner, theme_owner_window, p_name, type);
  990. }
  991. bool Window::has_theme_shader(const StringName &p_name, const StringName &p_type) const {
  992. StringName type = p_type ? p_type : get_class_name();
  993. return Control::has_shaders(theme_owner, theme_owner_window, p_name, type);
  994. }
  995. bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_type) const {
  996. StringName type = p_type ? p_type : get_class_name();
  997. return Control::has_styleboxs(theme_owner, theme_owner_window, p_name, type);
  998. }
  999. bool Window::has_theme_font(const StringName &p_name, const StringName &p_type) const {
  1000. StringName type = p_type ? p_type : get_class_name();
  1001. return Control::has_fonts(theme_owner, theme_owner_window, p_name, type);
  1002. }
  1003. bool Window::has_theme_color(const StringName &p_name, const StringName &p_type) const {
  1004. StringName type = p_type ? p_type : get_class_name();
  1005. return Control::has_colors(theme_owner, theme_owner_window, p_name, type);
  1006. }
  1007. bool Window::has_theme_constant(const StringName &p_name, const StringName &p_type) const {
  1008. StringName type = p_type ? p_type : get_class_name();
  1009. return Control::has_constants(theme_owner, theme_owner_window, p_name, type);
  1010. }
  1011. Rect2i Window::get_parent_rect() const {
  1012. ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
  1013. if (is_embedded()) {
  1014. //viewport
  1015. Node *n = get_parent();
  1016. ERR_FAIL_COND_V(!n, Rect2i());
  1017. Viewport *p = n->get_viewport();
  1018. ERR_FAIL_COND_V(!p, Rect2i());
  1019. return p->get_visible_rect();
  1020. } else {
  1021. int x = get_position().x;
  1022. int closest_dist = 0x7FFFFFFF;
  1023. Rect2i closest_rect;
  1024. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  1025. Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
  1026. int d;
  1027. if (x >= s.position.x && x < s.size.x) {
  1028. //contained
  1029. closest_rect = s;
  1030. break;
  1031. } else if (x < s.position.x) {
  1032. d = s.position.x - x;
  1033. } else {
  1034. d = x - (s.position.x + s.size.x);
  1035. }
  1036. if (d < closest_dist) {
  1037. closest_dist = d;
  1038. closest_rect = s;
  1039. }
  1040. }
  1041. return closest_rect;
  1042. }
  1043. }
  1044. void Window::set_clamp_to_embedder(bool p_enable) {
  1045. clamp_to_embedder = p_enable;
  1046. }
  1047. bool Window::is_clamped_to_embedder() const {
  1048. return clamp_to_embedder;
  1049. }
  1050. void Window::_bind_methods() {
  1051. ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
  1052. ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
  1053. ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
  1054. ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
  1055. ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
  1056. ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
  1057. ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
  1058. ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
  1059. ClassDB::bind_method(D_METHOD("get_real_size"), &Window::get_real_size);
  1060. ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
  1061. ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
  1062. ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
  1063. ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
  1064. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
  1065. ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
  1066. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
  1067. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
  1068. ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
  1069. ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
  1070. ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
  1071. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
  1072. ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
  1073. ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
  1074. ClassDB::bind_method(D_METHOD("show"), &Window::show);
  1075. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
  1076. ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
  1077. ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
  1078. ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
  1079. ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
  1080. ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
  1081. ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
  1082. ClassDB::bind_method(D_METHOD("set_ime_active"), &Window::set_ime_active);
  1083. ClassDB::bind_method(D_METHOD("set_ime_position"), &Window::set_ime_position);
  1084. ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
  1085. ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
  1086. ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
  1087. ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
  1088. ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
  1089. ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
  1090. ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
  1091. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
  1092. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
  1093. ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
  1094. ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
  1095. ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
  1096. ClassDB::bind_method(D_METHOD("_update_child_controls"), &Window::_update_child_controls);
  1097. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
  1098. ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
  1099. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "type"), &Window::get_theme_icon, DEFVAL(""));
  1100. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "type"), &Window::get_theme_stylebox, DEFVAL(""));
  1101. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "type"), &Window::get_theme_font, DEFVAL(""));
  1102. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "type"), &Window::get_theme_color, DEFVAL(""));
  1103. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "type"), &Window::get_theme_constant, DEFVAL(""));
  1104. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "type"), &Window::has_theme_icon, DEFVAL(""));
  1105. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "type"), &Window::has_theme_stylebox, DEFVAL(""));
  1106. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "type"), &Window::has_theme_font, DEFVAL(""));
  1107. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "type"), &Window::has_theme_color, DEFVAL(""));
  1108. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "type"), &Window::has_theme_constant, DEFVAL(""));
  1109. ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
  1110. ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
  1111. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
  1112. ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
  1113. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  1114. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  1115. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position"), "set_position", "get_position");
  1116. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
  1117. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,FullScreen"), "set_mode", "get_mode");
  1118. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_screen"), "set_current_screen", "get_current_screen");
  1119. ADD_GROUP("Flags", "");
  1120. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
  1122. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  1123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
  1124. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
  1125. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
  1126. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
  1127. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
  1128. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
  1129. ADD_GROUP("Limits", "");
  1130. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size"), "set_min_size", "get_min_size");
  1131. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size"), "set_max_size", "get_max_size");
  1132. ADD_GROUP("Content Scale", "content_scale_");
  1133. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
  1134. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,CanvasItems,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
  1135. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,KeepWidth,KeepHeight,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
  1136. ADD_GROUP("Theme", "");
  1137. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  1138. ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  1139. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  1140. ADD_SIGNAL(MethodInfo("mouse_entered"));
  1141. ADD_SIGNAL(MethodInfo("mouse_exited"));
  1142. ADD_SIGNAL(MethodInfo("focus_entered"));
  1143. ADD_SIGNAL(MethodInfo("focus_exited"));
  1144. ADD_SIGNAL(MethodInfo("close_requested"));
  1145. ADD_SIGNAL(MethodInfo("go_back_requested"));
  1146. ADD_SIGNAL(MethodInfo("visibility_changed"));
  1147. ADD_SIGNAL(MethodInfo("about_to_popup"));
  1148. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  1149. BIND_ENUM_CONSTANT(MODE_WINDOWED);
  1150. BIND_ENUM_CONSTANT(MODE_MINIMIZED);
  1151. BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
  1152. BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
  1153. BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
  1154. BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
  1155. BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
  1156. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  1157. BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
  1158. BIND_ENUM_CONSTANT(FLAG_MAX);
  1159. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
  1160. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
  1161. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
  1162. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
  1163. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
  1164. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
  1165. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
  1166. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
  1167. }
  1168. Window::Window() {
  1169. for (int i = 0; i < FLAG_MAX; i++) {
  1170. flags[i] = false;
  1171. }
  1172. content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
  1173. content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
  1174. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1175. }
  1176. Window::~Window() {
  1177. }