window.cpp 46 KB

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