123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- using System;
- namespace DragonBones
- {
- /**
- * @private
- */
- public class ColorTransform
- {
- public float alphaMultiplier = 1.0f;
- public float redMultiplier = 1.0f;
- public float greenMultiplier = 1.0f;
- public float blueMultiplier = 1.0f;
- public int alphaOffset = 0;
- public int redOffset = 0;
- public int greenOffset = 0;
- public int blueOffset = 0;
- public ColorTransform()
- {
- }
- public void CopyFrom(ColorTransform value)
- {
- alphaMultiplier = value.alphaMultiplier;
- redMultiplier = value.redMultiplier;
- greenMultiplier = value.greenMultiplier;
- blueMultiplier = value.blueMultiplier;
- alphaOffset = value.alphaOffset;
- redOffset = value.redOffset;
- redOffset = value.redOffset;
- greenOffset = value.blueOffset;
- }
- public void Identity()
- {
- alphaMultiplier = redMultiplier = greenMultiplier = blueMultiplier = 1.0f;
- alphaOffset = redOffset = greenOffset = blueOffset = 0;
- }
- }
-
- public class Point
- {
- public float x = 0.0f;
- public float y = 0.0f;
- public Point()
- {
- }
- public void CopyFrom(Point value)
- {
- x = value.x;
- y = value.y;
- }
- public void Clear()
- {
- x = y = 0.0f;
- }
- }
-
- public class Rectangle
- {
- public float x;
- public float y;
- public float width;
- public float height;
- public Rectangle()
- {
- }
- public void CopyFrom(Rectangle value)
- {
- x = value.x;
- y = value.y;
- width = value.width;
- height = value.height;
- }
- public void Clear()
- {
- x = y = 0.0f;
- width = height = 0.0f;
- }
- }
- /**
- * @language zh_CN
- * 2D 变换。
- * @version DragonBones 3.0
- */
- public class Transform
- {
- /**
- * @private
- */
- public static float NormalizeRadian(float value)
- {
- value = (value + DragonBones.PI) % (DragonBones.PI * 2);
- value += value > 0.0f ? -DragonBones.PI : DragonBones.PI;
- return value;
- }
- /**
- * @language zh_CN
- * 水平位移。
- * @version DragonBones 3.0
- */
- public float x = 0.0f;
- /**
- * @language zh_CN
- * 垂直位移。
- * @version DragonBones 3.0
- */
- public float y = 0.0f;
- /**
- * @language zh_CN
- * 水平倾斜。 (以弧度为单位)
- * @version DragonBones 3.0
- */
- public float skewX = 0.0f;
- /**
- * @language zh_CN
- * 垂直倾斜。 (以弧度为单位)
- * @version DragonBones 3.0
- */
- public float skewY = 0.0f;
- /**
- * @language zh_CN
- * 水平缩放。
- * @version DragonBones 3.0
- */
- public float scaleX = 1.0f;
- /**
- * @language zh_CN
- * 垂直缩放。
- * @version DragonBones 3.0
- */
- public float scaleY = 1.0f;
- /**
- * @private
- */
- public Transform()
- {
- }
- /**
- * @private
- */
- override public string ToString()
- {
- return "[object DragonBones.Transform] x:" + x + " y:" + y + " skewX:" + skewX + " skewY:" + skewY + " scaleX:" + scaleX + " scaleY:" + scaleY;
- }
- /**
- * @private
- */
- public Transform CopyFrom(Transform value)
- {
- x = value.x;
- y = value.y;
- skewX = value.skewX;
- skewY = value.skewY;
- scaleX = value.scaleX;
- scaleY = value.scaleY;
- return this;
- }
- /**
- * @private
- */
- public Transform Identity()
- {
- x = y = skewX = skewY = 0.0f;
- scaleX = scaleY = 1.0f;
- return this;
- }
- /**
- * @private
- */
- public Transform Add(Transform value)
- {
- x += value.x;
- y += value.y;
- skewX += value.skewX;
- skewY += value.skewY;
- scaleX *= value.scaleX;
- scaleY *= value.scaleY;
- return this;
- }
- /**
- * @private
- */
- public Transform Minus(Transform value)
- {
- x -= value.x;
- y -= value.y;
- skewX = NormalizeRadian(skewX - value.skewX);
- skewY = NormalizeRadian(skewY - value.skewY);
- scaleX /= value.scaleX;
- scaleY /= value.scaleY;
- return this;
- }
- /**
- * @private
- */
- public Transform FromMatrix(Matrix matrix)
- {
- var backupScaleX = scaleX;
- var backupScaleY = scaleY;
- x = matrix.tx;
- y = matrix.ty;
- skewX = (float)Math.Atan(-matrix.c / matrix.d);
- skewY = (float)Math.Atan(matrix.b / matrix.a);
- if (float.IsNaN(skewX))
- {
- skewX = 0.0f;
- }
- if (float.IsNaN(skewY))
- {
- skewY = 0.0f;
- }
- scaleY = (float)((skewX > -DragonBones.PI_Q && skewX < DragonBones.PI_Q) ? matrix.d / Math.Cos(skewX) : -matrix.c / Math.Sin(skewX));
- scaleX = (float)((skewY > -DragonBones.PI_Q && skewY < DragonBones.PI_Q) ? matrix.a / Math.Cos(skewY) : matrix.b / Math.Sin(skewY));
- if (backupScaleX >= 0.0f && scaleX < 0.0f)
- {
- scaleX = -scaleX;
- skewY = skewY - DragonBones.PI;
- }
- if (backupScaleY >= 0.0f && scaleY < 0.0f)
- {
- scaleY = -scaleY;
- skewX = skewX - DragonBones.PI;
- }
- return this;
- }
- /**
- * @language zh_CN
- * 转换为矩阵。
- * @param 矩阵。
- * @version DragonBones 3.0
- */
- public Transform ToMatrix(Matrix matrix)
- {
- if (skewX != 0.0f || skewY != 0.0f)
- {
- matrix.a = (float)Math.Cos(skewY);
- matrix.b = (float)Math.Sin(skewY);
- if (skewX == skewY)
- {
- matrix.c = -matrix.b;
- matrix.d = matrix.a;
- }
- else
- {
- matrix.c = -(float)Math.Sin(skewX);
- matrix.d = (float)Math.Cos(skewX);
- }
- if (scaleX != 1.0f || scaleY != 1.0f)
- {
- matrix.a *= scaleX;
- matrix.b *= scaleX;
- matrix.c *= scaleY;
- matrix.d *= scaleY;
- }
- }
- else
- {
- matrix.a = scaleX;
- matrix.b = 0.0f;
- matrix.c = 0.0f;
- matrix.d = scaleY;
- }
- matrix.tx = x;
- matrix.ty = y;
- return this;
- }
- /**
- * @language zh_CN
- * 旋转。 (以弧度为单位)
- * @version DragonBones 3.0
- */
- public float rotation
- {
- get { return skewY; }
- set
- {
- var dValue = value - skewY;
- skewX += dValue;
- skewY += dValue;
- }
- }
- }
- /**
- * @language zh_CN
- * 2D 矩阵。
- * @version DragonBones 3.0
- */
- public class Matrix
- {
- public float a = 1.0f;
- public float b = 0.0f;
- public float c = 0.0f;
- public float d = 1.0f;
- public float tx = 0.0f;
- public float ty = 0.0f;
- public Matrix()
- {
- }
- /**
- * @private
- */
- override public string ToString()
- {
- return "[object DragonBones.Matrix] a:" + a + " b:" + b + " c:" + c + " d:" + d + " tx:" + tx + " ty:" + ty;
- }
- /**
- * @language zh_CN
- * 复制矩阵。
- * @param value 需要复制的矩阵。
- * @version DragonBones 3.0
- */
- public Matrix CopyFrom(Matrix value)
- {
- a = value.a;
- b = value.b;
- c = value.c;
- d = value.d;
- tx = value.tx;
- ty = value.ty;
- return this;
- }
- /**
- * @language zh_CN
- * 转换为恒等矩阵。
- * @version DragonBones 3.0
- */
- public Matrix Identity()
- {
- a = d = 1.0f;
- b = c = 0.0f;
- tx = ty = 0.0f;
- return this;
- }
- /**
- * @language zh_CN
- * 将当前矩阵与另一个矩阵相乘。
- * @param value 需要相乘的矩阵。
- * @version DragonBones 3.0
- */
- public Matrix Concat(Matrix value)
- {
- var aA = a * value.a;
- var bA = 0.0f;
- var cA = 0.0f;
- var dA = d * value.d;
- var txA = tx * value.a + value.tx;
- var tyA = ty * value.d + value.ty;
- if (b != 0.0f || c != 0.0f)
- {
- aA += b * value.c;
- dA += c * value.b;
- bA += b * value.d;
- cA += c * value.a;
- }
- if (value.b != 0.0f || value.c != 0.0f)
- {
- bA += a * value.b;
- cA += d * value.c;
- txA += ty * value.c;
- tyA += tx * value.b;
- }
- a = aA;
- b = bA;
- c = cA;
- d = dA;
- tx = txA;
- ty = tyA;
- return this;
- }
- /**
- * @language zh_CN
- * 转换为逆矩阵。
- * @version DragonBones 3.0
- */
- public Matrix Invert()
- {
- var aA = a;
- var bA = b;
- var cA = c;
- var dA = d;
- var txA = tx;
- var tyA = ty;
- if (bA == 0.0f && cA == 0.0f)
- {
- b = c = 0.0f;
- if (aA == 0.0f || dA == 0.0f)
- {
- a = b = tx = ty = 0.0f;
- }
- else
- {
- aA = a = 1.0f / aA;
- dA = d = 1.0f / dA;
- tx = -aA * txA;
- ty = -dA * tyA;
- }
- return this;
- }
- var determinant = aA * dA - bA * cA;
- if (determinant == 0.0f)
- {
- a = d = 1.0f;
- b = c = 0.0f;
- tx = ty = 0.0f;
- return this;
- }
- determinant = 1.0f / determinant;
- var k = a = dA * determinant;
- bA = b = -bA * determinant;
- cA = c = -cA * determinant;
- dA = d = aA * determinant;
- tx = -(k * txA + cA * tyA);
- ty = -(bA * txA + dA * tyA);
- return this;
- }
- /**
- * @language zh_CN
- * 将矩阵转换应用于指定点。
- * @param x 横坐标。
- * @param y 纵坐标。
- * @param result 应用转换之后的坐标。
- * @params delta 是否忽略 tx,ty 对坐标的转换。
- * @version DragonBones 3.0
- */
- public void TransformPoint(float x, float y, Point result, bool delta = false)
- {
- result.x = a * x + c * y;
- result.y = b * x + d * y;
- if (!delta)
- {
- result.x += tx;
- result.y += ty;
- }
- }
- }
- }
|