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.

50 lines
998B

  1. #pragma once
  2. #include "widget/Widget.hpp"
  3. namespace rack {
  4. namespace widget {
  5. /** Transforms appearance only, not positions of events */
  6. struct TransformWidget : Widget {
  7. /** The transformation matrix */
  8. float transform[6];
  9. TransformWidget() {
  10. identity();
  11. }
  12. void identity() {
  13. nvgTransformIdentity(transform);
  14. }
  15. void translate(math::Vec delta) {
  16. float t[6];
  17. nvgTransformTranslate(t, delta.x, delta.y);
  18. nvgTransformPremultiply(transform, t);
  19. }
  20. void rotate(float angle) {
  21. float t[6];
  22. nvgTransformRotate(t, angle);
  23. nvgTransformPremultiply(transform, t);
  24. }
  25. void scale(math::Vec s) {
  26. float t[6];
  27. nvgTransformScale(t, s.x, s.y);
  28. nvgTransformPremultiply(transform, t);
  29. }
  30. void draw(const DrawContext &ctx) override {
  31. // No need to save the state because that is done in the parent
  32. nvgTransform(ctx.vg, transform[0], transform[1], transform[2], transform[3], transform[4], transform[5]);
  33. Widget::draw(ctx);
  34. }
  35. };
  36. } // namespace widget
  37. } // namespace rack