2
0

Box.cpp 3.7 KB

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