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 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, VEC_ARGS(delta));
  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 rotate(float angle, math::Vec origin) {
  26. translate(origin);
  27. rotate(angle);
  28. translate(origin.neg());
  29. }
  30. void scale(math::Vec s) {
  31. float t[6];
  32. nvgTransformScale(t, s.x, s.y);
  33. nvgTransformPremultiply(transform, t);
  34. }
  35. void draw(const DrawArgs& args) override {
  36. // No need to save the state because that is done in the parent
  37. nvgTransform(args.vg, transform[0], transform[1], transform[2], transform[3], transform[4], transform[5]);
  38. Widget::draw(args);
  39. }
  40. void drawLayer(const DrawArgs& args, int layer) override {
  41. // No need to save the state because that is done in the parent
  42. nvgTransform(args.vg, transform[0], transform[1], transform[2], transform[3], transform[4], transform[5]);
  43. Widget::drawLayer(args, layer);
  44. }
  45. };
  46. } // namespace widget
  47. } // namespace rack