Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_ComputedTimingFunction_h
8 : #define mozilla_ComputedTimingFunction_h
9 :
10 : #include "nsSMILKeySpline.h" // nsSMILKeySpline
11 : #include "nsStyleStruct.h" // nsTimingFunction
12 :
13 : namespace mozilla {
14 :
15 : class ComputedTimingFunction
16 : {
17 : public:
18 : static ComputedTimingFunction
19 0 : CubicBezier(double x1, double y1, double x2, double y2)
20 : {
21 0 : return ComputedTimingFunction(x1, y1, x2, y2);
22 : }
23 : static ComputedTimingFunction
24 0 : Steps(nsTimingFunction::Type aType, uint32_t aSteps)
25 : {
26 0 : MOZ_ASSERT(aType == nsTimingFunction::Type::StepStart ||
27 : aType == nsTimingFunction::Type::StepEnd,
28 : "The type of timing function should be either step-start or "
29 : "step-end");
30 0 : MOZ_ASSERT(aSteps > 0, "The number of steps should be 1 or more");
31 0 : return ComputedTimingFunction(aType, aSteps);
32 : }
33 : static ComputedTimingFunction
34 0 : Frames(uint32_t aFrames)
35 : {
36 0 : MOZ_ASSERT(aFrames > 1, "The number of frames should be 2 or more");
37 0 : return ComputedTimingFunction(nsTimingFunction::Type::Frames, aFrames);
38 : }
39 :
40 2 : ComputedTimingFunction() = default;
41 2 : explicit ComputedTimingFunction(const nsTimingFunction& aFunction)
42 2 : {
43 2 : Init(aFunction);
44 2 : }
45 : void Init(const nsTimingFunction& aFunction);
46 :
47 : // BeforeFlag is used in step timing function.
48 : // https://w3c.github.io/web-animations/#before-flag
49 : enum class BeforeFlag {
50 : Unset,
51 : Set
52 : };
53 : double GetValue(double aPortion, BeforeFlag aBeforeFlag) const;
54 0 : const nsSMILKeySpline* GetFunction() const
55 : {
56 0 : NS_ASSERTION(HasSpline(), "Type mismatch");
57 0 : return &mTimingFunction;
58 : }
59 0 : nsTimingFunction::Type GetType() const { return mType; }
60 18 : bool HasSpline() const { return nsTimingFunction::IsSplineType(mType); }
61 0 : uint32_t GetSteps() const
62 : {
63 0 : MOZ_ASSERT(mType == nsTimingFunction::Type::StepStart ||
64 : mType == nsTimingFunction::Type::StepEnd);
65 0 : return mStepsOrFrames;
66 : }
67 0 : uint32_t GetFrames() const
68 : {
69 0 : MOZ_ASSERT(mType == nsTimingFunction::Type::Frames);
70 0 : return mStepsOrFrames;
71 : }
72 14 : bool operator==(const ComputedTimingFunction& aOther) const
73 : {
74 56 : return mType == aOther.mType &&
75 14 : (HasSpline() ?
76 14 : mTimingFunction == aOther.mTimingFunction :
77 14 : mStepsOrFrames == aOther.mStepsOrFrames);
78 : }
79 : bool operator!=(const ComputedTimingFunction& aOther) const
80 : {
81 : return !(*this == aOther);
82 : }
83 0 : bool operator==(const nsTimingFunction& aOther) const
84 : {
85 0 : return mType == aOther.mType &&
86 0 : (HasSpline()
87 0 : ? mTimingFunction.X1() == aOther.mFunc.mX1 &&
88 0 : mTimingFunction.Y1() == aOther.mFunc.mY1 &&
89 0 : mTimingFunction.X2() == aOther.mFunc.mX2 &&
90 0 : mTimingFunction.Y2() == aOther.mFunc.mY2
91 0 : : mStepsOrFrames == aOther.mStepsOrFrames);
92 : }
93 : bool operator!=(const nsTimingFunction& aOther) const
94 : {
95 : return !(*this == aOther);
96 : }
97 : int32_t Compare(const ComputedTimingFunction& aRhs) const;
98 : void AppendToString(nsAString& aResult) const;
99 :
100 4 : static double GetPortion(const Maybe<ComputedTimingFunction>& aFunction,
101 : double aPortion,
102 : BeforeFlag aBeforeFlag)
103 : {
104 4 : return aFunction ? aFunction->GetValue(aPortion, aBeforeFlag) : aPortion;
105 : }
106 : static int32_t Compare(const Maybe<ComputedTimingFunction>& aLhs,
107 : const Maybe<ComputedTimingFunction>& aRhs);
108 :
109 : private:
110 0 : ComputedTimingFunction(double x1, double y1, double x2, double y2)
111 0 : : mType(nsTimingFunction::Type::CubicBezier)
112 0 : , mTimingFunction(x1, y1, x2, y2) { }
113 0 : ComputedTimingFunction(nsTimingFunction::Type aType, uint32_t aStepsOrFrames)
114 0 : : mType(aType)
115 0 : , mStepsOrFrames(aStepsOrFrames) { }
116 :
117 : nsTimingFunction::Type mType = nsTimingFunction::Type::Linear;
118 : nsSMILKeySpline mTimingFunction;
119 : uint32_t mStepsOrFrames;
120 : };
121 :
122 : inline bool
123 0 : operator==(const Maybe<ComputedTimingFunction>& aLHS,
124 : const nsTimingFunction& aRHS)
125 : {
126 0 : if (aLHS.isNothing()) {
127 0 : return aRHS.mType == nsTimingFunction::Type::Linear;
128 : }
129 0 : return aLHS.value() == aRHS;
130 : }
131 :
132 : inline bool
133 : operator!=(const Maybe<ComputedTimingFunction>& aLHS,
134 : const nsTimingFunction& aRHS)
135 : {
136 : return !(aLHS == aRHS);
137 : }
138 :
139 : } // namespace mozilla
140 :
141 : #endif // mozilla_ComputedTimingFunction_h
|