Geometry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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-2023 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/Core.h"
  31. #include "../../Include/RmlUi/Core/Geometry.h"
  32. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  33. #include "../../Include/RmlUi/Core/RenderManager.h"
  34. namespace Rml {
  35. namespace Debugger {
  36. static Context* g_context = nullptr;
  37. static RenderManager* g_render_manager = nullptr;
  38. Geometry::Geometry() {}
  39. void Geometry::SetContext(Context* context)
  40. {
  41. g_context = context;
  42. g_render_manager = context ? &context->GetRenderManager() : nullptr;
  43. }
  44. void Geometry::RenderOutline(const Vector2f origin, const Vector2f dimensions, const Colourb colour, float width)
  45. {
  46. if (!g_context || !g_render_manager)
  47. return;
  48. Mesh mesh;
  49. mesh.vertices.reserve(4 * 4);
  50. mesh.indices.reserve(6 * 4);
  51. ColourbPremultiplied colour_pre = colour.ToPremultiplied();
  52. MeshUtilities::GenerateQuad(mesh, Vector2f(0, 0), Vector2f(dimensions.x, width), colour_pre);
  53. MeshUtilities::GenerateQuad(mesh, Vector2f(0, dimensions.y - width), Vector2f(dimensions.x, width), colour_pre);
  54. MeshUtilities::GenerateQuad(mesh, Vector2f(0, 0), Vector2f(width, dimensions.y), colour_pre);
  55. MeshUtilities::GenerateQuad(mesh, Vector2f(dimensions.x - width, 0), Vector2f(width, dimensions.y), colour_pre);
  56. g_render_manager->MakeGeometry(std::move(mesh)).Render(origin);
  57. }
  58. void Geometry::RenderBox(const Vector2f origin, const Vector2f dimensions, const Colourb colour)
  59. {
  60. if (!g_context || !g_render_manager)
  61. return;
  62. Mesh mesh;
  63. MeshUtilities::GenerateQuad(mesh, Vector2f(0, 0), Vector2f(dimensions.x, dimensions.y), colour.ToPremultiplied());
  64. g_render_manager->MakeGeometry(std::move(mesh)).Render(origin);
  65. }
  66. void Geometry::RenderBox(const Vector2f origin, const Vector2f dimensions, const Vector2f hole_origin, const Vector2f hole_dimensions,
  67. const Colourb colour)
  68. {
  69. // Top box.
  70. float top_y_dimensions = hole_origin.y - origin.y;
  71. if (top_y_dimensions > 0)
  72. {
  73. RenderBox(origin, Vector2f(dimensions.x, top_y_dimensions), colour);
  74. }
  75. // Bottom box.
  76. float bottom_y_dimensions = (origin.y + dimensions.y) - (hole_origin.y + hole_dimensions.y);
  77. if (bottom_y_dimensions > 0)
  78. {
  79. RenderBox(Vector2f(origin.x, hole_origin.y + hole_dimensions.y), Vector2f(dimensions.x, bottom_y_dimensions), colour);
  80. }
  81. // Left box.
  82. float left_x_dimensions = hole_origin.x - origin.x;
  83. if (left_x_dimensions > 0)
  84. {
  85. RenderBox(Vector2f(origin.x, hole_origin.y), Vector2f(left_x_dimensions, hole_dimensions.y), colour);
  86. }
  87. // Right box.
  88. float right_x_dimensions = (origin.x + dimensions.x) - (hole_origin.x + hole_dimensions.x);
  89. if (right_x_dimensions > 0)
  90. {
  91. RenderBox(Vector2f(hole_origin.x + hole_dimensions.x, hole_origin.y), Vector2f(right_x_dimensions, hole_dimensions.y), colour);
  92. }
  93. }
  94. } // namespace Debugger
  95. } // namespace Rml