Skip to content

Commit 54f7ed3

Browse files
committed
Add debug code for bounding boxes
1 parent 70e82e2 commit 54f7ed3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/ProjectPlayer.qml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,52 @@ ProjectScene {
156156
}
157157
}
158158

159+
// Uncomment to display sprite bounding boxes (for debugging)
160+
/*Rectangle {
161+
function translateX(x) {
162+
// Translates Scratch X-coordinate to the scene coordinate system
163+
return root.stageScale * (root.stageWidth / 2 + x)
164+
}
165+
166+
function translateY(y) {
167+
// Translates Scratch Y-coordinate to the scene coordinate system
168+
return root.stageScale * (root.stageHeight / 2 - y)
169+
}
170+
171+
id: boundRect
172+
color: "transparent"
173+
border.color: "red"
174+
border.width: 3
175+
176+
function updatePosition() {
177+
let bounds = targetItem.getQmlBounds();
178+
boundRect.x = translateX(bounds.left);
179+
boundRect.y = translateY(bounds.top);
180+
width = bounds.width * root.stageScale;
181+
height = -bounds.height * root.stageScale;
182+
}
183+
184+
Connections {
185+
target: targetItem
186+
187+
function onXChanged() { boundRect.updatePosition() }
188+
function onYChanged() { boundRect.updatePosition() }
189+
function onRotationChanged() { boundRect.updatePosition() }
190+
function onWidthChanged() { boundRect.updatePosition() }
191+
function onHeightChanged() { boundRect.updatePosition() }
192+
function onScaleChanged() { boundRect.updatePosition() }
193+
}
194+
195+
Connections {
196+
property Scale transform: Scale {}
197+
target: transform
198+
199+
function onXScaleChanged() { boundRect.updatePosition() }
200+
201+
Component.onCompleted: transform = targetItem.transform[0]
202+
}
203+
}*/
204+
159205
Loader {
160206
readonly property alias model: targetItem.spriteModel
161207
active: model ? model.bubbleText !== "" : false

src/renderedtarget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ Rect RenderedTarget::getBounds() const
395395
return Rect(left + m_x, top + m_y, right + m_x, bottom + m_y);
396396
}
397397

398+
QRectF scratchcpprender::RenderedTarget::getQmlBounds() const
399+
{
400+
Rect bounds = getBounds();
401+
return QRectF(QPointF(bounds.left(), bounds.top()), QPointF(bounds.right(), bounds.bottom()));
402+
}
403+
398404
QRectF RenderedTarget::getBoundsForBubble() const
399405
{
400406
// https://github.com/scratchfoundation/scratch-render/blob/86dcb0151a04bc8c1ff39559e8531e7921102b56/src/Drawable.js#L536-L551

src/renderedtarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class RenderedTarget : public IRenderedTarget
7676
void setHeight(qreal height) override;
7777

7878
libscratchcpp::Rect getBounds() const override;
79+
Q_INVOKABLE QRectF getQmlBounds() const;
7980
Q_INVOKABLE QRectF getBoundsForBubble() const override;
8081
libscratchcpp::Rect getFastBounds() const override;
8182

0 commit comments

Comments
 (0)