2017-01-16 07:04:19 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* texture_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-01-16 07:04:19 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-01-16 07:04:19 +00: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-04 23:50:27 +00:00
|
|
|
|
2016-05-23 20:10:26 +00:00
|
|
|
#include "texture_editor_plugin.h"
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
2017-03-05 13:21:25 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void TextureEditor::_gui_input(Ref<InputEvent> p_event) {
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureEditor::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_READY) {
|
2016-05-23 20:10:26 +00:00
|
|
|
//get_scene()->connect("node_removed",this,"_node_removed");
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2020-03-12 12:37:40 +00:00
|
|
|
Ref<Texture2D> checkerboard = get_theme_icon("Checkerboard", "EditorIcons");
|
2016-05-23 20:10:26 +00:00
|
|
|
Size2 size = get_size();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int tex_width = texture->get_width() * size.height / texture->get_height();
|
2016-05-23 20:10:26 +00:00
|
|
|
int tex_height = size.height;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (tex_width > size.width) {
|
|
|
|
tex_width = size.width;
|
|
|
|
tex_height = texture->get_height() * tex_width / texture->get_width();
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
// Prevent the texture from being unpreviewable after the rescale, so that we can still see something
|
2020-05-14 14:41:43 +00:00
|
|
|
if (tex_height <= 0) {
|
2017-04-30 14:27:10 +00:00
|
|
|
tex_height = 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (tex_width <= 0) {
|
2017-04-30 14:27:10 +00:00
|
|
|
tex_width = 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-04-30 14:27:10 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs_x = (size.width - tex_width) / 2;
|
|
|
|
int ofs_y = (size.height - tex_height) / 2;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
if (Object::cast_to<CurveTexture>(*texture)) {
|
2017-04-30 14:27:10 +00: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 09:10:44 +00:00
|
|
|
} else if (Object::cast_to<GradientTexture>(*texture)) {
|
|
|
|
ofs_y = size.height / 4.0;
|
|
|
|
tex_height = size.height / 2.0;
|
2017-04-30 14:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2020-03-12 12:37:40 +00:00
|
|
|
Ref<Font> font = get_theme_font("font", "Label");
|
2020-09-03 11:22:16 +00:00
|
|
|
int font_size = get_theme_font_size("font_size", "Label");
|
2016-05-23 20:10:26 +00:00
|
|
|
|
|
|
|
String format;
|
2017-08-24 20:58:51 +00:00
|
|
|
if (Object::cast_to<ImageTexture>(*texture)) {
|
|
|
|
format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
|
2020-05-01 12:34:23 +00:00
|
|
|
} else if (Object::cast_to<StreamTexture2D>(*texture)) {
|
|
|
|
format = Image::get_format_name(Object::cast_to<StreamTexture2D>(*texture)->get_format());
|
2016-05-23 20:10:26 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
format = texture->get_class();
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
Size2 rect = font->get_string_size(text, font_size);
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
Vector2 draw_from = size - rect + Size2(-2, font->get_ascent(font_size) - 2);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (draw_from.x < 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_from.x = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
draw_string(font, draw_from + Vector2(2, 2), text, HALIGN_LEFT, size.width, font_size, Color(0, 0, 0, 0.5));
|
|
|
|
draw_string(font, draw_from - Vector2(2, 2), text, HALIGN_LEFT, size.width, font_size, Color(0, 0, 0, 0.5));
|
|
|
|
draw_string(font, draw_from, text, HALIGN_LEFT, size.width, font_size, Color(1, 1, 1, 1));
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 20:18:45 +00:00
|
|
|
void TextureEditor::_texture_changed() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_visible()) {
|
2017-03-29 23:30:24 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-29 23:30:24 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void TextureEditor::edit(Ref<Texture2D> p_texture) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!texture.is_null()) {
|
2021-02-10 20:18:45 +00:00
|
|
|
texture->disconnect("changed", callable_mp(this, &TextureEditor::_texture_changed));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-29 23:30:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
texture = p_texture;
|
2016-05-23 20:10:26 +00:00
|
|
|
|
2017-03-29 23:30:24 +00:00
|
|
|
if (!texture.is_null()) {
|
2021-02-10 20:18:45 +00:00
|
|
|
texture->connect("changed", callable_mp(this, &TextureEditor::_texture_changed));
|
2016-05-23 20:10:26 +00:00
|
|
|
update();
|
2017-03-29 23:30:24 +00:00
|
|
|
} else {
|
2016-05-23 20:10:26 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureEditor::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureEditor::TextureEditor() {
|
2019-11-06 11:16:32 +00:00
|
|
|
set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
|
2017-03-05 15:44:50 +00:00
|
|
|
set_custom_minimum_size(Size2(1, 150));
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:17:37 +00:00
|
|
|
TextureEditor::~TextureEditor() {
|
|
|
|
if (!texture.is_null()) {
|
2021-02-10 20:18:45 +00:00
|
|
|
texture->disconnect("changed", callable_mp(this, &TextureEditor::_texture_changed));
|
2019-03-08 17:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-03-04 18:52:39 +00:00
|
|
|
//
|
|
|
|
bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
|
2021-04-28 12:15:29 +00:00
|
|
|
return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<StreamTexture2D>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr;
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:52:39 +00:00
|
|
|
void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
|
2019-06-11 18:43:37 +00:00
|
|
|
Texture2D *texture = Object::cast_to<Texture2D>(p_object);
|
2019-03-04 18:52:39 +00:00
|
|
|
if (!texture) {
|
|
|
|
return;
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> m(texture);
|
2019-03-04 18:52:39 +00:00
|
|
|
|
|
|
|
TextureEditor *editor = memnew(TextureEditor);
|
|
|
|
editor->edit(m);
|
|
|
|
add_custom_control(editor);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
|
2019-03-04 18:52:39 +00:00
|
|
|
Ref<EditorInspectorPluginTexture> plugin;
|
|
|
|
plugin.instance();
|
|
|
|
add_inspector_plugin(plugin);
|
2016-05-23 20:10:26 +00:00
|
|
|
}
|