| 1 | /* |
| 2 | * Copyright (C) 2000 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 2000 Antti Koivisto (koivisto@kde.org) |
| 4 | * (C) 2000 Dirk Mueller (mueller@kde.org) |
| 5 | * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 6 | * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com) |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Library General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Library General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Library General Public License |
| 19 | * along with this library; see the file COPYING.LIB. If not, write to |
| 20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | * Boston, MA 02110-1301, USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include "CSSValue.h" |
| 28 | #include "ExceptionOr.h" |
| 29 | #include <wtf/Ref.h> |
| 30 | #include <wtf/RefCounted.h> |
| 31 | |
| 32 | namespace WTF { |
| 33 | class TextStream; |
| 34 | } |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class TimingFunction : public RefCounted<TimingFunction> { |
| 39 | public: |
| 40 | virtual Ref<TimingFunction> clone() const = 0; |
| 41 | |
| 42 | virtual ~TimingFunction() = default; |
| 43 | |
| 44 | enum TimingFunctionType { LinearFunction, CubicBezierFunction, StepsFunction, SpringFunction }; |
| 45 | TimingFunctionType type() const { return m_type; } |
| 46 | |
| 47 | bool isLinearTimingFunction() const { return m_type == LinearFunction; } |
| 48 | bool isCubicBezierTimingFunction() const { return m_type == CubicBezierFunction; } |
| 49 | bool isStepsTimingFunction() const { return m_type == StepsFunction; } |
| 50 | bool isSpringTimingFunction() const { return m_type == SpringFunction; } |
| 51 | |
| 52 | virtual bool operator==(const TimingFunction&) const = 0; |
| 53 | bool operator!=(const TimingFunction& other) const { return !(*this == other); } |
| 54 | |
| 55 | static ExceptionOr<RefPtr<TimingFunction>> createFromCSSText(const String&); |
| 56 | static RefPtr<TimingFunction> createFromCSSValue(const CSSValue&); |
| 57 | double transformTime(double, double, bool before = false) const; |
| 58 | String cssText() const; |
| 59 | |
| 60 | protected: |
| 61 | explicit TimingFunction(TimingFunctionType type) |
| 62 | : m_type(type) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | TimingFunctionType m_type; |
| 68 | }; |
| 69 | |
| 70 | class LinearTimingFunction final : public TimingFunction { |
| 71 | public: |
| 72 | static Ref<LinearTimingFunction> create() |
| 73 | { |
| 74 | return adoptRef(*new LinearTimingFunction); |
| 75 | } |
| 76 | |
| 77 | bool operator==(const TimingFunction& other) const final |
| 78 | { |
| 79 | return is<LinearTimingFunction>(other); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | LinearTimingFunction() |
| 84 | : TimingFunction(LinearFunction) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | Ref<TimingFunction> clone() const final |
| 89 | { |
| 90 | return adoptRef(*new LinearTimingFunction); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class CubicBezierTimingFunction final : public TimingFunction { |
| 95 | public: |
| 96 | enum TimingFunctionPreset { Ease, EaseIn, EaseOut, EaseInOut, Custom }; |
| 97 | |
| 98 | static Ref<CubicBezierTimingFunction> create(double x1, double y1, double x2, double y2) |
| 99 | { |
| 100 | return adoptRef(*new CubicBezierTimingFunction(Custom, x1, y1, x2, y2)); |
| 101 | } |
| 102 | |
| 103 | static Ref<CubicBezierTimingFunction> create() |
| 104 | { |
| 105 | return adoptRef(*new CubicBezierTimingFunction()); |
| 106 | } |
| 107 | |
| 108 | static Ref<CubicBezierTimingFunction> create(TimingFunctionPreset preset) |
| 109 | { |
| 110 | switch (preset) { |
| 111 | case Ease: |
| 112 | return adoptRef(*new CubicBezierTimingFunction); |
| 113 | case EaseIn: |
| 114 | return adoptRef(*new CubicBezierTimingFunction(EaseIn, 0.42, 0.0, 1.0, 1.0)); |
| 115 | case EaseOut: |
| 116 | return adoptRef(*new CubicBezierTimingFunction(EaseOut, 0.0, 0.0, 0.58, 1.0)); |
| 117 | case EaseInOut: |
| 118 | return adoptRef(*new CubicBezierTimingFunction(EaseInOut, 0.42, 0.0, 0.58, 1.0)); |
| 119 | case Custom: |
| 120 | break; |
| 121 | } |
| 122 | ASSERT_NOT_REACHED(); |
| 123 | return adoptRef(*new CubicBezierTimingFunction); |
| 124 | } |
| 125 | |
| 126 | bool operator==(const TimingFunction& other) const final |
| 127 | { |
| 128 | if (!is<CubicBezierTimingFunction>(other)) |
| 129 | return false; |
| 130 | auto& otherCubic = downcast<CubicBezierTimingFunction>(other); |
| 131 | if (m_timingFunctionPreset != otherCubic.m_timingFunctionPreset) |
| 132 | return false; |
| 133 | if (m_timingFunctionPreset != Custom) |
| 134 | return true; |
| 135 | return m_x1 == otherCubic.m_x1 && m_y1 == otherCubic.m_y1 && m_x2 == otherCubic.m_x2 && m_y2 == otherCubic.m_y2; |
| 136 | } |
| 137 | |
| 138 | double x1() const { return m_x1; } |
| 139 | double y1() const { return m_y1; } |
| 140 | double x2() const { return m_x2; } |
| 141 | double y2() const { return m_y2; } |
| 142 | |
| 143 | void setValues(double x1, double y1, double x2, double y2) |
| 144 | { |
| 145 | m_x1 = x1; |
| 146 | m_y1 = y1; |
| 147 | m_x2 = x2; |
| 148 | m_y2 = y2; |
| 149 | } |
| 150 | |
| 151 | TimingFunctionPreset timingFunctionPreset() const { return m_timingFunctionPreset; } |
| 152 | void setTimingFunctionPreset(TimingFunctionPreset preset) { m_timingFunctionPreset = preset; } |
| 153 | |
| 154 | static const CubicBezierTimingFunction& defaultTimingFunction() |
| 155 | { |
| 156 | static const CubicBezierTimingFunction& function = create().leakRef(); |
| 157 | return function; |
| 158 | } |
| 159 | |
| 160 | Ref<CubicBezierTimingFunction> createReversed() const |
| 161 | { |
| 162 | return create(1.0 - m_x2, 1.0 - m_y2, 1.0 - m_x1, 1.0 - m_y1); |
| 163 | } |
| 164 | |
| 165 | bool isLinear() const |
| 166 | { |
| 167 | return (!m_x1 && !m_y1 && !m_x2 && !m_y2) || (m_x1 == 1.0 && m_y1 == 1.0 && m_x2 == 1.0 && m_y2 == 1.0) || (m_x1 == 0.0 && m_y1 == 0.0 && m_x2 == 1.0 && m_y2 == 1.0); |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | explicit CubicBezierTimingFunction(TimingFunctionPreset preset = Ease, double x1 = 0.25, double y1 = 0.1, double x2 = 0.25, double y2 = 1.0) |
| 172 | : TimingFunction(CubicBezierFunction) |
| 173 | , m_x1(x1) |
| 174 | , m_y1(y1) |
| 175 | , m_x2(x2) |
| 176 | , m_y2(y2) |
| 177 | , m_timingFunctionPreset(preset) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | Ref<TimingFunction> clone() const final |
| 182 | { |
| 183 | return adoptRef(*new CubicBezierTimingFunction(m_timingFunctionPreset, m_x1, m_y1, m_x2, m_y2)); |
| 184 | } |
| 185 | |
| 186 | double m_x1; |
| 187 | double m_y1; |
| 188 | double m_x2; |
| 189 | double m_y2; |
| 190 | TimingFunctionPreset m_timingFunctionPreset; |
| 191 | }; |
| 192 | |
| 193 | class StepsTimingFunction final : public TimingFunction { |
| 194 | public: |
| 195 | static Ref<StepsTimingFunction> create(int steps, bool stepAtStart) |
| 196 | { |
| 197 | return adoptRef(*new StepsTimingFunction(steps, stepAtStart)); |
| 198 | } |
| 199 | static Ref<StepsTimingFunction> create() |
| 200 | { |
| 201 | return adoptRef(*new StepsTimingFunction(1, true)); |
| 202 | } |
| 203 | |
| 204 | bool operator==(const TimingFunction& other) const final |
| 205 | { |
| 206 | if (!is<StepsTimingFunction>(other)) |
| 207 | return false; |
| 208 | auto& otherSteps = downcast<StepsTimingFunction>(other); |
| 209 | return m_steps == otherSteps.m_steps && m_stepAtStart == otherSteps.m_stepAtStart; |
| 210 | } |
| 211 | |
| 212 | int numberOfSteps() const { return m_steps; } |
| 213 | void setNumberOfSteps(int steps) { m_steps = steps; } |
| 214 | |
| 215 | bool stepAtStart() const { return m_stepAtStart; } |
| 216 | void setStepAtStart(bool stepAtStart) { m_stepAtStart = stepAtStart; } |
| 217 | |
| 218 | private: |
| 219 | StepsTimingFunction(int steps, bool stepAtStart) |
| 220 | : TimingFunction(StepsFunction) |
| 221 | , m_steps(steps) |
| 222 | , m_stepAtStart(stepAtStart) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | Ref<TimingFunction> clone() const final |
| 227 | { |
| 228 | return adoptRef(*new StepsTimingFunction(m_steps, m_stepAtStart)); |
| 229 | } |
| 230 | |
| 231 | int m_steps; |
| 232 | bool m_stepAtStart; |
| 233 | }; |
| 234 | |
| 235 | class SpringTimingFunction final : public TimingFunction { |
| 236 | public: |
| 237 | static Ref<SpringTimingFunction> create(double mass, double stiffness, double damping, double initialVelocity) |
| 238 | { |
| 239 | return adoptRef(*new SpringTimingFunction(mass, stiffness, damping, initialVelocity)); |
| 240 | } |
| 241 | |
| 242 | static Ref<SpringTimingFunction> create() |
| 243 | { |
| 244 | // This create() function should only be used by the argument decoders, and it is expected that |
| 245 | // real values will be filled in using setValues(). |
| 246 | return create(0, 0, 0, 0); |
| 247 | } |
| 248 | |
| 249 | bool operator==(const TimingFunction& other) const final |
| 250 | { |
| 251 | if (!is<SpringTimingFunction>(other)) |
| 252 | return false; |
| 253 | auto& otherSpring = downcast<SpringTimingFunction>(other); |
| 254 | return m_mass == otherSpring.m_mass && m_stiffness == otherSpring.m_stiffness && m_damping == otherSpring.m_damping && m_initialVelocity == otherSpring.m_initialVelocity; |
| 255 | } |
| 256 | |
| 257 | double mass() const { return m_mass; } |
| 258 | double stiffness() const { return m_stiffness; } |
| 259 | double damping() const { return m_damping; } |
| 260 | double initialVelocity() const { return m_initialVelocity; } |
| 261 | |
| 262 | void setValues(double mass, double stiffness, double damping, double initialVelocity) |
| 263 | { |
| 264 | m_mass = mass; |
| 265 | m_stiffness = stiffness; |
| 266 | m_damping = damping; |
| 267 | m_initialVelocity = initialVelocity; |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | explicit SpringTimingFunction(double mass, double stiffness, double damping, double initialVelocity) |
| 272 | : TimingFunction(SpringFunction) |
| 273 | , m_mass(mass) |
| 274 | , m_stiffness(stiffness) |
| 275 | , m_damping(damping) |
| 276 | , m_initialVelocity(initialVelocity) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | Ref<TimingFunction> clone() const final |
| 281 | { |
| 282 | return adoptRef(*new SpringTimingFunction(m_mass, m_stiffness, m_damping, m_initialVelocity)); |
| 283 | } |
| 284 | |
| 285 | double m_mass; |
| 286 | double m_stiffness; |
| 287 | double m_damping; |
| 288 | double m_initialVelocity; |
| 289 | }; |
| 290 | |
| 291 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const TimingFunction&); |
| 292 | |
| 293 | } // namespace WebCore |
| 294 | |
| 295 | #define SPECIALIZE_TYPE_TRAITS_TIMINGFUNCTION(ToValueTypeName, predicate) \ |
| 296 | SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \ |
| 297 | static bool isType(const WebCore::TimingFunction& function) { return function.predicate; } \ |
| 298 | SPECIALIZE_TYPE_TRAITS_END() |
| 299 | |
| 300 | SPECIALIZE_TYPE_TRAITS_TIMINGFUNCTION(WebCore::LinearTimingFunction, isLinearTimingFunction()) |
| 301 | SPECIALIZE_TYPE_TRAITS_TIMINGFUNCTION(WebCore::CubicBezierTimingFunction, isCubicBezierTimingFunction()) |
| 302 | SPECIALIZE_TYPE_TRAITS_TIMINGFUNCTION(WebCore::StepsTimingFunction, isStepsTimingFunction()) |
| 303 | SPECIALIZE_TYPE_TRAITS_TIMINGFUNCTION(WebCore::SpringTimingFunction, isSpringTimingFunction()) |
| 304 | |