Box.cpp 3.7 KB

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