| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /**
- * 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
- {
- /// <summary>
- /// - 2D Transform.
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 2D 变换。
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public class TransformDB
- {
- /// <private/>
- public static readonly float PI = 3.141593f;
- /// <private/>
- public static readonly float PI_D = PI * 2.0f;
- /// <private/>
- public static readonly float PI_H = PI / 2.0f;
- /// <private/>
- public static readonly float PI_Q = PI / 4.0f;
- /// <private/>
- public static readonly float RAD_DEG = 180.0f / PI;
- /// <private/>
- public static readonly float DEG_RAD = PI / 180.0f;
- /// <private/>
- public static float NormalizeRadian(float value)
- {
- value = (value + PI) % (PI * 2.0f);
-
- value += value > 0.0f ? -PI : PI;
- return value;
- }
- /// <summary>
- /// - Horizontal translate.
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 水平位移。
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float x = 0.0f;
- /// <summary>
- /// - Vertical translate.
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 垂直位移。
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float y = 0.0f;
- /// <summary>
- /// - Skew. (In radians)
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 倾斜。 (以弧度为单位)
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float skew = 0.0f;
- /// <summary>
- /// - rotation. (In radians)
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 旋转。 (以弧度为单位)
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float rotation = 0.0f;
- /// <summary>
- /// - Horizontal Scaling.
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 水平缩放。
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float scaleX = 1.0f;
- /// <summary>
- /// - Vertical scaling.
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>en_US</language>
- /// <summary>
- /// - 垂直缩放。
- /// </summary>
- /// <version>DragonBones 3.0</version>
- /// <language>zh_CN</language>
- public float scaleY = 1.0f;
- /// <private/>
- 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;
- }
- /// <private/>
- 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;
- }
- /// <private/>
- public TransformDB Identity()
- {
- this.x = this.y = 0.0f;
- this.skew = this.rotation = 0.0f;
- this.scaleX = this.scaleY = 1.0f;
- return this;
- }
- /// <private/>
- 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;
- }
- /// <private/>
- 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;
- }
- /// <private/>
- 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;
- }
- /// <private/>
- 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;
- }
- }
- }
|