You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-20 14:45:44 +00:00
Improve cursor_set_custom_image() method
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include "display_server.h"
|
||||
|
||||
#include "core/input/input.h"
|
||||
#include "scene/resources/atlas_texture.h"
|
||||
#include "scene/resources/texture.h"
|
||||
#include "servers/display_server_headless.h"
|
||||
|
||||
@@ -987,6 +988,43 @@ void DisplayServer::_bind_methods() {
|
||||
BIND_ENUM_CONSTANT(TTS_UTTERANCE_BOUNDARY);
|
||||
}
|
||||
|
||||
Ref<Image> DisplayServer::_get_cursor_image_from_resource(const Ref<Resource> &p_cursor, const Vector2 &p_hotspot, Rect2 &r_atlas_rect) {
|
||||
Ref<Image> image;
|
||||
ERR_FAIL_COND_V_MSG(p_hotspot.x < 0 || p_hotspot.y < 0, image, "Hotspot outside cursor image.");
|
||||
|
||||
Size2 texture_size;
|
||||
|
||||
Ref<Texture2D> texture = p_cursor;
|
||||
if (texture.is_valid()) {
|
||||
Ref<AtlasTexture> atlas_texture = p_cursor;
|
||||
|
||||
if (atlas_texture.is_valid()) {
|
||||
texture = atlas_texture->get_atlas();
|
||||
r_atlas_rect.size = texture->get_size();
|
||||
r_atlas_rect.position = atlas_texture->get_region().position;
|
||||
texture_size = atlas_texture->get_region().size;
|
||||
} else {
|
||||
texture_size = texture->get_size();
|
||||
}
|
||||
image = texture->get_image();
|
||||
} else {
|
||||
image = p_cursor;
|
||||
ERR_FAIL_COND_V(image.is_null(), image);
|
||||
texture_size = image->get_size();
|
||||
}
|
||||
|
||||
ERR_FAIL_COND_V_MSG(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height, image, "Hotspot outside cursor image.");
|
||||
ERR_FAIL_COND_V_MSG(texture_size.width > 256 || texture_size.height > 256, image, "Cursor image too big. Max supported size is 256x256.");
|
||||
|
||||
ERR_FAIL_COND_V(image.is_null(), image);
|
||||
if (image->is_compressed()) {
|
||||
image = image->duplicate(true);
|
||||
Error err = image->decompress();
|
||||
ERR_FAIL_COND_V_MSG(err != OK, Ref<Image>(), "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
void DisplayServer::register_create_function(const char *p_name, CreateFunction p_function, GetRenderingDriversFunction p_get_drivers) {
|
||||
ERR_FAIL_COND(server_create_count == MAX_SERVERS);
|
||||
// Headless display server is always last
|
||||
|
||||
Reference in New Issue
Block a user