aspect_ratio_container.cpp 7.5 KB

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