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.

105 lines
3.2KB

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