Add custom-emoji wrapper with limited loops count.

This commit is contained in:
John Preston 2022-08-31 17:50:35 +04:00
parent 51657b3c8a
commit 4d45396174
2 changed files with 65 additions and 1 deletions

View file

@ -67,4 +67,50 @@ bool FirstFrameEmoji::readyInDefaultState() {
return _wrapped->readyInDefaultState();
}
LimitedLoopsEmoji::LimitedLoopsEmoji(
std::unique_ptr<CustomEmoji> 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

View file

@ -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<CustomEmoji> 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<Ui::Text::CustomEmoji> _wrapped;
const int _limit = 0;
int _played = 0;
bool _inLoop = false;
};
} // namespace Ui::Text