Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/canvas_modulate.cpp
9903 views
1
/**************************************************************************/
2
/* canvas_modulate.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "canvas_modulate.h"
32
33
void CanvasModulate::_on_in_canvas_visibility_changed(bool p_new_visibility) {
34
RID canvas = get_canvas();
35
StringName group_name = "_canvas_modulate_" + itos(canvas.get_id());
36
37
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"));
38
39
if (p_new_visibility) {
40
bool has_active_canvas_modulate = get_tree()->has_group(group_name); // Group would be removed if empty; otherwise one CanvasModulate within must be active.
41
add_to_group(group_name);
42
if (!has_active_canvas_modulate) {
43
is_active = true;
44
RS::get_singleton()->canvas_set_modulate(canvas, color);
45
}
46
} else {
47
remove_from_group(group_name);
48
if (is_active) {
49
is_active = false;
50
CanvasModulate *new_active = Object::cast_to<CanvasModulate>(get_tree()->get_first_node_in_group(group_name));
51
if (new_active) {
52
new_active->is_active = true;
53
RS::get_singleton()->canvas_set_modulate(canvas, new_active->color);
54
} else {
55
RS::get_singleton()->canvas_set_modulate(canvas, Color(1, 1, 1, 1));
56
}
57
}
58
}
59
60
update_configuration_warnings();
61
}
62
63
void CanvasModulate::_notification(int p_what) {
64
switch (p_what) {
65
case NOTIFICATION_ENTER_CANVAS: {
66
is_in_canvas = true;
67
bool visible_in_tree = is_visible_in_tree();
68
if (visible_in_tree) {
69
_on_in_canvas_visibility_changed(true);
70
}
71
was_visible_in_tree = visible_in_tree;
72
} break;
73
74
case NOTIFICATION_EXIT_CANVAS: {
75
is_in_canvas = false;
76
if (was_visible_in_tree) {
77
_on_in_canvas_visibility_changed(false);
78
}
79
} break;
80
81
case NOTIFICATION_VISIBILITY_CHANGED: {
82
if (!is_in_canvas) {
83
return;
84
}
85
86
bool visible_in_tree = is_visible_in_tree();
87
if (visible_in_tree == was_visible_in_tree) {
88
return;
89
}
90
91
_on_in_canvas_visibility_changed(visible_in_tree);
92
93
was_visible_in_tree = visible_in_tree;
94
} break;
95
}
96
}
97
98
void CanvasModulate::_bind_methods() {
99
ClassDB::bind_method(D_METHOD("set_color", "color"), &CanvasModulate::set_color);
100
ClassDB::bind_method(D_METHOD("get_color"), &CanvasModulate::get_color);
101
102
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
103
}
104
105
void CanvasModulate::set_color(const Color &p_color) {
106
color = p_color;
107
if (is_active) {
108
RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
109
}
110
}
111
112
Color CanvasModulate::get_color() const {
113
return color;
114
}
115
116
PackedStringArray CanvasModulate::get_configuration_warnings() const {
117
PackedStringArray warnings = Node2D::get_configuration_warnings();
118
119
if (is_in_canvas && is_visible_in_tree()) {
120
List<Node *> nodes;
121
get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);
122
123
if (nodes.size() > 1) {
124
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."));
125
}
126
}
127
128
return warnings;
129
}
130
131
CanvasModulate::CanvasModulate() {
132
}
133
134
CanvasModulate::~CanvasModulate() {
135
}
136
137