axmol/extensions/cocostudio/CCDisplayManager.cpp

434 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2019-11-24 23:15:56 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
#include "CCDisplayManager.h"
#include "CCBone.h"
#include "CCArmature.h"
#include "CCSkin.h"
2019-11-23 20:27:39 +08:00
#include "2d/CCParticleSystemQuad.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
DisplayManager* DisplayManager::create(Bone* bone)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DisplayManager* pDisplayManager = new DisplayManager();
2021-12-08 00:11:53 +08:00
if (pDisplayManager->init(bone))
2019-11-23 20:27:39 +08:00
{
pDisplayManager->autorelease();
return pDisplayManager;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(pDisplayManager);
2019-11-23 20:27:39 +08:00
return nullptr;
}
DisplayManager::DisplayManager()
: _displayRenderNode(nullptr)
, _displayType(CS_DISPLAY_MAX)
, _currentDecoDisplay(nullptr)
, _displayIndex(-1)
, _forceChangeDisplay(false)
, _visible(true)
, _bone(nullptr)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
DisplayManager::~DisplayManager()
{
_decoDisplayList.clear();
2021-12-25 10:04:45 +08:00
if (_displayRenderNode)
2019-11-23 20:27:39 +08:00
{
_displayRenderNode->removeFromParentAndCleanup(true);
2021-12-25 10:04:45 +08:00
if (_displayRenderNode->getReferenceCount() > 0)
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_displayRenderNode);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
bool DisplayManager::init(Bone* bone)
2019-11-23 20:27:39 +08:00
{
bool ret = false;
do
{
_bone = bone;
initDisplayList(bone->getBoneData());
ret = true;
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
void DisplayManager::addDisplay(DisplayData* displayData, int index)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DecorativeDisplay* decoDisplay = nullptr;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if ((index >= 0) && (index < _decoDisplayList.size()))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
decoDisplay = (DecorativeDisplay*)_decoDisplayList.at(index);
2019-11-23 20:27:39 +08:00
}
else
{
decoDisplay = DecorativeDisplay::create();
_decoDisplayList.pushBack(decoDisplay);
}
DisplayFactory::addDisplay(_bone, decoDisplay, displayData);
//! if changed display index is current display index, then change current display to the new display
2021-12-25 10:04:45 +08:00
if (index == _displayIndex)
2019-11-23 20:27:39 +08:00
{
_displayIndex = -1;
changeDisplayWithIndex(index, false);
}
}
2021-12-25 10:04:45 +08:00
void DisplayManager::addDisplay(Node* display, int index)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DecorativeDisplay* decoDisplay = nullptr;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if ((index >= 0) && (index < _decoDisplayList.size()))
2019-11-23 20:27:39 +08:00
{
decoDisplay = _decoDisplayList.at(index);
}
else
{
decoDisplay = DecorativeDisplay::create();
_decoDisplayList.pushBack(decoDisplay);
}
2021-12-25 10:04:45 +08:00
DisplayData* displayData = nullptr;
if (Skin* skin = dynamic_cast<Skin*>(display))
2019-11-23 20:27:39 +08:00
{
skin->setBone(_bone);
displayData = SpriteDisplayData::create();
DisplayFactory::initSpriteDisplay(_bone, decoDisplay, skin->getDisplayName().data(), skin);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (SpriteDisplayData* spriteDisplayData = (SpriteDisplayData*)decoDisplay->getDisplayData())
2019-11-23 20:27:39 +08:00
{
skin->setSkinData(spriteDisplayData->skinData);
2021-12-25 10:04:45 +08:00
((SpriteDisplayData*)displayData)->skinData = spriteDisplayData->skinData;
2019-11-23 20:27:39 +08:00
}
else
{
bool find = false;
2021-12-25 10:04:45 +08:00
for (long i = _decoDisplayList.size() - 2; i >= 0; i--)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DecorativeDisplay* dd = _decoDisplayList.at(i);
SpriteDisplayData* sdd = static_cast<SpriteDisplayData*>(dd->getDisplayData());
2019-11-23 20:27:39 +08:00
if (sdd)
{
find = true;
skin->setSkinData(sdd->skinData);
static_cast<SpriteDisplayData*>(displayData)->skinData = sdd->skinData;
break;
}
}
if (!find)
{
BaseData baseData;
skin->setSkinData(baseData);
}
}
}
2021-12-25 10:04:45 +08:00
else if (dynamic_cast<ParticleSystemQuad*>(display))
2019-11-23 20:27:39 +08:00
{
displayData = ParticleDisplayData::create();
display->removeFromParent();
display->cleanup();
2021-12-25 10:04:45 +08:00
Armature* armature = _bone->getArmature();
2019-11-23 20:27:39 +08:00
if (armature)
{
display->setParent(armature);
}
}
2021-12-25 10:04:45 +08:00
else if (Armature* armature = dynamic_cast<Armature*>(display))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
displayData = ArmatureDisplayData::create();
2019-11-23 20:27:39 +08:00
displayData->displayName = armature->getName();
armature->setParentBone(_bone);
}
else
{
displayData = DisplayData::create();
}
decoDisplay->setDisplay(display);
decoDisplay->setDisplayData(displayData);
//! if changed display index is current display index, then change current display to the new display
2021-12-25 10:04:45 +08:00
if (index == _displayIndex)
2019-11-23 20:27:39 +08:00
{
_displayIndex = -1;
changeDisplayWithIndex(index, false);
}
}
void DisplayManager::removeDisplay(int index)
{
2021-12-25 10:04:45 +08:00
if (index == _displayIndex)
2019-11-23 20:27:39 +08:00
{
setCurrentDecorativeDisplay(nullptr);
_displayIndex = -1;
}
_decoDisplayList.erase(index);
}
2022-08-08 18:02:17 +08:00
const ax::Vector<DecorativeDisplay*>& DisplayManager::getDecorativeDisplayList() const
2019-11-23 20:27:39 +08:00
{
return _decoDisplayList;
}
void DisplayManager::changeDisplayWithIndex(int index, bool force)
{
2022-07-16 10:43:05 +08:00
AXASSERT(index < (int)_decoDisplayList.size(), "the _index value is out of range");
2019-11-23 20:27:39 +08:00
_forceChangeDisplay = force;
//! If index is equal to current display index,then do nothing
2021-12-25 10:04:45 +08:00
if (_displayIndex == index)
2019-11-23 20:27:39 +08:00
return;
_displayIndex = index;
//! If displayIndex < 0, it means you want to hide you display
if (_displayIndex < 0)
{
2021-12-25 10:04:45 +08:00
if (_displayRenderNode)
2019-11-23 20:27:39 +08:00
{
_displayRenderNode->removeFromParentAndCleanup(true);
setCurrentDecorativeDisplay(nullptr);
}
return;
}
2021-12-25 10:04:45 +08:00
DecorativeDisplay* decoDisplay = (DecorativeDisplay*)_decoDisplayList.at(_displayIndex);
2019-11-23 20:27:39 +08:00
setCurrentDecorativeDisplay(decoDisplay);
}
void DisplayManager::changeDisplayWithName(std::string_view name, bool force)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (int i = 0; i < _decoDisplayList.size(); i++)
2019-11-23 20:27:39 +08:00
{
if (_decoDisplayList.at(i)->getDisplayData()->displayName == name)
{
changeDisplayWithIndex(i, force);
break;
}
}
}
2021-12-25 10:04:45 +08:00
void DisplayManager::setCurrentDecorativeDisplay(DecorativeDisplay* decoDisplay)
2019-11-23 20:27:39 +08:00
{
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector())
{
_currentDecoDisplay->getColliderDetector()->setActive(false);
}
#endif
_currentDecoDisplay = decoDisplay;
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector())
{
_currentDecoDisplay->getColliderDetector()->setActive(true);
}
#endif
2021-12-25 10:04:45 +08:00
Node* displayRenderNode = _currentDecoDisplay == nullptr ? nullptr : _currentDecoDisplay->getDisplay();
2019-11-23 20:27:39 +08:00
if (_displayRenderNode)
{
2021-12-25 10:04:45 +08:00
if (dynamic_cast<Armature*>(_displayRenderNode) != nullptr)
2019-11-23 20:27:39 +08:00
{
_bone->setChildArmature(nullptr);
}
_displayRenderNode->removeFromParentAndCleanup(true);
_displayRenderNode->release();
}
_displayRenderNode = displayRenderNode;
2021-12-25 10:04:45 +08:00
if (_displayRenderNode)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (Armature* armature = dynamic_cast<Armature*>(_displayRenderNode))
2019-11-23 20:27:39 +08:00
{
_bone->setChildArmature(armature);
armature->setParentBone(_bone);
}
2021-12-25 10:04:45 +08:00
else if (ParticleSystemQuad* particle = dynamic_cast<ParticleSystemQuad*>(_displayRenderNode))
2019-11-23 20:27:39 +08:00
{
particle->resetSystem();
}
_displayRenderNode->setColor(_bone->getDisplayedColor());
_displayRenderNode->setOpacity(_bone->getDisplayedOpacity());
_displayRenderNode->retain();
_displayRenderNode->setVisible(_visible);
_displayType = _currentDecoDisplay->getDisplayData()->displayType;
}
else
{
2021-12-25 10:04:45 +08:00
_displayType = CS_DISPLAY_MAX;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
Node* DisplayManager::getDisplayRenderNode() const
2019-11-23 20:27:39 +08:00
{
return _displayRenderNode;
}
DisplayType DisplayManager::getDisplayRenderNodeType() const
{
return _displayType;
}
int DisplayManager::getCurrentDisplayIndex() const
{
return _displayIndex;
}
2021-12-25 10:04:45 +08:00
DecorativeDisplay* DisplayManager::getCurrentDecorativeDisplay() const
2019-11-23 20:27:39 +08:00
{
return _currentDecoDisplay;
}
2021-12-25 10:04:45 +08:00
DecorativeDisplay* DisplayManager::getDecorativeDisplayByIndex(int index) const
2019-11-23 20:27:39 +08:00
{
return _decoDisplayList.at(index);
}
2021-12-25 10:04:45 +08:00
void DisplayManager::initDisplayList(BoneData* boneData)
2019-11-23 20:27:39 +08:00
{
_decoDisplayList.clear();
CS_RETURN_IF(!boneData);
for (auto&& object : boneData->displayDataList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DisplayData* displayData = static_cast<DisplayData*>(object);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
DecorativeDisplay* decoDisplay = DecorativeDisplay::create();
2019-11-23 20:27:39 +08:00
decoDisplay->setDisplayData(displayData);
DisplayFactory::createDisplay(_bone, decoDisplay);
_decoDisplayList.pushBack(decoDisplay);
}
}
2021-12-25 10:04:45 +08:00
bool DisplayManager::containPoint(Vec2& point)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_visible || _displayIndex < 0)
2019-11-23 20:27:39 +08:00
{
return false;
}
bool ret = false;
switch (_currentDecoDisplay->getDisplayData()->displayType)
{
case CS_DISPLAY_SPRITE:
{
/*
* First we first check if the point is in the sprite content rect. If false, then we continue to check
* the contour point. If this step is also false, then we can say the bone not contain this point.
*
*/
2021-12-25 10:04:45 +08:00
Sprite* sprite = (Sprite*)_currentDecoDisplay->getDisplay();
Sprite* child = (Sprite*)sprite->getChildByTag(0);
if (nullptr != child)
2019-11-23 20:27:39 +08:00
sprite = child;
if (nullptr != sprite)
2021-07-15 17:01:57 +08:00
ret = sprite->hitTest(point);
2019-11-23 20:27:39 +08:00
}
break;
default:
break;
}
return ret;
}
bool DisplayManager::containPoint(float x, float y)
{
Vec2 p(x, y);
return containPoint(p);
}
void DisplayManager::setVisible(bool visible)
{
2021-12-25 10:04:45 +08:00
if (!_displayRenderNode)
2019-11-23 20:27:39 +08:00
return;
_visible = visible;
_displayRenderNode->setVisible(visible);
}
bool DisplayManager::isVisible() const
{
return _visible;
}
Size DisplayManager::getContentSize() const
{
CS_RETURN_IF(!_displayRenderNode) Size(0, 0);
return _displayRenderNode->getContentSize();
}
Rect DisplayManager::getBoundingBox() const
{
CS_RETURN_IF(!_displayRenderNode) Rect(0, 0, 0, 0);
return _displayRenderNode->getBoundingBox();
}
Vec2 DisplayManager::getAnchorPoint() const
{
CS_RETURN_IF(!_displayRenderNode) Vec2(0, 0);
return _displayRenderNode->getAnchorPoint();
}
Vec2 DisplayManager::getAnchorPointInPoints() const
{
CS_RETURN_IF(!_displayRenderNode) Vec2(0, 0);
return _displayRenderNode->getAnchorPointInPoints();
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio