Add viewport preview plugin

Also includes a minor refactor of TextureEditorPlugin.
This commit is contained in:
Lightning_A 2021-03-21 15:33:17 -06:00
parent fc00a83901
commit b09f681768
5 changed files with 156 additions and 118 deletions

View file

@ -30,132 +30,69 @@
#include "texture_editor_plugin.h"
#include "core/config/project_settings.h"
#include "core/io/resource_loader.h"
#include "editor/editor_settings.h"
#include "editor/editor_scale.h"
void TextureEditor::_gui_input(Ref<InputEvent> p_event) {
TextureRect *TexturePreview::get_texture_display() {
return texture_display;
}
void TextureEditor::_notification(int p_what) {
if (p_what == NOTIFICATION_READY) {
//get_scene()->connect("node_removed",this,"_node_removed");
}
TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
if (p_what == NOTIFICATION_DRAW) {
Ref<Texture2D> checkerboard = get_theme_icon("Checkerboard", "EditorIcons");
Size2 size = get_size();
TextureRect *checkerboard = memnew(TextureRect);
checkerboard->set_texture(theme->get_icon("Checkerboard", "EditorIcons"));
checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
add_child(checkerboard);
draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
texture_display = memnew(TextureRect);
texture_display->set_texture(p_texture);
texture_display->set_anchors_preset(TextureRect::PRESET_WIDE);
texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
texture_display->set_expand(true);
add_child(texture_display);
int tex_width = texture->get_width() * size.height / texture->get_height();
int tex_height = size.height;
if (tex_width > size.width) {
tex_width = size.width;
tex_height = texture->get_height() * tex_width / texture->get_width();
}
// 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;
}
int ofs_x = (size.width - tex_width) / 2;
int ofs_y = (size.height - tex_height) / 2;
if (Object::cast_to<CurveTexture>(*texture)) {
// 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;
} else if (Object::cast_to<GradientTexture>(*texture)) {
ofs_y = size.height / 4.0;
tex_height = size.height / 2.0;
}
draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
Ref<Font> font = get_theme_font("font", "Label");
int font_size = get_theme_font_size("font_size", "Label");
if (p_show_metadata) {
Label *metadata_label = memnew(Label);
String format;
if (Object::cast_to<ImageTexture>(*texture)) {
format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
} else if (Object::cast_to<StreamTexture2D>(*texture)) {
format = Image::get_format_name(Object::cast_to<StreamTexture2D>(*texture)->get_format());
if (Object::cast_to<ImageTexture>(*p_texture)) {
format = Image::get_format_name(Object::cast_to<ImageTexture>(*p_texture)->get_format());
} else if (Object::cast_to<StreamTexture2D>(*p_texture)) {
format = Image::get_format_name(Object::cast_to<StreamTexture2D>(*p_texture)->get_format());
} else {
format = texture->get_class();
}
String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
Size2 rect = font->get_string_size(text, font_size);
Vector2 draw_from = size - rect + Size2(-2, font->get_ascent(font_size) - 2);
if (draw_from.x < 0) {
draw_from.x = 0;
format = p_texture->get_class();
}
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));
metadata_label->set_text(itos(p_texture->get_width()) + "x" + itos(p_texture->get_height()) + " " + format);
metadata_label->add_theme_font_size_override("font_size", 16 * EDSCALE);
metadata_label->add_theme_color_override("font_outline_color", Color::named("black"));
metadata_label->add_theme_constant_override("outline_size", 2 * EDSCALE);
Ref<Font> metadata_label_font = theme->get_font("expression", "EditorFonts");
metadata_label->add_theme_font_override("font", metadata_label_font);
// it's okay that these colors are static since the grid color is static too
metadata_label->add_theme_color_override("font_color", Color::named("white"));
metadata_label->add_theme_color_override("font_color_shadow", Color::named("black"));
metadata_label->add_theme_constant_override("shadow_as_outline", 1);
metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
add_child(metadata_label);
}
}
void TextureEditor::_texture_changed() {
if (!is_visible()) {
return;
}
update();
}
void TextureEditor::edit(Ref<Texture2D> p_texture) {
if (!texture.is_null()) {
texture->disconnect("changed", callable_mp(this, &TextureEditor::_texture_changed));
}
texture = p_texture;
if (!texture.is_null()) {
texture->connect("changed", callable_mp(this, &TextureEditor::_texture_changed));
update();
} else {
hide();
}
}
void TextureEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input);
}
TextureEditor::TextureEditor() {
set_texture_repeat(TextureRepeat::TEXTURE_REPEAT_ENABLED);
set_custom_minimum_size(Size2(1, 150));
}
TextureEditor::~TextureEditor() {
if (!texture.is_null()) {
texture->disconnect("changed", callable_mp(this, &TextureEditor::_texture_changed));
}
}
//
bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
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;
}
void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
Texture2D *texture = Object::cast_to<Texture2D>(p_object);
if (!texture) {
return;
}
Ref<Texture2D> m(texture);
Ref<Texture> texture(Object::cast_to<Texture>(p_object));
TextureEditor *editor = memnew(TextureEditor);
editor->edit(m);
add_custom_control(editor);
add_custom_control(memnew(TexturePreview(texture, true)));
}
TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {