EasingStyleValue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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. #pragma once
  11. #include <LibWeb/CSS/CSSStyleValue.h>
  12. namespace Web::CSS {
  13. class EasingStyleValue final : public StyleValueWithDefaultOperators<EasingStyleValue> {
  14. public:
  15. struct Linear {
  16. static Linear identity();
  17. struct Stop {
  18. double output;
  19. Optional<double> input;
  20. // "NOTE: Serialization relies on whether or not an input progress value was originally supplied,
  21. // so that information should be retained in the internal representation."
  22. bool had_explicit_input;
  23. bool operator==(Stop const&) const = default;
  24. };
  25. Vector<Stop> stops;
  26. bool operator==(Linear const&) const = default;
  27. double evaluate_at(double input_progress, bool before_flag) const;
  28. String to_string() const;
  29. Linear(Vector<Stop> stops);
  30. };
  31. struct CubicBezier {
  32. static CubicBezier ease();
  33. static CubicBezier ease_in();
  34. static CubicBezier ease_out();
  35. static CubicBezier ease_in_out();
  36. double x1;
  37. double y1;
  38. double x2;
  39. double y2;
  40. struct CachedSample {
  41. double x;
  42. double y;
  43. double t;
  44. };
  45. mutable Vector<CachedSample, 64> m_cached_x_samples {};
  46. bool operator==(CubicBezier const&) const;
  47. double evaluate_at(double input_progress, bool before_flag) const;
  48. String to_string() const;
  49. };
  50. struct Steps {
  51. enum class Position {
  52. JumpStart,
  53. JumpEnd,
  54. JumpNone,
  55. JumpBoth,
  56. Start,
  57. End,
  58. };
  59. static Steps step_start();
  60. static Steps step_end();
  61. unsigned int number_of_intervals;
  62. Position position { Position::End };
  63. bool operator==(Steps const&) const = default;
  64. double evaluate_at(double input_progress, bool before_flag) const;
  65. String to_string() const;
  66. };
  67. struct Function : public Variant<Linear, CubicBezier, Steps> {
  68. using Variant::Variant;
  69. double evaluate_at(double input_progress, bool before_flag) const;
  70. String to_string() const;
  71. };
  72. static ValueComparingNonnullRefPtr<EasingStyleValue> create(Function const& function)
  73. {
  74. return adopt_ref(*new (nothrow) EasingStyleValue(function));
  75. }
  76. virtual ~EasingStyleValue() override = default;
  77. Function const& function() const { return m_function; }
  78. virtual String to_string() const override { return m_function.to_string(); }
  79. bool properties_equal(EasingStyleValue const& other) const { return m_function == other.m_function; }
  80. private:
  81. EasingStyleValue(Function const& function)
  82. : StyleValueWithDefaultOperators(Type::Easing)
  83. , m_function(function)
  84. {
  85. }
  86. Function m_function;
  87. };
  88. }