File: spinner.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,151 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/vr/elements/spinner.h"

#include "cc/paint/skia_paint_canvas.h"
#include "chrome/browser/vr/elements/ui_texture.h"
#include "chrome/browser/vr/target_property.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "ui/gfx/animation/keyframe/keyframed_animation_curve.h"
#include "ui/gfx/animation/keyframe/timing_function.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/transform_operations.h"

namespace vr {

static constexpr base::TimeDelta kSweepDuration = base::Seconds(2.0 / 3.0);
static constexpr base::TimeDelta kRotationDuration = base::Milliseconds(1568);
static constexpr float kMinAngle = 0.0f;
static constexpr float kMaxAngle = 135.0f;
static constexpr float kThicknessFactor = 0.078125f;

std::unique_ptr<gfx::CubicBezierTimingFunction> CreateTimingFunction() {
  return gfx::CubicBezierTimingFunction::Create(0.4, 0.0, 0.2, 1.0);
}

class SpinnerTexture : public UiTexture {
 public:
  SpinnerTexture() = default;

  SpinnerTexture(const SpinnerTexture&) = delete;
  SpinnerTexture& operator=(const SpinnerTexture&) = delete;

  ~SpinnerTexture() override = default;

  void SetAngleSweep(float angle) { SetAndDirty(&angle_sweep_, angle); }
  void SetAngleStart(float angle) { SetAndDirty(&angle_start_, angle); }
  void SetRotation(float angle) { SetAndDirty(&rotation_, angle); }
  void SetColor(SkColor color) { SetAndDirty(&color_, color); }

 private:
  void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override {
    float thickness = kThicknessFactor * texture_size.width();
    SkPaint paint;
    paint.setStyle(SkPaint::kStroke_Style);
    paint.setStrokeCap(SkPaint::kRound_Cap);
    paint.setColor(SK_ColorWHITE);
    paint.setStrokeWidth(thickness);
    float padding = thickness * 0.5f;
    sk_canvas->drawArc(
        SkRect::MakeLTRB(padding, padding, texture_size.width() - padding,
                         texture_size.height() - padding),
        angle_start_ - angle_sweep_ + rotation_, 2.0f * angle_sweep_, false,
        paint);
  }

  float angle_sweep_ = 0.0f;
  float angle_start_ = 0.0f;
  float rotation_ = 0.0f;
  SkColor color_ = SK_ColorWHITE;
};

Spinner::Spinner(int texture_width)
    : TexturedElement(),
      texture_(std::make_unique<SpinnerTexture>()),
      texture_width_(texture_width) {
  std::unique_ptr<gfx::KeyframedFloatAnimationCurve> curve(
      gfx::KeyframedFloatAnimationCurve::Create());

  curve->AddKeyframe(
      gfx::FloatKeyframe::Create(base::TimeDelta(), 0.0f, nullptr));
  curve->AddKeyframe(
      gfx::FloatKeyframe::Create(kRotationDuration, 360.0f, nullptr));
  curve->set_target(this);

  std::unique_ptr<gfx::KeyframeModel> keyframe_model(gfx::KeyframeModel::Create(
      std::move(curve), gfx::KeyframeEffect::GetNextKeyframeModelId(),
      SPINNER_ROTATION));

  keyframe_model->set_iterations(std::numeric_limits<double>::infinity());
  AddKeyframeModel(std::move(keyframe_model));

  curve = gfx::KeyframedFloatAnimationCurve::Create();

  for (size_t i = 0; i < 3; ++i) {
    curve->AddKeyframe(gfx::FloatKeyframe::Create(kSweepDuration * i,
                                                  i % 2 ? kMaxAngle : kMinAngle,
                                                  CreateTimingFunction()));
  }

  curve->set_target(this);

  keyframe_model = gfx::KeyframeModel::Create(
      std::move(curve), gfx::KeyframeEffect::GetNextKeyframeModelId(),
      SPINNER_ANGLE_SWEEP);

  keyframe_model->set_iterations(std::numeric_limits<double>::infinity());
  AddKeyframeModel(std::move(keyframe_model));

  curve = gfx::KeyframedFloatAnimationCurve::Create();

  for (size_t i = 0; i < 9; ++i) {
    curve->AddKeyframe(gfx::FloatKeyframe::Create(
        kSweepDuration * i, kMaxAngle * i, CreateTimingFunction()));
  }

  curve->set_target(this);

  keyframe_model = gfx::KeyframeModel::Create(
      std::move(curve), gfx::KeyframeEffect::GetNextKeyframeModelId(),
      SPINNER_ANGLE_START);

  keyframe_model->set_iterations(std::numeric_limits<double>::infinity());
  AddKeyframeModel(std::move(keyframe_model));
}

Spinner::~Spinner() = default;

void Spinner::SetColor(SkColor color) {
  texture_->SetColor(color);
}

UiTexture* Spinner::GetTexture() const {
  return texture_.get();
}

bool Spinner::TextureDependsOnMeasurement() const {
  return false;
}

gfx::Size Spinner::MeasureTextureSize() {
  return gfx::Size(texture_width_, texture_width_);
}

void Spinner::OnFloatAnimated(const float& value,
                              int target_property_id,
                              gfx::KeyframeModel* keyframe_model) {
  switch (target_property_id) {
    case SPINNER_ANGLE_SWEEP:
      texture_->SetAngleSweep(value);
      break;
    case SPINNER_ANGLE_START:
      texture_->SetAngleStart(value);
      break;
    case SPINNER_ROTATION:
      texture_->SetRotation(value);
      break;
    default:
      TexturedElement::OnFloatAnimated(value, target_property_id,
                                       keyframe_model);
  }
}

}  // namespace vr