Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify and streamline connecting to Resource changes #78993

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,28 @@ bool Resource::editor_can_reload_from_file() {
return true; //by default yes
}

void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) {
if (!is_connected(CoreStringNames::get_singleton()->changed, p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) {
connect(CoreStringNames::get_singleton()->changed, p_callable, p_flags);
}
}

void Resource::disconnect_changed(const Callable &p_callable) {
if (is_connected(CoreStringNames::get_singleton()->changed, p_callable)) {
disconnect(CoreStringNames::get_singleton()->changed, p_callable);
}
}

void Resource::reset_state() {
}

Error Resource::copy_from(const Ref<Resource> &p_resource) {
ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
if (get_class() != p_resource->get_class()) {
return ERR_INVALID_PARAMETER;
}

reset_state(); //may want to reset state
reset_state(); // May want to reset state.

List<PropertyInfo> pi;
p_resource->get_property_list(&pi);
Expand Down Expand Up @@ -322,23 +335,6 @@ RID Resource::get_rid() const {
return RID();
}

void Resource::register_owner(Object *p_owner) {
owners.insert(p_owner->get_instance_id());
}

void Resource::unregister_owner(Object *p_owner) {
owners.erase(p_owner->get_instance_id());
}

void Resource::notify_change_to_owners() {
for (const ObjectID &E : owners) {
Object *obj = ObjectDB::get_instance(E);
ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
//TODO store string
obj->call("resource_changed", Ref<Resource>(this));
}
}

#ifdef TOOLS_ENABLED

uint32_t Resource::hash_edited_version() const {
Expand Down Expand Up @@ -443,6 +439,7 @@ void Resource::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);

ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);

ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
Expand All @@ -469,9 +466,6 @@ Resource::~Resource() {
ResourceCache::resources.erase(path_cache);
ResourceCache::lock.unlock();
}
if (owners.size()) {
WARN_PRINT("Resource is still owned.");
}
}

HashMap<String, Resource *> ResourceCache::resources;
Expand Down
11 changes: 3 additions & 8 deletions core/io/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class Resource : public RefCounted {
virtual String get_base_extension() const { return "res"; }

private:
HashSet<ObjectID> owners;

friend class ResBase;
friend class ResourceCache;

Expand All @@ -76,10 +74,6 @@ class Resource : public RefCounted {
SelfList<Resource> remapped_list;

protected:
void emit_changed();

void notify_change_to_owners();

virtual void _resource_path_changed();
static void _bind_methods();

Expand All @@ -96,8 +90,9 @@ class Resource : public RefCounted {
virtual Error copy_from(const Ref<Resource> &p_resource);
virtual void reload_from_file();

void register_owner(Object *p_owner);
void unregister_owner(Object *p_owner);
void emit_changed();
void connect_changed(const Callable &p_callable, uint32_t p_flags = 0);
void disconnect_changed(const Callable &p_callable);

void set_name(const String &p_name);
String get_name() const;
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/CollisionShape3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
Sets the collision shape's shape to the addition of all its convexed [MeshInstance3D] siblings geometry.
</description>
</method>
<method name="resource_changed">
<method name="resource_changed" is_deprecated="true">
<return type="void" />
<param index="0" name="resource" type="Resource" />
<description>
If this method exists within a script it will be called whenever the shape resource has been modified.
[i]Obsoleted.[/i] Use [signal Resource.changed] instead.
</description>
</method>
</methods>
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/ShapeCast3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@
Removes a collision exception so the shape does report collisions with the specified [RID].
</description>
</method>
<method name="resource_changed">
<method name="resource_changed" is_deprecated="true">
<return type="void" />
<param index="0" name="resource" type="Resource" />
<description>
This method is used internally to update the debug gizmo in the editor. Any code placed in this function will be called whenever the [member shape] resource is modified.
[i]Obsoleted.[/i] Use [signal Resource.changed] instead.
</description>
</method>
<method name="set_collision_mask_value">
Expand Down
4 changes: 2 additions & 2 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3297,7 +3297,7 @@ void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim, bool p_re
track_edits[_get_track_selected()]->release_focus();
}
if (animation.is_valid()) {
animation->disconnect("changed", callable_mp(this, &AnimationTrackEditor::_animation_changed));
animation->disconnect_changed(callable_mp(this, &AnimationTrackEditor::_animation_changed));
_clear_selection();
}
animation = p_anim;
Expand All @@ -3308,7 +3308,7 @@ void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim, bool p_re
_update_tracks();

if (animation.is_valid()) {
animation->connect("changed", callable_mp(this, &AnimationTrackEditor::_animation_changed));
animation->connect_changed(callable_mp(this, &AnimationTrackEditor::_animation_changed));

hscroll->show();
edit->set_disabled(read_only);
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,7 @@ void EditorNode::add_editor_plugin(EditorPlugin *p_editor, bool p_config_changed
if (icon.is_valid()) {
tb->set_icon(icon);
// Make sure the control is updated if the icon is reimported.
icon->connect("changed", callable_mp((Control *)tb, &Control::update_minimum_size));
icon->connect_changed(callable_mp((Control *)tb, &Control::update_minimum_size));
} else if (singleton->gui_base->has_theme_icon(p_editor->get_name(), SNAME("EditorIcons"))) {
tb->set_icon(singleton->gui_base->get_theme_icon(p_editor->get_name(), SNAME("EditorIcons")));
}
Expand Down
4 changes: 2 additions & 2 deletions editor/import/audio_stream_import_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void AudioStreamImportSettings::_seek_to(real_t p_x) {

void AudioStreamImportSettings::edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream) {
if (!stream.is_null()) {
stream->disconnect("changed", callable_mp(this, &AudioStreamImportSettings::_audio_changed));
stream->disconnect_changed(callable_mp(this, &AudioStreamImportSettings::_audio_changed));
}

importer = p_importer;
Expand All @@ -408,7 +408,7 @@ void AudioStreamImportSettings::edit(const String &p_path, const String &p_impor
_duration_label->set_text(text);

if (!stream.is_null()) {
stream->connect("changed", callable_mp(this, &AudioStreamImportSettings::_audio_changed));
stream->connect_changed(callable_mp(this, &AudioStreamImportSettings::_audio_changed));
_preview->queue_redraw();
_indicator->queue_redraw();
color_rect->queue_redraw();
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/audio_stream_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include "audio_stream_editor_plugin.h"

#include "core/core_string_names.h"
#include "editor/audio_stream_preview.h"
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
Expand Down Expand Up @@ -195,15 +194,15 @@ void AudioStreamEditor::_seek_to(real_t p_x) {

void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) {
if (stream.is_valid()) {
stream->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &AudioStreamEditor::_stream_changed));
stream->disconnect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));
}

stream = p_stream;
if (stream.is_null()) {
hide();
return;
}
stream->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &AudioStreamEditor::_stream_changed));
stream->connect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));

