/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/EasingStyleValue.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> |
4 | | * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> |
5 | | * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> |
6 | | * Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org> |
7 | | * |
8 | | * SPDX-License-Identifier: BSD-2-Clause |
9 | | */ |
10 | | |
11 | | #pragma once |
12 | | |
13 | | #include <LibWeb/CSS/CSSStyleValue.h> |
14 | | |
15 | | namespace Web::CSS { |
16 | | |
17 | | class EasingStyleValue final : public StyleValueWithDefaultOperators<EasingStyleValue> { |
18 | | public: |
19 | | struct Linear { |
20 | | struct Stop { |
21 | | double offset; |
22 | | Optional<double> position; |
23 | | |
24 | 0 | bool operator==(Stop const&) const = default; |
25 | | }; |
26 | | |
27 | | Vector<Stop> stops; |
28 | | |
29 | 0 | bool operator==(Linear const&) const = default; |
30 | | }; |
31 | | |
32 | | struct CubicBezier { |
33 | | static CubicBezier ease(); |
34 | | static CubicBezier ease_in(); |
35 | | static CubicBezier ease_out(); |
36 | | static CubicBezier ease_in_out(); |
37 | | |
38 | | double x1; |
39 | | double y1; |
40 | | double x2; |
41 | | double y2; |
42 | | |
43 | | struct CachedSample { |
44 | | double x; |
45 | | double y; |
46 | | double t; |
47 | | }; |
48 | | |
49 | | mutable Vector<CachedSample, 64> m_cached_x_samples {}; |
50 | | |
51 | | bool operator==(CubicBezier const&) const; |
52 | | }; |
53 | | |
54 | | struct Steps { |
55 | | enum class Position { |
56 | | JumpStart, |
57 | | JumpEnd, |
58 | | JumpNone, |
59 | | JumpBoth, |
60 | | Start, |
61 | | End, |
62 | | }; |
63 | | |
64 | | static Steps step_start(); |
65 | | static Steps step_end(); |
66 | | |
67 | | unsigned int number_of_intervals; |
68 | | Position position { Position::End }; |
69 | | |
70 | 0 | bool operator==(Steps const&) const = default; |
71 | | }; |
72 | | |
73 | | struct Function : public Variant<Linear, CubicBezier, Steps> { |
74 | | using Variant::Variant; |
75 | | |
76 | | double evaluate_at(double input_progress, bool before_flag) const; |
77 | | |
78 | | String to_string() const; |
79 | | }; |
80 | | |
81 | | static ValueComparingNonnullRefPtr<EasingStyleValue> create(Function const& function) |
82 | 0 | { |
83 | 0 | return adopt_ref(*new (nothrow) EasingStyleValue(function)); |
84 | 0 | } |
85 | 0 | virtual ~EasingStyleValue() override = default; |
86 | | |
87 | 0 | Function const& function() const { return m_function; } |
88 | | |
89 | 0 | virtual String to_string() const override { return m_function.to_string(); } |
90 | | |
91 | 0 | bool properties_equal(EasingStyleValue const& other) const { return m_function == other.m_function; } |
92 | | |
93 | | private: |
94 | | EasingStyleValue(Function const& function) |
95 | 0 | : StyleValueWithDefaultOperators(Type::Easing) |
96 | 0 | , m_function(function) |
97 | 0 | { |
98 | 0 | } |
99 | | |
100 | | Function m_function; |
101 | | }; |
102 | | |
103 | | } |