The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. Box2DRenderer::Box2DRenderer() noexcept : graphics (nullptr)
  21. {
  22. SetFlags (e_shapeBit);
  23. }
  24. void Box2DRenderer::render (Graphics& g, b2World& world,
  25. float left, float top, float right, float bottom,
  26. const Rectangle<float>& target)
  27. {
  28. graphics = &g;
  29. g.addTransform (AffineTransform::fromTargetPoints (left, top, target.getX(), target.getY(),
  30. right, top, target.getRight(), target.getY(),
  31. left, bottom, target.getX(), target.getBottom()));
  32. world.SetDebugDraw (this);
  33. world.DrawDebugData();
  34. }
  35. Colour Box2DRenderer::getColour (const b2Color& c) const
  36. {
  37. return Colour::fromFloatRGBA (c.r, c.g, c.b, 1.0f);
  38. }
  39. float Box2DRenderer::getLineThickness() const
  40. {
  41. return 0.1f;
  42. }
  43. static void createPath (Path& p, const b2Vec2* vertices, int32 vertexCount)
  44. {
  45. p.startNewSubPath (vertices[0].x, vertices[0].y);
  46. for (int i = 1; i < vertexCount; ++i)
  47. p.lineTo (vertices[i].x, vertices[i].y);
  48. p.closeSubPath();
  49. }
  50. void Box2DRenderer::DrawPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  51. {
  52. graphics->setColour (getColour (color));
  53. Path p;
  54. createPath (p, vertices, vertexCount);
  55. graphics->strokePath (p, PathStrokeType (getLineThickness()));
  56. }
  57. void Box2DRenderer::DrawSolidPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  58. {
  59. graphics->setColour (getColour (color));
  60. Path p;
  61. createPath (p, vertices, vertexCount);
  62. graphics->fillPath (p);
  63. }
  64. void Box2DRenderer::DrawCircle (const b2Vec2& center, float32 radius, const b2Color& color)
  65. {
  66. graphics->setColour (getColour (color));
  67. graphics->drawEllipse (center.x - radius, center.y - radius,
  68. radius * 2.0f, radius * 2.0f,
  69. getLineThickness());
  70. }
  71. void Box2DRenderer::DrawSolidCircle (const b2Vec2& center, float32 radius, const b2Vec2& /*axis*/, const b2Color& colour)
  72. {
  73. graphics->setColour (getColour (colour));
  74. graphics->fillEllipse (center.x - radius, center.y - radius,
  75. radius * 2.0f, radius * 2.0f);
  76. }
  77. void Box2DRenderer::DrawSegment (const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
  78. {
  79. graphics->setColour (getColour (color));
  80. graphics->drawLine (p1.x, p1.y, p2.x, p2.y, getLineThickness());
  81. }
  82. void Box2DRenderer::DrawTransform (const b2Transform&)
  83. {
  84. }
  85. } // namespace juce