center_container.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**************************************************************************/
  2. /* center_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 "center_container.h"
  31. Size2 CenterContainer::get_minimum_size() const {
  32. if (use_top_left) {
  33. return Size2();
  34. }
  35. Size2 ms;
  36. for (int i = 0; i < get_child_count(); i++) {
  37. Control *c = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  38. if (!c) {
  39. continue;
  40. }
  41. Size2 minsize = c->get_combined_minimum_size();
  42. ms = ms.max(minsize);
  43. }
  44. return ms;
  45. }
  46. void CenterContainer::set_use_top_left(bool p_enable) {
  47. if (use_top_left == p_enable) {
  48. return;
  49. }
  50. use_top_left = p_enable;
  51. update_minimum_size();
  52. queue_sort();
  53. }
  54. bool CenterContainer::is_using_top_left() const {
  55. return use_top_left;
  56. }
  57. Vector<int> CenterContainer::get_allowed_size_flags_horizontal() const {
  58. return Vector<int>();
  59. }
  60. Vector<int> CenterContainer::get_allowed_size_flags_vertical() const {
  61. return Vector<int>();
  62. }
  63. void CenterContainer::_notification(int p_what) {
  64. switch (p_what) {
  65. case NOTIFICATION_SORT_CHILDREN: {
  66. Size2 size = get_size();
  67. for (int i = 0; i < get_child_count(); i++) {
  68. Control *c = as_sortable_control(get_child(i));
  69. if (!c) {
  70. continue;
  71. }
  72. Size2 minsize = c->get_combined_minimum_size();
  73. Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor();
  74. fit_child_in_rect(c, Rect2(ofs, minsize));
  75. }
  76. } break;
  77. }
  78. }
  79. void CenterContainer::_bind_methods() {
  80. ClassDB::bind_method(D_METHOD("set_use_top_left", "enable"), &CenterContainer::set_use_top_left);
  81. ClassDB::bind_method(D_METHOD("is_using_top_left"), &CenterContainer::is_using_top_left);
  82. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left"), "set_use_top_left", "is_using_top_left");
  83. }
  84. CenterContainer::CenterContainer() {}