Skip to content

Commit 351561f

Browse files
committed
RenderedTarget: Move rect union calculation to a separate method
1 parent 2bb29e8 commit 351561f

File tree

2 files changed

+84
-25
lines changed

2 files changed

+84
-25
lines changed

src/renderedtarget.cpp

+81-25
Original file line numberDiff line numberDiff line change
@@ -610,34 +610,10 @@ bool RenderedTarget::touchingClones(const std::vector<libscratchcpp::Sprite *> &
610610
if (myRect.isEmpty())
611611
return false;
612612

613-
QRectF united;
614613
std::vector<IRenderedTarget *> candidates;
615614

616615
// Calculate the union of the bounding rectangle intersections
617-
for (auto clone : clones) {
618-
Q_ASSERT(clone);
619-
620-
if (!clone)
621-
continue;
622-
623-
SpriteModel *model = static_cast<SpriteModel *>(clone->getInterface());
624-
Q_ASSERT(model);
625-
626-
if (model) {
627-
// Calculate the intersection of the bounding rectangles
628-
IRenderedTarget *candidate = model->renderedTarget();
629-
Q_ASSERT(candidate);
630-
Rect scratchRect = candidate->getFastBounds();
631-
// TODO: Use Rect::snapToInt()
632-
QRect rect(QPoint(scratchRect.left(), scratchRect.bottom()), QPoint(scratchRect.right(), scratchRect.top()));
633-
QRectF intersected = myRect.intersected(rect);
634-
635-
// Add it to the union
636-
united = united.united(intersected);
637-
638-
candidates.push_back(candidate);
639-
}
640-
}
616+
QRectF united = candidatesBounds(myRect, clones, candidates);
641617

642618
if (united.isEmpty() || candidates.empty())
643619
return false;
@@ -813,6 +789,86 @@ QRectF RenderedTarget::touchingBounds() const
813789
return bounds;
814790
}
815791

792+
QRectF RenderedTarget::candidatesBounds(const QRectF &targetRect, const std::vector<Target *> &candidates, std::vector<IRenderedTarget *> &dst)
793+
{
794+
QRectF united;
795+
dst.clear();
796+
797+
for (auto candidate : candidates) {
798+
Q_ASSERT(candidate);
799+
800+
if (!candidate)
801+
continue;
802+
803+
IRenderedTarget *target = nullptr;
804+
805+
if (candidate->isStage()) {
806+
Stage *stage = static_cast<Stage *>(candidate);
807+
StageModel *model = static_cast<StageModel *>(stage->getInterface());
808+
Q_ASSERT(model);
809+
810+
if (model)
811+
target = model->renderedTarget();
812+
} else {
813+
Sprite *sprite = static_cast<Sprite *>(candidate);
814+
SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface());
815+
Q_ASSERT(model);
816+
817+
if (model)
818+
target = model->renderedTarget();
819+
}
820+
821+
united = united.united(candidateIntersection(targetRect, target));
822+
823+
if (target)
824+
dst.push_back(target);
825+
}
826+
827+
return united;
828+
}
829+
830+
QRectF RenderedTarget::candidatesBounds(const QRectF &targetRect, const std::vector<libscratchcpp::Sprite *> &candidates, std::vector<IRenderedTarget *> &dst)
831+
{
832+
QRectF united;
833+
dst.clear();
834+
835+
for (auto candidate : candidates) {
836+
Q_ASSERT(candidate);
837+
838+
if (!candidate)
839+
continue;
840+
841+
IRenderedTarget *target = nullptr;
842+
SpriteModel *model = static_cast<SpriteModel *>(candidate->getInterface());
843+
Q_ASSERT(model);
844+
845+
if (model)
846+
target = model->renderedTarget();
847+
848+
united = united.united(candidateIntersection(targetRect, target));
849+
850+
if (target)
851+
dst.push_back(target);
852+
}
853+
854+
return united;
855+
}
856+
857+
QRectF RenderedTarget::candidateIntersection(const QRectF &targetRect, IRenderedTarget *target)
858+
{
859+
Q_ASSERT(target);
860+
861+
if (target) {
862+
// Calculate the intersection of the bounding rectangles
863+
Rect scratchRect = target->getFastBounds();
864+
// TODO: Use Rect::snapToInt()
865+
QRect rect(QPoint(scratchRect.left(), scratchRect.bottom()), QPoint(scratchRect.right(), scratchRect.top()));
866+
return targetRect.intersected(rect);
867+
}
868+
869+
return QRectF();
870+
}
871+
816872
void RenderedTarget::clampRect(Rect &rect, double left, double right, double bottom, double top)
817873
{
818874
// TODO: Use Rect::clamp()

src/renderedtarget.h

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class RenderedTarget : public IRenderedTarget
123123
QPointF mapFromStageWithOriginPoint(const QPointF &scenePoint) const;
124124
CpuTextureManager *textureManager();
125125
QRectF touchingBounds() const;
126+
static QRectF candidatesBounds(const QRectF &targetRect, const std::vector<libscratchcpp::Target *> &candidates, std::vector<IRenderedTarget *> &dst);
127+
static QRectF candidatesBounds(const QRectF &targetRect, const std::vector<libscratchcpp::Sprite *> &candidates, std::vector<IRenderedTarget *> &dst);
128+
static QRectF candidateIntersection(const QRectF &targetRect, IRenderedTarget *target);
126129
static void clampRect(libscratchcpp::Rect &rect, double left, double right, double bottom, double top);
127130

128131
libscratchcpp::IEngine *m_engine = nullptr;

0 commit comments

Comments
 (0)