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.

SequentialLayout.hpp 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "widgets/Widget.hpp"
  3. #include "ui/common.hpp"
  4. namespace rack {
  5. /** Positions children in a row/column based on their widths/heights */
  6. struct SequentialLayout : VirtualWidget {
  7. enum Orientation {
  8. HORIZONTAL_ORIENTATION,
  9. VERTICAL_ORIENTATION,
  10. };
  11. Orientation orientation = HORIZONTAL_ORIENTATION;
  12. enum Alignment {
  13. LEFT_ALIGNMENT,
  14. CENTER_ALIGNMENT,
  15. RIGHT_ALIGNMENT,
  16. };
  17. Alignment alignment = LEFT_ALIGNMENT;
  18. /** Space between adjacent elements */
  19. float spacing = 0.0;
  20. void step() override {
  21. Widget::step();
  22. float offset = 0.0;
  23. for (Widget *child : children) {
  24. if (!child->visible)
  25. continue;
  26. // Set position
  27. (orientation == HORIZONTAL_ORIENTATION ? child->box.pos.x : child->box.pos.y) = offset;
  28. // Increment by size
  29. offset += (orientation == HORIZONTAL_ORIENTATION ? child->box.size.x : child->box.size.y);
  30. offset += spacing;
  31. }
  32. // We're done if left aligned
  33. if (alignment == LEFT_ALIGNMENT)
  34. return;
  35. // Adjust positions based on width of the layout itself
  36. offset -= spacing;
  37. if (alignment == RIGHT_ALIGNMENT)
  38. offset -= (orientation == HORIZONTAL_ORIENTATION ? box.size.x : box.size.y);
  39. else if (alignment == CENTER_ALIGNMENT)
  40. offset -= (orientation == HORIZONTAL_ORIENTATION ? box.size.x : box.size.y) / 2.0;
  41. for (Widget *child : children) {
  42. if (!child->visible)
  43. continue;
  44. (orientation == HORIZONTAL_ORIENTATION ? child->box.pos.x : child->box.pos.y) += offset;
  45. }
  46. }
  47. };
  48. } // namespace rack