spoiler_mess.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <crl/crl_time.h>
  9. namespace Images {
  10. struct CornersMaskRef;
  11. } // namespace Images
  12. namespace Ui {
  13. struct SpoilerMessDescriptor {
  14. crl::time particleFadeInDuration = 0;
  15. crl::time particleShownDuration = 0;
  16. crl::time particleFadeOutDuration = 0;
  17. float64 particleSizeMin = 0.;
  18. float64 particleSizeMax = 0.;
  19. float64 particleSpeedMin = 0.;
  20. float64 particleSpeedMax = 0.;
  21. int particleSpritesCount = 0;
  22. int particlesCount = 0;
  23. int canvasSize = 0;
  24. int framesCount = 0;
  25. crl::time frameDuration = 0;
  26. };
  27. struct SpoilerMessFrame {
  28. not_null<const QImage*> image;
  29. QRect source;
  30. };
  31. void FillSpoilerRect(
  32. QPainter &p,
  33. QRect rect,
  34. const SpoilerMessFrame &frame,
  35. QPoint originShift = {});
  36. void FillSpoilerRect(
  37. QPainter &p,
  38. QRect rect,
  39. Images::CornersMaskRef mask,
  40. const SpoilerMessFrame &frame,
  41. QImage &cornerCache,
  42. QPoint originShift = {});
  43. class SpoilerMessCached final {
  44. public:
  45. SpoilerMessCached(
  46. QImage image,
  47. int framesCount,
  48. crl::time frameDuration,
  49. int canvasSize);
  50. SpoilerMessCached(const SpoilerMessCached &mask, const QColor &color);
  51. [[nodiscard]] SpoilerMessFrame frame(int index) const;
  52. [[nodiscard]] SpoilerMessFrame frame() const; // Current by time.
  53. [[nodiscard]] crl::time frameDuration() const;
  54. [[nodiscard]] int framesCount() const;
  55. [[nodiscard]] int canvasSize() const;
  56. struct Validator {
  57. crl::time frameDuration = 0;
  58. int framesCount = 0;
  59. int canvasSize = 0;
  60. };
  61. [[nodiscard]] QByteArray serialize() const;
  62. [[nodiscard]] static std::optional<SpoilerMessCached> FromSerialized(
  63. QByteArray data,
  64. std::optional<Validator> validator = {});
  65. private:
  66. QImage _image;
  67. crl::time _frameDuration = 0;
  68. int _framesCount = 0;
  69. int _canvasSize = 0;
  70. };
  71. // Works with default frame duration and default frame count.
  72. class SpoilerAnimationManager;
  73. class SpoilerAnimation final {
  74. public:
  75. explicit SpoilerAnimation(Fn<void()> repaint);
  76. ~SpoilerAnimation();
  77. [[nodiscard]] int index(crl::time now, bool paused);
  78. [[nodiscard]] Fn<void()> repaintCallback() const;
  79. private:
  80. friend class SpoilerAnimationManager;
  81. [[nodiscard]] bool repaint(crl::time now);
  82. const Fn<void()> _repaint;
  83. crl::time _accumulated = 0;
  84. crl::time _last = 0;
  85. bool _animating : 1 = false;
  86. bool _scheduled : 1 = false;
  87. };
  88. [[nodiscard]] SpoilerMessCached GenerateSpoilerMess(
  89. const SpoilerMessDescriptor &descriptor);
  90. void PreloadTextSpoilerMask();
  91. [[nodiscard]] const SpoilerMessCached &DefaultTextSpoilerMask();
  92. void PreloadImageSpoiler();
  93. [[nodiscard]] const SpoilerMessCached &DefaultImageSpoiler();
  94. } // namespace Ui