blob.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. class Painter;
  9. namespace Ui::Paint {
  10. class Blob {
  11. public:
  12. struct Radiuses {
  13. float min = 0.;
  14. float max = 0.;
  15. };
  16. Blob(int n, float minSpeed = 0, float maxSpeed = 0);
  17. virtual ~Blob() = default;
  18. void update(float level, float speedScale, float64 rate);
  19. void generateBlob();
  20. void setRadiuses(Radiuses values);
  21. [[nodiscard]] Radiuses radiuses() const;
  22. protected:
  23. struct TwoValues {
  24. float current = 0.;
  25. float next = 0.;
  26. void setNext(float v) {
  27. current = next;
  28. next = v;
  29. }
  30. };
  31. struct Segment {
  32. float progress = 0.;
  33. float speed = 0.;
  34. };
  35. void generateSingleValues(int i);
  36. virtual void generateTwoValues(int i) = 0;
  37. virtual Segment &segmentAt(int i) = 0;
  38. const int _segmentsCount;
  39. const float _minSpeed;
  40. const float _maxSpeed;
  41. const QPen _pen;
  42. Radiuses _radiuses;
  43. };
  44. class RadialBlob final : public Blob {
  45. public:
  46. RadialBlob(int n, float minScale, float minSpeed = 0, float maxSpeed = 0);
  47. void paint(QPainter &p, const QBrush &brush, float outerScale = 1.);
  48. void update(float level, float speedScale, float64 rate);
  49. private:
  50. struct Segment : Blob::Segment {
  51. Blob::TwoValues radius;
  52. Blob::TwoValues angle;
  53. };
  54. void generateTwoValues(int i) override;
  55. Blob::Segment &segmentAt(int i) override;
  56. const float64 _segmentLength;
  57. const float _minScale;
  58. const float _segmentAngle;
  59. const float _angleDiff;
  60. std::vector<Segment> _segments;
  61. float64 _scale = 0;
  62. };
  63. class LinearBlob final : public Blob {
  64. public:
  65. enum class Direction {
  66. TopDown,
  67. BottomUp,
  68. };
  69. LinearBlob(
  70. int n,
  71. Direction direction = Direction::TopDown,
  72. float minSpeed = 0,
  73. float maxSpeed = 0);
  74. void paint(QPainter &p, const QBrush &brush, int width);
  75. private:
  76. struct Segment : Blob::Segment {
  77. Blob::TwoValues radius;
  78. };
  79. void generateTwoValues(int i) override;
  80. Blob::Segment &segmentAt(int i) override;
  81. const int _topDown;
  82. std::vector<Segment> _segments;
  83. };
  84. } // namespace Ui::Paint