Skip to content

Commit

Permalink
Merge pull request #60739 from KoBeWi/add_static_methods_everywhere!!
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Jul 8, 2022
2 parents 697805a + d290042 commit d26442e
Show file tree
Hide file tree
Showing 42 changed files with 117 additions and 243 deletions.
16 changes: 16 additions & 0 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,21 @@ Error Image::load(const String &p_path) {
return ImageLoader::load_image(p_path, this);
}

Ref<Image> Image::load_from_file(const String &p_path) {
#ifdef DEBUG_ENABLED
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
WARN_PRINT("Loaded resource as image file, this will not work on export: '" + p_path + "'. Instead, import the image file as an Image resource and load it normally as a resource.");
}
#endif
Ref<Image> image;
image.instantiate();
Error err = ImageLoader::load_image(p_path, image);
if (err != OK) {
ERR_FAIL_V_MSG(Ref<Image>(), vformat("Failed to load image. Error %d", err));
}
return image;
}

Error Image::save_png(const String &p_path) const {
if (save_png_func == nullptr) {
return ERR_UNAVAILABLE;
Expand Down Expand Up @@ -3183,6 +3198,7 @@ void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty);

ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
ClassDB::bind_static_method("Image", D_METHOD("load_from_file", "path"), &Image::load_from_file);
ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer);
ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75));
Expand Down
1 change: 1 addition & 0 deletions core/io/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class Image : public Resource {
Vector<uint8_t> get_data() const;

Error load(const String &p_path);
static Ref<Image> load_from_file(const String &p_path);
Error save_png(const String &p_path) const;
Error save_jpg(const String &p_path, float p_quality = 0.75) const;
Vector<uint8_t> save_png_to_buffer() const;
Expand Down
3 changes: 1 addition & 2 deletions doc/classes/HTTPRequest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@
if error != OK:
push_error("Couldn't load the image.")

var texture = ImageTexture.new()
texture.create_from_image(image)
var texture = ImageTexture.create_from_image(image)

# Display the image in a TextureRect node.
var texture_rect = TextureRect.new()
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@
[b]Note:[/b] Godot's BMP module doesn't support 16-bit per pixel images. Only 1-bit, 4-bit, 8-bit, 24-bit, and 32-bit per pixel images are supported.
</description>
</method>
<method name="load_from_file" qualifiers="static">
<return type="Image" />
<argument index="0" name="path" type="String" />
<description>
Creates a new [Image] and loads data from the specified file.
</description>
</method>
<method name="load_jpg_from_buffer">
<return type="int" enum="Error" />
<argument index="0" name="buffer" type="PackedByteArray" />
Expand Down
12 changes: 5 additions & 7 deletions doc/classes/ImageTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
<description>
A [Texture2D] based on an [Image]. For an image to be displayed, an [ImageTexture] has to be created from it using the [method create_from_image] method:
[codeblock]
var texture = ImageTexture.new()
var image = Image.new()
image.load("res://icon.png")
texture.create_from_image(image)
var image = Image.load_from_file("res://icon.png")
var texture = ImageTexture.create_from_image(image)
$Sprite2D.texture = texture
[/codeblock]
This way, textures can be created at run-time by loading images both from within the editor and externally.
Expand All @@ -31,11 +29,11 @@
<link title="Importing images">$DOCS_URL/tutorials/assets_pipeline/importing_images.html</link>
</tutorials>
<methods>
<method name="create_from_image">
<return type="void" />
<method name="create_from_image" qualifiers="static">
<return type="ImageTexture" />
<argument index="0" name="image" type="Image" />
<description>
Initializes the texture by allocating and setting the data from an [Image].
Creates a new [ImageTexture] and initializes it by allocating and setting the data from an [Image].
</description>
</method>
<method name="get_format" qualifiers="const">
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void EditorProfiler::_update_plot() {
if (graph_texture.is_null()) {
graph_texture.instantiate();
}
graph_texture->create_from_image(img);
graph_texture->set_image(img);
}

graph_texture->update(img);
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_visual_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void EditorVisualProfiler::_update_plot() {
if (graph_texture.is_null()) {
graph_texture.instantiate();
}
graph_texture->create_from_image(img);
graph_texture->set_image(img);
}

graph_texture->update(img);
Expand Down
8 changes: 2 additions & 6 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4043,10 +4043,8 @@ Ref<ImageTexture> EditorNode::_load_custom_class_icon(const String &p_path) cons
Ref<Image> img = memnew(Image);
Error err = ImageLoader::load_image(p_path, img);
if (err == OK) {
Ref<ImageTexture> icon = memnew(ImageTexture);
img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS);
icon->create_from_image(img);
return icon;
return ImageTexture::create_from_image(img);
}
}
return nullptr;
Expand Down Expand Up @@ -5393,9 +5391,7 @@ Variant EditorNode::drag_resource(const Ref<Resource> &p_res, Control *p_from) {
Ref<Image> img = texture->get_image();
img = img->duplicate();
img->resize(48, 48); // meh
Ref<ImageTexture> resized_pic = Ref<ImageTexture>(memnew(ImageTexture));
resized_pic->create_from_image(img);
preview = resized_pic;
preview = ImageTexture::create_from_image(img);
}

