[Fix] Node Camera Masking Design Flaw (#621)

* Update CCNode.cpp

* Add a separate method for assigning mask to children

* Update CCNode.cpp

* Tidy Up Stuff
This commit is contained in:
Turky Mohammed 2022-04-25 04:37:41 +03:00 committed by GitHub
parent 821be105dc
commit a1ebd965c9
2 changed files with 20 additions and 1 deletions

View File

@ -117,6 +117,7 @@ Node::Node()
, _realColor(Color3B::WHITE)
, _cascadeColorEnabled(false)
, _cascadeOpacityEnabled(false)
, _childFollowCameraMask(false)
, _cameraMask(1)
, _onEnterCallback(nullptr)
, _onExitCallback(nullptr)
@ -995,7 +996,10 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, std::string_vie
this->insertChild(child, localZOrder);
child->setParent(this);
if (_childFollowCameraMask)
child->setCameraMask(this->getCameraMask());
if (setTag)
{
child->setTag(tag);
@ -2177,6 +2181,11 @@ bool isScreenPointInRect(const Vec2& pt, const Camera* camera, const Mat4& w2l,
return rect.containsPoint(Vec2(P.x, P.y));
}
void Node::applyMaskOnEnter(bool applyChildren)
{
_childFollowCameraMask = applyChildren;
}
// MARK: Camera
void Node::setCameraMask(unsigned short mask, bool applyChildren)
{

View File

@ -1798,6 +1798,7 @@ public:
* get & set camera mask, the node is visible by the camera whose camera flag & node's camera mask is true
*/
unsigned short getCameraMask() const { return _cameraMask; }
/**
* Modify the camera mask for current node.
* If applyChildren is true, then it will modify the camera mask of its children recursively.
@ -1806,6 +1807,13 @@ public:
*/
virtual void setCameraMask(unsigned short mask, bool applyChildren = true);
/**
* Should addChild() make the child follow it's parent's mask?
* If applyChildren is true, then it will modify the camera mask of its children recursively when a child is added.
* @param applyChildren A boolean value to determine whether the mask bit should apply to its children or not.
*/
void applyMaskOnEnter(bool applyChildren);
virtual void setProgramState(uint32_t programType) { setProgramStateWithRegistry(programType, nullptr); }
void setProgramStateWithRegistry(uint32_t programType, Texture2D* texture);
@ -1966,6 +1974,8 @@ protected:
bool _usingNormalizedPosition;
bool _normalizedPositionDirty;
bool _childFollowCameraMask;
// camera mask, it is visible only when _cameraMask & current camera' camera flag is true
unsigned short _cameraMask;