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.

TransformWidget.hpp 958B

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