/**
* The MIT License (MIT)
*
* Copyright (c) 2012-2017 DragonBones team and other contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
namespace DragonBones
{
///
/// - 2D Transform.
///
/// DragonBones 3.0
/// en_US
///
/// - 2D 变换。
///
/// DragonBones 3.0
/// zh_CN
public class TransformDB
{
///
public static readonly float PI = 3.141593f;
///
public static readonly float PI_D = PI * 2.0f;
///
public static readonly float PI_H = PI / 2.0f;
///
public static readonly float PI_Q = PI / 4.0f;
///
public static readonly float RAD_DEG = 180.0f / PI;
///
public static readonly float DEG_RAD = PI / 180.0f;
///
public static float NormalizeRadian(float value)
{
value = (value + PI) % (PI * 2.0f);
value += value > 0.0f ? -PI : PI;
return value;
}
///
/// - Horizontal translate.
///
/// DragonBones 3.0
/// en_US
///
/// - 水平位移。
///
/// DragonBones 3.0
/// zh_CN
public float x = 0.0f;
///
/// - Vertical translate.
///
/// DragonBones 3.0
/// en_US
///
/// - 垂直位移。
///
/// DragonBones 3.0
/// zh_CN
public float y = 0.0f;
///
/// - Skew. (In radians)
///
/// DragonBones 3.0
/// en_US
///
/// - 倾斜。 (以弧度为单位)
///
/// DragonBones 3.0
/// zh_CN
public float skew = 0.0f;
///
/// - rotation. (In radians)
///
/// DragonBones 3.0
/// en_US
///
/// - 旋转。 (以弧度为单位)
///
/// DragonBones 3.0
/// zh_CN
public float rotation = 0.0f;
///
/// - Horizontal Scaling.
///
/// DragonBones 3.0
/// en_US
///
/// - 水平缩放。
///
/// DragonBones 3.0
/// zh_CN
public float scaleX = 1.0f;
///
/// - Vertical scaling.
///
/// DragonBones 3.0
/// en_US
///
/// - 垂直缩放。
///
/// DragonBones 3.0
/// zh_CN
public float scaleY = 1.0f;
///
public TransformDB()
{
}
public override string ToString()
{
return "[object dragonBones.Transform] x:" + this.x + " y:" + this.y + " skew:" + this.skew* 180.0 / PI + " rotation:" + this.rotation* 180.0 / PI + " scaleX:" + this.scaleX + " scaleY:" + this.scaleY;
}
///
public TransformDB CopyFrom(TransformDB value)
{
this.x = value.x;
this.y = value.y;
this.skew = value.skew;
this.rotation = value.rotation;
this.scaleX = value.scaleX;
this.scaleY = value.scaleY;
return this;
}
///
public TransformDB Identity()
{
this.x = this.y = 0.0f;
this.skew = this.rotation = 0.0f;
this.scaleX = this.scaleY = 1.0f;
return this;
}
///
public TransformDB Add(TransformDB value)
{
this.x += value.x;
this.y += value.y;
this.skew += value.skew;
this.rotation += value.rotation;
this.scaleX *= value.scaleX;
this.scaleY *= value.scaleY;
return this;
}
///
public TransformDB Minus(TransformDB value)
{
this.x -= value.x;
this.y -= value.y;
this.skew -= value.skew;
this.rotation -= value.rotation;
this.scaleX /= value.scaleX;
this.scaleY /= value.scaleY;
return this;
}
///
public TransformDB FromMatrix(Matrix matrix)
{
var backupScaleX = this.scaleX;
var backupScaleY = this.scaleY;
this.x = matrix.tx;
this.y = matrix.ty;
var skewX = (float)Math.Atan(-matrix.c / matrix.d);
this.rotation = (float)Math.Atan(matrix.b / matrix.a);
if(float.IsNaN(skewX))
{
skewX = 0.0f;
}
if(float.IsNaN(this.rotation))
{
this.rotation = 0.0f;
}
this.scaleX = (float)((this.rotation > -PI_Q && this.rotation < PI_Q) ? matrix.a / Math.Cos(this.rotation) : matrix.b / Math.Sin(this.rotation));
this.scaleY = (float)((skewX > -PI_Q && skewX < PI_Q) ? matrix.d / Math.Cos(skewX) : -matrix.c / Math.Sin(skewX));
if (backupScaleX >= 0.0f && this.scaleX < 0.0f)
{
this.scaleX = -this.scaleX;
this.rotation = this.rotation - PI;
}
if (backupScaleY >= 0.0f && this.scaleY < 0.0f)
{
this.scaleY = -this.scaleY;
skewX = skewX - PI;
}
this.skew = skewX - this.rotation;
return this;
}
///
public TransformDB ToMatrix(Matrix matrix)
{
if(this.rotation == 0.0f)
{
matrix.a = 1.0f;
matrix.b = 0.0f;
}
else
{
matrix.a = (float)Math.Cos(this.rotation);
matrix.b = (float)Math.Sin(this.rotation);
}
if(this.skew == 0.0f)
{
matrix.c = -matrix.b;
matrix.d = matrix.a;
}
else
{
matrix.c = -(float)Math.Sin(this.skew + this.rotation);
matrix.d = (float)Math.Cos(this.skew + this.rotation);
}
if(this.scaleX != 1.0f)
{
matrix.a *= this.scaleX;
matrix.b *= this.scaleX;
}
if(this.scaleY != 1.0f)
{
matrix.c *= this.scaleY;
matrix.d *= this.scaleY;
}
matrix.tx = this.x;
matrix.ty = this.y;
return this;
}
}
}