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