| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-10-24 12:59:04 +07:00
										 |  |  | /*  nine_patch_rect.cpp                                                  */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02: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-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 12:59:04 +07:00
										 |  |  | #include "nine_patch_rect.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/rendering_server.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void NinePatchRect::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_DRAW) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (texture.is_null()) { | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 21:09:25 -04:00
										 |  |  | 		Rect2 rect = Rect2(Point2(), get_size()); | 
					
						
							|  |  |  | 		Rect2 src_rect = region_rect; | 
					
						
							| 
									
										
										
										
											2017-03-30 23:35:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-30 21:09:25 -04:00
										 |  |  | 		texture->get_rect_region(rect, src_rect, rect, src_rect); | 
					
						
							| 
									
										
										
										
											2017-03-30 23:35:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 		RID ci = get_canvas_item(); | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 		RS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | Size2 NinePatchRect::get_minimum_size() const { | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 	return Size2(margin[SIDE_LEFT] + margin[SIDE_RIGHT], margin[SIDE_TOP] + margin[SIDE_BOTTOM]); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | void NinePatchRect::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center); | 
					
						
							| 
									
										
										
										
											2017-08-31 15:11:10 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled); | 
					
						
							| 
									
										
										
										
											2017-06-21 23:37:25 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 22:16:41 -03:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("texture_changed")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); | 
					
						
							| 
									
										
										
										
											2018-11-08 11:30:02 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect"); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_GROUP("Patch Margin", "patch_margin_"); | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", SIDE_LEFT); | 
					
						
							|  |  |  | 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", SIDE_TOP); | 
					
						
							|  |  |  | 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", SIDE_RIGHT); | 
					
						
							|  |  |  | 	ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", SIDE_BOTTOM); | 
					
						
							| 
									
										
										
										
											2017-06-21 23:37:25 -03:00
										 |  |  | 	ADD_GROUP("Axis Stretch", "axis_stretch_"); | 
					
						
							| 
									
										
										
										
											2018-11-08 11:30:02 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode"); | 
					
						
							| 
									
										
										
										
											2017-06-21 23:37:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-20 17:45:01 +02:00
										 |  |  | 	BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | void NinePatchRect::set_texture(const Ref<Texture2D> &p_tex) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (texture == p_tex) { | 
					
						
							| 
									
										
										
										
											2016-06-19 22:16:41 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	texture = p_tex; | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -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
 | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	minimum_size_changed(); | 
					
						
							| 
									
										
										
										
											2016-06-19 22:16:41 -03:00
										 |  |  | 	emit_signal("texture_changed"); | 
					
						
							| 
									
										
										
										
											2017-03-29 19:30:24 -04:00
										 |  |  | 	_change_notify("texture"); | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 15:43:37 -03:00
										 |  |  | Ref<Texture2D> NinePatchRect::get_texture() const { | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	return texture; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | void NinePatchRect::set_patch_margin(Side p_side, int p_size) { | 
					
						
							|  |  |  | 	ERR_FAIL_INDEX((int)p_side, 4); | 
					
						
							|  |  |  | 	margin[p_side] = p_size; | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	update(); | 
					
						
							|  |  |  | 	minimum_size_changed(); | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 	switch (p_side) { | 
					
						
							|  |  |  | 		case SIDE_LEFT: | 
					
						
							| 
									
										
										
										
											2017-04-03 18:34:44 +02:00
										 |  |  | 			_change_notify("patch_margin_left"); | 
					
						
							| 
									
										
										
										
											2016-06-04 04:04:44 +08:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 		case SIDE_TOP: | 
					
						
							| 
									
										
										
										
											2017-04-03 18:34:44 +02:00
										 |  |  | 			_change_notify("patch_margin_top"); | 
					
						
							| 
									
										
										
										
											2016-06-04 04:04:44 +08:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 		case SIDE_RIGHT: | 
					
						
							| 
									
										
										
										
											2017-04-03 18:34:44 +02:00
										 |  |  | 			_change_notify("patch_margin_right"); | 
					
						
							| 
									
										
										
										
											2016-06-04 04:04:44 +08:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 		case SIDE_BOTTOM: | 
					
						
							| 
									
										
										
										
											2017-04-03 18:34:44 +02:00
										 |  |  | 			_change_notify("patch_margin_bottom"); | 
					
						
							| 
									
										
										
										
											2016-06-04 04:04:44 +08:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | int NinePatchRect::get_patch_margin(Side p_side) const { | 
					
						
							|  |  |  | 	ERR_FAIL_INDEX_V((int)p_side, 4, 0); | 
					
						
							|  |  |  | 	return margin[p_side]; | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (region_rect == p_region_rect) { | 
					
						
							| 
									
										
										
										
											2016-06-04 00:54:53 +08:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-04 00:54:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	region_rect = p_region_rect; | 
					
						
							| 
									
										
										
										
											2016-06-04 00:54:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	item_rect_changed(); | 
					
						
							|  |  |  | 	_change_notify("region_rect"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | Rect2 NinePatchRect::get_region_rect() const { | 
					
						
							| 
									
										
										
										
											2016-06-04 00:54:53 +08:00
										 |  |  | 	return region_rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-31 15:11:10 +02:00
										 |  |  | void NinePatchRect::set_draw_center(bool p_enabled) { | 
					
						
							|  |  |  | 	draw_center = p_enabled; | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-31 15:11:10 +02:00
										 |  |  | bool NinePatchRect::is_draw_center_enabled() const { | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | 	return draw_center; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 23:37:25 -03:00
										 |  |  | void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) { | 
					
						
							|  |  |  | 	axis_h = p_mode; | 
					
						
							|  |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const { | 
					
						
							|  |  |  | 	return axis_h; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) { | 
					
						
							|  |  |  | 	axis_v = p_mode; | 
					
						
							|  |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const { | 
					
						
							|  |  |  | 	return axis_v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 18:27:27 -03:00
										 |  |  | NinePatchRect::NinePatchRect() { | 
					
						
							| 
									
										
										
										
											2020-12-22 16:24:29 +00:00
										 |  |  | 	margin[SIDE_LEFT] = 0; | 
					
						
							|  |  |  | 	margin[SIDE_RIGHT] = 0; | 
					
						
							|  |  |  | 	margin[SIDE_BOTTOM] = 0; | 
					
						
							|  |  |  | 	margin[SIDE_TOP] = 0; | 
					
						
							| 
									
										
										
										
											2017-01-08 12:31:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 19:54:19 -03:00
										 |  |  | 	set_mouse_filter(MOUSE_FILTER_IGNORE); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	draw_center = true; | 
					
						
							| 
									
										
										
										
											2017-06-21 23:37:25 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	axis_h = AXIS_STRETCH_MODE_STRETCH; | 
					
						
							|  |  |  | 	axis_v = AXIS_STRETCH_MODE_STRETCH; | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | NinePatchRect::~NinePatchRect() { | 
					
						
							| 
									
										
										
										
											2015-08-24 09:39:16 -03:00
										 |  |  | } |