1 #include "texture_editor_plugin.h"
2 
3 #include "editor/editor_settings.h"
4 #include "globals.h"
5 #include "io/resource_loader.h"
6 
_input_event(InputEvent p_event)7 void TextureEditor::_input_event(InputEvent p_event) {
8 }
9 
_notification(int p_what)10 void TextureEditor::_notification(int p_what) {
11 
12 	if (p_what == NOTIFICATION_FIXED_PROCESS) {
13 	}
14 
15 	if (p_what == NOTIFICATION_READY) {
16 
17 		//get_scene()->connect("node_removed",this,"_node_removed");
18 	}
19 
20 	if (p_what == NOTIFICATION_DRAW) {
21 
22 		Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
23 		Size2 size = get_size();
24 
25 		draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
26 
27 		int tex_width = texture->get_width() * size.height / texture->get_height();
28 		int tex_height = size.height;
29 
30 		if (tex_width > size.width) {
31 			tex_width = size.width;
32 			tex_height = texture->get_height() * tex_width / texture->get_width();
33 		}
34 
35 		int ofs_x = (size.width - tex_width) / 2;
36 		int ofs_y = (size.height - tex_height) / 2;
37 
38 		draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
39 
40 		Ref<Font> font = get_font("font", "Label");
41 
42 		String format;
43 		if (texture->cast_to<ImageTexture>()) {
44 			format = Image::get_format_name(texture->cast_to<ImageTexture>()->get_format());
45 		} else {
46 			format = texture->get_type();
47 		}
48 		String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
49 
50 		Size2 rect = font->get_string_size(text);
51 
52 		Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2);
53 		if (draw_from.x < 0)
54 			draw_from.x = 0;
55 
56 		draw_string(font, draw_from + Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
57 		draw_string(font, draw_from - Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
58 		draw_string(font, draw_from, text, Color(1, 1, 1, 1), size.width);
59 	}
60 }
61 
edit(Ref<Texture> p_texture)62 void TextureEditor::edit(Ref<Texture> p_texture) {
63 
64 	texture = p_texture;
65 
66 	if (!texture.is_null())
67 		update();
68 	else {
69 
70 		hide();
71 	}
72 }
73 
_bind_methods()74 void TextureEditor::_bind_methods() {
75 
76 	ObjectTypeDB::bind_method(_MD("_input_event"), &TextureEditor::_input_event);
77 }
78 
TextureEditor()79 TextureEditor::TextureEditor() {
80 
81 	set_custom_minimum_size(Size2(1, 150));
82 }
83 
edit(Object * p_object)84 void TextureEditorPlugin::edit(Object *p_object) {
85 
86 	Texture *s = p_object->cast_to<Texture>();
87 	if (!s)
88 		return;
89 
90 	texture_editor->edit(Ref<Texture>(s));
91 }
92 
handles(Object * p_object) const93 bool TextureEditorPlugin::handles(Object *p_object) const {
94 
95 	return p_object->is_type("Texture");
96 }
97 
make_visible(bool p_visible)98 void TextureEditorPlugin::make_visible(bool p_visible) {
99 
100 	if (p_visible) {
101 		texture_editor->show();
102 		//		texture_editor->set_process(true);
103 	} else {
104 
105 		texture_editor->hide();
106 		//		texture_editor->set_process(false);
107 	}
108 }
109 
TextureEditorPlugin(EditorNode * p_node)110 TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
111 
112 	editor = p_node;
113 	texture_editor = memnew(TextureEditor);
114 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, texture_editor);
115 	texture_editor->hide();
116 }
117 
~TextureEditorPlugin()118 TextureEditorPlugin::~TextureEditorPlugin() {
119 }
120