2016-06-18 14:46:12 +02:00
/**************************************************************************/
/* canvas_modulate.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2018-01-05 00:50:27 +01:00
2015-02-18 23:39:44 +01:00
# include "canvas_modulate.h"
2023-07-21 14:32:38 +02:00
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 ( ) ;
}
2015-02-18 23:39:44 +01:00
void CanvasModulate : : _notification ( int p_what ) {
2022-02-15 18:06:48 +01:00
switch ( p_what ) {
case NOTIFICATION_ENTER_CANVAS : {
2023-07-21 14:32:38 +02:00
is_in_canvas = true ;
bool visible_in_tree = is_visible_in_tree ( ) ;
if ( visible_in_tree ) {
_on_in_canvas_visibility_changed ( true ) ;
2022-02-15 18:06:48 +01:00
}
2023-07-21 14:32:38 +02:00
was_visible_in_tree = visible_in_tree ;
2022-02-15 18:06:48 +01:00
} break ;
2016-05-17 23:27:15 +02:00
2022-02-15 18:06:48 +01:00
case NOTIFICATION_EXIT_CANVAS : {
2023-07-21 14:32:38 +02:00
is_in_canvas = false ;
if ( was_visible_in_tree ) {
_on_in_canvas_visibility_changed ( false ) ;
2022-02-15 18:06:48 +01:00
}
} break ;
2016-05-17 23:27:15 +02:00
2022-02-15 18:06:48 +01:00
case NOTIFICATION_VISIBILITY_CHANGED : {
2023-07-21 14:32:38 +02:00
if ( ! is_in_canvas ) {
return ;
2022-02-15 18:06:48 +01:00
}
2023-07-21 14:32:38 +02:00
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 ;
2022-02-15 18:06:48 +01:00
} break ;
2015-02-18 23:39:44 +01:00
}
}
void CanvasModulate : : _bind_methods ( ) {
2017-02-13 12:47:24 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_color " , " color " ) , & CanvasModulate : : set_color ) ;
ClassDB : : bind_method ( D_METHOD ( " get_color " ) , & CanvasModulate : : get_color ) ;
2015-02-18 23:39:44 +01:00
2017-02-12 01:11:37 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : COLOR , " color " ) , " set_color " , " get_color " ) ;
2015-02-18 23:39:44 +01:00
}
void CanvasModulate : : set_color ( const Color & p_color ) {
color = p_color ;
2023-07-21 14:32:38 +02:00
if ( is_active ) {
2020-03-27 19:21:27 +01:00
RS : : get_singleton ( ) - > canvas_set_modulate ( get_canvas ( ) , color ) ;
2015-02-18 23:39:44 +01:00
}
}
2020-05-14 14:29:06 +02:00
2015-02-18 23:39:44 +01:00
Color CanvasModulate : : get_color ( ) const {
return color ;
}
2022-09-19 17:43:15 +02:00
PackedStringArray CanvasModulate : : get_configuration_warnings ( ) const {
PackedStringArray warnings = Node : : get_configuration_warnings ( ) ;
2020-05-14 22:59:27 +02:00
2023-07-21 14:32:38 +02:00
if ( is_in_canvas & & is_visible_in_tree ( ) ) {
2020-10-29 11:01:28 +01:00
List < Node * > nodes ;
get_tree ( ) - > get_nodes_in_group ( " _canvas_modulate_ " + itos ( get_canvas ( ) . get_id ( ) ) , & nodes ) ;
2016-05-17 23:27:15 +02:00
2020-10-29 11:01:28 +01:00
if ( nodes . size ( ) > 1 ) {
2023-07-21 14:32:38 +02:00
warnings . push_back ( RTR ( " Only one visible CanvasModulate is allowed per canvas. \n When there are more than one, only one of them will be active. Which one is undefined. " ) ) ;
2020-05-14 22:59:27 +02:00
}
2016-05-17 23:27:15 +02:00
}
2020-10-29 11:01:28 +01:00
return warnings ;
2016-05-17 23:27:15 +02:00
}
2015-02-18 23:39:44 +01:00
CanvasModulate : : CanvasModulate ( ) {
}
CanvasModulate : : ~ CanvasModulate ( ) {
}