From de459fa1fe75c723f993fea8c250a2357d20e672 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 30 Oct 2020 10:57:41 +0300 Subject: [PATCH] Improve slot machine animations. --- .../view/media/history_view_slot_machine.cpp | 11 +++++++++-- .../history/view/media/history_view_sticker.cpp | 5 +++-- .../history/view/media/history_view_sticker.h | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Telegram/SourceFiles/history/view/media/history_view_slot_machine.cpp b/Telegram/SourceFiles/history/view/media/history_view_slot_machine.cpp index e384e038a..0081d90e5 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_slot_machine.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_slot_machine.cpp @@ -29,8 +29,8 @@ constexpr auto kBarIndex = 2; constexpr auto kBerriesIndex = 3; constexpr auto kLemonIndex = 4; constexpr auto kStartIndex = 5; - constexpr auto kWinValue = 64; +constexpr auto kSkipFramesBeforeWinEnding = 90; const auto &kEmoji = ::Stickers::DicePacks::kSlotString; @@ -162,6 +162,7 @@ void SlotMachine::draw(Painter &p, const QRect &r, bool selected) { const auto pullReady = _pull && _pull->readyToDrawLottie(); const auto paintReady = [&] { auto result = pullReady; + auto allPlayedEnough = true; for (auto i = 1; i != 4; ++i) { if (!_end[i] || !_end[i]->readyToDrawLottie()) { switchedToEnd[i] = false; @@ -170,8 +171,14 @@ void SlotMachine::draw(Painter &p, const QRect &r, bool selected) { && (!_start[i] || !_start[i]->readyToDrawLottie())) { result = false; } + const auto playedTillFrame = !switchedToEnd[i] + ? 0 + : _end[i]->frameIndex().value_or(0); + if (playedTillFrame < kSkipFramesBeforeWinEnding) { + allPlayedEnough = false; + } } - if (!_end[0] || !_end[0]->readyToDrawLottie()) { + if (!_end[0] || !_end[0]->readyToDrawLottie() || !allPlayedEnough) { switchedToEnd[0] = false; } if (ranges::contains(switchedToEnd, false) diff --git a/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp b/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp index 6cc2e8cc2..68df01812 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp @@ -196,11 +196,12 @@ void Sticker::paintLottie(Painter &p, const QRect &r, bool selected) { : (isEmojiSticker() || !Core::App().settings().loopAnimatedStickers()); const auto count = _lottie->information().framesCount; - _atTheEnd = (frame.index + 1 == count); + _frameIndex = frame.index; + _framesCount = count; _nextLastDiceFrame = !paused && (_diceIndex > 0) && (frame.index + 2 == count); - const auto lastDiceFrame = (_diceIndex > 0) && _atTheEnd; + const auto lastDiceFrame = (_diceIndex > 0) && atTheEnd(); const auto switchToNext = !playOnce || (!lastDiceFrame && (frame.index != 0 || !_lottieOncePlayed)); if (!paused diff --git a/Telegram/SourceFiles/history/view/media/history_view_sticker.h b/Telegram/SourceFiles/history/view/media/history_view_sticker.h index 88edbfb7c..d20520b0f 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_sticker.h +++ b/Telegram/SourceFiles/history/view/media/history_view_sticker.h @@ -61,7 +61,17 @@ public: void setDiceIndex(const QString &emoji, int index); [[nodiscard]] bool atTheEnd() const { - return _atTheEnd; + return (_frameIndex >= 0) && (_frameIndex + 1 == _framesCount); + } + [[nodiscard]] std::optional frameIndex() const { + return (_frameIndex >= 0) + ? std::make_optional(_frameIndex) + : std::nullopt; + } + [[nodiscard]] std::optional framesCount() const { + return (_framesCount > 0) + ? std::make_optional(_framesCount) + : std::nullopt; } [[nodiscard]] bool readyToDrawLottie(); @@ -94,8 +104,9 @@ private: QImage _lastDiceFrame; QString _diceEmoji; int _diceIndex = -1; + mutable int _frameIndex = -1; + mutable int _framesCount = -1; mutable bool _lottieOncePlayed = false; - mutable bool _atTheEnd = false; mutable bool _nextLastDiceFrame = false; rpl::lifetime _lifetime;