123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #include "texture_editor_plugin.h"
- #include "core/io/resource_loader.h"
- #include "core/project_settings.h"
- #include "editor/editor_settings.h"
- void TextureEditor::_gui_input(Ref<InputEvent> p_event) {
- }
- void TextureEditor::_notification(int p_what) {
- if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
- }
- if (p_what == NOTIFICATION_READY) {
-
- }
- if (p_what == NOTIFICATION_DRAW) {
- Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
- Size2 size = get_size();
- draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
- 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();
- }
-
- 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)) {
-
- 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_font("font", "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<StreamTexture>(*texture)) {
- format = Image::get_format_name(Object::cast_to<StreamTexture>(*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);
- Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2);
- if (draw_from.x < 0)
- draw_from.x = 0;
- 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);
- }
- }
- void TextureEditor::_changed_callback(Object *p_changed, const char *p_prop) {
- if (!is_visible())
- return;
- update();
- }
- void TextureEditor::edit(Ref<Texture> p_texture) {
- if (!texture.is_null())
- texture->remove_change_receptor(this);
- texture = p_texture;
- if (!texture.is_null()) {
- texture->add_change_receptor(this);
- update();
- } else {
- hide();
- }
- }
- void TextureEditor::_bind_methods() {
- ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input);
- }
- TextureEditor::TextureEditor() {
- set_custom_minimum_size(Size2(1, 150));
- }
- void TextureEditorPlugin::edit(Object *p_object) {
- Texture *s = Object::cast_to<Texture>(p_object);
- if (!s)
- return;
- texture_editor->edit(Ref<Texture>(s));
- }
- bool TextureEditorPlugin::handles(Object *p_object) const {
- return p_object->is_class("Texture");
- }
- void TextureEditorPlugin::make_visible(bool p_visible) {
- if (p_visible) {
- texture_editor->show();
-
- } else {
- texture_editor->hide();
-
- }
- }
- TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
- editor = p_node;
- texture_editor = memnew(TextureEditor);
- add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, texture_editor);
- texture_editor->hide();
- }
- TextureEditorPlugin::~TextureEditorPlugin() {
- }
|