canvas_modulate.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* canvas_modulate.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. void CanvasModulate::_notification(int p_what) {
  32. if (p_what == NOTIFICATION_ENTER_CANVAS) {
  33. if (is_visible_in_tree()) {
  34. VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  35. add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  36. }
  37. } else if (p_what == NOTIFICATION_EXIT_CANVAS) {
  38. if (is_visible_in_tree()) {
  39. VS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
  40. remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  41. }
  42. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  43. if (is_visible_in_tree()) {
  44. VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  45. add_to_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  46. } else {
  47. VS::get_singleton()->canvas_set_modulate(get_canvas(), Color(1, 1, 1, 1));
  48. remove_from_group("_canvas_modulate_" + itos(get_canvas().get_id()));
  49. }
  50. update_configuration_warning();
  51. }
  52. }
  53. void CanvasModulate::_bind_methods() {
  54. ClassDB::bind_method(D_METHOD("set_color", "color"), &CanvasModulate::set_color);
  55. ClassDB::bind_method(D_METHOD("get_color"), &CanvasModulate::get_color);
  56. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  57. }
  58. void CanvasModulate::set_color(const Color &p_color) {
  59. color = p_color;
  60. if (is_inside_tree()) {
  61. VS::get_singleton()->canvas_set_modulate(get_canvas(), color);
  62. }
  63. }
  64. Color CanvasModulate::get_color() const {
  65. return color;
  66. }
  67. String CanvasModulate::get_configuration_warning() const {
  68. if (!is_visible_in_tree() || !is_inside_tree())
  69. return String();
  70. List<Node *> nodes;
  71. get_tree()->get_nodes_in_group("_canvas_modulate_" + itos(get_canvas().get_id()), &nodes);
  72. if (nodes.size() > 1) {
  73. 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.");
  74. }
  75. return String();
  76. }
  77. CanvasModulate::CanvasModulate() {
  78. color = Color(1, 1, 1, 1);
  79. }
  80. CanvasModulate::~CanvasModulate() {
  81. }