aspect_ratio_container.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. ratio = p_ratio;
  52. queue_sort();
  53. }
  54. void AspectRatioContainer::set_stretch_mode(StretchMode p_mode) {
  55. stretch_mode = p_mode;
  56. queue_sort();
  57. }
  58. void AspectRatioContainer::set_alignment_horizontal(AlignMode p_alignment_horizontal) {
  59. alignment_horizontal = p_alignment_horizontal;
  60. queue_sort();
  61. }
  62. void AspectRatioContainer::set_alignment_vertical(AlignMode p_alignment_vertical) {
  63. alignment_vertical = p_alignment_vertical;
  64. queue_sort();
  65. }
  66. void AspectRatioContainer::_notification(int p_what) {
  67. switch (p_what) {
  68. case NOTIFICATION_SORT_CHILDREN: {
  69. bool rtl = is_layout_rtl();
  70. Size2 size = get_size();
  71. for (int i = 0; i < get_child_count(); i++) {
  72. Control *c = Object::cast_to<Control>(get_child(i));
  73. if (!c) {
  74. continue;
  75. }
  76. if (c->is_set_as_top_level()) {
  77. continue;
  78. }
  79. Size2 child_minsize = c->get_combined_minimum_size();
  80. Size2 child_size = Size2(ratio, 1.0);
  81. float scale_factor = 1.0;
  82. switch (stretch_mode) {
  83. case STRETCH_WIDTH_CONTROLS_HEIGHT: {
  84. scale_factor = size.x / child_size.x;
  85. } break;
  86. case STRETCH_HEIGHT_CONTROLS_WIDTH: {
  87. scale_factor = size.y / child_size.y;
  88. } break;
  89. case STRETCH_FIT: {
  90. scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
  91. } break;
  92. case STRETCH_COVER: {
  93. scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
  94. } break;
  95. }
  96. child_size *= scale_factor;
  97. child_size.x = MAX(child_size.x, child_minsize.x);
  98. child_size.y = MAX(child_size.y, child_minsize.y);
  99. float align_x = 0.5;
  100. switch (alignment_horizontal) {
  101. case ALIGN_BEGIN: {
  102. align_x = 0.0;
  103. } break;
  104. case ALIGN_CENTER: {
  105. align_x = 0.5;
  106. } break;
  107. case ALIGN_END: {
  108. align_x = 1.0;
  109. } break;
  110. }
  111. float align_y = 0.5;
  112. switch (alignment_vertical) {
  113. case ALIGN_BEGIN: {
  114. align_y = 0.0;
  115. } break;
  116. case ALIGN_CENTER: {
  117. align_y = 0.5;
  118. } break;
  119. case ALIGN_END: {
  120. align_y = 1.0;
  121. } break;
  122. }
  123. Vector2 offset = (size - child_size) * Vector2(align_x, align_y);
  124. if (rtl) {
  125. fit_child_in_rect(c, Rect2(Vector2(size.x - offset.x - child_size.x, offset.y), child_size));
  126. } else {
  127. fit_child_in_rect(c, Rect2(offset, child_size));
  128. }
  129. }
  130. } break;
  131. }
  132. }
  133. void AspectRatioContainer::_bind_methods() {
  134. ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
  135. ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);
  136. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
  137. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);
  138. ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
  139. ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);
  140. ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
  141. ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);
  142. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio"), "set_ratio", "get_ratio");
  143. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width Controls Height,Height Controls Width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");
  144. ADD_GROUP("Alignment", "alignment_");
  145. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
  146. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");
  147. BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
  148. BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
  149. BIND_ENUM_CONSTANT(STRETCH_FIT);
  150. BIND_ENUM_CONSTANT(STRETCH_COVER);
  151. BIND_ENUM_CONSTANT(ALIGN_BEGIN);
  152. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  153. BIND_ENUM_CONSTANT(ALIGN_END);
  154. }