Box.cpp 3.8 KB

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