diff --git a/ui/text/text_custom_emoji.cpp b/ui/text/text_custom_emoji.cpp index ee2bae9..4eb1919 100644 --- a/ui/text/text_custom_emoji.cpp +++ b/ui/text/text_custom_emoji.cpp @@ -67,4 +67,50 @@ bool FirstFrameEmoji::readyInDefaultState() { return _wrapped->readyInDefaultState(); } +LimitedLoopsEmoji::LimitedLoopsEmoji( + std::unique_ptr wrapped, + int limit) +: _wrapped(std::move(wrapped)) +, _limit(limit) { +} + +QString LimitedLoopsEmoji::entityData() { + return _wrapped->entityData(); +} + +void LimitedLoopsEmoji::paint(QPainter &p, const Context &context) { + if (_played < _limit) { + if (_wrapped->readyInDefaultState()) { + if (_inLoop) { + _inLoop = false; + ++_played; + } + } else if (_wrapped->ready()) { + _inLoop = true; + } + } + if (_played == _limit) { + const auto was = context.firstFrameOnly; + context.firstFrameOnly = true; + _wrapped->paint(p, context); + context.firstFrameOnly = was; + } else { + _wrapped->paint(p, context); + } +} + +void LimitedLoopsEmoji::unload() { + _wrapped->unload(); + _inLoop = false; + _played = 0; +} + +bool LimitedLoopsEmoji::ready() { + return _wrapped->ready(); +} + +bool LimitedLoopsEmoji::readyInDefaultState() { + return _wrapped->readyInDefaultState(); +} + } // namespace Ui::Text diff --git a/ui/text/text_custom_emoji.h b/ui/text/text_custom_emoji.h index 052fcff..a972e8b 100644 --- a/ui/text/text_custom_emoji.h +++ b/ui/text/text_custom_emoji.h @@ -31,7 +31,7 @@ struct CustomEmojiPaintContext { crl::time now = 0; float64 scale = 0.; QPoint position; - bool firstFrameOnly = false; + mutable bool firstFrameOnly = false; bool paused = false; bool scaled = false; }; @@ -85,4 +85,22 @@ private: }; +class LimitedLoopsEmoji final : public CustomEmoji { +public: + LimitedLoopsEmoji(std::unique_ptr wrapped, int limit); + + QString entityData() override; + void paint(QPainter &p, const Context &context) override; + void unload() override; + bool ready() override; + bool readyInDefaultState() override; + +private: + const std::unique_ptr _wrapped; + const int _limit = 0; + int _played = 0; + bool _inLoop = false; + +}; + } // namespace Ui::Text