Box.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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. // Returns the top-left position of one of the areas.
  35. Vector2f Box::GetPosition(Area area) const
  36. {
  37. Vector2f area_position(-area_edges[MARGIN][LEFT], -area_edges[MARGIN][TOP]);
  38. for (int i = 0; i < area; i++)
  39. {
  40. area_position.x += area_edges[i][LEFT];
  41. area_position.y += area_edges[i][TOP];
  42. }
  43. return area_position;
  44. }
  45. Vector2f Box::GetSize() const
  46. {
  47. return content;
  48. }
  49. Vector2f Box::GetSize(Area area) const
  50. {
  51. Vector2f area_size(content);
  52. for (int i = area; i <= PADDING; i++)
  53. {
  54. area_size.x += (area_edges[i][LEFT] + area_edges[i][RIGHT]);
  55. area_size.y += (area_edges[i][TOP] + area_edges[i][BOTTOM]);
  56. }
  57. return area_size;
  58. }
  59. // Sets the size of the content area.
  60. void Box::SetContent(Vector2f _content)
  61. {
  62. content = _content;
  63. }
  64. // Sets the size of one of the segments of one of the box's outer areas.
  65. void Box::SetEdge(Area area, Edge edge, float size)
  66. {
  67. area_edges[area][edge] = size;
  68. }
  69. // Returns the size of one of the area segments.
  70. float Box::GetEdge(Area area, Edge edge) const
  71. {
  72. return area_edges[area][edge];
  73. }
  74. // Returns the cumulative size of one edge up to one of the box's areas.
  75. float Box::GetCumulativeEdge(Area area, Edge edge) const
  76. {
  77. float size = 0;
  78. int max_area = Math::Min((int)area, (int)PADDING);
  79. for (int i = 0; i <= max_area; i++)
  80. size += area_edges[i][edge];
  81. return size;
  82. }
  83. float Box::GetSizeAcross(Direction direction, Area area_outer, Area area_inner) const
  84. {
  85. static_assert(HORIZONTAL == 1 && VERTICAL == 0, "");
  86. RMLUI_ASSERT(area_outer <= area_inner && direction <= 1);
  87. float size = 0.0f;
  88. if (area_inner == CONTENT)
  89. size = (direction == HORIZONTAL ? content.x : content.y);
  90. for (int i = area_outer; i <= area_inner && i < CONTENT; i++)
  91. size += (area_edges[i][TOP + (int)direction] + area_edges[i][BOTTOM + (int)direction]);
  92. return size;
  93. }
  94. Vector2f Box::GetFrameSize(Area area) const
  95. {
  96. if (area == CONTENT)
  97. return content;
  98. return {
  99. area_edges[area][RIGHT] + area_edges[area][LEFT],
  100. area_edges[area][TOP] + area_edges[area][BOTTOM],
  101. };
  102. }
  103. // Compares the size of the content area and the other area edges.
  104. bool Box::operator==(const Box& rhs) const
  105. {
  106. return content == rhs.content && memcmp(area_edges, rhs.area_edges, sizeof(area_edges)) == 0;
  107. }
  108. // Compares the size of the content area and the other area edges.
  109. bool Box::operator!=(const Box& rhs) const
  110. {
  111. return !(*this == rhs);
  112. }
  113. } // namespace Rml