0.2.0-dev Download ZIP
112 lines ·4.0 KB Raw
| 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 | #pragma once |
| 32 | |
| 33 | #include "scene/2d/node_2d.h" |
| 34 | |
| 35 | class Viewport; |
| 36 | class VisibleOnScreenNotifier2D : public Node2D { |
| 37 | GDCLASS(VisibleOnScreenNotifier2D, Node2D); |
| 38 | |
| 39 | Rect2 rect; |
| 40 | bool show_rect = true; |
| 41 | |
| 42 | private: |
| 43 | bool on_screen = false; |
| 44 | void _visibility_enter(); |
| 45 | void _visibility_exit(); |
| 46 | |
| 47 | protected: |
| 48 | virtual void _screen_enter() {} |
| 49 | virtual void _screen_exit() {} |
| 50 | |
| 51 | void _notification(int p_what); |
| 52 | static void _bind_methods(); |
| 53 | |
| 54 | public: |
| 55 | #ifdef DEBUG_ENABLED |
| 56 | virtual Dictionary _edit_get_state() const override; |
| 57 | virtual void _edit_set_state(const Dictionary &p_state) override; |
| 58 | |
| 59 | virtual Vector2 _edit_get_minimum_size() const override { return Vector2(); } |
| 60 | |
| 61 | virtual void _edit_set_rect(const Rect2 &p_edit_rect) override; |
| 62 | |
| 63 | virtual Rect2 _edit_get_rect() const override; |
| 64 | |
| 65 | virtual bool _edit_use_rect() const override; |
| 66 | #endif // DEBUG_ENABLED |
| 67 | |
| 68 | void set_rect(const Rect2 &p_rect); |
| 69 | Rect2 get_rect() const; |
| 70 | |
| 71 | void set_show_rect(bool p_show_rect); |
| 72 | bool is_showing_rect() const; |
| 73 | |
| 74 | bool is_on_screen() const; |
| 75 | |
| 76 | VisibleOnScreenNotifier2D(); |
| 77 | }; |
| 78 | |
| 79 | class VisibleOnScreenEnabler2D : public VisibleOnScreenNotifier2D { |
| 80 | GDCLASS(VisibleOnScreenEnabler2D, VisibleOnScreenNotifier2D); |
| 81 | |
| 82 | public: |
| 83 | enum EnableMode { |
| 84 | ENABLE_MODE_INHERIT, |
| 85 | ENABLE_MODE_ALWAYS, |
| 86 | ENABLE_MODE_WHEN_PAUSED, |
| 87 | }; |
| 88 | |
| 89 | protected: |
| 90 | ObjectID node_id; |
| 91 | virtual void _screen_enter() override; |
| 92 | virtual void _screen_exit() override; |
| 93 | |
| 94 | EnableMode enable_mode = ENABLE_MODE_INHERIT; |
| 95 | NodePath enable_node_path = NodePath(".."); |
| 96 | |
| 97 | void _notification(int p_what); |
| 98 | static void _bind_methods(); |
| 99 | |
| 100 | void _update_enable_mode(bool p_enable); |
| 101 | |
| 102 | public: |
| 103 | void set_enable_mode(EnableMode p_mode); |
| 104 | EnableMode get_enable_mode(); |
| 105 | |
| 106 | void set_enable_node_path(NodePath p_path); |
| 107 | NodePath get_enable_node_path(); |
| 108 | |
| 109 | VisibleOnScreenEnabler2D(); |
| 110 | }; |
| 111 | |
| 112 | VARIANT_ENUM_CAST(VisibleOnScreenEnabler2D::EnableMode); |