CCDisplayManager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCDisplayManager.h"
  21. #include "editor-support/cocostudio/CCBone.h"
  22. #include "editor-support/cocostudio/CCArmature.h"
  23. #include "editor-support/cocostudio/CCUtilMath.h"
  24. #include "editor-support/cocostudio/CCSkin.h"
  25. #include "2d/CCParticleSystemQuad.h"
  26. using namespace cocos2d;
  27. namespace cocostudio {
  28. DisplayManager *DisplayManager::create(Bone *bone)
  29. {
  30. DisplayManager *pDisplayManager = new (std::nothrow) DisplayManager();
  31. if (pDisplayManager && pDisplayManager->init(bone))
  32. {
  33. pDisplayManager->autorelease();
  34. return pDisplayManager;
  35. }
  36. CC_SAFE_DELETE(pDisplayManager);
  37. return nullptr;
  38. }
  39. DisplayManager::DisplayManager()
  40. : _displayRenderNode(nullptr)
  41. , _displayType(CS_DISPLAY_MAX)
  42. , _currentDecoDisplay(nullptr)
  43. , _displayIndex(-1)
  44. , _forceChangeDisplay(false)
  45. , _visible(true)
  46. , _bone(nullptr)
  47. {
  48. }
  49. DisplayManager::~DisplayManager()
  50. {
  51. _decoDisplayList.clear();
  52. if( _displayRenderNode )
  53. {
  54. _displayRenderNode->removeFromParentAndCleanup(true);
  55. if(_displayRenderNode->getReferenceCount() > 0)
  56. CC_SAFE_RELEASE_NULL(_displayRenderNode);
  57. }
  58. }
  59. bool DisplayManager::init(Bone *bone)
  60. {
  61. bool ret = false;
  62. do
  63. {
  64. _bone = bone;
  65. initDisplayList(bone->getBoneData());
  66. ret = true;
  67. }
  68. while (0);
  69. return ret;
  70. }
  71. void DisplayManager::addDisplay(DisplayData *displayData, int index)
  72. {
  73. DecorativeDisplay *decoDisplay = nullptr;
  74. if( (index >= 0) && (index < _decoDisplayList.size()) )
  75. {
  76. decoDisplay = (DecorativeDisplay *)_decoDisplayList.at(index);
  77. }
  78. else
  79. {
  80. decoDisplay = DecorativeDisplay::create();
  81. _decoDisplayList.pushBack(decoDisplay);
  82. }
  83. DisplayFactory::addDisplay(_bone, decoDisplay, displayData);
  84. //! if changed display index is current display index, then change current display to the new display
  85. if(index == _displayIndex)
  86. {
  87. _displayIndex = -1;
  88. changeDisplayWithIndex(index, false);
  89. }
  90. }
  91. void DisplayManager::addDisplay(Node *display, int index)
  92. {
  93. DecorativeDisplay *decoDisplay = nullptr;
  94. if( (index >= 0) && (index < _decoDisplayList.size()) )
  95. {
  96. decoDisplay = _decoDisplayList.at(index);
  97. }
  98. else
  99. {
  100. decoDisplay = DecorativeDisplay::create();
  101. _decoDisplayList.pushBack(decoDisplay);
  102. }
  103. DisplayData *displayData = nullptr;
  104. if (Skin *skin = dynamic_cast<Skin *>(display))
  105. {
  106. skin->setBone(_bone);
  107. displayData = SpriteDisplayData::create();
  108. DisplayFactory::initSpriteDisplay(_bone, decoDisplay, skin->getDisplayName().c_str(), skin);
  109. if (SpriteDisplayData *spriteDisplayData = (SpriteDisplayData *)decoDisplay->getDisplayData())
  110. {
  111. skin->setSkinData(spriteDisplayData->skinData);
  112. ((SpriteDisplayData *)displayData)->skinData = spriteDisplayData->skinData;
  113. }
  114. else
  115. {
  116. bool find = false;
  117. for (long i = _decoDisplayList.size()-2; i>=0; i--)
  118. {
  119. DecorativeDisplay *dd = _decoDisplayList.at(i);
  120. SpriteDisplayData *sdd = static_cast<SpriteDisplayData*>(dd->getDisplayData());
  121. if (sdd)
  122. {
  123. find = true;
  124. skin->setSkinData(sdd->skinData);
  125. static_cast<SpriteDisplayData*>(displayData)->skinData = sdd->skinData;
  126. break;
  127. }
  128. }
  129. if (!find)
  130. {
  131. BaseData baseData;
  132. skin->setSkinData(baseData);
  133. }
  134. }
  135. }
  136. else if (dynamic_cast<ParticleSystemQuad *>(display))
  137. {
  138. displayData = ParticleDisplayData::create();
  139. display->removeFromParent();
  140. display->cleanup();
  141. Armature *armature = _bone->getArmature();
  142. if (armature)
  143. {
  144. display->setParent(armature);
  145. }
  146. }
  147. else if(Armature *armature = dynamic_cast<Armature *>(display))
  148. {
  149. displayData = ArmatureDisplayData::create();
  150. displayData->displayName = armature->getName();
  151. armature->setParentBone(_bone);
  152. }
  153. else
  154. {
  155. displayData = DisplayData::create();
  156. }
  157. decoDisplay->setDisplay(display);
  158. decoDisplay->setDisplayData(displayData);
  159. //! if changed display index is current display index, then change current display to the new display
  160. if(index == _displayIndex)
  161. {
  162. _displayIndex = -1;
  163. changeDisplayWithIndex(index, false);
  164. }
  165. }
  166. void DisplayManager::removeDisplay(int index)
  167. {
  168. if(index == _displayIndex)
  169. {
  170. setCurrentDecorativeDisplay(nullptr);
  171. _displayIndex = -1;
  172. }
  173. _decoDisplayList.erase(index);
  174. }
  175. const cocos2d::Vector<DecorativeDisplay*>& DisplayManager::getDecorativeDisplayList() const
  176. {
  177. return _decoDisplayList;
  178. }
  179. void DisplayManager::changeDisplayWithIndex(int index, bool force)
  180. {
  181. CCASSERT( index < (int)_decoDisplayList.size(), "the _index value is out of range");
  182. _forceChangeDisplay = force;
  183. //! If index is equal to current display index,then do nothing
  184. if ( _displayIndex == index)
  185. return;
  186. _displayIndex = index;
  187. //! If displayIndex < 0, it means you want to hide you display
  188. if (_displayIndex < 0)
  189. {
  190. if(_displayRenderNode)
  191. {
  192. _displayRenderNode->removeFromParentAndCleanup(true);
  193. setCurrentDecorativeDisplay(nullptr);
  194. }
  195. return;
  196. }
  197. DecorativeDisplay *decoDisplay = (DecorativeDisplay *)_decoDisplayList.at(_displayIndex);
  198. setCurrentDecorativeDisplay(decoDisplay);
  199. }
  200. void DisplayManager::changeDisplayWithName(const std::string& name, bool force)
  201. {
  202. for (int i = 0; i<_decoDisplayList.size(); i++)
  203. {
  204. if (_decoDisplayList.at(i)->getDisplayData()->displayName == name)
  205. {
  206. changeDisplayWithIndex(i, force);
  207. break;
  208. }
  209. }
  210. }
  211. void DisplayManager::setCurrentDecorativeDisplay(DecorativeDisplay *decoDisplay)
  212. {
  213. #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
  214. if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector())
  215. {
  216. _currentDecoDisplay->getColliderDetector()->setActive(false);
  217. }
  218. #endif
  219. _currentDecoDisplay = decoDisplay;
  220. #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
  221. if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector())
  222. {
  223. _currentDecoDisplay->getColliderDetector()->setActive(true);
  224. }
  225. #endif
  226. Node *displayRenderNode = _currentDecoDisplay == nullptr ? nullptr : _currentDecoDisplay->getDisplay();
  227. if (_displayRenderNode)
  228. {
  229. if (dynamic_cast<Armature *>(_displayRenderNode) != nullptr)
  230. {
  231. _bone->setChildArmature(nullptr);
  232. }
  233. _displayRenderNode->removeFromParentAndCleanup(true);
  234. _displayRenderNode->release();
  235. }
  236. _displayRenderNode = displayRenderNode;
  237. if(_displayRenderNode)
  238. {
  239. if (Armature *armature = dynamic_cast<Armature *>(_displayRenderNode))
  240. {
  241. _bone->setChildArmature(armature);
  242. armature->setParentBone(_bone);
  243. }
  244. else if (ParticleSystemQuad *particle = dynamic_cast<ParticleSystemQuad *>(_displayRenderNode))
  245. {
  246. particle->resetSystem();
  247. }
  248. _displayRenderNode->setColor(_bone->getDisplayedColor());
  249. _displayRenderNode->setOpacity(_bone->getDisplayedOpacity());
  250. _displayRenderNode->retain();
  251. _displayRenderNode->setVisible(_visible);
  252. _displayType = _currentDecoDisplay->getDisplayData()->displayType;
  253. }
  254. else
  255. {
  256. _displayType = CS_DISPLAY_MAX;
  257. }
  258. }
  259. Node *DisplayManager::getDisplayRenderNode() const
  260. {
  261. return _displayRenderNode;
  262. }
  263. DisplayType DisplayManager::getDisplayRenderNodeType() const
  264. {
  265. return _displayType;
  266. }
  267. int DisplayManager::getCurrentDisplayIndex() const
  268. {
  269. return _displayIndex;
  270. }
  271. DecorativeDisplay *DisplayManager::getCurrentDecorativeDisplay() const
  272. {
  273. return _currentDecoDisplay;
  274. }
  275. DecorativeDisplay *DisplayManager::getDecorativeDisplayByIndex( int index) const
  276. {
  277. return _decoDisplayList.at(index);
  278. }
  279. void DisplayManager::initDisplayList(BoneData *boneData)
  280. {
  281. _decoDisplayList.clear();
  282. CS_RETURN_IF(!boneData);
  283. for(auto& object : boneData->displayDataList)
  284. {
  285. DisplayData *displayData = static_cast<DisplayData *>(object);
  286. DecorativeDisplay *decoDisplay = DecorativeDisplay::create();
  287. decoDisplay->setDisplayData(displayData);
  288. DisplayFactory::createDisplay(_bone, decoDisplay);
  289. _decoDisplayList.pushBack(decoDisplay);
  290. }
  291. }
  292. bool DisplayManager::containPoint(Vec2 &point)
  293. {
  294. if(!_visible || _displayIndex < 0)
  295. {
  296. return false;
  297. }
  298. bool ret = false;
  299. switch (_currentDecoDisplay->getDisplayData()->displayType)
  300. {
  301. case CS_DISPLAY_SPRITE:
  302. {
  303. /*
  304. * First we first check if the point is in the sprite content rect. If false, then we continue to check
  305. * the contour point. If this step is also false, then we can say the bone not contain this point.
  306. *
  307. */
  308. Vec2 outPoint;
  309. Sprite *sprite = (Sprite *)_currentDecoDisplay->getDisplay();
  310. Sprite *child = (Sprite *)sprite->getChildByTag(0);
  311. if(nullptr != child)
  312. sprite = child;
  313. if (nullptr != sprite)
  314. ret = CC_SPRITE_CONTAIN_POINT_WITH_RETURN(sprite, point, outPoint);
  315. }
  316. break;
  317. default:
  318. break;
  319. }
  320. return ret;
  321. }
  322. bool DisplayManager::containPoint(float x, float y)
  323. {
  324. Vec2 p(x, y);
  325. return containPoint(p);
  326. }
  327. void DisplayManager::setVisible(bool visible)
  328. {
  329. if(!_displayRenderNode)
  330. return;
  331. _visible = visible;
  332. _displayRenderNode->setVisible(visible);
  333. }
  334. bool DisplayManager::isVisible() const
  335. {
  336. return _visible;
  337. }
  338. Size DisplayManager::getContentSize() const
  339. {
  340. CS_RETURN_IF(!_displayRenderNode) Size(0, 0);
  341. return _displayRenderNode->getContentSize();
  342. }
  343. Rect DisplayManager::getBoundingBox() const
  344. {
  345. CS_RETURN_IF(!_displayRenderNode) Rect(0, 0, 0, 0);
  346. return _displayRenderNode->getBoundingBox();
  347. }
  348. Vec2 DisplayManager::getAnchorPoint() const
  349. {
  350. CS_RETURN_IF(!_displayRenderNode) Vec2(0, 0);
  351. return _displayRenderNode->getAnchorPoint();
  352. }
  353. Vec2 DisplayManager::getAnchorPointInPoints() const
  354. {
  355. CS_RETURN_IF(!_displayRenderNode) Vec2(0, 0);
  356. return _displayRenderNode->getAnchorPointInPoints();
  357. }
  358. }