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.

108 lines
3.4KB

  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. Box2DRenderer::Box2DRenderer() noexcept : graphics (nullptr)
  20. {
  21. SetFlags (e_shapeBit);
  22. }
  23. void Box2DRenderer::render (Graphics& g, b2World& world,
  24. float left, float top, float right, float bottom,
  25. const Rectangle<float>& target)
  26. {
  27. graphics = &g;
  28. g.addTransform (AffineTransform::fromTargetPoints (left, top, target.getX(), target.getY(),
  29. right, top, target.getRight(), target.getY(),
  30. left, bottom, target.getX(), target.getBottom()));
  31. world.SetDebugDraw (this);
  32. world.DrawDebugData();
  33. }
  34. Colour Box2DRenderer::getColour (const b2Color& c) const
  35. {
  36. return Colour::fromFloatRGBA (c.r, c.g, c.b, 1.0f);
  37. }
  38. float Box2DRenderer::getLineThickness() const
  39. {
  40. return 0.1f;
  41. }
  42. static void createPath (Path& p, const b2Vec2* vertices, int32 vertexCount)
  43. {
  44. p.startNewSubPath (vertices[0].x, vertices[0].y);
  45. for (int i = 1; i < vertexCount; ++i)
  46. p.lineTo (vertices[i].x, vertices[i].y);
  47. p.closeSubPath();
  48. }
  49. void Box2DRenderer::DrawPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  50. {
  51. graphics->setColour (getColour (color));
  52. Path p;
  53. createPath (p, vertices, vertexCount);
  54. graphics->strokePath (p, PathStrokeType (getLineThickness()));
  55. }
  56. void Box2DRenderer::DrawSolidPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  57. {
  58. graphics->setColour (getColour (color));
  59. Path p;
  60. createPath (p, vertices, vertexCount);
  61. graphics->fillPath (p);
  62. }
  63. void Box2DRenderer::DrawCircle (const b2Vec2& center, float32 radius, const b2Color& color)
  64. {
  65. graphics->setColour (getColour (color));
  66. graphics->drawEllipse (center.x - radius, center.y - radius,
  67. radius * 2.0f, radius * 2.0f,
  68. getLineThickness());
  69. }
  70. void Box2DRenderer::DrawSolidCircle (const b2Vec2& center, float32 radius, const b2Vec2& /*axis*/, const b2Color& colour)
  71. {
  72. graphics->setColour (getColour (colour));
  73. graphics->fillEllipse (center.x - radius, center.y - radius,
  74. radius * 2.0f, radius * 2.0f);
  75. }
  76. void Box2DRenderer::DrawSegment (const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
  77. {
  78. graphics->setColour (getColour (color));
  79. graphics->drawLine (p1.x, p1.y, p2.x, p2.y, getLineThickness());
  80. }
  81. void Box2DRenderer::DrawTransform (const b2Transform&)
  82. {
  83. }