CCClippingRectangleNode.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "2d/CCClippingRectangleNode.h"
  2. #include "base/CCDirector.h"
  3. #include "renderer/CCRenderer.h"
  4. #include "math/Vec2.h"
  5. #include "platform/CCGLView.h"
  6. NS_CC_BEGIN
  7. ClippingRectangleNode* ClippingRectangleNode::create(const Rect& clippingRegion)
  8. {
  9. ClippingRectangleNode* node = new (std::nothrow) ClippingRectangleNode();
  10. if (node && node->init()) {
  11. node->setClippingRegion(clippingRegion);
  12. node->autorelease();
  13. } else {
  14. CC_SAFE_DELETE(node);
  15. }
  16. return node;
  17. }
  18. ClippingRectangleNode* ClippingRectangleNode::create()
  19. {
  20. ClippingRectangleNode* node = new (std::nothrow) ClippingRectangleNode();
  21. if (node && node->init()) {
  22. node->autorelease();
  23. } else {
  24. CC_SAFE_DELETE(node);
  25. }
  26. return node;
  27. }
  28. void ClippingRectangleNode::setClippingRegion(const Rect &clippingRegion)
  29. {
  30. _clippingRegion = clippingRegion;
  31. }
  32. void ClippingRectangleNode::onBeforeVisitScissor()
  33. {
  34. if (_clippingEnabled) {
  35. glEnable(GL_SCISSOR_TEST);
  36. float scaleX = _scaleX;
  37. float scaleY = _scaleY;
  38. Node *parent = this->getParent();
  39. while (parent) {
  40. scaleX *= parent->getScaleX();
  41. scaleY *= parent->getScaleY();
  42. parent = parent->getParent();
  43. }
  44. const Point pos = convertToWorldSpace(Point(_clippingRegion.origin.x, _clippingRegion.origin.y));
  45. GLView* glView = Director::getInstance()->getOpenGLView();
  46. glView->setScissorInPoints(pos.x,
  47. pos.y,
  48. _clippingRegion.size.width * scaleX,
  49. _clippingRegion.size.height * scaleY);
  50. }
  51. }
  52. void ClippingRectangleNode::onAfterVisitScissor()
  53. {
  54. if (_clippingEnabled)
  55. {
  56. glDisable(GL_SCISSOR_TEST);
  57. }
  58. }
  59. void ClippingRectangleNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  60. {
  61. _beforeVisitCmdScissor.init(_globalZOrder);
  62. _beforeVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onBeforeVisitScissor, this);
  63. renderer->addCommand(&_beforeVisitCmdScissor);
  64. Node::visit(renderer, parentTransform, parentFlags);
  65. _afterVisitCmdScissor.init(_globalZOrder);
  66. _afterVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onAfterVisitScissor, this);
  67. renderer->addCommand(&_afterVisitCmdScissor);
  68. }
  69. NS_CC_END