window.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*************************************************************************/
  2. /* window.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef WINDOW_H
  31. #define WINDOW_H
  32. #include "scene/main/viewport.h"
  33. class Control;
  34. class Font;
  35. class Shortcut;
  36. class StyleBox;
  37. class Theme;
  38. class Window : public Viewport {
  39. GDCLASS(Window, Viewport)
  40. public:
  41. enum Mode {
  42. MODE_WINDOWED = DisplayServer::WINDOW_MODE_WINDOWED,
  43. MODE_MINIMIZED = DisplayServer::WINDOW_MODE_MINIMIZED,
  44. MODE_MAXIMIZED = DisplayServer::WINDOW_MODE_MAXIMIZED,
  45. MODE_FULLSCREEN = DisplayServer::WINDOW_MODE_FULLSCREEN,
  46. MODE_EXCLUSIVE_FULLSCREEN = DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN,
  47. };
  48. enum Flags {
  49. FLAG_RESIZE_DISABLED = DisplayServer::WINDOW_FLAG_RESIZE_DISABLED,
  50. FLAG_BORDERLESS = DisplayServer::WINDOW_FLAG_BORDERLESS,
  51. FLAG_ALWAYS_ON_TOP = DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP,
  52. FLAG_TRANSPARENT = DisplayServer::WINDOW_FLAG_TRANSPARENT,
  53. FLAG_NO_FOCUS = DisplayServer::WINDOW_FLAG_NO_FOCUS,
  54. FLAG_POPUP = DisplayServer::WINDOW_FLAG_POPUP,
  55. FLAG_EXTEND_TO_TITLE = DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE,
  56. FLAG_MAX = DisplayServer::WINDOW_FLAG_MAX,
  57. };
  58. enum ContentScaleMode {
  59. CONTENT_SCALE_MODE_DISABLED,
  60. CONTENT_SCALE_MODE_CANVAS_ITEMS,
  61. CONTENT_SCALE_MODE_VIEWPORT,
  62. };
  63. enum ContentScaleAspect {
  64. CONTENT_SCALE_ASPECT_IGNORE,
  65. CONTENT_SCALE_ASPECT_KEEP,
  66. CONTENT_SCALE_ASPECT_KEEP_WIDTH,
  67. CONTENT_SCALE_ASPECT_KEEP_HEIGHT,
  68. CONTENT_SCALE_ASPECT_EXPAND,
  69. };
  70. enum LayoutDirection {
  71. LAYOUT_DIRECTION_INHERITED,
  72. LAYOUT_DIRECTION_LOCALE,
  73. LAYOUT_DIRECTION_LTR,
  74. LAYOUT_DIRECTION_RTL
  75. };
  76. enum {
  77. DEFAULT_WINDOW_SIZE = 100,
  78. };
  79. private:
  80. DisplayServer::WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
  81. String title;
  82. mutable int current_screen = 0;
  83. mutable Vector2i position;
  84. mutable Size2i size = Size2i(DEFAULT_WINDOW_SIZE, DEFAULT_WINDOW_SIZE);
  85. mutable Size2i min_size;
  86. mutable Size2i max_size;
  87. mutable Mode mode = MODE_WINDOWED;
  88. mutable bool flags[FLAG_MAX] = {};
  89. bool visible = true;
  90. bool focused = false;
  91. bool use_font_oversampling = false;
  92. bool transient = false;
  93. bool exclusive = false;
  94. bool wrap_controls = false;
  95. bool updating_child_controls = false;
  96. bool clamp_to_embedder = false;
  97. LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED;
  98. bool auto_translate = true;
  99. void _update_child_controls();
  100. Size2i content_scale_size;
  101. ContentScaleMode content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
  102. ContentScaleAspect content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
  103. real_t content_scale_factor = 1.0;
  104. void _make_window();
  105. void _clear_window();
  106. void _update_from_window();
  107. void _update_viewport_size();
  108. void _update_window_size();
  109. void _propagate_window_notification(Node *p_node, int p_notification);
  110. void _update_window_callbacks();
  111. void _clear_transient();
  112. void _make_transient();
  113. Window *transient_parent = nullptr;
  114. Window *exclusive_child = nullptr;
  115. HashSet<Window *> transient_children;
  116. friend class Control;
  117. Ref<Theme> theme;
  118. Control *theme_owner = nullptr;
  119. Window *theme_owner_window = nullptr;
  120. StringName theme_type_variation;
  121. Viewport *embedder = nullptr;
  122. friend class Viewport; //friend back, can call the methods below
  123. void _window_input(const Ref<InputEvent> &p_ev);
  124. void _window_input_text(const String &p_text);
  125. void _window_drop_files(const Vector<String> &p_files);
  126. void _rect_changed_callback(const Rect2i &p_callback);
  127. void _event_callback(DisplayServer::WindowEvent p_event);
  128. virtual bool _can_consume_input_events() const override;
  129. Ref<Shortcut> debugger_stop_shortcut;
  130. protected:
  131. Viewport *_get_embedder() const;
  132. virtual Rect2i _popup_adjust_rect() const { return Rect2i(); }
  133. virtual void _post_popup() {}
  134. virtual Size2 _get_contents_minimum_size() const;
  135. static void _bind_methods();
  136. void _notification(int p_what);
  137. void _validate_property(PropertyInfo &p_property) const;
  138. virtual void add_child_notify(Node *p_child) override;
  139. virtual void remove_child_notify(Node *p_child) override;
  140. public:
  141. enum {
  142. NOTIFICATION_VISIBILITY_CHANGED = 30,
  143. NOTIFICATION_POST_POPUP = 31,
  144. NOTIFICATION_THEME_CHANGED = 32
  145. };
  146. void set_title(const String &p_title);
  147. String get_title() const;
  148. void set_current_screen(int p_screen);
  149. int get_current_screen() const;
  150. void set_position(const Point2i &p_position);
  151. Point2i get_position() const;
  152. void set_size(const Size2i &p_size);
  153. Size2i get_size() const;
  154. void reset_size();
  155. Size2i get_real_size() const;
  156. void set_max_size(const Size2i &p_max_size);
  157. Size2i get_max_size() const;
  158. void set_min_size(const Size2i &p_min_size);
  159. Size2i get_min_size() const;
  160. void set_mode(Mode p_mode);
  161. Mode get_mode() const;
  162. void set_flag(Flags p_flag, bool p_enabled);
  163. bool get_flag(Flags p_flag) const;
  164. bool is_maximize_allowed() const;
  165. void request_attention();
  166. void move_to_foreground();
  167. void set_visible(bool p_visible);
  168. bool is_visible() const;
  169. void show();
  170. void hide();
  171. void set_transient(bool p_transient);
  172. bool is_transient() const;
  173. void set_exclusive(bool p_exclusive);
  174. bool is_exclusive() const;
  175. void set_clamp_to_embedder(bool p_enable);
  176. bool is_clamped_to_embedder() const;
  177. bool can_draw() const;
  178. void set_ime_active(bool p_active);
  179. void set_ime_position(const Point2i &p_pos);
  180. bool is_embedded() const;
  181. void set_content_scale_size(const Size2i &p_size);
  182. Size2i get_content_scale_size() const;
  183. void set_content_scale_mode(ContentScaleMode p_mode);
  184. ContentScaleMode get_content_scale_mode() const;
  185. void set_content_scale_aspect(ContentScaleAspect p_aspect);
  186. ContentScaleAspect get_content_scale_aspect() const;
  187. void set_content_scale_factor(real_t p_factor);
  188. real_t get_content_scale_factor() const;
  189. void set_use_font_oversampling(bool p_oversampling);
  190. bool is_using_font_oversampling() const;
  191. void warp_mouse(const Vector2 &p_position) override;
  192. void set_wrap_controls(bool p_enable);
  193. bool is_wrapping_controls() const;
  194. void child_controls_changed();
  195. Window *get_parent_visible_window() const;
  196. Viewport *get_parent_viewport() const;
  197. void popup(const Rect2i &p_rect = Rect2i());
  198. void popup_on_parent(const Rect2i &p_parent_rect);
  199. void popup_centered_ratio(float p_ratio = 0.8);
  200. void popup_centered(const Size2i &p_minsize = Size2i());
  201. void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
  202. void set_theme(const Ref<Theme> &p_theme);
  203. Ref<Theme> get_theme() const;
  204. void _theme_changed();
  205. void set_theme_type_variation(const StringName &p_theme_type);
  206. StringName get_theme_type_variation() const;
  207. _FORCE_INLINE_ void _get_theme_type_dependencies(const StringName &p_theme_type, List<StringName> *p_list) const;
  208. Size2 get_contents_minimum_size() const;
  209. void grab_focus();
  210. bool has_focus() const;
  211. void set_layout_direction(LayoutDirection p_direction);
  212. LayoutDirection get_layout_direction() const;
  213. bool is_layout_rtl() const;
  214. void set_auto_translate(bool p_enable);
  215. bool is_auto_translating() const;
  216. _FORCE_INLINE_ String atr(const String p_string) const { return is_auto_translating() ? tr(p_string) : p_string; };
  217. Rect2i get_usable_parent_rect() const;
  218. Ref<Texture2D> get_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  219. Ref<StyleBox> get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  220. Ref<Font> get_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  221. int get_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  222. Color get_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  223. int get_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  224. bool has_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  225. bool has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  226. bool has_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  227. bool has_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  228. bool has_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  229. bool has_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  230. float get_theme_default_base_scale() const;
  231. Ref<Font> get_theme_default_font() const;
  232. int get_theme_default_font_size() const;
  233. virtual Transform2D get_screen_transform() const override;
  234. Rect2i get_parent_rect() const;
  235. virtual DisplayServer::WindowID get_window_id() const override;
  236. Window();
  237. ~Window();
  238. };
  239. VARIANT_ENUM_CAST(Window::Mode);
  240. VARIANT_ENUM_CAST(Window::Flags);
  241. VARIANT_ENUM_CAST(Window::ContentScaleMode);
  242. VARIANT_ENUM_CAST(Window::ContentScaleAspect);
  243. VARIANT_ENUM_CAST(Window::LayoutDirection);
  244. #endif // WINDOW_H