Geometry.cpp 4.1 KB

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