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.

59 lines
1.4KB

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