2021-12-31 12:12:40 +08:00
|
|
|
#include "CCArmatureDisplay.h"
|
2020-10-18 00:27:23 +08:00
|
|
|
#include "CCSlot.h"
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
CCArmatureDisplay* CCArmatureDisplay::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
CCArmatureDisplay* displayContainer = new CCArmatureDisplay();
|
|
|
|
if (displayContainer->init())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
displayContainer->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(displayContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCArmatureDisplay::dbInit(Armature* armature)
|
|
|
|
{
|
|
|
|
_armature = armature;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCArmatureDisplay::dbClear()
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
setEventDispatcher(axis::Director::getInstance()->getEventDispatcher());
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
_armature = nullptr;
|
|
|
|
CC_SAFE_RELEASE(_dispatcher);
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCArmatureDisplay::dispose(bool disposeProxy)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_armature != nullptr)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_armature->dispose();
|
|
|
|
_armature = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCArmatureDisplay::dbUpdate()
|
|
|
|
{
|
|
|
|
const auto drawed = DragonBones::debugDraw;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (drawed || _debugDraw)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_debugDraw = drawed;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_debugDraw)
|
|
|
|
{}
|
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void CCArmatureDisplay::addDBEventListener(std::string_view type, const std::function<void(EventObject*)>& callback)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
auto lambda = [callback](axis::EventCustom* event) -> void {
|
2020-10-18 00:27:23 +08:00
|
|
|
callback(static_cast<EventObject*>(event->getUserData()));
|
|
|
|
};
|
|
|
|
_dispatcher->addCustomEventListener(type, lambda);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void CCArmatureDisplay::dispatchDBEvent(std::string_view type, EventObject* value)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_dispatcher->dispatchCustomEvent(type, value);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void CCArmatureDisplay::removeDBEventListener(std::string_view type, const std::function<void(EventObject*)>& callback)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
_dispatcher->removeCustomEventListeners(type);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Rect CCArmatureDisplay::getBoundingBox() const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
auto isFirst = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
float minX = 0.0f;
|
|
|
|
float minY = 0.0f;
|
|
|
|
float maxX = 0.0f;
|
|
|
|
float maxY = 0.0f;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
for (const auto slot : _armature->getSlots())
|
|
|
|
{
|
|
|
|
if (!slot->getVisible() || !slot->getDisplay())
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto display = static_cast<CCSlot*>(slot)->getCCDisplay();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto bounds = display->getBoundingBox();
|
2020-10-18 00:27:23 +08:00
|
|
|
if (isFirst)
|
|
|
|
{
|
|
|
|
isFirst = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
minX = bounds.getMinX();
|
|
|
|
minY = bounds.getMinY();
|
|
|
|
maxX = bounds.getMaxX();
|
|
|
|
maxY = bounds.getMaxY();
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
minX = std::min(minX, bounds.getMinX());
|
|
|
|
minY = std::min(minY, bounds.getMinY());
|
|
|
|
maxX = std::max(maxX, bounds.getMaxX());
|
|
|
|
maxY = std::max(maxY, bounds.getMaxY());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Rect rect(minX, minY, maxX - minX, maxY - minY);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
return axis::RectApplyTransform(rect, getNodeToParentTransform());
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DBCCSprite* DBCCSprite::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
DBCCSprite* sprite = new DBCCSprite();
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (sprite->init())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
sprite->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
bool DBCCSprite::_checkVisibility(const axis::Mat4& transform, const axis::Size& size, const axis::Rect& rect)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
auto scene = axis::Director::getInstance()->getRunningScene();
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// If draw to Rendertexture, return true directly.
|
|
|
|
// only cull the default camera. The culling algorithm is valid for default camera.
|
2022-07-11 17:50:21 +08:00
|
|
|
if (!scene || (scene && scene->getDefaultCamera() != axis::Camera::getVisitingCamera()))
|
2020-10-18 00:27:23 +08:00
|
|
|
return true;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
auto director = axis::Director::getInstance();
|
|
|
|
axis::Rect visiableRect(director->getVisibleOrigin(), director->getVisibleSize());
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
// transform center point to screen space
|
|
|
|
float hSizeX = size.width / 2;
|
|
|
|
float hSizeY = size.height / 2;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Vec3 v3p(hSizeX, hSizeY, 0);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
transform.transformPoint(&v3p);
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Vec2 v2p = axis::Camera::getVisitingCamera()->projectGL(v3p);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
// convert content size to world coordinates
|
2021-12-25 10:04:45 +08:00
|
|
|
float wshw = std::max(fabsf(hSizeX * transform.m[0] + hSizeY * transform.m[4]),
|
|
|
|
fabsf(hSizeX * transform.m[0] - hSizeY * transform.m[4]));
|
|
|
|
float wshh = std::max(fabsf(hSizeX * transform.m[1] + hSizeY * transform.m[5]),
|
|
|
|
fabsf(hSizeX * transform.m[1] - hSizeY * transform.m[5]));
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
// enlarge visible rect half size in screen coord
|
|
|
|
visiableRect.origin.x -= wshw;
|
|
|
|
visiableRect.origin.y -= wshh;
|
|
|
|
visiableRect.size.width += wshw * 2;
|
|
|
|
visiableRect.size.height += wshh * 2;
|
|
|
|
bool ret = visiableRect.containsPoint(v2p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void DBCCSprite::draw(axis::Renderer* renderer, const axis::Mat4& transform, uint32_t flags)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
#if CC_USE_CULLING
|
2021-12-25 10:04:45 +08:00
|
|
|
# if COCOS2D_VERSION >= 0x00031400
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto& rect = _polyInfo.getRect();
|
2021-12-25 10:04:45 +08:00
|
|
|
# else
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto& rect = _polyInfo.rect;
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
|
|
|
|
2020-10-18 00:27:23 +08:00
|
|
|
// Don't do calculate the culling if the transform was not updated
|
2022-07-11 17:50:21 +08:00
|
|
|
auto visitingCamera = axis::Camera::getVisitingCamera();
|
|
|
|
auto defaultCamera = axis::Camera::getDefaultCamera();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (visitingCamera == defaultCamera)
|
|
|
|
{
|
|
|
|
_insideBounds = ((flags & FLAGS_TRANSFORM_DIRTY) || visitingCamera->isViewProjectionUpdated())
|
|
|
|
? _checkVisibility(transform, _contentSize, rect)
|
|
|
|
: _insideBounds;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_insideBounds = _checkVisibility(transform, _contentSize, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_insideBounds)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if COCOS2D_VERSION >= 0x00040000
|
|
|
|
_trianglesCommand.init(_globalZOrder, _texture, _blendFunc, _polyInfo.triangles, transform, flags);
|
|
|
|
#else
|
2021-12-25 10:04:45 +08:00
|
|
|
_trianglesCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles,
|
|
|
|
transform, flags);
|
2020-10-18 00:27:23 +08:00
|
|
|
#endif
|
|
|
|
renderer->addCommand(&_trianglesCommand);
|
|
|
|
|
|
|
|
#if CC_SPRITE_DEBUG_DRAW
|
|
|
|
_debugDrawNode->clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto count = _polyInfo.triangles.indexCount / 3;
|
2020-10-18 00:27:23 +08:00
|
|
|
auto indices = _polyInfo.triangles.indices;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto verts = _polyInfo.triangles.verts;
|
2020-10-18 00:27:23 +08:00
|
|
|
for (ssize_t i = 0; i < count; i++)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// draw 3 lines
|
2020-10-18 00:27:23 +08:00
|
|
|
auto from = verts[indices[i * 3]].vertices;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto to = verts[indices[i * 3 + 1]].vertices;
|
2022-07-11 17:50:21 +08:00
|
|
|
_debugDrawNode->drawLine(axis::Vec2(from.x, from.y), axis::Vec2(to.x, to.y), axis::Color4F::WHITE);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
from = verts[indices[i * 3 + 1]].vertices;
|
2021-12-25 10:04:45 +08:00
|
|
|
to = verts[indices[i * 3 + 2]].vertices;
|
2022-07-11 17:50:21 +08:00
|
|
|
_debugDrawNode->drawLine(axis::Vec2(from.x, from.y), axis::Vec2(to.x, to.y), axis::Color4F::WHITE);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
from = verts[indices[i * 3 + 2]].vertices;
|
2021-12-25 10:04:45 +08:00
|
|
|
to = verts[indices[i * 3]].vertices;
|
2022-07-11 17:50:21 +08:00
|
|
|
_debugDrawNode->drawLine(axis::Vec2(from.x, from.y), axis::Vec2(to.x, to.y), axis::Color4F::WHITE);
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_SPRITE_DEBUG_DRAW
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::PolygonInfo& DBCCSprite::getPolygonInfoModify()
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
return _polyInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_END
|