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.

113 lines
3.5KB

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