_player->set_stream(stream);
_current = 0;
Expand Down
9 changes: 4 additions & 5 deletions editor/plugins/curve_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "curve_editor_plugin.h"

#include "canvas_item_editor_plugin.h"
#include "core/core_string_names.h"
#include "core/input/input.h"
#include "core/math/geometry_2d.h"
#include "core/os/keyboard.h"
Expand Down Expand Up @@ -62,15 +61,15 @@ void CurveEdit::set_curve(Ref<Curve> p_curve) {
}

if (curve.is_valid()) {
curve->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveEdit::_curve_changed));
curve->disconnect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
curve->disconnect_changed(callable_mp(this, &CurveEdit::_curve_changed));
curve->disconnect_changed(callable_mp(this, &CurveEdit::_curve_changed));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no .-.

Copy link
Contributor

@YuriSizov YuriSizov Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to a crude regex, these are the only two

(checked for removed lines with ^-(.*)connect\(("changed"|CoreStringNames|SNAME\("changed"), 270 total, 268 matching this regex)

}

curve = p_curve;

if (curve.is_valid()) {
curve->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveEdit::_curve_changed));
curve->connect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
curve->connect_changed(callable_mp(this, &CurveEdit::_curve_changed));
curve->connect_changed(callable_mp(this, &CurveEdit::_curve_changed));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too :)

}

