center_container.cpp 3.7 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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "center_container.h"
  31. Size2 CenterContainer::get_minimum_size() const {
  32. if (use_top_left)
  33. return Size2();
  34. Size2 ms;
  35. for (int i = 0; i < get_child_count(); i++) {
  36. Control *c = Object::cast_to<Control>(get_child(i));
  37. if (!c)
  38. continue;
  39. if (c->is_set_as_toplevel())
  40. continue;
  41. if (!c->is_visible())
  42. continue;
  43. Size2 minsize = c->get_combined_minimum_size();
  44. ms.width = MAX(ms.width, minsize.width);
  45. ms.height = MAX(ms.height, minsize.height);
  46. }
  47. return ms;
  48. }
  49. void CenterContainer::set_use_top_left(bool p_enable) {
  50. use_top_left = p_enable;
  51. queue_sort();
  52. }
  53. bool CenterContainer::is_using_top_left() const {
  54. return use_top_left;
  55. }
  56. void CenterContainer::_notification(int p_what) {
  57. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  58. Size2 size = get_size();
  59. for (int i = 0; i < get_child_count(); i++) {
  60. Control *c = Object::cast_to<Control>(get_child(i));
  61. if (!c)
  62. continue;
  63. if (c->is_set_as_toplevel())
  64. continue;
  65. Size2 minsize = c->get_combined_minimum_size();
  66. Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor();
  67. fit_child_in_rect(c, Rect2(ofs, minsize));
  68. }
  69. }
  70. }
  71. void CenterContainer::_bind_methods() {
  72. ClassDB::bind_method(D_METHOD("set_use_top_left", "enable"), &CenterContainer::set_use_top_left);
  73. ClassDB::bind_method(D_METHOD("is_using_top_left"), &CenterContainer::is_using_top_left);
  74. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left"), "set_use_top_left", "is_using_top_left");
  75. }
  76. CenterContainer::CenterContainer() {
  77. use_top_left = false;
  78. }