Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ScrollView sub node position error when render by RenderTexture. #20549

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cocos/2d/CCRenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ THE SOFTWARE.

NS_CC_BEGIN

static RenderTexture* g_current = nullptr;

// implementation RenderTexture
RenderTexture::RenderTexture()
: _keepMatrix(false)
Expand Down Expand Up @@ -206,6 +208,10 @@ RenderTexture * RenderTexture::create(int w, int h)
return nullptr;
}

RenderTexture * RenderTexture::current() {
return g_current;
}

bool RenderTexture::initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat eFormat)
{
return initWithWidthAndHeight(w, h, eFormat, 0);
Expand Down Expand Up @@ -684,8 +690,21 @@ Image* RenderTexture::newImage(bool flipImage)
return image;
}

Rect& RenderTexture::getVirtualViewport() {
Rect viewport;
viewport.size.width = _fullviewPort.size.width;
viewport.size.height = _fullviewPort.size.height;
float viewPortRectWidthRatio = float(viewport.size.width)/_fullRect.size.width;
float viewPortRectHeightRatio = float(viewport.size.height)/_fullRect.size.height;
viewport.origin.x = (_fullRect.origin.x - _rtTextureRect.origin.x) * viewPortRectWidthRatio;
viewport.origin.y = (_fullRect.origin.y - _rtTextureRect.origin.y) * viewPortRectHeightRatio;
return viewport;
}

void RenderTexture::onBegin()
{
g_current = this;

//
Director *director = Director::getInstance();

Expand Down Expand Up @@ -755,6 +774,7 @@ void RenderTexture::onEnd()
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _oldProjMatrix);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _oldTransMatrix);

g_current = nullptr;
}

void RenderTexture::onClear()
Expand Down
6 changes: 6 additions & 0 deletions cocos/2d/CCRenderTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ class CC_DLL RenderTexture : public Node
* @param fullViewport The total viewportSize.
*/
void setVirtualViewport(const Vec2& rtBegin, const Rect& fullRect, const Rect& fullViewport);
/** Get the RenderTexture object visit now.
*/
static RenderTexture * current();
/** Get RenderTexture viewport.
*/
Rect& getVirtualViewport();

public:
/** FIXME: should be protected.
Expand Down
14 changes: 13 additions & 1 deletion extensions/GUI/CCScrollView/CCScrollView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "2d/CCActionInstant.h"
#include "2d/CCActionInterval.h"
#include "2d/CCActionTween.h"
#include "2d/CCRenderTexture.h"
#include "2d/CCCamera.h"
#include "base/CCDirector.h"
#include "base/CCEventDispatcher.h"
#include "renderer/CCRenderer.h"
Expand Down Expand Up @@ -901,7 +903,17 @@ Rect ScrollView::getViewRect()
screenPos.y += _viewSize.height*scaleY;
scaleY = -scaleY;
}


auto rt = RenderTexture::current();
if (rt != nullptr) {
auto cam = Camera::getDefaultCamera();
auto viewport = cam->getDefaultViewport();
auto rect2 = rt->getVirtualViewport();

screenPos.x = screenPos.x + (rect2.origin.x - viewport._left);
screenPos.y = screenPos.y + (rect2.origin.y - viewport._bottom);
}

return Rect(screenPos.x, screenPos.y, _viewSize.width*scaleX, _viewSize.height*scaleY);
}
NS_CC_EXT_END