shake_animation.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/effects/shake_animation.h"
  8. #include "styles/style_basic.h"
  9. namespace Ui {
  10. Fn<void(float64)> DefaultShakeCallback(Fn<void(int)> applyShift) {
  11. constexpr auto kShiftProgress = 6;
  12. constexpr auto kSegmentsCount = 5;
  13. return [=, applyShift = std::move(applyShift)](float64 value) {
  14. const auto fullProgress = value * kShiftProgress;
  15. const auto segment = std::clamp(
  16. int(std::floor(fullProgress)),
  17. 0,
  18. kSegmentsCount);
  19. const auto part = fullProgress - segment;
  20. const auto from = (segment == 0)
  21. ? 0.
  22. : (segment == 1 || segment == 3 || segment == 5)
  23. ? 1.
  24. : -1.;
  25. const auto to = (segment == 0 || segment == 2 || segment == 4)
  26. ? 1.
  27. : (segment == 1 || segment == 3)
  28. ? -1.
  29. : 0.;
  30. const auto shift = from * (1. - part) + to * part;
  31. applyShift(int(base::SafeRound(shift * st::shakeShift)));
  32. };
  33. }
  34. } // namespace Ui