Skip to content

Commit

Permalink
Merge pull request #79747 from kleonc/canvas_modulate_fix_updating_logic
Browse files Browse the repository at this point in the history
Fix `CanvasModulate` logic for modulating the canvas
  • Loading branch information
akien-mga committed Aug 16, 2023
2 parents 20127ab + f12164d commit 132b97c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
67 changes: 51 additions & 16 deletions scene/2d/canvas_modulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,67 @@

#include "canvas_modulate.h"

void CanvasModulate::_on_in_canvas_visibility_changed(bool p_new_visibility) {
RID canvas = get_canvas();
StringName group_name = "_canvas_modulate_" + itos(canvas.get_id());

ERR_FAIL_COND_MSG(p_new_visibility == is_in_group(group_name), vformat("CanvasModulate becoming %s in the canvas already %s in the modulate group. Buggy logic, please report.", p_new_visibility ? "visible" : "invisible", p_new_visibility ? "was" : "was not"));

if (p_new_visibility) {
bool has_active_canvas_modulate = get_tree()->has_group(group_name); // Group would be removed if empty; otherwise one CanvasModulate within must be active.
add_to_group(group_name);
if (!has_active_canvas_modulate) {
is_active = true;
RS::get_singleton()->canvas_set_modulate(canvas, color);
}
} else {
remove_from_group(group_name);
if (is_active) {
is_active = false;
CanvasModulate *new_active = Object::cast_to<CanvasModulate>(get_tree()->get_first_node_in_group(group_name));
if (new_active) {
new_active->is_active = true;
RS::get_singleton()->canvas_set_modulate(canvas, new_active->color);
} else {
RS::get_singleton()->canvas_set_modulate(canvas, Color(1, 1, 1, 1));
}
}
}

update_configuration_warnings();
}

void CanvasModulate::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_CANVAS: {
if (is_visible_in_tree()) {
RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
is_in_canvas = true;
bool visible_in_tree = is_visible_in_tree();
if (visible_in_tree) {
_on_in_canvas_visibility_changed(true);
}
was_visible_in_tree = visible_in_tree;
} break;

case NOTIFICATION_EXIT_CANVAS: {
if (is_visible_in_tree()) {
RS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
is_in_canvas = false;
if (was_visible_in_tree) {
_on_in_canvas_visibility_changed(false);
}
} break;

case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_visible_in_tree()) {
RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
} else {
RS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
if (!is_in_canvas) {
return;
}

update_configuration_warnings();
bool visible_in_tree = is_visible_in_tree();
if (visible_in_tree == was_visible_in_tree) {
return;
}

_on_in_canvas_visibility_changed(visible_in_tree);

was_visible_in_tree = visible_in_tree;
} break;
}
}
Expand All @@ -69,7 +104,7 @@ void CanvasModulate::_bind_methods() {

void CanvasModulate::set_color(const Color &p_color) {
color = p_color;
if (is_visible_in_tree()) {
if (is_active) {
RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
}
}
Expand All @@ -81,12 +116,12 @@ Color CanvasModulate::get_color() const {
PackedStringArray CanvasModulate::get_configuration_warnings() const {
PackedStringArray warnings = Node::get_configuration_warnings();

if (is_visible_in_tree() && is_inside_tree()) {
if (is_in_canvas && is_visible_in_tree()) {
List<Node *> nodes;
get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);

if (nodes.size() > 1) {
warnings.push_back(RTR("Only one visible CanvasModulate is allowed per scene (or set of instantiated scenes). The first created one will work, while the rest will be ignored."));
warnings.push_back(RTR("Only one visible CanvasModulate is allowed per canvas.\nWhen there are more than one, only one of them will be active. Which one is undefined."));
}
}

Expand Down
8 changes: 8 additions & 0 deletions scene/2d/canvas_modulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class CanvasModulate : public Node2D {

Color color = Color(1, 1, 1, 1);

// CanvasModulate is in canvas-specific modulate group when both in canvas and visible in tree.
// Exactly one CanvasModulate in each such non-empty group is active.
bool is_in_canvas = false;
bool was_visible_in_tree = false; // Relevant only when in canvas.
bool is_active = false;

void _on_in_canvas_visibility_changed(bool p_new_visibility);

protected:
void _notification(int p_what);
static void _bind_methods();
Expand Down

0 comments on commit 132b97c

Please sign in to comment.