2020-08-04 12:31:33 +08:00
|
|
|
#include "HitTest.h"
|
|
|
|
#include "GComponent.h"
|
|
|
|
#include "utils/ByteBuffer.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2020-08-04 12:31:33 +08:00
|
|
|
NS_FGUI_BEGIN
|
|
|
|
|
|
|
|
PixelHitTestData::PixelHitTestData() :
|
|
|
|
pixels(nullptr),
|
|
|
|
pixelsLength(0),
|
|
|
|
pixelWidth(0),
|
|
|
|
scale(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelHitTestData::~PixelHitTestData()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(pixels);
|
2020-08-04 12:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PixelHitTestData::load(ByteBuffer* buffer)
|
|
|
|
{
|
|
|
|
buffer->skip(4);
|
|
|
|
pixelWidth = buffer->readInt();
|
|
|
|
scale = 1.0f / buffer->readByte();
|
|
|
|
pixelsLength = buffer->readInt();
|
|
|
|
pixels = new unsigned char[pixelsLength];
|
|
|
|
for (size_t i = 0; i < pixelsLength; i++)
|
|
|
|
pixels[i] = buffer->readByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelHitTest::PixelHitTest(PixelHitTestData * data, int offsetX, int offsetY) :
|
|
|
|
offsetX(offsetX),
|
|
|
|
offsetY(offsetY),
|
|
|
|
scaleX(1),
|
|
|
|
scaleY(1),
|
|
|
|
_data(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool PixelHitTest::hitTest(GComponent * obj, const ax::Vec2 & localPoint)
|
2020-08-04 12:31:33 +08:00
|
|
|
{
|
|
|
|
int x = floor((localPoint.x / scaleX - offsetX) * _data->scale);
|
|
|
|
int y = floor(((obj->getHeight() - localPoint.y) / scaleY - offsetY) * _data->scale);
|
|
|
|
if (x < 0 || y < 0 || x >= _data->pixelWidth)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ssize_t pos = y * _data->pixelWidth + x;
|
|
|
|
ssize_t pos2 = pos / 8;
|
|
|
|
ssize_t pos3 = pos % 8;
|
|
|
|
|
|
|
|
if (pos2 >= 0 && pos2 < (ssize_t)_data->pixelsLength)
|
|
|
|
return ((_data->pixels[pos2] >> pos3) & 0x1) > 0;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_FGUI_END
|