Box.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Box.h"
  29. #include <string.h>
  30. namespace Rml {
  31. Box::Box() {}
  32. Box::Box(Vector2f content) : content(content) {}
  33. Box::~Box() {}
  34. Vector2f Box::GetPosition(BoxArea area) const
  35. {
  36. RMLUI_ASSERT(area != BoxArea::Auto);
  37. Vector2f area_position(-GetEdge(BoxArea::Margin, BoxEdge::Left), -GetEdge(BoxArea::Margin, BoxEdge::Top));
  38. for (int i = 0; i < (int)area; i++)
  39. {
  40. area_position.x += area_edges[i][(int)BoxEdge::Left];
  41. area_position.y += area_edges[i][(int)BoxEdge::Top];
  42. }
  43. return area_position;
  44. }
  45. Vector2f Box::GetSize() const
  46. {
  47. return content;
  48. }
  49. Vector2f Box::GetSize(BoxArea area) const
  50. {
  51. RMLUI_ASSERT(area != BoxArea::Auto);
  52. Vector2f area_size(content);
  53. for (int i = (int)area; i <= (int)BoxArea::Padding; i++)
  54. {
  55. area_size.x += (area_edges[i][(int)BoxEdge::Left] + area_edges[i][(int)BoxEdge::Right]);
  56. area_size.y += (area_edges[i][(int)BoxEdge::Top] + area_edges[i][(int)BoxEdge::Bottom]);
  57. }
  58. return area_size;
  59. }
  60. void Box::SetContent(Vector2f _content)
  61. {
  62. content = _content;
  63. }
  64. void Box::SetEdge(BoxArea area, BoxEdge edge, float size)
  65. {
  66. RMLUI_ASSERT(area != BoxArea::Auto);
  67. area_edges[(int)area][(int)edge] = size;
  68. }
  69. float Box::GetEdge(BoxArea area, BoxEdge edge) const
  70. {
  71. RMLUI_ASSERT(area != BoxArea::Auto);
  72. return area_edges[(int)area][(int)edge];
  73. }
  74. float Box::GetCumulativeEdge(BoxArea area, BoxEdge edge) const
  75. {
  76. RMLUI_ASSERT(area != BoxArea::Auto);
  77. float size = 0;
  78. int max_area = Math::Min((int)area, (int)BoxArea::Padding);
  79. for (int i = 0; i <= max_area; i++)
  80. size += area_edges[i][(int)edge];
  81. return size;
  82. }
  83. float Box::GetSizeAcross(BoxDirection direction, BoxArea area_outer, BoxArea area_inner) const
  84. {
  85. static_assert((int)BoxDirection::Horizontal == 1 && (int)BoxDirection::Vertical == 0, "");
  86. RMLUI_ASSERT((int)area_outer <= (int)area_inner && (int)direction <= 1 && area_inner != BoxArea::Auto);
  87. float size = 0.0f;
  88. if (area_inner == BoxArea::Content)
  89. size = (direction == BoxDirection::Horizontal ? content.x : content.y);
  90. for (int i = (int)area_outer; i <= (int)area_inner && i < (int)BoxArea::Content; i++)
  91. size += (area_edges[i][(int)BoxEdge::Top + (int)direction] + area_edges[i][(int)BoxEdge::Bottom + (int)direction]);
  92. return size;
  93. }
  94. Vector2f Box::GetFrameSize(BoxArea area) const
  95. {
  96. if (area == BoxArea::Content)
  97. return content;
  98. return {
  99. area_edges[(int)area][(int)BoxEdge::Right] + area_edges[(int)area][(int)BoxEdge::Left],
  100. area_edges[(int)area][(int)BoxEdge::Top] + area_edges[(int)area][(int)BoxEdge::Bottom],
  101. };
  102. }
  103. bool Box::operator==(const Box& rhs) const
  104. {
  105. return content == rhs.content && memcmp(area_edges, rhs.area_edges, sizeof(area_edges)) == 0;
  106. }
  107. bool Box::operator!=(const Box& rhs) const
  108. {
  109. return !(*this == rhs);
  110. }
  111. } // namespace Rml