Skip to content

Commit e49c8af

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

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

src/renderedtarget.cpp

+49-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,54 @@ 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+
Q_ASSERT(target);
822+
823+
if (target) {
824+
// Calculate the intersection of the bounding rectangles
825+
Rect scratchRect = target->getFastBounds();
826+
// TODO: Use Rect::snapToInt()
827+
QRect rect(QPoint(scratchRect.left(), scratchRect.bottom()), QPoint(scratchRect.right(), scratchRect.top()));
828+
QRectF intersected = targetRect.intersected(rect);
829+
830+
// Add it to the union
831+
united = united.united(intersected);
832+
833+
dst.push_back(target);
834+
}
835+
}
836+
837+
return united;
838+
}
839+
816840
void RenderedTarget::clampRect(Rect &rect, double left, double right, double bottom, double top)
817841
{
818842
// TODO: Use Rect::clamp()

src/renderedtarget.h

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ 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);
126127
static void clampRect(libscratchcpp::Rect &rect, double left, double right, double bottom, double top);
127128

128129
libscratchcpp::IEngine *m_engine = nullptr;

0 commit comments

Comments
 (0)