Box.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREBOX_H
  28. #define ROCKETCOREBOX_H
  29. //#include <Rocket/Core/Header.h>
  30. //#include <Rocket/Core/Types.h>
  31. #include <Rocket/Core/Types.h>
  32. namespace Rocket {
  33. namespace Core {
  34. /**
  35. Stores a box with four sized areas; content, padding, a border and margin. See
  36. http://www.w3.org/TR/REC-CSS2/box.html#box-dimensions for a diagram.
  37. @author Peter Curry
  38. */
  39. class ROCKETCORE_API Box
  40. {
  41. public:
  42. enum Area
  43. {
  44. MARGIN = 0,
  45. BORDER = 1,
  46. PADDING = 2,
  47. CONTENT = 3,
  48. NUM_AREAS = 3, // ignores CONTENT
  49. };
  50. enum Edge
  51. {
  52. TOP = 0,
  53. RIGHT = 1,
  54. BOTTOM = 2,
  55. LEFT = 3,
  56. NUM_EDGES = 4
  57. };
  58. /// Initialises a zero-sized box.
  59. Box();
  60. /// Initialises a box with a default content area and no padding, borders and margins.
  61. Box(const Vector2f& content);
  62. ~Box();
  63. /// Returns the offset of this box. This will usually be (0, 0).
  64. /// @return The box's offset.
  65. const Vector2f& GetOffset() const;
  66. /// Returns the top-left position of one of the box's areas, relative to the top-left of the border area. This
  67. /// means the position of the margin area is likely to be negative.
  68. /// @param area[in] The desired area.
  69. /// @return The position of the area.
  70. Vector2f GetPosition(Area area = Box::CONTENT) const;
  71. /// Returns the size of one of the box's areas. This will include all inner areas.
  72. /// @param area[in] The desired area.
  73. /// @return The size of the requested area.
  74. Vector2f GetSize(Area area = Box::CONTENT) const;
  75. /// Sets the offset of the box, relative usually to the owning element. This should only be set for auxiliary
  76. /// boxes of an element.
  77. /// @param offset[in] The offset of the box from the primary box.
  78. void SetOffset(const Vector2f& offset);
  79. /// Sets the size of the content area.
  80. /// @param content[in] The size of the new content area.
  81. void SetContent(const Vector2f& content);
  82. /// Sets the size of one of the edges of one of the box's outer areas.
  83. /// @param area[in] The area to change.
  84. /// @param edge[in] The area edge to change.
  85. /// @param size[in] The new size of the area segment.
  86. void SetEdge(Area area, Edge edge, float size);
  87. /// Returns the size of one of the area edges.
  88. /// @param area[in] The desired area.
  89. /// @param edge[in] The desired edge.
  90. /// @return The size of the requested area edge.
  91. float GetEdge(Area area, Edge edge) const;
  92. /// Returns the cumulative size of one edge up to one of the box's areas.
  93. /// @param area[in] The area to measure up to (and including). So, MARGIN will return the width of the margin, and PADDING will be the sum of the margin, border and padding.
  94. /// @param edge[in] The desired edge.
  95. /// @return The cumulative size of the edge.
  96. float GetCumulativeEdge(Area area, Edge edge) const;
  97. /// Compares the size of the content area and the other area edges.
  98. /// @return True if the boxes represent the same area.
  99. bool operator==(const Box& rhs) const;
  100. /// Compares the size of the content area and the other area edges.
  101. /// @return True if the boxes do not represent the same area.
  102. bool operator!=(const Box& rhs) const;
  103. private:
  104. Vector2f content;
  105. float area_edges[NUM_AREAS][NUM_EDGES];
  106. Vector2f offset;
  107. };
  108. }
  109. }
  110. #endif