jitter_calculator.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/ambient/ui/jitter_calculator.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/check_op.h"
  8. #include "base/rand_util.h"
  9. namespace ash {
  10. JitterCalculator::JitterCalculator(Config config)
  11. : JitterCalculator(std::move(config),
  12. base::BindRepeating(&base::RandInt, 0, 1)) {}
  13. JitterCalculator::JitterCalculator(
  14. Config config,
  15. RandomBinaryGenerator random_binary_generator)
  16. : config_(std::move(config)),
  17. random_binary_generator_(std::move(random_binary_generator)) {
  18. DCHECK_GE(config_.step_size, 0);
  19. DCHECK_LE(config_.x_min_translation, 0);
  20. DCHECK_GE(config_.x_max_translation, 0);
  21. DCHECK_LE(config_.y_min_translation, 0);
  22. DCHECK_GE(config_.y_max_translation, 0);
  23. DCHECK(random_binary_generator_);
  24. }
  25. JitterCalculator::~JitterCalculator() = default;
  26. gfx::Vector2d JitterCalculator::Calculate() {
  27. AssetCurrentTranslationWithinBounds();
  28. // Move the translation point randomly one step on each x/y direction.
  29. int x_increment = config_.step_size * random_binary_generator_.Run();
  30. int y_increment = x_increment == 0
  31. ? config_.step_size
  32. : config_.step_size * random_binary_generator_.Run();
  33. current_translation_.Add(gfx::Vector2d(translate_x_direction * x_increment,
  34. translate_y_direction * y_increment));
  35. // If the translation point is out of bounds, reset it within bounds and
  36. // reverse the direction.
  37. if (current_translation_.x() < config_.x_min_translation) {
  38. translate_x_direction = 1;
  39. current_translation_.set_x(config_.x_min_translation);
  40. } else if (current_translation_.x() > config_.x_max_translation) {
  41. translate_x_direction = -1;
  42. current_translation_.set_x(config_.x_max_translation);
  43. }
  44. if (current_translation_.y() > config_.y_max_translation) {
  45. translate_y_direction = -1;
  46. current_translation_.set_y(config_.y_max_translation);
  47. } else if (current_translation_.y() < config_.y_min_translation) {
  48. translate_y_direction = 1;
  49. current_translation_.set_y(config_.y_min_translation);
  50. }
  51. AssetCurrentTranslationWithinBounds();
  52. return current_translation_;
  53. }
  54. void JitterCalculator::AssetCurrentTranslationWithinBounds() const {
  55. DCHECK_LE(current_translation_.x(), config_.x_max_translation);
  56. DCHECK_GE(current_translation_.x(), config_.x_min_translation);
  57. DCHECK_LE(current_translation_.y(), config_.y_max_translation);
  58. DCHECK_GE(current_translation_.y(), config_.y_min_translation);
  59. }
  60. } // namespace ash