bool Sprite::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { if (texture.is_null()) return false; Rect2 src_rect, dst_rect; bool filter_clip; _get_rects(src_rect, dst_rect, filter_clip); dst_rect.size = dst_rect.size.abs(); if (!dst_rect.has_point(p_point)) return false; Vector2 q = (p_point - dst_rect.position) / dst_rect.size; if (hflip) q.x = 1.0f - q.x; if (vflip) q.y = 1.0f - q.y; q = q * src_rect.size + src_rect.position; Ref<Image> image; Ref<AtlasTexture> atlasTexture = texture; if (atlasTexture.is_null()) { image = texture->get_data(); } else { ERR_FAIL_COND_V(atlasTexture->get_atlas().is_null(), false); image = atlasTexture->get_atlas()->get_data(); Rect2 region = atlasTexture->get_region(); Rect2 margin = atlasTexture->get_margin(); q -= margin.position; if ((q.x > region.size.width) || (q.y > region.size.height)) { return false; } q += region.position; } ERR_FAIL_COND_V(image.is_null(), false); image->lock(); const Color c = image->get_pixel((int)q.x, (int)q.y); image->unlock(); return c.a > 0.01; }
bool Sprite::is_pixel_opaque(const Point2 &p_point) const { if (texture.is_null()) return false; Rect2 src_rect, dst_rect; bool filter_clip; _get_rects(src_rect, dst_rect, filter_clip); dst_rect.size = dst_rect.size.abs(); if (!dst_rect.has_point(p_point)) return false; Vector2 q = (p_point - dst_rect.position) / dst_rect.size; if (hflip) q.x = 1.0f - q.x; if (vflip) q.y = 1.0f - q.y; q = q * src_rect.size + src_rect.position; bool is_repeat = texture->get_flags() & Texture::FLAG_REPEAT; bool is_mirrored_repeat = texture->get_flags() & Texture::FLAG_MIRRORED_REPEAT; if (is_repeat) { int mirror_x = 0; int mirror_y = 0; if (is_mirrored_repeat) { mirror_x = (int)(q.x / texture->get_size().width); mirror_y = (int)(q.y / texture->get_size().height); } q.x = Math::fmod(q.x, texture->get_size().width); q.y = Math::fmod(q.y, texture->get_size().height); if (mirror_x % 2 == 1) { q.x = texture->get_size().width - q.x - 1; } if (mirror_y % 2 == 1) { q.y = texture->get_size().height - q.y - 1; } } else { q.x = MIN(q.x, texture->get_size().width - 1); q.y = MIN(q.y, texture->get_size().height - 1); } return texture->is_pixel_opaque((int)q.x, (int)q.y); }
void Sprite::_notification(int p_what) { switch (p_what) { case NOTIFICATION_DRAW: { if (texture.is_null()) return; RID ci = get_canvas_item(); /* texture->draw(ci,Point2()); break; */ Rect2 src_rect, dst_rect; bool filter_clip; _get_rects(src_rect, dst_rect, filter_clip); texture->draw_rect_region(ci, dst_rect, src_rect, Color(1, 1, 1), false, normal_map, filter_clip); } break; } }