| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  texture_editor_plugin.cpp                                            */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-01 14:40:08 +01:00
										 |  |  | /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | #include "texture_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 14:21:25 +01:00
										 |  |  | #include "editor/editor_settings.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "io/resource_loader.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-30 22:53:40 +02:00
										 |  |  | #include "project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 12:38:03 -03:00
										 |  |  | void TextureEditor::_gui_input(Ref<InputEvent> p_event) { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextureEditor::_notification(int p_what) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-30 16:19:07 +02:00
										 |  |  | 	if (p_what == NOTIFICATION_PHYSICS_PROCESS) { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_READY) { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//get_scene()->connect("node_removed",this,"_node_removed");
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_DRAW) { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons"); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		Size2 size = get_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		draw_texture_rect(checkerboard, Rect2(Point2(), size), true); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int tex_width = texture->get_width() * size.height / texture->get_height(); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -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-05-23 17:10:26 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-30 16:27:10 +02:00
										 |  |  | 		// Prevent the texture from being unpreviewable after the rescale, so that we can still see something
 | 
					
						
							|  |  |  | 		if (tex_height <= 0) | 
					
						
							|  |  |  | 			tex_height = 1; | 
					
						
							|  |  |  | 		if (tex_width <= 0) | 
					
						
							|  |  |  | 			tex_width = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int ofs_x = (size.width - tex_width) / 2; | 
					
						
							|  |  |  | 		int ofs_y = (size.height - tex_height) / 2; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 		if (Object::cast_to<CurveTexture>(*texture)) { | 
					
						
							| 
									
										
										
										
											2017-04-30 16:27:10 +02:00
										 |  |  | 			// In the case of CurveTextures we know they are 1 in height, so fill the preview to see the gradient
 | 
					
						
							|  |  |  | 			ofs_y = 0; | 
					
						
							|  |  |  | 			tex_height = size.height; | 
					
						
							| 
									
										
										
										
											2018-02-03 12:10:44 +03:00
										 |  |  | 		} else if (Object::cast_to<GradientTexture>(*texture)) { | 
					
						
							|  |  |  | 			ofs_y = size.height / 4.0; | 
					
						
							|  |  |  | 			tex_height = size.height / 2.0; | 
					
						
							| 
									
										
										
										
											2017-04-30 16:27:10 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height)); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Ref<Font> font = get_font("font", "Label"); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		String format; | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 		if (Object::cast_to<ImageTexture>(*texture)) { | 
					
						
							|  |  |  | 			format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format()); | 
					
						
							|  |  |  | 		} else if (Object::cast_to<StreamTexture>(*texture)) { | 
					
						
							|  |  |  | 			format = Image::get_format_name(Object::cast_to<StreamTexture>(*texture)->get_format()); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			format = texture->get_class(); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Size2 rect = font->get_string_size(text); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2); | 
					
						
							|  |  |  | 		if (draw_from.x < 0) | 
					
						
							|  |  |  | 			draw_from.x = 0; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		draw_string(font, draw_from + Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width); | 
					
						
							|  |  |  | 		draw_string(font, draw_from - Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width); | 
					
						
							|  |  |  | 		draw_string(font, draw_from, text, Color(1, 1, 1, 1), size.width); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 19:30:24 -04:00
										 |  |  | void TextureEditor::_changed_callback(Object *p_changed, const char *p_prop) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!is_visible()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void TextureEditor::edit(Ref<Texture> p_texture) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 19:30:24 -04:00
										 |  |  | 	if (!texture.is_null()) | 
					
						
							|  |  |  | 		texture->remove_change_receptor(this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	texture = p_texture; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-29 19:30:24 -04:00
										 |  |  | 	if (!texture.is_null()) { | 
					
						
							|  |  |  | 		texture->add_change_receptor(this); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		update(); | 
					
						
							| 
									
										
										
										
											2017-03-29 19:30:24 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		hide(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextureEditor::_bind_methods() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TextureEditor::TextureEditor() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	set_custom_minimum_size(Size2(1, 150)); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextureEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 	Texture *s = Object::cast_to<Texture>(p_object); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	if (!s) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	texture_editor->edit(Ref<Texture>(s)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TextureEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 23:03:46 -03:00
										 |  |  | 	return p_object->is_class("Texture"); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextureEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							|  |  |  | 		texture_editor->show(); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 		//texture_editor->set_process(true);
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		texture_editor->hide(); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 		//texture_editor->set_process(false);
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	editor = p_node; | 
					
						
							|  |  |  | 	texture_editor = memnew(TextureEditor); | 
					
						
							|  |  |  | 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, texture_editor); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	texture_editor->hide(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | TextureEditorPlugin::~TextureEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } |