EasingStyleValue.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. struct Stop {
  17. double offset;
  18. Optional<double> position;
  19. bool operator==(Stop const&) const = default;
  20. };
  21. Vector<Stop> stops;
  22. bool operator==(Linear const&) const = default;
  23. };
  24. struct CubicBezier {
  25. static CubicBezier ease();
  26. static CubicBezier ease_in();
  27. static CubicBezier ease_out();
  28. static CubicBezier ease_in_out();
  29. double x1;
  30. double y1;
  31. double x2;
  32. double y2;
  33. struct CachedSample {
  34. double x;
  35. double y;
  36. double t;
  37. };
  38. mutable Vector<CachedSample, 64> m_cached_x_samples {};
  39. bool operator==(CubicBezier const&) const;
  40. };
  41. struct Steps {
  42. enum class Position {
  43. JumpStart,
  44. JumpEnd,
  45. JumpNone,
  46. JumpBoth,
  47. Start,
  48. End,
  49. };
  50. static Steps step_start();
  51. static Steps step_end();
  52. unsigned int number_of_intervals;
  53. Position position { Position::End };
  54. bool operator==(Steps const&) const = default;
  55. };
  56. struct Function : public Variant<Linear, CubicBezier, Steps> {
  57. using Variant::Variant;
  58. double evaluate_at(double input_progress, bool before_flag) const;
  59. String to_string() const;
  60. };
  61. static ValueComparingNonnullRefPtr<EasingStyleValue> create(Function const& function)
  62. {
  63. return adopt_ref(*new (nothrow) EasingStyleValue(function));
  64. }
  65. virtual ~EasingStyleValue() override = default;
  66. Function const& function() const { return m_function; }
  67. virtual String to_string() const override { return m_function.to_string(); }
  68. bool properties_equal(EasingStyleValue const& other) const { return m_function == other.m_function; }
  69. private:
  70. EasingStyleValue(Function const& function)
  71. : StyleValueWithDefaultOperators(Type::Easing)
  72. , m_function(function)
  73. {
  74. }
  75. Function m_function;
  76. };
  77. }