visible_on_screen_notifier_2d.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**************************************************************************/
  2. /* visible_on_screen_notifier_2d.h */
  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. #ifndef VISIBLE_ON_SCREEN_NOTIFIER_2D_H
  31. #define VISIBLE_ON_SCREEN_NOTIFIER_2D_H
  32. #include "scene/2d/node_2d.h"
  33. class Viewport;
  34. class VisibleOnScreenNotifier2D : public Node2D {
  35. GDCLASS(VisibleOnScreenNotifier2D, Node2D);
  36. HashSet<Viewport *> viewports;
  37. Rect2 rect;
  38. private:
  39. bool on_screen = false;
  40. void _visibility_enter();
  41. void _visibility_exit();
  42. protected:
  43. virtual void _screen_enter() {}
  44. virtual void _screen_exit() {}
  45. void _notification(int p_what);
  46. static void _bind_methods();
  47. public:
  48. #ifdef DEBUG_ENABLED
  49. virtual Rect2 _edit_get_rect() const override;
  50. virtual bool _edit_use_rect() const override;
  51. #endif // DEBUG_ENABLED
  52. void set_rect(const Rect2 &p_rect);
  53. Rect2 get_rect() const;
  54. bool is_on_screen() const;
  55. VisibleOnScreenNotifier2D();
  56. };
  57. class VisibleOnScreenEnabler2D : public VisibleOnScreenNotifier2D {
  58. GDCLASS(VisibleOnScreenEnabler2D, VisibleOnScreenNotifier2D);
  59. public:
  60. enum EnableMode {
  61. ENABLE_MODE_INHERIT,
  62. ENABLE_MODE_ALWAYS,
  63. ENABLE_MODE_WHEN_PAUSED,
  64. };
  65. protected:
  66. ObjectID node_id;
  67. virtual void _screen_enter() override;
  68. virtual void _screen_exit() override;
  69. EnableMode enable_mode = ENABLE_MODE_INHERIT;
  70. NodePath enable_node_path = NodePath("..");
  71. void _notification(int p_what);
  72. static void _bind_methods();
  73. void _update_enable_mode(bool p_enable);
  74. public:
  75. void set_enable_mode(EnableMode p_mode);
  76. EnableMode get_enable_mode();
  77. void set_enable_node_path(NodePath p_path);
  78. NodePath get_enable_node_path();
  79. VisibleOnScreenEnabler2D();
  80. };
  81. VARIANT_ENUM_CAST(VisibleOnScreenEnabler2D::EnableMode);
  82. #endif // VISIBLE_ON_SCREEN_NOTIFIER_2D_H