Geom.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. namespace DragonBones
  3. {
  4. /**
  5. * @private
  6. */
  7. public class ColorTransform
  8. {
  9. public float alphaMultiplier = 1.0f;
  10. public float redMultiplier = 1.0f;
  11. public float greenMultiplier = 1.0f;
  12. public float blueMultiplier = 1.0f;
  13. public int alphaOffset = 0;
  14. public int redOffset = 0;
  15. public int greenOffset = 0;
  16. public int blueOffset = 0;
  17. public ColorTransform()
  18. {
  19. }
  20. public void CopyFrom(ColorTransform value)
  21. {
  22. alphaMultiplier = value.alphaMultiplier;
  23. redMultiplier = value.redMultiplier;
  24. greenMultiplier = value.greenMultiplier;
  25. blueMultiplier = value.blueMultiplier;
  26. alphaOffset = value.alphaOffset;
  27. redOffset = value.redOffset;
  28. redOffset = value.redOffset;
  29. greenOffset = value.blueOffset;
  30. }
  31. public void Identity()
  32. {
  33. alphaMultiplier = redMultiplier = greenMultiplier = blueMultiplier = 1.0f;
  34. alphaOffset = redOffset = greenOffset = blueOffset = 0;
  35. }
  36. }
  37. public class Point
  38. {
  39. public float x = 0.0f;
  40. public float y = 0.0f;
  41. public Point()
  42. {
  43. }
  44. public void CopyFrom(Point value)
  45. {
  46. x = value.x;
  47. y = value.y;
  48. }
  49. public void Clear()
  50. {
  51. x = y = 0.0f;
  52. }
  53. }
  54. public class Rectangle
  55. {
  56. public float x;
  57. public float y;
  58. public float width;
  59. public float height;
  60. public Rectangle()
  61. {
  62. }
  63. public void CopyFrom(Rectangle value)
  64. {
  65. x = value.x;
  66. y = value.y;
  67. width = value.width;
  68. height = value.height;
  69. }
  70. public void Clear()
  71. {
  72. x = y = 0.0f;
  73. width = height = 0.0f;
  74. }
  75. }
  76. /**
  77. * @language zh_CN
  78. * 2D 变换。
  79. * @version DragonBones 3.0
  80. */
  81. public class Transform
  82. {
  83. /**
  84. * @private
  85. */
  86. public static float NormalizeRadian(float value)
  87. {
  88. value = (value + DragonBones.PI) % (DragonBones.PI * 2);
  89. value += value > 0.0f ? -DragonBones.PI : DragonBones.PI;
  90. return value;
  91. }
  92. /**
  93. * @language zh_CN
  94. * 水平位移。
  95. * @version DragonBones 3.0
  96. */
  97. public float x = 0.0f;
  98. /**
  99. * @language zh_CN
  100. * 垂直位移。
  101. * @version DragonBones 3.0
  102. */
  103. public float y = 0.0f;
  104. /**
  105. * @language zh_CN
  106. * 水平倾斜。 (以弧度为单位)
  107. * @version DragonBones 3.0
  108. */
  109. public float skewX = 0.0f;
  110. /**
  111. * @language zh_CN
  112. * 垂直倾斜。 (以弧度为单位)
  113. * @version DragonBones 3.0
  114. */
  115. public float skewY = 0.0f;
  116. /**
  117. * @language zh_CN
  118. * 水平缩放。
  119. * @version DragonBones 3.0
  120. */
  121. public float scaleX = 1.0f;
  122. /**
  123. * @language zh_CN
  124. * 垂直缩放。
  125. * @version DragonBones 3.0
  126. */
  127. public float scaleY = 1.0f;
  128. /**
  129. * @private
  130. */
  131. public Transform()
  132. {
  133. }
  134. /**
  135. * @private
  136. */
  137. override public string ToString()
  138. {
  139. return "[object DragonBones.Transform] x:" + x + " y:" + y + " skewX:" + skewX + " skewY:" + skewY + " scaleX:" + scaleX + " scaleY:" + scaleY;
  140. }
  141. /**
  142. * @private
  143. */
  144. public Transform CopyFrom(Transform value)
  145. {
  146. x = value.x;
  147. y = value.y;
  148. skewX = value.skewX;
  149. skewY = value.skewY;
  150. scaleX = value.scaleX;
  151. scaleY = value.scaleY;
  152. return this;
  153. }
  154. /**
  155. * @private
  156. */
  157. public Transform Identity()
  158. {
  159. x = y = skewX = skewY = 0.0f;
  160. scaleX = scaleY = 1.0f;
  161. return this;
  162. }
  163. /**
  164. * @private
  165. */
  166. public Transform Add(Transform value)
  167. {
  168. x += value.x;
  169. y += value.y;
  170. skewX += value.skewX;
  171. skewY += value.skewY;
  172. scaleX *= value.scaleX;
  173. scaleY *= value.scaleY;
  174. return this;
  175. }
  176. /**
  177. * @private
  178. */
  179. public Transform Minus(Transform value)
  180. {
  181. x -= value.x;
  182. y -= value.y;
  183. skewX = NormalizeRadian(skewX - value.skewX);
  184. skewY = NormalizeRadian(skewY - value.skewY);
  185. scaleX /= value.scaleX;
  186. scaleY /= value.scaleY;
  187. return this;
  188. }
  189. /**
  190. * @private
  191. */
  192. public Transform FromMatrix(Matrix matrix)
  193. {
  194. var backupScaleX = scaleX;
  195. var backupScaleY = scaleY;
  196. x = matrix.tx;
  197. y = matrix.ty;
  198. skewX = (float)Math.Atan(-matrix.c / matrix.d);
  199. skewY = (float)Math.Atan(matrix.b / matrix.a);
  200. if (float.IsNaN(skewX))
  201. {
  202. skewX = 0.0f;
  203. }
  204. if (float.IsNaN(skewY))
  205. {
  206. skewY = 0.0f;
  207. }
  208. scaleY = (float)((skewX > -DragonBones.PI_Q && skewX < DragonBones.PI_Q) ? matrix.d / Math.Cos(skewX) : -matrix.c / Math.Sin(skewX));
  209. scaleX = (float)((skewY > -DragonBones.PI_Q && skewY < DragonBones.PI_Q) ? matrix.a / Math.Cos(skewY) : matrix.b / Math.Sin(skewY));
  210. if (backupScaleX >= 0.0f && scaleX < 0.0f)
  211. {
  212. scaleX = -scaleX;
  213. skewY = skewY - DragonBones.PI;
  214. }
  215. if (backupScaleY >= 0.0f && scaleY < 0.0f)
  216. {
  217. scaleY = -scaleY;
  218. skewX = skewX - DragonBones.PI;
  219. }
  220. return this;
  221. }
  222. /**
  223. * @language zh_CN
  224. * 转换为矩阵。
  225. * @param 矩阵。
  226. * @version DragonBones 3.0
  227. */
  228. public Transform ToMatrix(Matrix matrix)
  229. {
  230. if (skewX != 0.0f || skewY != 0.0f)
  231. {
  232. matrix.a = (float)Math.Cos(skewY);
  233. matrix.b = (float)Math.Sin(skewY);
  234. if (skewX == skewY)
  235. {
  236. matrix.c = -matrix.b;
  237. matrix.d = matrix.a;
  238. }
  239. else
  240. {
  241. matrix.c = -(float)Math.Sin(skewX);
  242. matrix.d = (float)Math.Cos(skewX);
  243. }
  244. if (scaleX != 1.0f || scaleY != 1.0f)
  245. {
  246. matrix.a *= scaleX;
  247. matrix.b *= scaleX;
  248. matrix.c *= scaleY;
  249. matrix.d *= scaleY;
  250. }
  251. }
  252. else
  253. {
  254. matrix.a = scaleX;
  255. matrix.b = 0.0f;
  256. matrix.c = 0.0f;
  257. matrix.d = scaleY;
  258. }
  259. matrix.tx = x;
  260. matrix.ty = y;
  261. return this;
  262. }
  263. /**
  264. * @language zh_CN
  265. * 旋转。 (以弧度为单位)
  266. * @version DragonBones 3.0
  267. */
  268. public float rotation
  269. {
  270. get { return skewY; }
  271. set
  272. {
  273. var dValue = value - skewY;
  274. skewX += dValue;
  275. skewY += dValue;
  276. }
  277. }
  278. }
  279. /**
  280. * @language zh_CN
  281. * 2D 矩阵。
  282. * @version DragonBones 3.0
  283. */
  284. public class Matrix
  285. {
  286. public float a = 1.0f;
  287. public float b = 0.0f;
  288. public float c = 0.0f;
  289. public float d = 1.0f;
  290. public float tx = 0.0f;
  291. public float ty = 0.0f;
  292. public Matrix()
  293. {
  294. }
  295. /**
  296. * @private
  297. */
  298. override public string ToString()
  299. {
  300. return "[object DragonBones.Matrix] a:" + a + " b:" + b + " c:" + c + " d:" + d + " tx:" + tx + " ty:" + ty;
  301. }
  302. /**
  303. * @language zh_CN
  304. * 复制矩阵。
  305. * @param value 需要复制的矩阵。
  306. * @version DragonBones 3.0
  307. */
  308. public Matrix CopyFrom(Matrix value)
  309. {
  310. a = value.a;
  311. b = value.b;
  312. c = value.c;
  313. d = value.d;
  314. tx = value.tx;
  315. ty = value.ty;
  316. return this;
  317. }
  318. /**
  319. * @language zh_CN
  320. * 转换为恒等矩阵。
  321. * @version DragonBones 3.0
  322. */
  323. public Matrix Identity()
  324. {
  325. a = d = 1.0f;
  326. b = c = 0.0f;
  327. tx = ty = 0.0f;
  328. return this;
  329. }
  330. /**
  331. * @language zh_CN
  332. * 将当前矩阵与另一个矩阵相乘。
  333. * @param value 需要相乘的矩阵。
  334. * @version DragonBones 3.0
  335. */
  336. public Matrix Concat(Matrix value)
  337. {
  338. var aA = a * value.a;
  339. var bA = 0.0f;
  340. var cA = 0.0f;
  341. var dA = d * value.d;
  342. var txA = tx * value.a + value.tx;
  343. var tyA = ty * value.d + value.ty;
  344. if (b != 0.0f || c != 0.0f)
  345. {
  346. aA += b * value.c;
  347. dA += c * value.b;
  348. bA += b * value.d;
  349. cA += c * value.a;
  350. }
  351. if (value.b != 0.0f || value.c != 0.0f)
  352. {
  353. bA += a * value.b;
  354. cA += d * value.c;
  355. txA += ty * value.c;
  356. tyA += tx * value.b;
  357. }
  358. a = aA;
  359. b = bA;
  360. c = cA;
  361. d = dA;
  362. tx = txA;
  363. ty = tyA;
  364. return this;
  365. }
  366. /**
  367. * @language zh_CN
  368. * 转换为逆矩阵。
  369. * @version DragonBones 3.0
  370. */
  371. public Matrix Invert()
  372. {
  373. var aA = a;
  374. var bA = b;
  375. var cA = c;
  376. var dA = d;
  377. var txA = tx;
  378. var tyA = ty;
  379. if (bA == 0.0f && cA == 0.0f)
  380. {
  381. b = c = 0.0f;
  382. if (aA == 0.0f || dA == 0.0f)
  383. {
  384. a = b = tx = ty = 0.0f;
  385. }
  386. else
  387. {
  388. aA = a = 1.0f / aA;
  389. dA = d = 1.0f / dA;
  390. tx = -aA * txA;
  391. ty = -dA * tyA;
  392. }
  393. return this;
  394. }
  395. var determinant = aA * dA - bA * cA;
  396. if (determinant == 0.0f)
  397. {
  398. a = d = 1.0f;
  399. b = c = 0.0f;
  400. tx = ty = 0.0f;
  401. return this;
  402. }
  403. determinant = 1.0f / determinant;
  404. var k = a = dA * determinant;
  405. bA = b = -bA * determinant;
  406. cA = c = -cA * determinant;
  407. dA = d = aA * determinant;
  408. tx = -(k * txA + cA * tyA);
  409. ty = -(bA * txA + dA * tyA);
  410. return this;
  411. }
  412. /**
  413. * @language zh_CN
  414. * 将矩阵转换应用于指定点。
  415. * @param x 横坐标。
  416. * @param y 纵坐标。
  417. * @param result 应用转换之后的坐标。
  418. * @params delta 是否忽略 tx,ty 对坐标的转换。
  419. * @version DragonBones 3.0
  420. */
  421. public void TransformPoint(float x, float y, Point result, bool delta = false)
  422. {
  423. result.x = a * x + c * y;
  424. result.y = b * x + d * y;
  425. if (!delta)
  426. {
  427. result.x += tx;
  428. result.y += ty;
  429. }
  430. }
  431. }
  432. }