aspect_ratio_container.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  36. if (!c) {
  37. continue;
  38. }
  39. Size2 minsize = c->get_combined_minimum_size();
  40. ms = ms.max(minsize);
  41. }
  42. return ms;
  43. }
  44. void AspectRatioContainer::set_ratio(float p_ratio) {
  45. if (ratio == p_ratio) {
  46. return;
  47. }
  48. ratio = p_ratio;
  49. queue_sort();
  50. }
  51. void AspectRatioContainer::set_stretch_mode(StretchMode p_mode) {
  52. if (stretch_mode == p_mode) {
  53. return;
  54. }
  55. stretch_mode = p_mode;
  56. queue_sort();
  57. }
  58. void AspectRatioContainer::set_alignment_horizontal(AlignmentMode p_alignment_horizontal) {
  59. if (alignment_horizontal == p_alignment_horizontal) {
  60. return;
  61. }
  62. alignment_horizontal = p_alignment_horizontal;
  63. queue_sort();
  64. }
  65. void AspectRatioContainer::set_alignment_vertical(AlignmentMode p_alignment_vertical) {
  66. if (alignment_vertical == p_alignment_vertical) {
  67. return;
  68. }
  69. alignment_vertical = p_alignment_vertical;
  70. queue_sort();
  71. }
  72. Vector<int> AspectRatioContainer::get_allowed_size_flags_horizontal() const {
  73. Vector<int> flags;
  74. flags.append(SIZE_FILL);
  75. flags.append(SIZE_SHRINK_BEGIN);
  76. flags.append(SIZE_SHRINK_CENTER);
  77. flags.append(SIZE_SHRINK_END);
  78. return flags;
  79. }
  80. Vector<int> AspectRatioContainer::get_allowed_size_flags_vertical() const {
  81. Vector<int> flags;
  82. flags.append(SIZE_FILL);
  83. flags.append(SIZE_SHRINK_BEGIN);
  84. flags.append(SIZE_SHRINK_CENTER);
  85. flags.append(SIZE_SHRINK_END);
  86. return flags;
  87. }
  88. void AspectRatioContainer::_notification(int p_what) {
  89. switch (p_what) {
  90. case NOTIFICATION_SORT_CHILDREN: {
  91. bool rtl = is_layout_rtl();
  92. Size2 size = get_size();
  93. for (int i = 0; i < get_child_count(); i++) {
  94. Control *c = as_sortable_control(get_child(i));
  95. if (!c) {
  96. continue;
  97. }
  98. // Temporary fix for editor crash.
  99. TextureRect *trect = Object::cast_to<TextureRect>(c);
  100. if (trect) {
  101. if (trect->get_expand_mode() == TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL || trect->get_expand_mode() == TextureRect::EXPAND_FIT_HEIGHT_PROPORTIONAL) {
  102. WARN_PRINT_ONCE("Proportional TextureRect is currently not supported inside AspectRatioContainer");
  103. continue;
  104. }
  105. }
  106. Size2 child_minsize = c->get_combined_minimum_size();
  107. Size2 child_size = Size2(ratio, 1.0);
  108. float scale_factor = 1.0;
  109. switch (stretch_mode) {
  110. case STRETCH_WIDTH_CONTROLS_HEIGHT: {
  111. scale_factor = size.x / child_size.x;
  112. } break;
  113. case STRETCH_HEIGHT_CONTROLS_WIDTH: {
  114. scale_factor = size.y / child_size.y;
  115. } break;
  116. case STRETCH_FIT: {
  117. scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
  118. } break;
  119. case STRETCH_COVER: {
  120. scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
  121. } break;
  122. }
  123. child_size *= scale_factor;
  124. child_size = child_size.max(child_minsize);
  125. float align_x = 0.5;
  126. switch (alignment_horizontal) {
  127. case ALIGNMENT_BEGIN: {
  128. align_x = 0.0;
  129. } break;
  130. case ALIGNMENT_CENTER: {
  131. align_x = 0.5;
  132. } break;
  133. case ALIGNMENT_END: {
  134. align_x = 1.0;
  135. } break;
  136. }
  137. float align_y = 0.5;
  138. switch (alignment_vertical) {
  139. case ALIGNMENT_BEGIN: {
  140. align_y = 0.0;
  141. } break;
  142. case ALIGNMENT_CENTER: {
  143. align_y = 0.5;
  144. } break;
  145. case ALIGNMENT_END: {
  146. align_y = 1.0;
  147. } break;
  148. }
  149. Vector2 offset = (size - child_size) * Vector2(align_x, align_y);
  150. if (rtl) {
  151. fit_child_in_rect(c, Rect2(Vector2(size.x - offset.x - child_size.x, offset.y), child_size));
  152. } else {
  153. fit_child_in_rect(c, Rect2(offset, child_size));
  154. }
  155. }
  156. } break;
  157. }
  158. }
  159. void AspectRatioContainer::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
  161. ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);
  162. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
  163. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);
  164. ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
  165. ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);
  166. ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
  167. ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);
  168. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0.001,10.0,0.0001,or_greater"), "set_ratio", "get_ratio");
  169. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width Controls Height,Height Controls Width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");
  170. ADD_GROUP("Alignment", "alignment_");
  171. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
  172. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");
  173. BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
  174. BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
  175. BIND_ENUM_CONSTANT(STRETCH_FIT);
  176. BIND_ENUM_CONSTANT(STRETCH_COVER);
  177. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  178. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  179. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  180. }