canvas_modulate.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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::_notification(int p_what) {
  32. switch (p_what) {
  33. case NOTIFICATION_ENTER_CANVAS: {
  34. if (is_visible_in_tree()) {
  35. RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  36. add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  37. }
  38. } break;
  39. case NOTIFICATION_EXIT_CANVAS: {
  40. if (is_visible_in_tree()) {
  41. RS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
  42. remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  43. }
  44. } break;
  45. case NOTIFICATION_VISIBILITY_CHANGED: {
  46. if (is_visible_in_tree()) {
  47. RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  48. add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  49. } else {
  50. RS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
  51. remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  52. }
  53. update_configuration_warnings();
  54. } break;
  55. }
  56. }
  57. void CanvasModulate::_bind_methods() {
  58. ClassDB::bind_method(D_METHOD("set_color", "color"), &CanvasModulate::set_color);
  59. ClassDB::bind_method(D_METHOD("get_color"), &CanvasModulate::get_color);
  60. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  61. }
  62. void CanvasModulate::set_color(const Color &p_color) {
  63. color = p_color;
  64. if (is_visible_in_tree()) {
  65. RS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  66. }
  67. }
  68. Color CanvasModulate::get_color() const {
  69. return color;
  70. }
  71. PackedStringArray CanvasModulate::get_configuration_warnings() const {
  72. PackedStringArray warnings = Node::get_configuration_warnings();
  73. if (is_visible_in_tree() && is_inside_tree()) {
  74. List<Node *> nodes;
  75. get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);
  76. if (nodes.size() > 1) {
  77. 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."));
  78. }
  79. }
  80. return warnings;
  81. }
  82. CanvasModulate::CanvasModulate() {
  83. }
  84. CanvasModulate::~CanvasModulate() {
  85. }