canvas_modulate.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "canvas_modulate.h"
  31. void CanvasModulate::_on_in_canvas_visibility_changed(bool p_new_visibility) {
  32. RID canvas = get_canvas();
  33. StringName group_name = "_canvas_modulate_" + itos(canvas.get_id());
  34. 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"));
  35. if (p_new_visibility) {
  36. bool has_active_canvas_modulate = get_tree()->has_group(group_name); // Group would be removed if empty; otherwise one CanvasModulate within must be active.
  37. add_to_group(group_name);
  38. if (!has_active_canvas_modulate) {
  39. is_active = true;
  40. RS::get_singleton()->canvas_set_modulate(canvas, color);
  41. }
  42. } else {
  43. remove_from_group(group_name);
  44. if (is_active) {
  45. is_active = false;
  46. CanvasModulate *new_active = Object::cast_to<CanvasModulate>(get_tree()->get_first_node_in_group(group_name));
  47. if (new_active) {
  48. new_active->is_active = true;
  49. RS::get_singleton()->canvas_set_modulate(canvas, new_active->color);
  50. } else {
  51. RS::get_singleton()->canvas_set_modulate(canvas, Color(1, 1, 1, 1));
  52. }
  53. }
  54. }
  55. update_configuration_warnings();
  56. }
  57. void CanvasModulate::_notification(int p_what) {
  58. switch (p_what) {
  59. case NOTIFICATION_ENTER_CANVAS: {
  60. is_in_canvas = true;
  61. bool visible_in_tree = is_visible_in_tree();
  62. if (visible_in_tree) {
  63. _on_in_canvas_visibility_changed(true);
  64. }
  65. was_visible_in_tree = visible_in_tree;
  66. } break;
  67. case NOTIFICATION_EXIT_CANVAS: {
  68. is_in_canvas = false;
  69. if (was_visible_in_tree) {
  70. _on_in_canvas_visibility_changed(false);
  71. }
  72. } break;
  73. case NOTIFICATION_VISIBILITY_CHANGED: {
  74. if (!is_in_canvas) {
  75. return;
  76. }
  77. bool visible_in_tree = is_visible_in_tree();
  78. if (visible_in_tree == was_visible_in_tree) {
  79. return;
  80. }
  81. _on_in_canvas_visibility_changed(visible_in_tree);
  82. was_visible_in_tree = visible_in_tree;
  83. } break;
  84. }
  85. }
  86. void CanvasModulate::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("set_color", "color"), &CanvasModulate::set_color);
  88. ClassDB::bind_method(D_METHOD("get_color"), &CanvasModulate::get_color);
  89. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  90. }
  91. void CanvasModulate::set_color(const Color &p_color) {
  92. color = p_color;
  93. if (is_active) {
  94. RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  95. }
  96. }
  97. Color CanvasModulate::get_color() const {
  98. return color;
  99. }
  100. PackedStringArray CanvasModulate::get_configuration_warnings() const {
  101. PackedStringArray warnings = Node2D::get_configuration_warnings();
  102. if (is_in_canvas && is_visible_in_tree()) {
  103. List<Node *> nodes;
  104. get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);
  105. if (nodes.size() > 1) {
  106. 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."));
  107. }
  108. }
  109. return warnings;
  110. }
  111. CanvasModulate::CanvasModulate() {
  112. }
  113. CanvasModulate::~CanvasModulate() {
  114. }