drag_preview->set_texture(preview);
Expand Down
3 changes: 1 addition & 2 deletions editor/editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh
Main::iteration();
Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
ERR_CONTINUE(!img.is_valid() || img->is_empty());
Ref<ImageTexture> it(memnew(ImageTexture));
it->create_from_image(img);
Ref<ImageTexture> it = ImageTexture::create_from_image(img);

RS::get_singleton()->free(inst);

Expand Down
2 changes: 1 addition & 1 deletion editor/editor_resource_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
if (!texture.is_valid()) {
texture.instantiate();
}
texture->create_from_image(dropped_resource);
texture->set_image(dropped_resource);
dropped_resource = texture;
break;
}
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
small_image = small_image->duplicate();
small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
r_small_texture.instantiate();
r_small_texture->create_from_image(small_image);
r_small_texture->set_image(small_image);
}

break;
Expand Down Expand Up @@ -300,14 +300,14 @@ void EditorResourcePreview::_iterate() {
cache_valid = false;
} else {
texture.instantiate();
texture->create_from_image(img);
texture->set_image(img);

if (has_small_texture) {
if (small_img->load(cache_base + "_small.png") != OK) {
cache_valid = false;
} else {
small_texture.instantiate();
small_texture->create_from_image(small_img);
small_texture->set_image(small_img);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions editor/editor_run_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ void EditorRunNative::_notification(int p_what) {
im->clear_mipmaps();
if (!im->is_empty()) {
im->resize(16 * EDSCALE, 16 * EDSCALE);
Ref<ImageTexture> small_icon;
small_icon.instantiate();
small_icon->create_from_image(im);
Ref<ImageTexture> small_icon = ImageTexture::create_from_image(im);
MenuButton *mb = memnew(MenuButton);
mb->get_popup()->connect("id_pressed", callable_mp(this, &EditorRunNative::run_native), varray(i));
mb->connect("pressed", callable_mp(this, &EditorRunNative::run_native), varray(-1, i));
Expand Down
9 changes: 3 additions & 6 deletions editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ static Ref<Texture2D> flip_icon(Ref<Texture2D> p_texture, bool p_flip_y = false,
return p_texture;
}

Ref<ImageTexture> texture(memnew(ImageTexture));
Ref<Image> img = p_texture->get_image();
ERR_FAIL_NULL_V(img, Ref<Texture2D>());
img = img->duplicate();
Expand All @@ -109,14 +108,12 @@ static Ref<Texture2D> flip_icon(Ref<Texture2D> p_texture, bool p_flip_y = false,
img->flip_x();
}

texture->create_from_image(img);
return texture;
return ImageTexture::create_from_image(img);
}

#ifdef MODULE_SVG_ENABLED
// See also `generate_icon()` in `scene/resources/default_theme.cpp`.
static Ref<ImageTexture> editor_generate_icon(int p_index, bool p_convert_color, float p_scale = EDSCALE, float p_saturation = 1.0, Dictionary p_convert_colors = Dictionary()) {
Ref<ImageTexture> icon = memnew(ImageTexture);
Ref<Image> img = memnew(Image);

// Upsample icon generation only if the editor scale isn't an integer multiplier.
Expand All @@ -129,9 +126,9 @@ static Ref<ImageTexture> editor_generate_icon(int p_index, bool p_convert_color,
if (p_saturation != 1.0) {
img->adjust_bcs(1.0, 1.0, p_saturation);
}
icon->create_from_image(img); // in this case filter really helps

return icon;
// In this case filter really helps.
return ImageTexture::create_from_image(img);
}
#endif

Expand Down
12 changes: 2 additions & 10 deletions editor/import/resource_importer_texture_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ Error ResourceImporterTextureAtlas::import(const String &p_source_file, const St
//use an xpm because it's size independent, the editor images are vector and size dependent
//it's a simple hack
Ref<Image> broken = memnew(Image((const char **)atlas_import_failed_xpm));
Ref<ImageTexture> broken_texture;
broken_texture.instantiate();
broken_texture->create_from_image(broken);

String target_file = p_save_path + ".tex";

ResourceSaver::save(target_file, broken_texture);
ResourceSaver::save(p_save_path + ".tex", ImageTexture::create_from_image(broken));

return OK;
}
Expand Down Expand Up @@ -308,9 +302,7 @@ Error ResourceImporterTextureAtlas::import_group_file(const String &p_group_file
Ref<Texture2D> cache;
cache = ResourceCache::get_ref(p_group_file);
if (!cache.is_valid()) {
Ref<ImageTexture> res_cache;
res_cache.instantiate();
res_cache->create_from_image(new_atlas);
Ref<ImageTexture> res_cache = ImageTexture::create_from_image(new_atlas);
res_cache->set_path(p_group_file);
cache = res_cache;
}
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/animation_player_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void AnimationPlayerEditor::_notification(int p_what) {
autoplay_reset_img->blit_rect(autoplay_img, Rect2(Point2(), icon_size), Point2());
autoplay_reset_img->blit_rect(reset_img, Rect2(Point2(), icon_size), Point2(icon_size.x, 0));
autoplay_reset_icon.instantiate();
autoplay_reset_icon->create_from_image(autoplay_reset_img);
autoplay_reset_icon->set_image(autoplay_reset_img);
}
stop->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));

Expand Down
11 changes: 2 additions & 9 deletions editor/plugins/asset_library_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,9 @@ void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const

// Overlay and thumbnail need the same format for `blend_rect` to work.
thumbnail->convert(Image::FORMAT_RGBA8);

thumbnail->blend_rect(overlay, overlay->get_used_rect(), overlay_pos);
preview_images[i].button->set_icon(ImageTexture::create_from_image(thumbnail));

Ref<ImageTexture> tex;
tex.instantiate();
tex->create_from_image(thumbnail);

preview_images[i].button->set_icon(tex);
// Make it clearer that clicking it will open an external link
preview_images[i].button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
} else {
Expand Down Expand Up @@ -790,9 +785,7 @@ void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PackedB
} break;
}

Ref<ImageTexture> tex;
tex.instantiate();
tex->create_from_image(image);
Ref<ImageTexture> tex = ImageTexture::create_from_image(image);

obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
image_set = true;
Expand Down
6 changes: 1 addition & 5 deletions editor/plugins/bit_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
#include "editor/editor_scale.h"

void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {
Ref<ImageTexture> texture;
texture.instantiate();
texture->create_from_image(p_bitmap->convert_to_image());
texture_rect->set_texture(texture);

texture_rect->set_texture(ImageTexture::create_from_image(p_bitmap->convert_to_image()));
size_label->set_text(vformat(String::utf8("%s×%s"), p_bitmap->get_size().width, p_bitmap->get_size().height));
}

Expand Down
6 changes: 1 addition & 5 deletions editor/plugins/curve_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,5 @@ Ref<Texture2D> CurvePreviewGenerator::generate(const Ref<Resource> &p_from, cons

prev_y = y;
}

Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));

ptex->create_from_image(img_ref);
return ptex;
return ImageTexture::create_from_image(img_ref);
}
Loading

0 comments on commit d26442e

Please sign in to comment.