Skip to content

Commit

Permalink
Fix GUI ScrollView clipping bug and add lua support (#1634)
Browse files Browse the repository at this point in the history
Apply to at least 2.1.1
  • Loading branch information
bintester authored Jan 25, 2024
1 parent 6543931 commit 553f23d
Show file tree
Hide file tree
Showing 5 changed files with 3,704 additions and 53 deletions.
100 changes: 49 additions & 51 deletions extensions/GUI/ScrollView/ScrollView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,53 +565,52 @@ void ScrollView::addChild(Node* child, int zOrder, std::string_view name)

void ScrollView::beforeDraw()
{
// TODO: minggo
// ScrollView don't support drawing in 3D space
// _beforeDrawCommand.init(_globalZOrder);
// _beforeDrawCommand.func = AX_CALLBACK_0(ScrollView::onBeforeDraw, this);
// Director::getInstance()->getRenderer()->addCommand(&_beforeDrawCommand);
}

//ScrollView don't support drawing in 3D space
//_beforeDrawCommand.init(_globalZOrder);
//_beforeDrawCommand.func = AX_CALLBACK_0(ScrollView::onBeforeDraw, this);
Director::getInstance()->getRenderer()->addCallbackCommand(AX_CALLBACK_0(ScrollView::onBeforeDraw, this),
_globalZOrder);
}

/**
* clip this view so that outside of the visible bounds can be hidden.
*/
void ScrollView::onBeforeDraw()
{
// TODO: minggo
// if (_clippingToBounds)
// {
// _scissorRestored = false;
// Rect frame = getViewRect();
// auto glView = Director::getInstance()->getGLView();
//
// if (glView->getVR() == nullptr) {
// if (glView->isScissorEnabled()) {
// _scissorRestored = true;
// _parentScissorRect = glView->getScissorRect();
// //set the intersection of _parentScissorRect and frame as the new scissor rect
// if (frame.intersectsRect(_parentScissorRect)) {
// float x = MAX(frame.origin.x, _parentScissorRect.origin.x);
// float y = MAX(frame.origin.y, _parentScissorRect.origin.y);
// float xx = MIN(frame.origin.x + frame.size.width, _parentScissorRect.origin.x +
// _parentScissorRect.size.width); float yy = MIN(frame.origin.y + frame.size.height,
// _parentScissorRect.origin.y + _parentScissorRect.size.height); glView->setScissorInPoints(x,
// y, xx - x, yy - y);
// }
// }
// else {
// glEnable(GL_SCISSOR_TEST);
// glView->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
// }
// }
// }

if (_clippingToBounds)
{
_scissorRestored = false;
Rect frame = getViewRect();
auto glview = Director::getInstance()->getGLView();

if (glview->isScissorEnabled()) {
_scissorRestored = true;
_parentScissorRect = glview->getScissorRect();
//set the intersection of _parentScissorRect and frame as the new scissor rect
if (frame.intersectsRect(_parentScissorRect)) {
float x = MAX(frame.origin.x, _parentScissorRect.origin.x);
float y = MAX(frame.origin.y, _parentScissorRect.origin.y);
float xx = MIN(frame.origin.x + frame.size.width, _parentScissorRect.origin.x + _parentScissorRect.size.width);
float yy = MIN(frame.origin.y + frame.size.height, _parentScissorRect.origin.y + _parentScissorRect.size.height);
glview->setScissorInPoints(x, y, xx - x, yy - y);
}
}
else {

Director::getInstance()->getRenderer()->setScissorTest(true);
glview->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
}

}
}

void ScrollView::afterDraw()
{
// TODO: minggo
// _afterDrawCommand.init(_globalZOrder);
// _afterDrawCommand.func = AX_CALLBACK_0(ScrollView::onAfterDraw, this);
// Director::getInstance()->getRenderer()->addCommand(&_afterDrawCommand);

Director::getInstance()->getRenderer()->addCallbackCommand(AX_CALLBACK_0(ScrollView::onAfterDraw, this),
_globalZOrder);
}

/**
Expand All @@ -620,20 +619,19 @@ void ScrollView::afterDraw()
*/
void ScrollView::onAfterDraw()
{
// TODO:minggo
// if (_clippingToBounds)
// {
// auto glView = Director::getInstance()->getGLView();
// if (glView->getVR() == nullptr) {
// if (_scissorRestored) {//restore the parent's scissor rect
// glView->setScissorInPoints(_parentScissorRect.origin.x, _parentScissorRect.origin.y,
// _parentScissorRect.size.width, _parentScissorRect.size.height);
// }
// else {
// glDisable(GL_SCISSOR_TEST);
// }
// }
// }

if (_clippingToBounds)
{
auto glview = Director::getInstance()->getGLView();

if (_scissorRestored) {//restore the parent's scissor rect
glview->setScissorInPoints(_parentScissorRect.origin.x, _parentScissorRect.origin.y, _parentScissorRect.size.width, _parentScissorRect.size.height);
}
else {
Director::getInstance()->getRenderer()->setScissorTest(false);
}

}
}

void ScrollView::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
Expand Down
4 changes: 2 additions & 2 deletions extensions/GUI/ScrollView/ScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ class AX_EX_DLL ScrollView : public Layer, public ActionTweenDelegate
/** Touch listener */
EventListenerTouchOneByOne* _touchListener;

CustomCommand _beforeDrawCommand;
CustomCommand _afterDrawCommand;
//CustomCommand _beforeDrawCommand;
//CustomCommand _afterDrawCommand;

/**
* Action created with setContentOffsetInDuration(), saved so it can be halted
Expand Down
3 changes: 3 additions & 0 deletions extensions/axmol-ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#include "ExtensionMacros.h"

#include "GUI/ScrollView/ScrollView.h"
#include "GUI/ScrollView/TableView.h"

// Physics integration
#include "physics-nodes/PhysicsDebugNode.h"
#include "physics-nodes/PhysicsDebugNodeBox2D.h"
Expand Down
Loading

0 comments on commit 553f23d

Please sign in to comment.