2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-14 09:52:54 +00:00
|
|
|
/* texture_rect.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-01-14 09:52:54 +00:00
|
|
|
#include "texture_rect.h"
|
2019-09-25 18:48:48 +00:00
|
|
|
#include "core/core_string_names.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "servers/visual_server.h"
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void TextureRect::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2020-01-02 00:56:12 +00:00
|
|
|
if (texture.is_null()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-01-02 00:56:12 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
Size2 size;
|
|
|
|
Point2 offset;
|
|
|
|
Rect2 region;
|
|
|
|
bool tile = false;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (stretch_mode) {
|
2016-05-24 02:24:17 +00:00
|
|
|
case STRETCH_SCALE_ON_EXPAND: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = expand ? get_size() : texture->get_size();
|
2016-05-24 02:24:17 +00:00
|
|
|
} break;
|
|
|
|
case STRETCH_SCALE: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = get_size();
|
2016-05-24 02:24:17 +00:00
|
|
|
} break;
|
|
|
|
case STRETCH_TILE: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = get_size();
|
|
|
|
tile = true;
|
2016-05-24 02:24:17 +00:00
|
|
|
} break;
|
|
|
|
case STRETCH_KEEP: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = texture->get_size();
|
2016-05-24 02:24:17 +00:00
|
|
|
} break;
|
|
|
|
case STRETCH_KEEP_CENTERED: {
|
2019-02-05 04:12:44 +00:00
|
|
|
offset = (get_size() - texture->get_size()) / 2;
|
|
|
|
size = texture->get_size();
|
2016-05-24 02:24:17 +00:00
|
|
|
} break;
|
2016-06-11 21:49:35 +00:00
|
|
|
case STRETCH_KEEP_ASPECT_CENTERED:
|
|
|
|
case STRETCH_KEEP_ASPECT: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = get_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
int tex_width = texture->get_width() * size.height / texture->get_height();
|
2016-06-11 21:49:35 +00:00
|
|
|
int tex_height = size.height;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (tex_width > size.width) {
|
|
|
|
tex_width = size.width;
|
|
|
|
tex_height = texture->get_height() * tex_width / texture->get_width();
|
2016-06-11 21:49:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
|
2019-02-05 04:12:44 +00:00
|
|
|
offset.x += (size.width - tex_width) / 2;
|
|
|
|
offset.y += (size.height - tex_height) / 2;
|
2016-06-11 21:49:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
size.width = tex_width;
|
|
|
|
size.height = tex_height;
|
2016-06-11 21:49:35 +00:00
|
|
|
} break;
|
2017-02-09 14:46:35 +00:00
|
|
|
case STRETCH_KEEP_ASPECT_COVERED: {
|
2019-02-05 04:12:44 +00:00
|
|
|
size = get_size();
|
|
|
|
|
2017-02-09 14:46:35 +00:00
|
|
|
Size2 tex_size = texture->get_size();
|
2020-01-02 00:56:12 +00:00
|
|
|
Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
|
|
|
|
float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
|
|
|
|
Size2 scaled_tex_size = tex_size * scale;
|
2019-02-05 04:12:44 +00:00
|
|
|
|
2020-01-02 00:56:12 +00:00
|
|
|
region.position = ((scaled_tex_size - size) / scale).abs() / 2.0f;
|
2019-02-05 04:12:44 +00:00
|
|
|
region.size = size / scale;
|
2017-02-09 14:46:35 +00:00
|
|
|
} break;
|
2016-05-24 02:24:17 +00:00
|
|
|
}
|
2019-02-05 04:12:44 +00:00
|
|
|
|
2020-04-05 20:43:02 +00:00
|
|
|
Ref<AtlasTexture> p_atlas = texture;
|
|
|
|
|
|
|
|
if (p_atlas.is_valid() && region.has_no_area()) {
|
|
|
|
Size2 scale_size(size.width / texture->get_width(), size.height / texture->get_height());
|
|
|
|
|
|
|
|
offset.width += hflip ? p_atlas->get_margin().get_position().width * scale_size.width * 2 : 0;
|
|
|
|
offset.height += vflip ? p_atlas->get_margin().get_position().height * scale_size.height * 2 : 0;
|
|
|
|
}
|
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
size.width *= hflip ? -1.0f : 1.0f;
|
|
|
|
size.height *= vflip ? -1.0f : 1.0f;
|
|
|
|
|
2019-04-30 10:33:02 +00:00
|
|
|
if (region.has_no_area()) {
|
2019-02-05 04:12:44 +00:00
|
|
|
draw_texture_rect(texture, Rect2(offset, size), tile);
|
|
|
|
} else {
|
|
|
|
draw_texture_rect_region(texture, Rect2(offset, size), region);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
Size2 TextureRect::get_minimum_size() const {
|
2020-01-02 00:56:12 +00:00
|
|
|
if (!expand && !texture.is_null()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return texture->get_size();
|
2020-01-02 00:56:12 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
return Size2();
|
2020-01-02 00:56:12 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-01-02 00:56:12 +00:00
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void TextureRect::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand);
|
2019-02-05 04:12:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
|
2019-09-25 18:48:48 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureRect::_texture_changed);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
|
2019-02-05 04:12:44 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(STRETCH_SCALE_ON_EXPAND);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_SCALE);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_TILE);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
|
|
|
|
BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 18:48:48 +00:00
|
|
|
void TextureRect::_texture_changed() {
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void TextureRect::set_texture(const Ref<Texture> &p_tex) {
|
2020-01-02 00:56:12 +00:00
|
|
|
if (p_tex == texture) {
|
2019-09-25 18:48:48 +00:00
|
|
|
return;
|
2020-01-02 00:56:12 +00:00
|
|
|
}
|
2019-09-25 18:48:48 +00:00
|
|
|
|
2020-01-02 00:56:12 +00:00
|
|
|
if (texture.is_valid()) {
|
2019-09-25 18:48:48 +00:00
|
|
|
texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
|
2020-01-02 00:56:12 +00:00
|
|
|
}
|
2019-09-25 18:48:48 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
texture = p_tex;
|
2019-09-25 18:48:48 +00:00
|
|
|
|
2020-01-02 00:56:12 +00:00
|
|
|
if (texture.is_valid()) {
|
2019-09-25 18:48:48 +00:00
|
|
|
texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
|
2020-01-02 00:56:12 +00:00
|
|
|
}
|
2019-09-25 18:48:48 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
Ref<Texture> TextureRect::get_texture() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void TextureRect::set_expand(bool p_expand) {
|
2017-03-05 15:44:50 +00:00
|
|
|
expand = p_expand;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2020-01-02 00:56:12 +00:00
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
bool TextureRect::has_expand() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return expand;
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void TextureRect::set_stretch_mode(StretchMode p_mode) {
|
2017-03-05 15:44:50 +00:00
|
|
|
stretch_mode = p_mode;
|
2016-05-24 02:24:17 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
TextureRect::StretchMode TextureRect::get_stretch_mode() const {
|
2016-05-24 02:24:17 +00:00
|
|
|
return stretch_mode;
|
|
|
|
}
|
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
void TextureRect::set_flip_h(bool p_flip) {
|
|
|
|
hflip = p_flip;
|
|
|
|
update();
|
|
|
|
}
|
2020-01-02 00:56:12 +00:00
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
bool TextureRect::is_flipped_h() const {
|
|
|
|
return hflip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureRect::set_flip_v(bool p_flip) {
|
|
|
|
vflip = p_flip;
|
|
|
|
update();
|
|
|
|
}
|
2020-01-02 00:56:12 +00:00
|
|
|
|
2019-02-05 04:12:44 +00:00
|
|
|
bool TextureRect::is_flipped_v() const {
|
|
|
|
return vflip;
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
TextureRect::TextureRect() {
|
2017-03-05 15:44:50 +00:00
|
|
|
expand = false;
|
2019-02-05 04:12:44 +00:00
|
|
|
hflip = false;
|
|
|
|
vflip = false;
|
2017-01-21 22:00:25 +00:00
|
|
|
set_mouse_filter(MOUSE_FILTER_PASS);
|
2017-03-05 15:44:50 +00:00
|
|
|
stretch_mode = STRETCH_SCALE_ON_EXPAND;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TextureRect::~TextureRect() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|