aspect_ratio_container.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**************************************************************************/
  2. /* aspect_ratio_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "aspect_ratio_container.h"
  31. #include "scene/gui/texture_rect.h"
  32. Size2 AspectRatioContainer::get_minimum_size() const {
  33. Size2 ms;
  34. for (int i = 0; i < get_child_count(); i++) {
  35. Control *c = Object::cast_to<Control>(get_child(i));
  36. if (!c) {
  37. continue;
  38. }
  39. if (c->is_set_as_top_level()) {
  40. continue;
  41. }
  42. if (!c->is_visible()) {
  43. continue;
  44. }
  45. Size2 minsize = c->get_combined_minimum_size();
  46. ms.width = MAX(ms.width, minsize.width);
  47. ms.height = MAX(ms.height, minsize.height);
  48. }
  49. return ms;
  50. }
  51. void AspectRatioContainer::set_ratio(float p_ratio) {
  52. if (ratio == p_ratio) {
  53. return;
  54. }
  55. ratio = p_ratio;
  56. queue_sort();
  57. }
  58. void AspectRatioContainer::set_stretch_mode(StretchMode p_mode) {
  59. if (stretch_mode == p_mode) {
  60. return;
  61. }
  62. stretch_mode = p_mode;
  63. queue_sort();
  64. }
  65. void AspectRatioContainer::set_alignment_horizontal(AlignmentMode p_alignment_horizontal) {
  66. if (alignment_horizontal == p_alignment_horizontal) {
  67. return;
  68. }
  69. alignment_horizontal = p_alignment_horizontal;
  70. queue_sort();
  71. }
  72. void AspectRatioContainer::set_alignment_vertical(AlignmentMode p_alignment_vertical) {
  73. if (alignment_vertical == p_alignment_vertical) {
  74. return;
  75. }
  76. alignment_vertical = p_alignment_vertical;
  77. queue_sort();
  78. }
  79. Vector<int> AspectRatioContainer::get_allowed_size_flags_horizontal() const {
  80. Vector<int> flags;
  81. flags.append(SIZE_FILL);
  82. flags.append(SIZE_SHRINK_BEGIN);
  83. flags.append(SIZE_SHRINK_CENTER);
  84. flags.append(SIZE_SHRINK_END);
  85. return flags;
  86. }
  87. Vector<int> AspectRatioContainer::get_allowed_size_flags_vertical() const {
  88. Vector<int> flags;
  89. flags.append(SIZE_FILL);
  90. flags.append(SIZE_SHRINK_BEGIN);
  91. flags.append(SIZE_SHRINK_CENTER);
  92. flags.append(SIZE_SHRINK_END);
  93. return flags;
  94. }
  95. void AspectRatioContainer::_notification(int p_what) {
  96. switch (p_what) {
  97. case NOTIFICATION_SORT_CHILDREN: {
  98. bool rtl = is_layout_rtl();
  99. Size2 size = get_size();
  100. for (int i = 0; i < get_child_count(); i++) {
  101. Control *c = Object::cast_to<Control>(get_child(i));
  102. if (!c) {
  103. continue;
  104. }
  105. if (c->is_set_as_top_level()) {
  106. continue;
  107. }
  108. // Temporary fix for editor crash.
  109. TextureRect *trect = Object::cast_to<TextureRect>(c);
  110. if (trect) {
  111. if (trect->get_expand_mode() == TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL || trect->get_expand_mode() == TextureRect::EXPAND_FIT_HEIGHT_PROPORTIONAL) {
  112. WARN_PRINT_ONCE("Proportional TextureRect is currently not supported inside AspectRatioContainer");
  113. continue;
  114. }
  115. }
  116. Size2 child_minsize = c->get_combined_minimum_size();
  117. Size2 child_size = Size2(ratio, 1.0);
  118. float scale_factor = 1.0;
  119. switch (stretch_mode) {
  120. case STRETCH_WIDTH_CONTROLS_HEIGHT: {
  121. scale_factor = size.x / child_size.x;
  122. } break;
  123. case STRETCH_HEIGHT_CONTROLS_WIDTH: {
  124. scale_factor = size.y / child_size.y;
  125. } break;
  126. case STRETCH_FIT: {
  127. scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
  128. } break;
  129. case STRETCH_COVER: {
  130. scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
  131. } break;
  132. }
  133. child_size *= scale_factor;
  134. child_size.x = MAX(child_size.x, child_minsize.x);
  135. child_size.y = MAX(child_size.y, child_minsize.y);
  136. float align_x = 0.5;
  137. switch (alignment_horizontal) {
  138. case ALIGNMENT_BEGIN: {
  139. align_x = 0.0;
  140. } break;
  141. case ALIGNMENT_CENTER: {
  142. align_x = 0.5;
  143. } break;
  144. case ALIGNMENT_END: {
  145. align_x = 1.0;
  146. } break;
  147. }
  148. float align_y = 0.5;
  149. switch (alignment_vertical) {
  150. case ALIGNMENT_BEGIN: {
  151. align_y = 0.0;
  152. } break;
  153. case ALIGNMENT_CENTER: {
  154. align_y = 0.5;
  155. } break;
  156. case ALIGNMENT_END: {
  157. align_y = 1.0;
  158. } break;
  159. }
  160. Vector2 offset = (size - child_size) * Vector2(align_x, align_y);
  161. if (rtl) {
  162. fit_child_in_rect(c, Rect2(Vector2(size.x - offset.x - child_size.x, offset.y), child_size));
  163. } else {
  164. fit_child_in_rect(c, Rect2(offset, child_size));
  165. }
  166. }
  167. } break;
  168. }
  169. }
  170. void AspectRatioContainer::_bind_methods() {
  171. ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
  172. ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);
  173. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
  174. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);
  175. ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
  176. ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);
  177. ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
  178. ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);
  179. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0.001,10.0,0.0001,or_greater"), "set_ratio", "get_ratio");
  180. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width Controls Height,Height Controls Width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");
  181. ADD_GROUP("Alignment", "alignment_");
  182. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
  183. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");
  184. BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
  185. BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
  186. BIND_ENUM_CONSTANT(STRETCH_FIT);
  187. BIND_ENUM_CONSTANT(STRETCH_COVER);
  188. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  189. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  190. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  191. }