Geometry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "Geometry.h"
  28. #include <Rocket/Core.h>
  29. namespace Rocket {
  30. namespace Debugger {
  31. static Core::Context* context;
  32. Geometry::Geometry()
  33. {
  34. }
  35. void Geometry::SetContext(Core::Context* _context)
  36. {
  37. context = _context;
  38. }
  39. // Renders a one-pixel rectangular outline.
  40. void Geometry::RenderOutline(const Core::Vector2f& origin, const Core::Vector2f& dimensions, const Core::Colourb& colour, float width)
  41. {
  42. if (context == NULL)
  43. return;
  44. Core::RenderInterface* render_interface = context->GetRenderInterface();
  45. Core::Vertex vertices[4 * 4];
  46. int indices[6 * 4];
  47. Core::GeometryUtilities::GenerateQuad(vertices + 0, indices + 0, Core::Vector2f(0, 0), Core::Vector2f(dimensions.x, width), colour, 0);
  48. Core::GeometryUtilities::GenerateQuad(vertices + 4, indices + 6, Core::Vector2f(0, dimensions.y - width), Core::Vector2f(dimensions.x, width), colour, 4);
  49. Core::GeometryUtilities::GenerateQuad(vertices + 8, indices + 12, Core::Vector2f(0, 0), Core::Vector2f(width, dimensions.y), colour, 8);
  50. Core::GeometryUtilities::GenerateQuad(vertices + 12, indices + 18, Core::Vector2f(dimensions.x - width, 0), Core::Vector2f(width, dimensions.y), colour, 12);
  51. render_interface->RenderGeometry(vertices, 4 * 4, indices, 6 * 4, 0, origin);
  52. }
  53. // Renders a box.
  54. void Geometry::RenderBox(const Core::Vector2f& origin, const Core::Vector2f& dimensions, const Core::Colourb& colour)
  55. {
  56. if (context == NULL)
  57. return;
  58. Core::RenderInterface* render_interface = context->GetRenderInterface();
  59. Core::Vertex vertices[4];
  60. int indices[6];
  61. Core::GeometryUtilities::GenerateQuad(vertices, indices, Core::Vector2f(0, 0), Core::Vector2f(dimensions.x, dimensions.y), colour, 0);
  62. render_interface->RenderGeometry(vertices, 4, indices, 6, 0, origin);
  63. }
  64. // Renders a box with a hole in the middle.
  65. void Geometry::RenderBox(const Core::Vector2f& origin, const Core::Vector2f& dimensions, const Core::Vector2f& hole_origin, const Core::Vector2f& hole_dimensions, const Core::Colourb& colour)
  66. {
  67. // Top box.
  68. float top_y_dimensions = hole_origin.y - origin.y;
  69. if (top_y_dimensions > 0)
  70. {
  71. RenderBox(origin, Core::Vector2f(dimensions.x, top_y_dimensions), colour);
  72. }
  73. // Bottom box.
  74. float bottom_y_dimensions = (origin.y + dimensions.y) - (hole_origin.y + hole_dimensions.y);
  75. if (bottom_y_dimensions > 0)
  76. {
  77. RenderBox(Core::Vector2f(origin.x, hole_origin.y + hole_dimensions.y), Core::Vector2f(dimensions.x, bottom_y_dimensions), colour);
  78. }
  79. // Left box.
  80. float left_x_dimensions = hole_origin.x - origin.x;
  81. if (left_x_dimensions > 0)
  82. {
  83. RenderBox(Core::Vector2f(origin.x, hole_origin.y), Core::Vector2f(left_x_dimensions, hole_dimensions.y), colour);
  84. }
  85. // Right box.
  86. float right_x_dimensions = (origin.x + dimensions.x) - (hole_origin.x + hole_dimensions.x);
  87. if (right_x_dimensions > 0)
  88. {
  89. RenderBox(Core::Vector2f(hole_origin.x + hole_dimensions.x, hole_origin.y), Core::Vector2f(right_x_dimensions, hole_dimensions.y), colour);
  90. }
  91. }
  92. }
  93. }