aspect_ratio_container.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_toplevel()) {
  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. Size2 size = get_size();
  70. for (int i = 0; i < get_child_count(); i++) {
  71. Control *c = Object::cast_to<Control>(get_child(i));
  72. if (!c) {
  73. continue;
  74. }
  75. if (c->is_set_as_toplevel()) {
  76. continue;
  77. }
  78. Size2 child_minsize = c->get_combined_minimum_size();
  79. Size2 child_size = Size2(ratio, 1.0);
  80. float scale_factor = 1.0;
  81. switch (stretch_mode) {
  82. case STRETCH_WIDTH_CONTROLS_HEIGHT: {
  83. scale_factor = size.x / child_size.x;
  84. } break;
  85. case STRETCH_HEIGHT_CONTROLS_WIDTH: {
  86. scale_factor = size.y / child_size.y;
  87. } break;
  88. case STRETCH_FIT: {
  89. scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
  90. } break;
  91. case STRETCH_COVER: {
  92. scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
  93. } break;
  94. }
  95. child_size *= scale_factor;
  96. child_size.x = MAX(child_size.x, child_minsize.x);
  97. child_size.y = MAX(child_size.y, child_minsize.y);
  98. float align_x = 0.5;
  99. switch (alignment_horizontal) {
  100. case ALIGN_BEGIN: {
  101. align_x = 0.0;
  102. } break;
  103. case ALIGN_CENTER: {
  104. align_x = 0.5;
  105. } break;
  106. case ALIGN_END: {
  107. align_x = 1.0;
  108. } break;
  109. }
  110. float align_y = 0.5;
  111. switch (alignment_vertical) {
  112. case ALIGN_BEGIN: {
  113. align_y = 0.0;
  114. } break;
  115. case ALIGN_CENTER: {
  116. align_y = 0.5;
  117. } break;
  118. case ALIGN_END: {
  119. align_y = 1.0;
  120. } break;
  121. }
  122. Vector2 offset = (size - child_size) * Vector2(align_x, align_y);
  123. fit_child_in_rect(c, Rect2(offset, child_size));
  124. }
  125. } break;
  126. }
  127. }
  128. void AspectRatioContainer::_bind_methods() {
  129. ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
  130. ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);
  131. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
  132. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);
  133. ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
  134. ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);
  135. ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
  136. ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);
  137. ADD_PROPERTY(PropertyInfo(Variant::REAL, "ratio"), "set_ratio", "get_ratio");
  138. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width controls height,Height controls width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");
  139. ADD_GROUP("Alignment", "alignment_");
  140. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
  141. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");
  142. BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
  143. BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
  144. BIND_ENUM_CONSTANT(STRETCH_FIT);
  145. BIND_ENUM_CONSTANT(STRETCH_COVER);
  146. BIND_ENUM_CONSTANT(ALIGN_BEGIN);
  147. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  148. BIND_ENUM_CONSTANT(ALIGN_END);
  149. }