1 /*************************************************************************/
2 /*  texture_editor_plugin.cpp                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "texture_editor_plugin.h"
32 
33 #include "core/io/resource_loader.h"
34 #include "core/project_settings.h"
35 #include "editor/editor_settings.h"
36 
_gui_input(Ref<InputEvent> p_event)37 void TextureEditor::_gui_input(Ref<InputEvent> p_event) {
38 }
39 
_notification(int p_what)40 void TextureEditor::_notification(int p_what) {
41 
42 	if (p_what == NOTIFICATION_READY) {
43 
44 		//get_scene()->connect("node_removed",this,"_node_removed");
45 	}
46 
47 	if (p_what == NOTIFICATION_DRAW) {
48 
49 		Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
50 		Size2 size = get_size();
51 
52 		draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
53 
54 		int tex_width = texture->get_width() * size.height / texture->get_height();
55 		int tex_height = size.height;
56 
57 		if (tex_width > size.width) {
58 			tex_width = size.width;
59 			tex_height = texture->get_height() * tex_width / texture->get_width();
60 		}
61 
62 		// Prevent the texture from being unpreviewable after the rescale, so that we can still see something
63 		if (tex_height <= 0)
64 			tex_height = 1;
65 		if (tex_width <= 0)
66 			tex_width = 1;
67 
68 		int ofs_x = (size.width - tex_width) / 2;
69 		int ofs_y = (size.height - tex_height) / 2;
70 
71 		if (Object::cast_to<CurveTexture>(*texture)) {
72 			// In the case of CurveTextures we know they are 1 in height, so fill the preview to see the gradient
73 			ofs_y = 0;
74 			tex_height = size.height;
75 		} else if (Object::cast_to<GradientTexture>(*texture)) {
76 			ofs_y = size.height / 4.0;
77 			tex_height = size.height / 2.0;
78 		}
79 
80 		draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
81 
82 		Ref<Font> font = get_font("font", "Label");
83 
84 		String format;
85 		if (Object::cast_to<ImageTexture>(*texture)) {
86 			format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
87 		} else if (Object::cast_to<StreamTexture>(*texture)) {
88 			format = Image::get_format_name(Object::cast_to<StreamTexture>(*texture)->get_format());
89 		} else {
90 			format = texture->get_class();
91 		}
92 		String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
93 
94 		Size2 rect = font->get_string_size(text);
95 
96 		Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2);
97 		if (draw_from.x < 0)
98 			draw_from.x = 0;
99 
100 		draw_string(font, draw_from + Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
101 		draw_string(font, draw_from - Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
102 		draw_string(font, draw_from, text, Color(1, 1, 1, 1), size.width);
103 	}
104 }
105 
_changed_callback(Object * p_changed,const char * p_prop)106 void TextureEditor::_changed_callback(Object *p_changed, const char *p_prop) {
107 
108 	if (!is_visible())
109 		return;
110 	update();
111 }
112 
edit(Ref<Texture> p_texture)113 void TextureEditor::edit(Ref<Texture> p_texture) {
114 
115 	if (!texture.is_null())
116 		texture->remove_change_receptor(this);
117 
118 	texture = p_texture;
119 
120 	if (!texture.is_null()) {
121 		texture->add_change_receptor(this);
122 		update();
123 	} else {
124 		hide();
125 	}
126 }
127 
_bind_methods()128 void TextureEditor::_bind_methods() {
129 
130 	ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input);
131 }
132 
TextureEditor()133 TextureEditor::TextureEditor() {
134 
135 	set_custom_minimum_size(Size2(1, 150));
136 }
137 
~TextureEditor()138 TextureEditor::~TextureEditor() {
139 	if (!texture.is_null()) {
140 		texture->remove_change_receptor(this);
141 	}
142 }
143 //
can_handle(Object * p_object)144 bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
145 
146 	return Object::cast_to<ImageTexture>(p_object) != NULL || Object::cast_to<AtlasTexture>(p_object) != NULL || Object::cast_to<StreamTexture>(p_object) != NULL || Object::cast_to<LargeTexture>(p_object) != NULL || Object::cast_to<AnimatedTexture>(p_object) != NULL;
147 }
148 
parse_begin(Object * p_object)149 void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
150 
151 	Texture *texture = Object::cast_to<Texture>(p_object);
152 	if (!texture) {
153 		return;
154 	}
155 	Ref<Texture> m(texture);
156 
157 	TextureEditor *editor = memnew(TextureEditor);
158 	editor->edit(m);
159 	add_custom_control(editor);
160 }
161 
TextureEditorPlugin(EditorNode * p_node)162 TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
163 
164 	Ref<EditorInspectorPluginTexture> plugin;
165 	plugin.instance();
166 	add_inspector_plugin(plugin);
167 }
168