Skip to content

Commit 4610e76

Browse files
committed
fix #146: Ignore color effects in textureContainsPoint()
1 parent d18b326 commit 4610e76

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/cputexturemanager.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,26 @@ QRgb CpuTextureManager::getPointColor(const Texture &texture, int x, int y, cons
8181
bool CpuTextureManager::textureContainsPoint(const Texture &texture, const QPointF &localPoint, const std::unordered_map<ShaderManager::Effect, double> &effects)
8282
{
8383
// https://github.com/scratchfoundation/scratch-render/blob/7b823985bc6fe92f572cc3276a8915e550f7c5e6/src/Silhouette.js#L219-L226
84-
return qAlpha(getPointColor(texture, localPoint.x(), localPoint.y(), effects)) > 0;
84+
const int width = texture.width();
85+
const int height = texture.height();
86+
int x = localPoint.x();
87+
int y = localPoint.y();
88+
89+
if (!effects.empty()) {
90+
// Get local position with effect transform
91+
QVector2D transformedCoords;
92+
const QVector2D localCoords(x / static_cast<float>(width), y / static_cast<float>(height));
93+
EffectTransform::transformPoint(effects, localCoords, transformedCoords);
94+
x = transformedCoords.x() * width;
95+
y = transformedCoords.y() * height;
96+
}
97+
98+
if ((x < 0 || x >= width) || (y < 0 || y >= height))
99+
return false;
100+
101+
GLubyte *pixels = getTextureData(texture);
102+
QRgb color = qRgba(pixels[(y * width + x) * 4], pixels[(y * width + x) * 4 + 1], pixels[(y * width + x) * 4 + 2], pixels[(y * width + x) * 4 + 3]);
103+
return qAlpha(color) > 0;
85104
}
86105

87106
void CpuTextureManager::removeTexture(const Texture &texture)

test/texture/cputexturemanager_test.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,6 @@ TEST_F(CpuTextureManagerTest, TextureContainsPoint)
209209
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3, 3 }, {}));
210210
ASSERT_TRUE(manager.textureContainsPoint(texture, { 3.3, 3.5 }, {}));
211211

212-
std::unordered_map<ShaderManager::Effect, double> effects = { { ShaderManager::Effect::Ghost, 100 } };
213-
ASSERT_FALSE(manager.textureContainsPoint(texture, { 1, 3 }, effects));
214-
ASSERT_FALSE(manager.textureContainsPoint(texture, { 2, 3 }, effects));
215-
ASSERT_FALSE(manager.textureContainsPoint(texture, { 3, 3 }, effects));
216-
ASSERT_FALSE(manager.textureContainsPoint(texture, { 3.3, 3.5 }, effects));
217-
218212
// TODO: Test point transform (graphic effects that change shape)
219213

220214
// Cleanup

0 commit comments

Comments
 (0)