File indexing completed on 2025-02-02 04:11:04

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include "model/assets/brush_style.hpp"
0010 #include "model/animation/animatable.hpp"
0011 #include "math/vector.hpp"
0012 
0013 namespace glaxnimate::math {
0014 
0015 template<>
0016 QGradientStops lerp<QGradientStops>(const QGradientStops& a, const QGradientStops& b, double factor);
0017 
0018 } // namespace glaxnimate::math
0019 
0020 namespace glaxnimate::model {
0021 
0022 
0023 namespace detail {
0024 
0025 template<>
0026 std::optional<QGradientStops> variant_cast<QGradientStops>(const QVariant& val);
0027 
0028 } // namespace detail
0029 
0030 
0031 class GradientColors : public Asset
0032 {
0033     GLAXNIMATE_OBJECT(GradientColors)
0034 
0035     GLAXNIMATE_ANIMATABLE(QGradientStops, colors, {}, &GradientColors::colors_changed)
0036 
0037 public:
0038     using Asset::Asset;
0039 
0040     QIcon instance_icon() const override;
0041     QString type_name_human() const override;
0042 
0043     bool remove_if_unused(bool clean_lists) override;
0044 
0045     Q_INVOKABLE void split_segment(int segment_index, float factor = 0.5, const QColor& new_color = {});
0046     Q_INVOKABLE void remove_stop(int index);
0047 
0048 Q_SIGNALS:
0049     void colors_changed(const QGradientStops&);
0050 };
0051 
0052 class Gradient : public BrushStyle
0053 {
0054     GLAXNIMATE_OBJECT(Gradient)
0055 
0056 public:
0057     enum GradientType
0058     {
0059         Linear = 1,
0060         Radial = 2,
0061         Conical = 3
0062     };
0063 
0064     Q_ENUM(GradientType)
0065 
0066     GLAXNIMATE_PROPERTY_REFERENCE(GradientColors, colors, &Gradient::valid_refs, &Gradient::is_valid_ref, &Gradient::on_ref_changed)
0067     GLAXNIMATE_PROPERTY(GradientType, type, Linear, {}, {}, PropertyTraits::Visual)
0068 
0069     GLAXNIMATE_ANIMATABLE(QPointF, start_point, {})
0070     GLAXNIMATE_ANIMATABLE(QPointF, end_point, {})
0071 
0072     GLAXNIMATE_ANIMATABLE(QPointF, highlight, {})
0073 
0074 public:
0075     using BrushStyle::BrushStyle;
0076 
0077     QString type_name_human() const override;
0078     QBrush brush_style(FrameTime t) const override;
0079     QBrush constrained_brush_style(FrameTime t, const QRectF& bounds) const override;
0080 
0081     Q_INVOKABLE qreal radius(double t) const;
0082 
0083     static QString gradient_type_name(GradientType t);
0084 
0085     bool remove_if_unused(bool clean_lists) override;
0086 
0087 private:
0088     std::vector<DocumentNode*> valid_refs() const;
0089     bool is_valid_ref(DocumentNode* node) const;
0090 
0091     void on_ref_changed(GradientColors* new_ref, GradientColors* old_ref);
0092     void on_ref_visual_changed();
0093 
0094 
0095     void fill_icon(QPixmap& icon) const override;
0096 
0097     void on_property_changed(const BaseProperty* prop, const QVariant& value) override;
0098 
0099 Q_SIGNALS:
0100     void colors_changed_from(GradientColors* old_use, GradientColors* new_use);
0101 };
0102 
0103 } // namespace glaxnimate::model