-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for TransformConstraints.
- Loading branch information
1 parent
1013fa5
commit 8214b5c
Showing
7 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
lib/src/generated/constraints/transform_constraint_base.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/// Core automatically generated | ||
/// lib/src/generated/constraints/transform_constraint_base.dart. | ||
/// Do not modify manually. | ||
import 'package:rive/src/generated/component_base.dart'; | ||
import 'package:rive/src/generated/constraints/constraint_base.dart'; | ||
import 'package:rive/src/generated/constraints/targeted_constraint_base.dart'; | ||
import 'package:rive/src/rive_core/constraints/targeted_constraint.dart'; | ||
|
||
abstract class TransformConstraintBase extends TargetedConstraint { | ||
static const int typeKey = 83; | ||
@override | ||
int get coreType => TransformConstraintBase.typeKey; | ||
@override | ||
Set<int> get coreTypes => { | ||
TransformConstraintBase.typeKey, | ||
TargetedConstraintBase.typeKey, | ||
ConstraintBase.typeKey, | ||
ComponentBase.typeKey | ||
}; | ||
|
||
/// -------------------------------------------------------------------------- | ||
/// SourceSpaceValue field with key 179. | ||
static const int sourceSpaceValueInitialValue = 0; | ||
int _sourceSpaceValue = sourceSpaceValueInitialValue; | ||
static const int sourceSpaceValuePropertyKey = 179; | ||
|
||
/// The source transform space. | ||
int get sourceSpaceValue => _sourceSpaceValue; | ||
|
||
/// Change the [_sourceSpaceValue] field value. | ||
/// [sourceSpaceValueChanged] will be invoked only if the field's value has | ||
/// changed. | ||
set sourceSpaceValue(int value) { | ||
if (_sourceSpaceValue == value) { | ||
return; | ||
} | ||
int from = _sourceSpaceValue; | ||
_sourceSpaceValue = value; | ||
if (hasValidated) { | ||
sourceSpaceValueChanged(from, value); | ||
} | ||
} | ||
|
||
void sourceSpaceValueChanged(int from, int to); | ||
|
||
/// -------------------------------------------------------------------------- | ||
/// DestSpaceValue field with key 180. | ||
static const int destSpaceValueInitialValue = 0; | ||
int _destSpaceValue = destSpaceValueInitialValue; | ||
static const int destSpaceValuePropertyKey = 180; | ||
|
||
/// The destination transform space. | ||
int get destSpaceValue => _destSpaceValue; | ||
|
||
/// Change the [_destSpaceValue] field value. | ||
/// [destSpaceValueChanged] will be invoked only if the field's value has | ||
/// changed. | ||
set destSpaceValue(int value) { | ||
if (_destSpaceValue == value) { | ||
return; | ||
} | ||
int from = _destSpaceValue; | ||
_destSpaceValue = value; | ||
if (hasValidated) { | ||
destSpaceValueChanged(from, value); | ||
} | ||
} | ||
|
||
void destSpaceValueChanged(int from, int to); | ||
|
||
@override | ||
void copy(TransformConstraintBase source) { | ||
super.copy(source); | ||
_sourceSpaceValue = source._sourceSpaceValue; | ||
_destSpaceValue = source._destSpaceValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:rive/src/rive_core/constraints/constraint.dart'; | ||
import 'package:rive/src/rive_core/math/mat2d.dart'; | ||
import 'package:rive/src/rive_core/math/transform_components.dart'; | ||
import 'package:rive/src/generated/constraints/transform_constraint_base.dart'; | ||
import 'package:rive/src/rive_core/transform_component.dart'; | ||
import 'package:rive/src/rive_core/transform_space.dart'; | ||
export 'package:rive/src/generated/constraints/transform_constraint_base.dart'; | ||
|
||
/// A constraint copies the transform from the target component to the | ||
/// constrained component in world or local space. | ||
class TransformConstraint extends TransformConstraintBase { | ||
final TransformComponents componentsA = TransformComponents(); | ||
final TransformComponents componentsB = TransformComponents(); | ||
|
||
TransformSpace get destSpace => TransformSpace.values[destSpaceValue]; | ||
set destSpace(TransformSpace value) => destSpaceValue = value.index; | ||
|
||
TransformSpace get sourceSpace => TransformSpace.values[sourceSpaceValue]; | ||
set sourceSpace(TransformSpace value) => sourceSpaceValue = value.index; | ||
|
||
@override | ||
void destSpaceValueChanged(int from, int to) => markConstraintDirty(); | ||
|
||
@override | ||
void sourceSpaceValueChanged(int from, int to) => markConstraintDirty(); | ||
|
||
@override | ||
void constrain(TransformComponent component) { | ||
if (target == null) { | ||
return; | ||
} | ||
var transformA = component.worldTransform; | ||
var transformB = Mat2D.clone(target!.worldTransform); | ||
if (sourceSpace == TransformSpace.local) { | ||
var targetParentWorld = parentWorld(target!); | ||
|
||
var inverse = Mat2D(); | ||
if (!Mat2D.invert(inverse, targetParentWorld)) { | ||
return; | ||
} | ||
Mat2D.multiply(transformB, inverse, transformB); | ||
} | ||
if (destSpace == TransformSpace.local && component.parent != null) { | ||
var targetParentWorld = parentWorld(component); | ||
Mat2D.multiply(transformB, targetParentWorld, transformB); | ||
} | ||
|
||
Mat2D.decompose(transformA, componentsA); | ||
Mat2D.decompose(transformB, componentsB); | ||
|
||
var angleA = componentsA[4] % (pi * 2); | ||
var angleB = componentsB[4] % (pi * 2); | ||
var diff = angleB - angleA; | ||
if (diff > pi) { | ||
diff -= pi * 2; | ||
} else if (diff < -pi) { | ||
diff += pi * 2; | ||
} | ||
|
||
var t = strength; | ||
var ti = 1 - t; | ||
|
||
componentsB[4] = angleA + diff * t; | ||
componentsB[0] = componentsA[0] * ti + componentsB[0] * t; | ||
componentsB[1] = componentsA[1] * ti + componentsB[1] * t; | ||
componentsB[2] = componentsA[2] * ti + componentsB[2] * t; | ||
componentsB[3] = componentsA[3] * ti + componentsB[3] * t; | ||
componentsB[5] = componentsA[5] * ti + componentsB[5] * t; | ||
|
||
Mat2D.compose(component.worldTransform, componentsB); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters