Slot.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @language zh_CN
  7. * 插槽,附着在骨骼上,控制显示对象的显示状态和属性。
  8. * 一个骨骼上可以包含多个插槽。
  9. * 一个插槽中可以包含多个显示对象,同一时间只能显示其中的一个显示对象,但可以在动画播放的过程中切换显示对象实现帧动画。
  10. * 显示对象可以是普通的图片纹理,也可以是子骨架的显示容器,网格显示对象,还可以是自定义的其他显示对象。
  11. * @see DragonBones.Armature
  12. * @see DragonBones.Bone
  13. * @see DragonBones.SlotData
  14. * @version DragonBones 3.0
  15. */
  16. public abstract class Slot : TransformObject
  17. {
  18. /**
  19. * @private
  20. */
  21. protected static readonly Point _helpPoint = new Point();
  22. /**
  23. * @private
  24. */
  25. protected static readonly Matrix _helpMatrix = new Matrix();
  26. /**
  27. * @language zh_CN
  28. * 显示对象受到控制的动画状态或混合组名称,设置为 null 则表示受所有的动画状态控制。
  29. * @default null
  30. * @see DragonBones.AnimationState#displayControl
  31. * @see DragonBones.AnimationState#name
  32. * @see DragonBones.AnimationState#group
  33. * @version DragonBones 4.5
  34. */
  35. public string displayController;
  36. /**
  37. * @private
  38. */
  39. protected bool _displayDirty;
  40. /**
  41. * @private
  42. */
  43. protected bool _zOrderDirty;
  44. /**
  45. * @private
  46. */
  47. protected bool _blendModeDirty;
  48. /**
  49. * @private
  50. */
  51. internal bool _colorDirty;
  52. /**
  53. * @private
  54. */
  55. protected bool _originalDirty;
  56. /**
  57. * @private
  58. */
  59. protected bool _transformDirty;
  60. /**
  61. * @private
  62. */
  63. internal bool _meshDirty;
  64. /**
  65. * @private
  66. */
  67. protected int _updateState;
  68. /**
  69. * @private
  70. */
  71. protected BlendMode _blendMode;
  72. /**
  73. * @private
  74. */
  75. protected int _displayIndex;
  76. /**
  77. * @private
  78. */
  79. protected int _cachedFrameIndex;
  80. /**
  81. * @private
  82. */
  83. internal int _zOrder;
  84. /**
  85. * @private
  86. */
  87. protected float _pivotX;
  88. /**
  89. * @private
  90. */
  91. protected float _pivotY;
  92. /**
  93. * @private
  94. */
  95. protected readonly Matrix _localMatrix = new Matrix();
  96. /**
  97. * @private
  98. */
  99. internal readonly ColorTransform _colorTransform = new ColorTransform();
  100. /**
  101. * @private
  102. */
  103. internal readonly List<float> _ffdVertices = new List<float>();
  104. /**
  105. * @private
  106. */
  107. protected readonly List<object> _displayList = new List<object>();
  108. /**
  109. * @private
  110. */
  111. internal readonly List<TextureData> _textureDatas = new List<TextureData>();
  112. /**
  113. * @private
  114. */
  115. internal readonly List<DisplayData> _replacedDisplayDatas = new List<DisplayData>();
  116. /**
  117. * @private
  118. */
  119. protected readonly List<Bone> _meshBones = new List<Bone>();
  120. /**
  121. * @private
  122. */
  123. protected SkinSlotData _skinSlotData;
  124. /**
  125. * @private
  126. */
  127. protected DisplayData _displayData;
  128. /**
  129. * @private
  130. */
  131. protected DisplayData _replacedDisplayData;
  132. /**
  133. * @private
  134. */
  135. protected TextureData _textureData;
  136. /**
  137. * @private
  138. */
  139. internal MeshData _meshData;
  140. /**
  141. * @private
  142. */
  143. protected BoundingBoxData _boundingBoxData;
  144. /**
  145. * @private
  146. */
  147. protected object _rawDisplay;
  148. /**
  149. * @private
  150. */
  151. protected object _meshDisplay;
  152. /**
  153. * @private
  154. */
  155. protected object _display;
  156. /**
  157. * @private
  158. */
  159. protected Armature _childArmature;
  160. /**
  161. * @private
  162. */
  163. internal List<int> _cachedFrameIndices;
  164. /**
  165. * @private
  166. */
  167. public Slot()
  168. {
  169. }
  170. /**
  171. * @private
  172. */
  173. protected override void _onClear()
  174. {
  175. base._onClear();
  176. var disposeDisplayList = new List<object>();
  177. for (int i = 0, l = _displayList.Count; i < l; ++i)
  178. {
  179. var eachDisplay = _displayList[i];
  180. if (
  181. eachDisplay != _rawDisplay && eachDisplay != _meshDisplay &&
  182. !disposeDisplayList.Contains(eachDisplay)
  183. )
  184. {
  185. disposeDisplayList.Add(eachDisplay);
  186. }
  187. }
  188. for (int i = 0, l = disposeDisplayList.Count; i < l; ++i)
  189. {
  190. var eachDisplay = disposeDisplayList[i];
  191. if (eachDisplay is Armature)
  192. {
  193. (eachDisplay as Armature).Dispose();
  194. }
  195. else
  196. {
  197. _disposeDisplay(eachDisplay);
  198. }
  199. }
  200. if (_meshDisplay != null && _meshDisplay != _rawDisplay) // May be _meshDisplay and _rawDisplay is the same one.
  201. {
  202. _disposeDisplay(_meshDisplay);
  203. }
  204. if (_rawDisplay != null)
  205. {
  206. _disposeDisplay(_rawDisplay);
  207. }
  208. displayController = null;
  209. _displayDirty = false;
  210. _zOrderDirty = false;
  211. _blendModeDirty = false;
  212. _colorDirty = false;
  213. _originalDirty = false;
  214. _transformDirty = false;
  215. _meshDirty = false;
  216. _updateState = -1;
  217. _blendMode = BlendMode.Normal;
  218. _displayIndex = -1;
  219. _cachedFrameIndex = -1;
  220. _zOrder = -1;
  221. _pivotX = 0.0f;
  222. _pivotY = 0.0f;
  223. _localMatrix.Identity();
  224. _colorTransform.Identity();
  225. _ffdVertices.Clear();
  226. _displayList.Clear();
  227. _textureDatas.Clear();
  228. _replacedDisplayDatas.Clear();
  229. _meshBones.Clear();
  230. _skinSlotData = null;
  231. _displayData = null;
  232. _replacedDisplayData = null;
  233. _textureData = null;
  234. _meshData = null;
  235. _boundingBoxData = null;
  236. _rawDisplay = null;
  237. _meshDisplay = null;
  238. _display = null;
  239. _childArmature = null;
  240. _cachedFrameIndices = null;
  241. }
  242. /**
  243. * @private
  244. */
  245. protected abstract void _initDisplay(object value);
  246. /**
  247. * @private
  248. */
  249. protected abstract void _disposeDisplay(object value);
  250. /**
  251. * @private
  252. */
  253. protected abstract void _onUpdateDisplay();
  254. /**
  255. * @private
  256. */
  257. protected abstract void _addDisplay();
  258. /**
  259. * @private
  260. */
  261. protected abstract void _replaceDisplay(object value);
  262. /**
  263. * @private
  264. */
  265. protected abstract void _removeDisplay();
  266. /**
  267. * @private
  268. */
  269. protected abstract void _updateZOrder();
  270. /**
  271. * @private
  272. */
  273. internal abstract void _updateVisible();
  274. /**
  275. * @private
  276. */
  277. protected abstract void _updateBlendMode();
  278. /**
  279. * @private
  280. */
  281. protected abstract void _updateColor();
  282. /**
  283. * @private
  284. */
  285. protected abstract void _updateFrame();
  286. /**
  287. * @private
  288. */
  289. protected abstract void _updateMesh();
  290. /**
  291. * @private
  292. */
  293. protected abstract void _updateTransform(bool isSkinnedMesh);
  294. /**
  295. * @private
  296. */
  297. protected bool _isMeshBonesUpdate()
  298. {
  299. foreach (var bone in _meshBones)
  300. {
  301. if (bone._transformDirty != BoneTransformDirty.None)
  302. {
  303. return true;
  304. }
  305. }
  306. return false;
  307. }
  308. /**
  309. * @private
  310. */
  311. protected void _updateDisplayData()
  312. {
  313. var prevDisplayData = _displayData;
  314. var prevReplaceDisplayData = _replacedDisplayData;
  315. var prevTextureData = _textureData;
  316. var prevMeshData = _meshData;
  317. var currentDisplay = _displayIndex >= 0 && _displayIndex < _displayList.Count ? _displayList[_displayIndex] : null;
  318. if (_displayIndex >= 0 && _displayIndex < _skinSlotData.displays.Count)
  319. {
  320. _displayData = _skinSlotData.displays[_displayIndex];
  321. }
  322. else
  323. {
  324. _displayData = null;
  325. }
  326. if (_displayIndex >= 0 && _displayIndex < _replacedDisplayDatas.Count)
  327. {
  328. _replacedDisplayData = _replacedDisplayDatas[_displayIndex];
  329. }
  330. else
  331. {
  332. _replacedDisplayData = null;
  333. }
  334. if (_displayData != prevDisplayData || _replacedDisplayData != prevReplaceDisplayData || this._display != currentDisplay)
  335. {
  336. var currentDisplayData = _replacedDisplayData != null ? _replacedDisplayData : _displayData;
  337. if (currentDisplayData != null && (currentDisplay == _rawDisplay || currentDisplay == _meshDisplay))
  338. {
  339. if (_replacedDisplayData != null)
  340. {
  341. _textureData = _replacedDisplayData.texture;
  342. }
  343. else if (_displayIndex < _textureDatas.Count && _textureDatas[_displayIndex] != null)
  344. {
  345. _textureData = _textureDatas[_displayIndex];
  346. }
  347. else
  348. {
  349. _textureData = _displayData.texture;
  350. }
  351. if (currentDisplay == _meshDisplay)
  352. {
  353. if (_replacedDisplayData != null && _replacedDisplayData.mesh != null)
  354. {
  355. _meshData = _replacedDisplayData.mesh;
  356. }
  357. else
  358. {
  359. _meshData = _displayData.mesh;
  360. }
  361. }
  362. else
  363. {
  364. _meshData = null;
  365. }
  366. // Update pivot offset.
  367. if (_meshData != null)
  368. {
  369. _pivotX = 0.0f;
  370. _pivotY = 0.0f;
  371. }
  372. else if (_textureData != null)
  373. {
  374. var scale = _armature.armatureData.scale;
  375. _pivotX = currentDisplayData.pivot.x;
  376. _pivotY = currentDisplayData.pivot.y;
  377. if (currentDisplayData.isRelativePivot)
  378. {
  379. var rect = _textureData.frame != null ? _textureData.frame : _textureData.region;
  380. var width = rect.width * scale;
  381. var height = rect.height * scale;
  382. if (_textureData.rotated)
  383. {
  384. width = rect.height;
  385. height = rect.width;
  386. }
  387. _pivotX *= width;
  388. _pivotY *= height;
  389. }
  390. if (_textureData.frame != null)
  391. {
  392. _pivotX += _textureData.frame.x * scale;
  393. _pivotY += _textureData.frame.y * scale;
  394. }
  395. }
  396. else
  397. {
  398. _pivotX = 0.0f;
  399. _pivotY = 0.0f;
  400. }
  401. if (
  402. _displayData != null && currentDisplayData != _displayData &&
  403. (_meshData == null || _meshData != _displayData.mesh)
  404. )
  405. {
  406. _displayData.transform.ToMatrix(_helpMatrix);
  407. _helpMatrix.Invert();
  408. _helpMatrix.TransformPoint(0.0f, 0.0f, _helpPoint);
  409. _pivotX -= _helpPoint.x;
  410. _pivotY -= _helpPoint.y;
  411. currentDisplayData.transform.ToMatrix(_helpMatrix);
  412. _helpMatrix.Invert();
  413. _helpMatrix.TransformPoint(0.0f, 0.0f, _helpPoint);
  414. _pivotX += _helpPoint.x;
  415. _pivotY += _helpPoint.y;
  416. }
  417. if (_meshData != prevMeshData) // Update mesh bones and ffd vertices.
  418. {
  419. if (_meshData != null && _displayData != null && _meshData == _displayData.mesh)
  420. {
  421. if (_meshData.skinned)
  422. {
  423. DragonBones.ResizeList(_meshBones, _meshData.bones.Count, null);
  424. for (int i = 0, l = _meshBones.Count; i < l; ++i)
  425. {
  426. _meshBones[i] = _armature.GetBone(_meshData.bones[i].name);
  427. }
  428. int ffdVerticesCount = 0;
  429. for (int i = 0, l = _meshData.boneIndices.Count; i < l; ++i)
  430. {
  431. ffdVerticesCount += _meshData.boneIndices[i].Length;
  432. }
  433. DragonBones.ResizeList(_ffdVertices, ffdVerticesCount * 2, 0.0f);
  434. }
  435. else
  436. {
  437. _meshBones.Clear();
  438. DragonBones.ResizeList(_ffdVertices, _meshData.vertices.Count, 0.0f);
  439. }
  440. for (int i = 0, l = _ffdVertices.Count; i < l; ++i)
  441. {
  442. _ffdVertices[i] = 0.0f;
  443. }
  444. _meshDirty = true;
  445. }
  446. else
  447. {
  448. _meshBones.Clear();
  449. _ffdVertices.Clear();
  450. }
  451. }
  452. else if (_textureData != prevTextureData)
  453. {
  454. _meshDirty = true;
  455. }
  456. }
  457. else
  458. {
  459. _textureData = null;
  460. _meshData = null;
  461. _pivotX = 0.0f;
  462. _pivotY = 0.0f;
  463. _meshBones.Clear();
  464. _ffdVertices.Clear();
  465. }
  466. _displayDirty = true;
  467. _originalDirty = true;
  468. if (_displayData != null)
  469. {
  470. origin = _displayData.transform;
  471. }
  472. else if (_replacedDisplayData != null)
  473. {
  474. origin = _replacedDisplayData.transform;
  475. }
  476. }
  477. // Update bounding box data.
  478. if (_replacedDisplayData != null)
  479. {
  480. _boundingBoxData = _replacedDisplayData.boundingBox;
  481. }
  482. else if (_displayData != null)
  483. {
  484. _boundingBoxData = _displayData.boundingBox;
  485. }
  486. else
  487. {
  488. _boundingBoxData = null;
  489. }
  490. }
  491. /**
  492. * @private
  493. */
  494. protected void _updateDisplay()
  495. {
  496. var prevDisplay = _display != null ? _display : _rawDisplay;
  497. var prevChildArmature = _childArmature;
  498. if (_displayIndex >= 0 && _displayIndex < _displayList.Count)
  499. {
  500. _display = _displayList[_displayIndex];
  501. if (_display is Armature)
  502. {
  503. _childArmature = (Armature)_display;
  504. _display = _childArmature.display;
  505. }
  506. else
  507. {
  508. _childArmature = null;
  509. }
  510. }
  511. else
  512. {
  513. _display = null;
  514. _childArmature = null;
  515. }
  516. var currentDisplay = _display != null ? _display : _rawDisplay;
  517. if (currentDisplay != prevDisplay)
  518. {
  519. _onUpdateDisplay();
  520. _replaceDisplay(prevDisplay);
  521. _blendModeDirty = true;
  522. _colorDirty = true;
  523. }
  524. // Update frame.
  525. if (currentDisplay == _rawDisplay || currentDisplay == _meshDisplay)
  526. {
  527. _updateFrame();
  528. }
  529. // Update child armature.
  530. if (_childArmature != prevChildArmature)
  531. {
  532. if (prevChildArmature != null)
  533. {
  534. prevChildArmature.clock = null;
  535. prevChildArmature._parent = null; // Update child armature parent.
  536. if (prevChildArmature.inheritAnimation)
  537. {
  538. prevChildArmature.animation.Reset();
  539. }
  540. }
  541. if (_childArmature != null)
  542. {
  543. _childArmature.clock = _armature.clock;
  544. _childArmature._parent = this; // Update child armature parent.
  545. // Update child armature flip.
  546. _childArmature.flipX = _armature.flipX;
  547. _childArmature.flipY = _armature.flipY;
  548. if (_childArmature.inheritAnimation)
  549. {
  550. if (_childArmature.cacheFrameRate == 0) // Set child armature frameRate.
  551. {
  552. var cacheFrameRate = _armature.cacheFrameRate;
  553. if (cacheFrameRate != 0)
  554. {
  555. _childArmature.cacheFrameRate = cacheFrameRate;
  556. }
  557. }
  558. // Child armature action.
  559. var actions = _skinSlotData.slot.actions.Count > 0 ? _skinSlotData.slot.actions : _childArmature.armatureData.actions;
  560. if (actions.Count > 0)
  561. {
  562. foreach (var action in actions)
  563. {
  564. _childArmature._bufferAction(action);
  565. }
  566. }
  567. else
  568. {
  569. _childArmature.animation.Play();
  570. }
  571. }
  572. }
  573. }
  574. }
  575. /**
  576. * @private
  577. */
  578. protected void _updateLocalTransformMatrix()
  579. {
  580. if (origin != null)
  581. {
  582. global.CopyFrom(origin).Add(offset).ToMatrix(_localMatrix);
  583. }
  584. else
  585. {
  586. global.CopyFrom(offset).ToMatrix(_localMatrix);
  587. }
  588. }
  589. /**
  590. * @private
  591. */
  592. protected void _updateGlobalTransformMatrix()
  593. {
  594. globalTransformMatrix.CopyFrom(_localMatrix);
  595. globalTransformMatrix.Concat(_parent.globalTransformMatrix);
  596. global.FromMatrix(globalTransformMatrix);
  597. }
  598. /**
  599. * @private
  600. */
  601. internal void _init(SkinSlotData skinSlotData, object rawDisplay, object meshDisplay)
  602. {
  603. if (_skinSlotData != null)
  604. {
  605. return;
  606. }
  607. _skinSlotData = skinSlotData;
  608. var slotData = _skinSlotData.slot;
  609. name = slotData.name;
  610. _zOrder = slotData.zOrder;
  611. _blendMode = slotData.blendMode;
  612. _colorTransform.CopyFrom(slotData.color);
  613. _rawDisplay = rawDisplay;
  614. _meshDisplay = meshDisplay;
  615. DragonBones.ResizeList(_textureDatas, _skinSlotData.displays.Count, null);
  616. _blendModeDirty = true;
  617. _colorDirty = true;
  618. }
  619. /**
  620. * @private
  621. */
  622. internal override void _setArmature(Armature value)
  623. {
  624. if (_armature == value)
  625. {
  626. return;
  627. }
  628. if (_armature != null)
  629. {
  630. _armature._removeSlotFromSlotList(this);
  631. }
  632. _armature = value;
  633. _onUpdateDisplay();
  634. if (_armature != null)
  635. {
  636. _armature._addSlotToSlotList(this);
  637. _addDisplay();
  638. }
  639. else
  640. {
  641. _removeDisplay();
  642. }
  643. }
  644. /**
  645. * @private
  646. */
  647. internal void _update(int cacheFrameIndex)
  648. {
  649. _updateState = -1;
  650. if (_displayDirty)
  651. {
  652. _displayDirty = false;
  653. _updateDisplay();
  654. }
  655. if (_zOrderDirty)
  656. {
  657. _zOrderDirty = false;
  658. _updateZOrder();
  659. }
  660. if (_display == null)
  661. {
  662. return;
  663. }
  664. if (_blendModeDirty)
  665. {
  666. _blendModeDirty = false;
  667. _updateBlendMode();
  668. }
  669. if (_colorDirty)
  670. {
  671. _colorDirty = false;
  672. _updateColor();
  673. }
  674. if (_originalDirty)
  675. {
  676. _originalDirty = false;
  677. _transformDirty = true;
  678. _updateLocalTransformMatrix();
  679. }
  680. if (cacheFrameIndex >= 0 && _cachedFrameIndices != null)
  681. {
  682. var cachedFrameIndex = _cachedFrameIndices[cacheFrameIndex];
  683. if (_cachedFrameIndex >= 0 && _cachedFrameIndex == cachedFrameIndex) // Same cache.
  684. {
  685. _transformDirty = false;
  686. }
  687. else if (cachedFrameIndex >= 0) // Has been Cached.
  688. {
  689. _transformDirty = true;
  690. _cachedFrameIndex = cachedFrameIndex;
  691. }
  692. else if (_transformDirty || _parent._transformDirty != BoneTransformDirty.None) // Dirty.
  693. {
  694. _transformDirty = true;
  695. _cachedFrameIndex = -1;
  696. }
  697. else if (_cachedFrameIndex >= 0) // Same cache but not cached yet.
  698. {
  699. _transformDirty = false;
  700. _cachedFrameIndices[cacheFrameIndex] = _cachedFrameIndex;
  701. }
  702. else // Dirty.
  703. {
  704. _transformDirty = true;
  705. _cachedFrameIndex = -1;
  706. }
  707. }
  708. else if (_transformDirty || _parent._transformDirty != BoneTransformDirty.None) // Dirty.
  709. {
  710. cacheFrameIndex = -1;
  711. _transformDirty = true;
  712. _cachedFrameIndex = -1;
  713. }
  714. if (_meshData != null && _displayData != null && _meshData == _displayData.mesh)
  715. {
  716. if (_meshDirty || (_meshData.skinned && _isMeshBonesUpdate()))
  717. {
  718. _meshDirty = false;
  719. _updateMesh();
  720. }
  721. if (_meshData.skinned)
  722. {
  723. if (_transformDirty)
  724. {
  725. _transformDirty = false;
  726. _updateTransform(true);
  727. }
  728. return;
  729. }
  730. }
  731. if (_transformDirty)
  732. {
  733. _transformDirty = false;
  734. if (_cachedFrameIndex < 0)
  735. {
  736. _updateGlobalTransformMatrix();
  737. if (cacheFrameIndex >= 0)
  738. {
  739. _cachedFrameIndex = _cachedFrameIndices[cacheFrameIndex] = _armature._armatureData.SetCacheFrame(globalTransformMatrix, global);
  740. }
  741. }
  742. else
  743. {
  744. _armature._armatureData.GetCacheFrame(globalTransformMatrix, global, _cachedFrameIndex);
  745. }
  746. _updateTransform(false);
  747. _updateState = 0;
  748. }
  749. }
  750. /**
  751. * @private
  752. */
  753. internal void _updateTransformAndMatrix()
  754. {
  755. if (_updateState < 0)
  756. {
  757. _updateState = 0;
  758. _updateLocalTransformMatrix();
  759. _updateGlobalTransformMatrix();
  760. }
  761. }
  762. /**
  763. * @private
  764. */
  765. internal bool _setDisplayList(List<object> value)
  766. {
  767. if (value != null && value.Count > 0)
  768. {
  769. if (_displayList.Count != value.Count)
  770. {
  771. DragonBones.ResizeList(_displayList, value.Count, null);
  772. }
  773. for (int i = 0, l = value.Count; i < l; ++i) // Retain input render displays.
  774. {
  775. var eachDisplay = value[i];
  776. if (
  777. eachDisplay != null && eachDisplay != _rawDisplay && eachDisplay != _meshDisplay &&
  778. !(eachDisplay is Armature) && !_displayList.Contains(eachDisplay)
  779. )
  780. {
  781. _initDisplay(eachDisplay);
  782. }
  783. _displayList[i] = eachDisplay;
  784. }
  785. }
  786. else if (_displayList.Count > 0)
  787. {
  788. _displayList.Clear();
  789. }
  790. if (_displayIndex >= 0 && _displayIndex < _displayList.Count)
  791. {
  792. _displayDirty = _display != _displayList[_displayIndex];
  793. }
  794. else
  795. {
  796. _displayDirty = _display != null;
  797. }
  798. _updateDisplayData();
  799. return _displayDirty;
  800. }
  801. /**
  802. * @private
  803. */
  804. internal bool _setDisplayIndex(int value)
  805. {
  806. if (_displayIndex == value)
  807. {
  808. return false;
  809. }
  810. _displayIndex = value;
  811. _displayDirty = true;
  812. _updateDisplayData();
  813. return true;
  814. }
  815. /**
  816. * @private
  817. */
  818. internal bool _setZorder(int value)
  819. {
  820. if (_zOrder == value)
  821. {
  822. //return false;
  823. }
  824. _zOrder = value;
  825. _zOrderDirty = true;
  826. return true;
  827. }
  828. /**
  829. * @private
  830. */
  831. internal bool _setColor(ColorTransform value)
  832. {
  833. _colorTransform.CopyFrom(value);
  834. _colorDirty = true;
  835. return true;
  836. }
  837. /**
  838. * @language zh_CN
  839. * 判断指定的点是否在插槽的自定义包围盒内。
  840. * @param x 点的水平坐标。(骨架内坐标系)
  841. * @param y 点的垂直坐标。(骨架内坐标系)
  842. * @version DragonBones 5.0
  843. */
  844. public bool ContainsPoint(float x, float y)
  845. {
  846. if (_boundingBoxData == null)
  847. {
  848. return false;
  849. }
  850. _updateTransformAndMatrix();
  851. _helpMatrix.CopyFrom(globalTransformMatrix);
  852. _helpMatrix.Invert();
  853. _helpMatrix.TransformPoint(x, y, _helpPoint);
  854. return _boundingBoxData.ContainsPoint(_helpPoint.x, _helpPoint.y);
  855. }
  856. /**
  857. * @language zh_CN
  858. * 判断指定的线段与插槽的自定义包围盒是否相交。
  859. * @param xA 线段起点的水平坐标。(骨架内坐标系)
  860. * @param yA 线段起点的垂直坐标。(骨架内坐标系)
  861. * @param xB 线段终点的水平坐标。(骨架内坐标系)
  862. * @param yB 线段终点的垂直坐标。(骨架内坐标系)
  863. * @param intersectionPointA 线段从起点到终点与包围盒相交的第一个交点。(骨架内坐标系)
  864. * @param intersectionPointB 线段从终点到起点与包围盒相交的第一个交点。(骨架内坐标系)
  865. * @param normalRadians 碰撞点处包围盒切线的法线弧度。 [x: 第一个碰撞点处切线的法线弧度, y: 第二个碰撞点处切线的法线弧度]
  866. * @returns 相交的情况。 [-1: 不相交且线段在包围盒内, 0: 不相交, 1: 相交且有一个交点且终点在包围盒内, 2: 相交且有一个交点且起点在包围盒内, 3: 相交且有两个交点, N: 相交且有 N 个交点]
  867. * @version DragonBones 5.0
  868. */
  869. public int IntersectsSegment(
  870. float xA, float yA, float xB, float yB,
  871. Point intersectionPointA = null,
  872. Point intersectionPointB = null,
  873. Point normalRadians = null
  874. )
  875. {
  876. if (_boundingBoxData == null)
  877. {
  878. return 0;
  879. }
  880. _updateTransformAndMatrix();
  881. _helpMatrix.CopyFrom(globalTransformMatrix);
  882. _helpMatrix.Invert();
  883. _helpMatrix.TransformPoint(xA, yA, _helpPoint);
  884. xA = _helpPoint.x;
  885. yA = _helpPoint.y;
  886. _helpMatrix.TransformPoint(xB, yB, _helpPoint);
  887. xB = _helpPoint.x;
  888. yB = _helpPoint.y;
  889. var intersectionCount = _boundingBoxData.IntersectsSegment(xA, yA, xB, yB, intersectionPointA, intersectionPointB, normalRadians);
  890. if (intersectionCount > 0)
  891. {
  892. if (intersectionCount == 1 || intersectionCount == 2)
  893. {
  894. if (intersectionPointA != null)
  895. {
  896. globalTransformMatrix.TransformPoint(intersectionPointA.x, intersectionPointA.y, intersectionPointA);
  897. if (intersectionPointB != null)
  898. {
  899. intersectionPointB.x = intersectionPointA.x;
  900. intersectionPointB.y = intersectionPointA.y;
  901. }
  902. }
  903. else if (intersectionPointB != null)
  904. {
  905. globalTransformMatrix.TransformPoint(intersectionPointB.x, intersectionPointB.y, intersectionPointB);
  906. }
  907. }
  908. else
  909. {
  910. if (intersectionPointA != null)
  911. {
  912. globalTransformMatrix.TransformPoint(intersectionPointA.x, intersectionPointA.y, intersectionPointA);
  913. }
  914. if (intersectionPointB != null)
  915. {
  916. globalTransformMatrix.TransformPoint(intersectionPointB.x, intersectionPointB.y, intersectionPointB);
  917. }
  918. }
  919. if (normalRadians != null)
  920. {
  921. globalTransformMatrix.TransformPoint((float)Math.Cos(normalRadians.x), (float)Math.Sin(normalRadians.x), _helpPoint, true);
  922. normalRadians.x = (float)Math.Atan2(_helpPoint.y, _helpPoint.x);
  923. globalTransformMatrix.TransformPoint((float)Math.Cos(normalRadians.y), (float)Math.Sin(normalRadians.y), _helpPoint, true);
  924. normalRadians.y = (float)Math.Atan2(_helpPoint.y, _helpPoint.x);
  925. }
  926. }
  927. return intersectionCount;
  928. }
  929. /**
  930. * @language zh_CN
  931. * 在下一帧更新显示对象的状态。
  932. * @version DragonBones 4.5
  933. */
  934. public void InvalidUpdate()
  935. {
  936. _displayDirty = true;
  937. _transformDirty = true;
  938. }
  939. /**
  940. * @private
  941. */
  942. public SkinSlotData skinSlotData
  943. {
  944. get
  945. {
  946. return _skinSlotData;
  947. }
  948. }
  949. /**
  950. * @private
  951. */
  952. public BoundingBoxData boundingBoxData
  953. {
  954. get
  955. {
  956. return _boundingBoxData;
  957. }
  958. }
  959. /**
  960. * @private
  961. */
  962. public object rawDisplay
  963. {
  964. get { return _rawDisplay; }
  965. }
  966. /**
  967. * @private
  968. */
  969. public object meshDisplay
  970. {
  971. get { return _meshDisplay; }
  972. }
  973. /**
  974. * @language zh_CN
  975. * 此时显示的显示对象在显示列表中的索引。
  976. * @version DragonBones 4.5
  977. */
  978. public int displayIndex
  979. {
  980. get { return _displayIndex; }
  981. set
  982. {
  983. if (_setDisplayIndex(value))
  984. {
  985. _update(-1);
  986. }
  987. }
  988. }
  989. /**
  990. * @language zh_CN
  991. * 包含显示对象或子骨架的显示列表。
  992. * @version DragonBones 3.0
  993. */
  994. public List<object> displayList
  995. {
  996. get { return new List<object>(_displayList.ToArray()); }
  997. set
  998. {
  999. var backupDisplayList = _displayList.ToArray(); // Copy.
  1000. var disposeDisplayList = new List<object>();
  1001. if (_setDisplayList(value))
  1002. {
  1003. _update(-1);
  1004. }
  1005. // Release replaced render displays.
  1006. for (int i = 0, l = backupDisplayList.Length; i < l; ++i)
  1007. {
  1008. var eachDisplay = backupDisplayList[i];
  1009. if (
  1010. eachDisplay != null && eachDisplay != _rawDisplay && eachDisplay != _meshDisplay &&
  1011. !_displayList.Contains(eachDisplay) &&
  1012. !disposeDisplayList.Contains(eachDisplay)
  1013. )
  1014. {
  1015. disposeDisplayList.Add(eachDisplay);
  1016. }
  1017. }
  1018. for (int i = 0, l = disposeDisplayList.Count; i < l; ++i)
  1019. {
  1020. var eachDisplay = disposeDisplayList[i];
  1021. if (eachDisplay is Armature)
  1022. {
  1023. (eachDisplay as Armature).Dispose();
  1024. }
  1025. else
  1026. {
  1027. _disposeDisplay(eachDisplay);
  1028. }
  1029. }
  1030. }
  1031. }
  1032. /**
  1033. * @language zh_CN
  1034. * 此时显示的显示对象。
  1035. * @version DragonBones 3.0
  1036. */
  1037. public object display
  1038. {
  1039. get { return _display; }
  1040. set
  1041. {
  1042. if (_display == value)
  1043. {
  1044. return;
  1045. }
  1046. var displayListLength = _displayList.Count;
  1047. if (_displayIndex < 0 && displayListLength == 0) // Emprty.
  1048. {
  1049. _displayIndex = 0;
  1050. }
  1051. if (_displayIndex < 0)
  1052. {
  1053. return;
  1054. }
  1055. else
  1056. {
  1057. var replaceDisplayList = displayList; // Copy.
  1058. if (displayListLength <= _displayIndex)
  1059. {
  1060. DragonBones.ResizeList(replaceDisplayList, _displayIndex + 1, null);
  1061. }
  1062. replaceDisplayList[_displayIndex] = value;
  1063. displayList = replaceDisplayList;
  1064. }
  1065. }
  1066. }
  1067. /**
  1068. * @language zh_CN
  1069. * 此时显示的子骨架。
  1070. * @see DragonBones.Armature
  1071. * @version DragonBones 3.0
  1072. */
  1073. public Armature childArmature
  1074. {
  1075. get { return _childArmature; }
  1076. set
  1077. {
  1078. if (_childArmature == value)
  1079. {
  1080. return;
  1081. }
  1082. display = value;
  1083. }
  1084. }
  1085. }
  1086. }