OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "platform/animation/TimingFunction.h" | 6 #include "platform/animation/TimingFunction.h" |
7 | 7 |
8 namespace WebCore { | 8 namespace WebCore { |
9 | 9 |
10 String LinearTimingFunction::toString() const | 10 String LinearTimingFunction::toString() const |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 m_bezier = adoptPtr(new UnitBezier(m_x1, m_y1, m_x2, m_y2)); | 46 m_bezier = adoptPtr(new UnitBezier(m_x1, m_y1, m_x2, m_y2)); |
47 return m_bezier->solve(fraction, accuracy); | 47 return m_bezier->solve(fraction, accuracy); |
48 } | 48 } |
49 | 49 |
50 String StepsTimingFunction::toString() const | 50 String StepsTimingFunction::toString() const |
51 { | 51 { |
52 StringBuilder builder; | 52 StringBuilder builder; |
53 switch (this->subType()) { | 53 switch (this->subType()) { |
54 case StepsTimingFunction::Start: | 54 case StepsTimingFunction::Start: |
55 return "step-start"; | 55 return "step-start"; |
| 56 case StepsTimingFunction::Middle: |
| 57 return "step-middle"; |
56 case StepsTimingFunction::End: | 58 case StepsTimingFunction::End: |
57 return "step-end"; | 59 return "step-end"; |
58 case StepsTimingFunction::Custom: | 60 case StepsTimingFunction::Custom: |
59 builder.append("steps(" + String::numberToStringECMAScript(this->numberO
fSteps()) + ", "); | 61 builder.append("steps(" + String::numberToStringECMAScript(this->numberO
fSteps()) + ", "); |
60 builder.append(this->stepAtStart() ? "start" : "end"); | 62 |
| 63 if (this->stepAtPosition() == StepsTimingFunction::StepAtStart) |
| 64 builder.append("start"); |
| 65 else if (this->stepAtPosition() == StepsTimingFunction::StepAtMiddle) |
| 66 builder.append("middle"); |
| 67 else if (this->stepAtPosition() == StepsTimingFunction::StepAtEnd) |
| 68 builder.append("end"); |
| 69 else |
| 70 ASSERT_NOT_REACHED(); |
| 71 |
61 builder.append(")"); | 72 builder.append(")"); |
62 break; | 73 break; |
63 default: | 74 default: |
64 ASSERT_NOT_REACHED(); | 75 ASSERT_NOT_REACHED(); |
65 } | 76 } |
66 return builder.toString(); | 77 return builder.toString(); |
67 } | 78 } |
68 | 79 |
69 double StepsTimingFunction::evaluate(double fraction, double) const | 80 double StepsTimingFunction::evaluate(double fraction, double) const |
70 { | 81 { |
71 ASSERT_WITH_MESSAGE(fraction >= 0 && fraction <= 1, "Web Animations not yet
implemented: Timing function behavior outside the range [0, 1] is not yet specif
ied"); | 82 ASSERT_WITH_MESSAGE(fraction >= 0 && fraction <= 1, "Web Animations not yet
implemented: Timing function behavior outside the range [0, 1] is not yet specif
ied"); |
72 return std::min(1.0, (floor(m_steps * fraction) + m_stepAtStart) / m_steps); | 83 double startOffset = 0; |
| 84 switch (m_stepAtPosition) { |
| 85 case StepAtStart: |
| 86 startOffset = 1; |
| 87 break; |
| 88 case StepAtMiddle: |
| 89 startOffset = 0.5; |
| 90 break; |
| 91 case StepAtEnd: |
| 92 startOffset = 0; |
| 93 break; |
| 94 default: |
| 95 ASSERT_NOT_REACHED(); |
| 96 break; |
| 97 } |
| 98 return std::min(1.0, floor((m_steps * fraction) + startOffset) / m_steps); |
73 } | 99 } |
74 | 100 |
75 String ChainedTimingFunction::toString() const | 101 String ChainedTimingFunction::toString() const |
76 { | 102 { |
77 StringBuilder builder; | 103 StringBuilder builder; |
78 builder.append("chained("); | 104 builder.append("chained("); |
79 for (size_t i = 0; i < this->m_segments.size(); i++) { | 105 for (size_t i = 0; i < this->m_segments.size(); i++) { |
80 ChainedTimingFunction::Segment segment = this->m_segments[i]; | 106 ChainedTimingFunction::Segment segment = this->m_segments[i]; |
81 builder.append(segment.m_timingFunction->toString()); | 107 builder.append(segment.m_timingFunction->toString()); |
82 builder.append("[" + String::numberToStringECMAScript(segment.m_min) + "
-> " + String::numberToStringECMAScript(segment.m_max) + "]"); | 108 builder.append("[" + String::numberToStringECMAScript(segment.m_min) + "
-> " + String::numberToStringECMAScript(segment.m_max) + "]"); |
(...skipping 11 matching lines...) Expand all Loading... |
94 ASSERT(!m_segments.isEmpty()); | 120 ASSERT(!m_segments.isEmpty()); |
95 ASSERT(m_segments.last().max() == 1); | 121 ASSERT(m_segments.last().max() == 1); |
96 size_t i = 0; | 122 size_t i = 0; |
97 const Segment* segment = &m_segments[i++]; | 123 const Segment* segment = &m_segments[i++]; |
98 while (fraction >= segment->max() && i < m_segments.size()) { | 124 while (fraction >= segment->max() && i < m_segments.size()) { |
99 segment = &m_segments[i++]; | 125 segment = &m_segments[i++]; |
100 } | 126 } |
101 return segment->evaluate(fraction, accuracy); | 127 return segment->evaluate(fraction, accuracy); |
102 } | 128 } |
103 | 129 |
| 130 // Equals operators |
| 131 bool operator==(const LinearTimingFunction& lhs, const TimingFunction& rhs) |
| 132 { |
| 133 return rhs.type() == TimingFunction::LinearFunction; |
| 134 } |
| 135 |
| 136 bool operator==(const CubicBezierTimingFunction& lhs, const TimingFunction& rhs) |
| 137 { |
| 138 if (rhs.type() != TimingFunction::CubicBezierFunction) |
| 139 return false; |
| 140 |
| 141 const CubicBezierTimingFunction& ctf = toCubicBezierTimingFunction(rhs); |
| 142 if ((lhs.subType() == CubicBezierTimingFunction::Custom) && (ctf.subType() =
= CubicBezierTimingFunction::Custom)) |
| 143 return (lhs.x1() == ctf.x1()) && (lhs.y1() == ctf.y1()) && (lhs.x2() ==
ctf.x2()) && (lhs.y2() == ctf.y2()); |
| 144 |
| 145 return lhs.subType() == ctf.subType(); |
| 146 } |
| 147 |
| 148 bool operator==(const StepsTimingFunction& lhs, const TimingFunction& rhs) |
| 149 { |
| 150 if (rhs.type() != TimingFunction::StepsFunction) |
| 151 return false; |
| 152 |
| 153 const StepsTimingFunction& stf = toStepsTimingFunction(rhs); |
| 154 if ((lhs.subType() == StepsTimingFunction::Custom) && (stf.subType() == Step
sTimingFunction::Custom)) |
| 155 return (lhs.numberOfSteps() == stf.numberOfSteps()) && (lhs.stepAtPositi
on() == stf.stepAtPosition()); |
| 156 |
| 157 return lhs.subType() == stf.subType(); |
| 158 } |
| 159 |
| 160 // The generic operator== *must* come after the |
| 161 // non-generic operator== otherwise it will end up calling itself. |
| 162 bool operator==(const TimingFunction& lhs, const TimingFunction& rhs) |
| 163 { |
| 164 switch (lhs.type()) { |
| 165 case TimingFunction::LinearFunction: { |
| 166 const LinearTimingFunction& linear = toLinearTimingFunction(lhs); |
| 167 return (linear == rhs); |
| 168 } |
| 169 case TimingFunction::CubicBezierFunction: { |
| 170 const CubicBezierTimingFunction& cubic = toCubicBezierTimingFunction(lhs
); |
| 171 return (cubic == rhs); |
| 172 } |
| 173 case TimingFunction::StepsFunction: { |
| 174 const StepsTimingFunction& step = toStepsTimingFunction(lhs); |
| 175 return (step == rhs); |
| 176 } |
| 177 case TimingFunction::ChainedFunction: { |
| 178 const ChainedTimingFunction& chained = toChainedTimingFunction(lhs); |
| 179 return (chained == rhs); |
| 180 } |
| 181 default: |
| 182 ASSERT_NOT_REACHED(); |
| 183 } |
| 184 return false; |
| 185 } |
| 186 |
| 187 // No need to define specific operator!= as they can all come via this function. |
| 188 bool operator!=(const TimingFunction& lhs, const TimingFunction& rhs) |
| 189 { |
| 190 return !(lhs == rhs); |
| 191 } |
| 192 |
104 } // namespace WebCore | 193 } // namespace WebCore |
OLD | NEW |