// Note: if you edit a curve, then set another, and try to undo,
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/gradient_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
gradient = p_gradient;
connect("ramp_changed", callable_mp(this, &GradientEditor::_ramp_changed));
gradient->connect("changed", callable_mp(this, &GradientEditor::_gradient_changed));
gradient->connect_changed(callable_mp(this, &GradientEditor::_gradient_changed));
set_points(gradient->get_points());
set_interpolation_mode(gradient->get_interpolation_mode());
set_interpolation_color_space(gradient->get_interpolation_color_space());
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/gradient_texture_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void GradientTexture2DEdit::gui_input(const Ref<InputEvent> &p_event) {

void GradientTexture2DEdit::set_texture(Ref<GradientTexture2D> &p_texture) {
texture = p_texture;
texture->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
texture->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
}

void GradientTexture2DEdit::set_snap_enabled(bool p_snap_enabled) {
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/input_event_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void InputEventConfigContainer::set_event(const Ref<InputEvent> &p_event) {

input_event = p_event;
_event_changed();
input_event->connect("changed", callable_mp(this, &InputEventConfigContainer::_event_changed));
input_event->connect_changed(callable_mp(this, &InputEventConfigContainer::_event_changed));
}

InputEventConfigContainer::InputEventConfigContainer() {
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4929,7 +4929,7 @@ void Node3DEditorViewport::register_shortcut_action(const String &p_path, const
Ref<Shortcut> sc = ED_SHORTCUT(p_path, p_name, p_keycode, p_physical);
shortcut_changed_callback(sc, p_path);
// Connect to the change event on the shortcut so the input binding can be updated.
sc->connect("changed", callable_mp(this, &Node3DEditorViewport::shortcut_changed_callback).bind(sc, p_path));
sc->connect_changed(callable_mp(this, &Node3DEditorViewport::shortcut_changed_callback).bind(sc, p_path));
}

// Update the action in the InputMap to the provided shortcut events.
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/polygon_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include "polygon_3d_editor_plugin.h"

#include "core/core_string_names.h"
#include "core/input/input.h"
#include "core/io/file_access.h"
#include "core/math/geometry_2d.h"
Expand Down Expand Up @@ -497,7 +496,7 @@ void Polygon3DEditor::edit(Node *p_node) {
node_resource = node->call("_get_editable_3d_polygon_resource");

if (node_resource.is_valid()) {
node_resource->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Polygon3DEditor::_polygon_draw));
node_resource->connect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
}
//Enable the pencil tool if the polygon is empty
if (_get_polygon().is_empty()) {
Expand All @@ -518,7 +517,7 @@ void Polygon3DEditor::edit(Node *p_node) {
} else {
node = nullptr;
if (node_resource.is_valid()) {
node_resource->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Polygon3DEditor::_polygon_draw));
node_resource->disconnect_changed(callable_mp(this, &Polygon3DEditor::_polygon_draw));
}
node_resource.unref();

Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/shader_file_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void ShaderFileEditor::_bind_methods() {
void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {
if (p_shader.is_null()) {
if (shader_file.is_valid()) {
shader_file->disconnect("changed", callable_mp(this, &ShaderFileEditor::_shader_changed));
shader_file->disconnect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
}
return;
}
Expand All @@ -234,7 +234,7 @@ void ShaderFileEditor::edit(const Ref<RDShaderFile> &p_shader) {
shader_file = p_shader;

if (shader_file.is_valid()) {
shader_file->connect("changed", callable_mp(this, &ShaderFileEditor::_shader_changed));
shader_file->connect_changed(callable_mp(this, &ShaderFileEditor::_shader_changed));
}

_update_options();
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/style_box_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ void StyleBoxPreview::_grid_preview_toggled(bool p_active) {

void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {
if (stylebox.is_valid()) {
stylebox->disconnect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
stylebox->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
}
stylebox = p_stylebox;
if (stylebox.is_valid()) {
stylebox->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
stylebox->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
}
Ref<StyleBoxTexture> sbt = stylebox;
grid_preview->set_visible(sbt.is_valid());
Expand Down
8 changes: 4 additions & 4 deletions editor/plugins/text_shader_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, const Stri
return;
}
if (shader.is_valid()) {
shader->disconnect(SNAME("changed"), callable_mp(this, &ShaderTextEditor::_shader_changed));
shader->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
}
shader = p_shader;
shader_inc = Ref<ShaderInclude>();

set_edited_code(p_code);

if (shader.is_valid()) {
shader->connect(SNAME("changed"), callable_mp(this, &ShaderTextEditor::_shader_changed));
shader->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
}
}

Expand All @@ -152,15 +152,15 @@ void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_sha
return;
}
if (shader_inc.is_valid()) {
shader_inc->disconnect(SNAME("changed"), callable_mp(this, &ShaderTextEditor::_shader_changed));
shader_inc->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
}
shader_inc = p_shader_inc;
shader = Ref<Shader>();

set_edited_code(p_code);

if (shader_inc.is_valid()) {
shader_inc->connect(SNAME("changed"), callable_mp(this, &ShaderTextEditor::_shader_changed));
shader_inc->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
}
}

Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/texture_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void Texture3DEditor::_texture_rect_update_area() {

void Texture3DEditor::edit(Ref<Texture3D> p_texture) {
if (!texture.is_null()) {
texture->disconnect("changed", callable_mp(this, &Texture3DEditor::_texture_changed));
texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
}

texture = p_texture;
Expand All @@ -125,7 +125,7 @@ void Texture3DEditor::edit(Ref<Texture3D> p_texture) {
_make_shaders();
}

texture->connect("changed", callable_mp(this, &Texture3DEditor::_texture_changed));
texture->connect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
queue_redraw();
texture_rect->set_material(material);
setting = true;
Expand Down Expand Up @@ -176,7 +176,7 @@ Texture3DEditor::Texture3DEditor() {

Texture3DEditor::~Texture3DEditor() {
if (!texture.is_null()) {
texture->disconnect("changed", callable_mp(this, &Texture3DEditor::_texture_changed));
texture->disconnect_changed(callable_mp(this, &Texture3DEditor::_texture_changed));
}
}

Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/texture_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
metadata_label = memnew(Label);

_update_metadata_label_text();
p_texture->connect("changed", callable_mp(this, &TexturePreview::_update_metadata_label_text));
p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text));

// 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"));
Expand Down
Loading