Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / src / constraints / transform_constraint.cpp
1 #include "rive/constraints/transform_constraint.hpp"
2 #include "rive/transform_component.hpp"
3 #include "rive/math/mat2d.hpp"
4 #include "rive/math/math_types.hpp"
5
6 using namespace rive;
7
8 void TransformConstraint::constrain(TransformComponent* component) {
9     if (m_Target == nullptr) {
10         return;
11     }
12
13     const Mat2D& transformA = component->worldTransform();
14     Mat2D transformB(m_Target->worldTransform());
15     if (sourceSpace() == TransformSpace::local) {
16         const Mat2D& targetParentWorld = getParentWorld(*m_Target);
17
18         Mat2D inverse;
19         if (!targetParentWorld.invert(&inverse)) {
20             return;
21         }
22         transformB = inverse * transformB;
23     }
24     if (destSpace() == TransformSpace::local) {
25         const Mat2D& targetParentWorld = getParentWorld(*component);
26         transformB = targetParentWorld * transformB;
27     }
28
29     m_ComponentsA = transformA.decompose();
30     m_ComponentsB = transformB.decompose();
31
32     float angleA = std::fmod(m_ComponentsA.rotation(), math::PI * 2);
33     float angleB = std::fmod(m_ComponentsB.rotation(), math::PI * 2);
34     float diff = angleB - angleA;
35     if (diff > math::PI) {
36         diff -= math::PI * 2;
37     } else if (diff < -math::PI) {
38         diff += math::PI * 2;
39     }
40
41     float t = strength();
42     float ti = 1.0f - t;
43
44     m_ComponentsB.rotation(angleA + diff * t);
45     m_ComponentsB.x(m_ComponentsA.x() * ti + m_ComponentsB.x() * t);
46     m_ComponentsB.y(m_ComponentsA.y() * ti + m_ComponentsB.y() * t);
47     m_ComponentsB.scaleX(m_ComponentsA.scaleX() * ti + m_ComponentsB.scaleX() * t);
48     m_ComponentsB.scaleY(m_ComponentsA.scaleY() * ti + m_ComponentsB.scaleY() * t);
49     m_ComponentsB.skew(m_ComponentsA.skew() * ti + m_ComponentsB.skew() * t);
50
51     component->mutableWorldTransform() = Mat2D::compose(m_ComponentsB);
52 }