From d25efcc17c5d5c241e787e0ece3c0c6939228fe4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 10 Jun 2022 01:01:32 +0200 Subject: [PATCH] Initial cleanup for the ImagePickerButton. --- .../image_picker_button.cpp | 133 ++++++++---------- .../image_picker_button.ctscn | 46 ------ .../image_picker_button/image_picker_button.h | 33 +++-- 3 files changed, 76 insertions(+), 136 deletions(-) delete mode 100644 modules/material_maker/editor/widgets/image_picker_button/image_picker_button.ctscn diff --git a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.cpp b/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.cpp index 16330899d..0ab70a73c 100644 --- a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.cpp +++ b/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.cpp @@ -1,98 +1,85 @@ #include "image_picker_button.h" - -Variant ImagePickerButton::get_Variant() { - return Variant; +String ImagePickerButton::get_image_path() { + return image_path; } -void ImagePickerButton::set_Variant(const Variant &val) { -Variant = val; +void ImagePickerButton::set_image_path(const String &path) { + do_set_image_path(path); + emit_signal("on_file_selected", path); } +void ImagePickerButton::do_set_image_path(const String &path) { + if (path == "") { + return; + } - - //tool; - Variant = ""; - signal on_file_selected(f); - - void ImagePickerButton::_ready() { - texture_normal = ImageTexture.new(); + image_path = path; + texture_normal.load(image_path); } +void ImagePickerButton::_on_ImagePicker_pressed() { + //var dialog = preload("res://addons/mat_maker_gd/windows/file_dialog/file_dialog.tscn").instance(); + add_child(dialog); + dialog->set_rect_min_size(Vector2(500, 500)); + dialog->set_access(FileDialog.ACCESS_FILESYSTEM); + dialog->set_mode(FileDialog.MODE_OPEN_FILE); + dialog->add_filter("*.bmp;BMP Image"); + dialog->add_filter("*.exr;EXR Image"); + dialog->add_filter("*.hdr;Radiance HDR Image"); + dialog->add_filter("*.jpg,*.jpeg;JPEG Image"); + dialog->add_filter("*.png;PNG Image"); + dialog->add_filter("*.svg;SVG Image"); + dialog->add_filter("*.tga;TGA Image"); + dialog->add_filter("*.webp;WebP Image"); + image_path = dialog->select_files(); - void ImagePickerButton::do_set_image_path(const Variant &path) { + while (files is GDScriptFunctionState) { + files = yield(files, "completed"); + } - if (path == null) { - return; + if (files.size() > 0) { + set_image_path(files[0]); + } } - image_path = path; - texture_normal.load(image_path); +void ImagePickerButton::on_drop_image_file(const String &file_name) { + set_image_path(file_name); } +ImagePickerButton::ImagePickerButton() { + set_custom_minimum_size(Vector2(64, 64)); + set_clip_contents(true); - void ImagePickerButton::set_image_path(const Variant &path) { - do_set_image_path(path); - emit_signal("on_file_selected", path); + Ref imagepicker_prop_texture_normal; + imagepicker_prop_texture_normal.instance(); + set_texture_normal(imagepicker_prop_texture_normal); + set_expand(true); + set_stretch_mode(STRETCH_KEEP_ASPECT_CENTERED); } - - void ImagePickerButton::_on_ImagePicker_pressed() { - //var dialog = preload("res://addons/mat_maker_gd/windows/file_dialog/file_dialog.tscn").instance(); - add_child(dialog); - dialog.rect_min_size = Vector2(500, 500); - dialog.access = FileDialog.ACCESS_FILESYSTEM; - dialog.mode = FileDialog.MODE_OPEN_FILE; - dialog.add_filter("*.bmp;BMP Image"); - dialog.add_filter("*.exr;EXR Image"); - dialog.add_filter("*.hdr;Radiance HDR Image"); - dialog.add_filter("*.jpg,*.jpeg;JPEG Image"); - dialog.add_filter("*.png;PNG Image"); - dialog.add_filter("*.svg;SVG Image"); - dialog.add_filter("*.tga;TGA Image"); - dialog.add_filter("*.webp;WebP Image"); - Variant = dialog.select_files(); - - while (files is GDScriptFunctionState) { - files = yield(files, "completed"); +ImagePickerButton::~ImagePickerButton() { } +void ImagePickerButton::_notification(int p_what) { + if (p_what == NOTIFICATION_POSTINITIALIZE) { + texture_normal = memnew(ImageTexture); - if (files.size() > 0) { - set_image_path(files[0]); + connect("pressed", this, "_on_ImagePicker_pressed"); + } } +void ImagePickerButton::_bind_methods() { + ADD_SIGNAL(MethodInfo("on_file_selected", PropertyInfo(Variant::STRING, "file"))); + + ClassDB::bind_method(D_METHOD("get_image_path"), &ImagePickerButton::get_image_path); + ClassDB::bind_method(D_METHOD("set_image_path", "path"), &ImagePickerButton::set_image_path); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "image_path"), "set_image_path", "get_image_path"); + + ClassDB::bind_method(D_METHOD("_ready"), &ImagePickerButton::_ready); + ClassDB::bind_method(D_METHOD("do_set_image_path", "path"), &ImagePickerButton::do_set_image_path); + ClassDB::bind_method(D_METHOD("set_image_path", "path"), &ImagePickerButton::set_image_path); + ClassDB::bind_method(D_METHOD("_on_ImagePicker_pressed"), &ImagePickerButton::_on_ImagePicker_pressed); + ClassDB::bind_method(D_METHOD("on_drop_image_file", "file_name"), &ImagePickerButton::on_drop_image_file); } - - - void ImagePickerButton::on_drop_image_file(const String &file_name) { - set_image_path(file_name); -} - -} - - ImagePickerButton::ImagePickerButton() { - = ""; - } - - ImagePickerButton::~ImagePickerButton() { - } - - - static void ImagePickerButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_Variant"), &ImagePickerButton::get_Variant); - ClassDB::bind_method(D_METHOD("set_Variant", "value"), &ImagePickerButton::set_Variant); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "Variant", PROPERTY_HINT_RESOURCE_TYPE, "Variant"), "set_Variant", "get_Variant"); - - - ClassDB::bind_method(D_METHOD("_ready"), &ImagePickerButton::_ready); - ClassDB::bind_method(D_METHOD("do_set_image_path", "path"), &ImagePickerButton::do_set_image_path); - ClassDB::bind_method(D_METHOD("set_image_path", "path"), &ImagePickerButton::set_image_path); - ClassDB::bind_method(D_METHOD("_on_ImagePicker_pressed"), &ImagePickerButton::_on_ImagePicker_pressed); - ClassDB::bind_method(D_METHOD("on_drop_image_file", "file_name"), &ImagePickerButton::on_drop_image_file); - - } - - - diff --git a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.ctscn b/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.ctscn deleted file mode 100644 index 461b5a3b9..000000000 --- a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.ctscn +++ /dev/null @@ -1,46 +0,0 @@ - -void construct() { - -//Script: res://addons/mat_maker_gd/widgets/image_picker_button/image_picker_button.gd -TextureButton *imagepicker = memnew(TextureButton); -imagepicker->set_name("ImagePicker"); - -imagepicker->set_name("ImagePicker"); -//imagepicker->set("name", ImagePicker)); - -imagepicker->set_filename("res://addons/mat_maker_gd/widgets/image_picker_button/image_picker_button.tscn"); -//imagepicker->set("filename", "res://addons/mat_maker_gd/widgets/image_picker_button/image_picker_button.tscn"); - -imagepicker->set_margin_right(64); -//imagepicker->set("margin_right", 64); - -imagepicker->set_margin_bottom(64); -//imagepicker->set("margin_bottom", 64); - -imagepicker->set_rect_size(Vector2(64, 64)); -//imagepicker->set("rect_size", Vector2(64, 64)); - -imagepicker->set_rect_min_size(Vector2(64, 64)); -//imagepicker->set("rect_min_size", Vector2(64, 64)); - -imagepicker->set_rect_clip_content(True); -//imagepicker->set("rect_clip_content", True); - -//imagepicker property texture_normal TYPE_OBJECT value: [ImageTexture:46513] -Ref imagepicker_prop_texture_normal; -imagepicker_prop_texture_normal.instance(); -imagepicker->set_texture_normal(imagepicker_prop_texture_normal); -//imagepicker->set("texture_normal", imagepicker_prop_texture_normal); - -imagepicker->set_expand(True); -//imagepicker->set("expand", True); - -imagepicker->set_stretch_mode(5); -//imagepicker->set("stretch_mode", 5); - -//imagepicker property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False} - - - - -} diff --git a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.h b/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.h index c1aca9980..e403b89bc 100644 --- a/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.h +++ b/modules/material_maker/editor/widgets/image_picker_button/image_picker_button.h @@ -1,31 +1,30 @@ #ifndef IMAGE_PICKER_BUTTON_H #define IMAGE_PICKER_BUTTON_H +#include "core/ustring.h" + +#include "scene/gui/texture_button.h" class ImagePickerButton : public TextureButton { - GDCLASS(ImagePickerButton, TextureButton); + GDCLASS(ImagePickerButton, TextureButton); - public: +public: + String get_image_path(); + void set_image_path(const String &path); - Variant get_Variant(); - void set_Variant(const Variant &val); + void do_set_image_path(const String &path); - void _ready(); - void do_set_image_path(const Variant &path); - void set_image_path(const Variant &path); - void _on_ImagePicker_pressed(); - void on_drop_image_file(const String &file_name); + void _on_ImagePicker_pressed(); + void on_drop_image_file(const String &file_name); - ImagePickerButton(); - ~ImagePickerButton(); + ImagePickerButton(); + ~ImagePickerButton(); - protected: - static void _bind_methods(); +protected: + void _notification(int p_what); + static void _bind_methods(); - //tool - Variant = ""; - signal on_file_selected(f); + String image_path; }; - #endif