Merge pull request #14566 from zilongshanren/fixIssue14564

fix scale9sprite be culled unexpectedly
This commit is contained in:
zilongshanren 2015-12-03 19:17:40 +08:00
commit ea42c09243
3 changed files with 74 additions and 19 deletions

View File

@ -31,7 +31,9 @@
#include "renderer/ccShaders.h"
#include "platform/CCImage.h"
#include "base/CCNinePatchImageParser.h"
#include "2d/CCDrawNode.h"
#include "2d/CCCamera.h"
#include "renderer/CCRenderer.h"
NS_CC_BEGIN
namespace ui {
@ -54,8 +56,13 @@ namespace ui {
,_sliceIndices(nullptr)
,_sliceSpriteDirty(false)
,_renderingType(RenderingType::SLICE)
,_insideBounds(true)
{
this->setAnchorPoint(Vec2(0.5,0.5));
#if CC_SPRITE_DEBUG_DRAW
_debugDrawNode = DrawNode::create();
addChild(_debugDrawNode);
#endif //CC_SPRITE_DEBUG_DRAW
}
Scale9Sprite::~Scale9Sprite()
@ -673,10 +680,60 @@ namespace ui {
this->_insetBottom = insetBottom;
this->updateCapInset();
}
void Scale9Sprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
if (_scale9Image) {
#if CC_USE_CULLING
// Don't do calculate the culling if the transform was not updated
auto visitingCamera = Camera::getVisitingCamera();
auto defaultCamera = Camera::getDefaultCamera();
if (visitingCamera == defaultCamera) {
_insideBounds = ((flags & FLAGS_TRANSFORM_DIRTY)|| visitingCamera->isViewProjectionUpdated()) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
}
else
{
_insideBounds = renderer->checkVisibility(transform, _contentSize);
}
if(_insideBounds)
#endif
{
auto textureName = _scale9Image->getTexture()->getName();
auto programState = _scale9Image->getGLProgramState();
auto blendFunc = _scale9Image->getBlendFunc();
auto& polyInfo = _scale9Image->getPolygonInfo();
auto globalZOrder = _scale9Image->getGlobalZOrder();
_trianglesCommand.init(globalZOrder,textureName, programState, blendFunc, polyInfo.triangles, transform, flags);
renderer->addCommand(&_trianglesCommand);
#if CC_SPRITE_DEBUG_DRAW
_debugDrawNode->clear();
auto count = polyInfo.triangles.indexCount/3;
auto indices = polyInfo.triangles.indices;
auto verts = polyInfo.triangles.verts;
for(ssize_t i = 0; i < count; i++)
{
//draw 3 lines
Vec3 from =verts[indices[i*3]].vertices;
Vec3 to = verts[indices[i*3+1]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
from =verts[indices[i*3+1]].vertices;
to = verts[indices[i*3+2]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
from =verts[indices[i*3+2]].vertices;
to = verts[indices[i*3]].vertices;
_debugDrawNode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::WHITE);
}
#endif //CC_SPRITE_DEBUG_DRAW
}
}
}
void Scale9Sprite::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
// quick return if not visible. children won't be drawn.
if (!_visible)
{
@ -714,26 +771,11 @@ namespace ui {
break;
}
if (_scale9Image && _scale9Image->getLocalZOrder() < 0 )
{
_scale9Image->visit(renderer, _modelViewTransform, flags);
}
//
// draw self
//
if (isVisitableByVisitingCamera())
this->draw(renderer, _modelViewTransform, flags);
//
// draw children and protectedChildren zOrder >= 0
//
if (_scale9Image && _scale9Image->getLocalZOrder() >= 0 )
{
_scale9Image->visit(renderer, _modelViewTransform, flags);
}
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, flags);

View File

@ -37,6 +37,8 @@
* @{
*/
NS_CC_BEGIN
class DrawNode;
namespace ui {
/**
@ -590,6 +592,7 @@ namespace ui {
/// @} end of Children and Parent
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
@ -715,11 +718,17 @@ namespace ui {
bool _isPatch9;
State _brightState;
Vec2 _nonSliceSpriteAnchor;
V3F_C4B_T2F* _sliceVertices;
unsigned short* _sliceIndices;
bool _sliceSpriteDirty;
RenderingType _renderingType;
#if CC_SPRITE_DEBUG_DRAW
DrawNode *_debugDrawNode;
#endif //CC_SPRITE_DEBUG_DRAW
bool _insideBounds; /// whether or not the sprite was inside bounds the previous frame
TrianglesCommand _trianglesCommand; ///
};
}} //end of namespace

View File

@ -153,8 +153,12 @@ bool UIButtonTest_Scale9::init()
Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
// open scale9 render
button->setScale9Enabled(true);
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->setContentSize(Size(150, 70));
button->setPosition(Vec2(-button->getContentSize().width - 10, widgetSize.height / 2.0f));
auto moveBy = MoveBy::create(1.0, Vec2(widgetSize.width/2, 0));
auto moveByReverse = moveBy->reverse()->clone();
button->runAction(RepeatForever::create(
Sequence::create(moveBy,moveByReverse, NULL)));
button->setPressedActionEnabled(true);
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
_uiLayer->addChild(button);