| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-14 10:52:54 +01:00
										 |  |  | /*  texture_rect.cpp                                                     */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                    http://www.godotengine.org                         */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-01 22:01:57 +01:00
										 |  |  | /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							| 
									
										
										
										
											2017-04-08 00:11:42 +02:00
										 |  |  | /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-14 10:52:54 +01:00
										 |  |  | #include "texture_rect.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "servers/visual_server.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void TextureRect::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_DRAW) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (texture.is_null()) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		switch (stretch_mode) { | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 			case STRETCH_SCALE_ON_EXPAND: { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Size2 s = expand ? get_size() : texture->get_size(); | 
					
						
							|  |  |  | 				draw_texture_rect(texture, Rect2(Point2(), s), false); | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 			} break; | 
					
						
							|  |  |  | 			case STRETCH_SCALE: { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				draw_texture_rect(texture, Rect2(Point2(), get_size()), false); | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 			} break; | 
					
						
							|  |  |  | 			case STRETCH_TILE: { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				draw_texture_rect(texture, Rect2(Point2(), get_size()), true); | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 			} break; | 
					
						
							|  |  |  | 			case STRETCH_KEEP: { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				draw_texture_rect(texture, Rect2(Point2(), texture->get_size()), false); | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			case STRETCH_KEEP_CENTERED: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Vector2 ofs = (get_size() - texture->get_size()) / 2; | 
					
						
							|  |  |  | 				draw_texture_rect(texture, Rect2(ofs, texture->get_size()), false); | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 			} break; | 
					
						
							| 
									
										
										
										
											2016-06-11 18:49:35 -03:00
										 |  |  | 			case STRETCH_KEEP_ASPECT_CENTERED: | 
					
						
							|  |  |  | 			case STRETCH_KEEP_ASPECT: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Size2 size = get_size(); | 
					
						
							|  |  |  | 				int tex_width = texture->get_width() * size.height / texture->get_height(); | 
					
						
							| 
									
										
										
										
											2016-06-11 18:49:35 -03:00
										 |  |  | 				int tex_height = size.height; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				if (tex_width > size.width) { | 
					
						
							|  |  |  | 					tex_width = size.width; | 
					
						
							|  |  |  | 					tex_height = texture->get_height() * tex_width / texture->get_width(); | 
					
						
							| 
									
										
										
										
											2016-06-11 18:49:35 -03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				int ofs_x = 0; | 
					
						
							|  |  |  | 				int ofs_y = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) { | 
					
						
							|  |  |  | 					ofs_x += (size.width - tex_width) / 2; | 
					
						
							|  |  |  | 					ofs_y += (size.height - tex_height) / 2; | 
					
						
							| 
									
										
										
										
											2016-06-11 18:49:35 -03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height)); | 
					
						
							| 
									
										
										
										
											2016-06-11 18:49:35 -03:00
										 |  |  | 			} break; | 
					
						
							| 
									
										
										
										
											2017-02-09 22:46:35 +08:00
										 |  |  | 			case STRETCH_KEEP_ASPECT_COVERED: { | 
					
						
							|  |  |  | 				Size2 size = get_size(); | 
					
						
							|  |  |  | 				Size2 tex_size = texture->get_size(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height); | 
					
						
							|  |  |  | 				float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height; | 
					
						
							| 
									
										
										
										
											2017-02-09 22:46:35 +08:00
										 |  |  | 				Size2 scaledTexSize = tex_size * scale; | 
					
						
							|  |  |  | 				Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				draw_texture_rect_region(texture, Rect2(Point2(), size), Rect2(ofs, size / scale)); | 
					
						
							| 
									
										
										
										
											2017-02-09 22:46:35 +08:00
										 |  |  | 			} break; | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | Size2 TextureRect::get_minimum_size() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!expand && !texture.is_null()) | 
					
						
							|  |  |  | 		return texture->get_size(); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return Size2(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void TextureRect::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02: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 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand); | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); | 
					
						
							|  |  |  | 	ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand"); | 
					
						
							|  |  |  | 	ADD_PROPERTYNO(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"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_SCALE_ON_EXPAND); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_SCALE); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_TILE); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_KEEP); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_KEEP_CENTERED); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_KEEP_ASPECT); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED); | 
					
						
							|  |  |  | 	BIND_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void TextureRect::set_texture(const Ref<Texture> &p_tex) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	texture = p_tex; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	update(); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	if (texture.is_valid()) | 
					
						
							|  |  |  | 		texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
 | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	minimum_size_changed(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | Ref<Texture> TextureRect::get_texture() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return texture; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void TextureRect::set_expand(bool p_expand) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	expand = p_expand; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	update(); | 
					
						
							|  |  |  | 	minimum_size_changed(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | bool TextureRect::has_expand() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return expand; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void TextureRect::set_stretch_mode(StretchMode p_mode) { | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	stretch_mode = p_mode; | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | TextureRect::StretchMode TextureRect::get_stretch_mode() const { | 
					
						
							| 
									
										
										
										
											2016-05-23 23:24:17 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return stretch_mode; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | TextureRect::TextureRect() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	expand = false; | 
					
						
							| 
									
										
										
										
											2017-01-21 19:00:25 -03:00
										 |  |  | 	set_mouse_filter(MOUSE_FILTER_PASS); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	stretch_mode = STRETCH_SCALE_ON_EXPAND; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | TextureRect::~TextureRect() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |