Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
property_resolver.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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
6
7#include <algorithm>
8#include <iterator>
9#include <memory>
10
13#include "impeller/scene/node.h"
14
15namespace impeller {
16namespace scene {
17
18std::unique_ptr<TranslationTimelineResolver>
20 std::vector<Vector3> values) {
21 FML_DCHECK(times.size() == values.size());
22 auto result = std::unique_ptr<TranslationTimelineResolver>(
24 result->times_ = std::move(times);
25 result->values_ = std::move(values);
26 return result;
27}
28
29std::unique_ptr<RotationTimelineResolver>
31 std::vector<Quaternion> values) {
32 FML_DCHECK(times.size() == values.size());
33 auto result =
34 std::unique_ptr<RotationTimelineResolver>(new RotationTimelineResolver());
35 result->times_ = std::move(times);
36 result->values_ = std::move(values);
37 return result;
38}
39
40std::unique_ptr<ScaleTimelineResolver> PropertyResolver::MakeScaleTimeline(
41 std::vector<Scalar> times,
42 std::vector<Vector3> values) {
43 FML_DCHECK(times.size() == values.size());
44 auto result =
45 std::unique_ptr<ScaleTimelineResolver>(new ScaleTimelineResolver());
46 result->times_ = std::move(times);
47 result->values_ = std::move(values);
48 return result;
49}
50
52
54
56 if (times_.empty()) {
57 return SecondsF::zero();
58 }
59 return SecondsF(times_.back());
60}
61
63 if (times_.size() <= 1 || time.count() <= times_.front()) {
64 return {.index = 0, .lerp = 1};
65 }
66 if (time.count() >= times_.back()) {
67 return {.index = times_.size() - 1, .lerp = 1};
68 }
69 auto it = std::lower_bound(times_.begin(), times_.end(), time.count());
70 size_t index = std::distance(times_.begin(), it);
71
72 Scalar previous_time = *(it - 1);
73 Scalar next_time = *it;
74 return {.index = index,
75 .lerp = (time.count() - previous_time) / (next_time - previous_time)};
76}
77
78TranslationTimelineResolver::TranslationTimelineResolver() = default;
79
81
83 SecondsF time,
84 Scalar weight) {
85 if (values_.empty()) {
86 return;
87 }
88 auto key = GetTimelineKey(time);
89 auto value = values_[key.index];
90 if (key.lerp < 1) {
91 value = values_[key.index - 1].Lerp(value, key.lerp);
92 }
93
94 target.animated_pose.translation +=
95 (value - target.bind_pose.translation) * weight;
96}
97
98RotationTimelineResolver::RotationTimelineResolver() = default;
99
101
103 SecondsF time,
104 Scalar weight) {
105 if (values_.empty()) {
106 return;
107 }
108 auto key = GetTimelineKey(time);
109 auto value = values_[key.index];
110 if (key.lerp < 1) {
111 value = values_[key.index - 1].Slerp(value, key.lerp);
112 }
113
114 target.animated_pose.rotation =
115 target.animated_pose.rotation *
116 Quaternion().Slerp(target.bind_pose.rotation.Invert() * value, weight);
117}
118
119ScaleTimelineResolver::ScaleTimelineResolver() = default;
120
122
124 SecondsF time,
125 Scalar weight) {
126 if (values_.empty()) {
127 return;
128 }
129 auto key = GetTimelineKey(time);
130 auto value = values_[key.index];
131 if (key.lerp < 1) {
132 value = values_[key.index - 1].Lerp(value, key.lerp);
133 }
134
135 target.animated_pose.scale *=
136 Vector3(1, 1, 1).Lerp(value / target.bind_pose.scale, weight);
137}
138
139} // namespace scene
140} // namespace impeller
static SkISize times(const SkISize &size, float factor)
static std::unique_ptr< RotationTimelineResolver > MakeRotationTimeline(std::vector< Scalar > times, std::vector< Quaternion > values)
static std::unique_ptr< ScaleTimelineResolver > MakeScaleTimeline(std::vector< Scalar > times, std::vector< Vector3 > values)
static std::unique_ptr< TranslationTimelineResolver > MakeTranslationTimeline(std::vector< Scalar > times, std::vector< Vector3 > values)
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
TimelineKey GetTimelineKey(SecondsF time)
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
uint8_t value
GAsyncResult * result
uint32_t * target
#define FML_DCHECK(condition)
Definition logging.h:103
float Scalar
Definition scalar.h:18
std::chrono::duration< float > SecondsF
Definition timing.h:13
Quaternion Slerp(const Quaternion &to, double time) const
Definition quaternion.cc:10
constexpr Vector3 Lerp(const Vector3 &v, Scalar t) const
Definition vector.h:178
size_t index
The index of the closest previous keyframe.