1 /*************************************************************************/
2 /* canvas_modulate.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
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 #include "canvas_modulate.h"
31
_notification(int p_what)32 void CanvasModulate::_notification(int p_what) {
33
34 if (p_what == NOTIFICATION_ENTER_CANVAS) {
35
36 if (is_visible()) {
37 VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
38 add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
39 }
40
41 } else if (p_what == NOTIFICATION_EXIT_CANVAS) {
42
43 if (is_visible()) {
44 VS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
45 remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
46 }
47 } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
48
49 if (is_visible()) {
50 VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
51 add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
52 } else {
53 VS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
54 remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
55 }
56
57 update_configuration_warning();
58 }
59 }
60
_bind_methods()61 void CanvasModulate::_bind_methods() {
62
63 ObjectTypeDB::bind_method(_MD("set_color", "color"), &CanvasModulate::set_color);
64 ObjectTypeDB::bind_method(_MD("get_color"), &CanvasModulate::get_color);
65
66 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), _SCS("set_color"), _SCS("get_color"));
67 }
68
set_color(const Color & p_color)69 void CanvasModulate::set_color(const Color &p_color) {
70
71 color = p_color;
72 if (is_inside_tree()) {
73 VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
74 }
75 }
get_color() const76 Color CanvasModulate::get_color() const {
77
78 return color;
79 }
80
get_configuration_warning() const81 String CanvasModulate::get_configuration_warning() const {
82
83 if (!is_visible() || !is_inside_tree())
84 return String();
85
86 List<Node *> nodes;
87 get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);
88
89 if (nodes.size() > 1) {
90 return TTR("Only one visible CanvasModulate is allowed per scene (or set of instanced scenes). The first created one will work, while the rest will be ignored.");
91 }
92
93 return String();
94 }
95
CanvasModulate()96 CanvasModulate::CanvasModulate() {
97 color = Color(1, 1, 1, 1);
98 }
99
~CanvasModulate()100 CanvasModulate::~CanvasModulate() {
101 }
102