0.2.0-dev Download ZIP
191 lines ·7.4 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* parallax_background.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 | |
| 31 | #include "parallax_background.h" |
| 32 | |
| 33 | #include "core/object/class_db.h" |
| 34 | #include "scene/2d/parallax_layer.h" |
| 35 | |
| 36 | void ParallaxBackground::_notification(int p_what) { |
| 37 | switch (p_what) { |
| 38 | case NOTIFICATION_ENTER_TREE: { |
| 39 | group_name = "__cameras_" + itos(get_viewport().get_id()); |
| 40 | add_to_group(group_name); |
| 41 | } break; |
| 42 | |
| 43 | case NOTIFICATION_EXIT_TREE: { |
| 44 | remove_from_group(group_name); |
| 45 | } break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void ParallaxBackground::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset, const Point2 &p_adj_screen_offset) { |
| 50 | screen_offset = p_screen_offset; |
| 51 | |
| 52 | set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5))); |
| 53 | set_scroll_offset(p_transform.get_origin()); |
| 54 | } |
| 55 | |
| 56 | void ParallaxBackground::set_scroll_scale(real_t p_scale) { |
| 57 | scale = p_scale; |
| 58 | } |
| 59 | |
| 60 | real_t ParallaxBackground::get_scroll_scale() const { |
| 61 | return scale; |
| 62 | } |
| 63 | |
| 64 | void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) { |
| 65 | offset = p_ofs; |
| 66 | |
| 67 | _update_scroll(); |
| 68 | } |
| 69 | |
| 70 | void ParallaxBackground::_update_scroll() { |
| 71 | if (!is_inside_tree()) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | Vector2 scroll_ofs = base_offset + offset * base_scale; |
| 76 | |
| 77 | Size2 vps = get_viewport_size(); |
| 78 | |
| 79 | scroll_ofs = -scroll_ofs; |
| 80 | if (limit_begin.x < limit_end.x) { |
| 81 | if (scroll_ofs.x < limit_begin.x) { |
| 82 | scroll_ofs.x = limit_begin.x; |
| 83 | } else if (scroll_ofs.x + vps.x > limit_end.x) { |
| 84 | scroll_ofs.x = limit_end.x - vps.x; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (limit_begin.y < limit_end.y) { |
| 89 | if (scroll_ofs.y < limit_begin.y) { |
| 90 | scroll_ofs.y = limit_begin.y; |
| 91 | } else if (scroll_ofs.y + vps.y > limit_end.y) { |
| 92 | scroll_ofs.y = limit_end.y - vps.y; |
| 93 | } |
| 94 | } |
| 95 | scroll_ofs = -scroll_ofs; |
| 96 | |
| 97 | final_offset = scroll_ofs; |
| 98 | |
| 99 | for (int i = 0; i < get_child_count(); i++) { |
| 100 | ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i)); |
| 101 | if (!l) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (ignore_camera_zoom) { |
| 106 | l->set_base_offset_and_scale((scroll_ofs + screen_offset * (scale - 1)) / scale, 1.0); |
| 107 | } else { |
| 108 | l->set_base_offset_and_scale(scroll_ofs, scale); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Point2 ParallaxBackground::get_scroll_offset() const { |
| 114 | return offset; |
| 115 | } |
| 116 | |
| 117 | void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) { |
| 118 | base_offset = p_ofs; |
| 119 | _update_scroll(); |
| 120 | } |
| 121 | |
| 122 | Point2 ParallaxBackground::get_scroll_base_offset() const { |
| 123 | return base_offset; |
| 124 | } |
| 125 | |
| 126 | void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) { |
| 127 | base_scale = p_ofs; |
| 128 | _update_scroll(); |
| 129 | } |
| 130 | |
| 131 | Point2 ParallaxBackground::get_scroll_base_scale() const { |
| 132 | return base_scale; |
| 133 | } |
| 134 | |
| 135 | void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) { |
| 136 | limit_begin = p_ofs; |
| 137 | _update_scroll(); |
| 138 | } |
| 139 | |
| 140 | Point2 ParallaxBackground::get_limit_begin() const { |
| 141 | return limit_begin; |
| 142 | } |
| 143 | |
| 144 | void ParallaxBackground::set_limit_end(const Point2 &p_ofs) { |
| 145 | limit_end = p_ofs; |
| 146 | _update_scroll(); |
| 147 | } |
| 148 | |
| 149 | Point2 ParallaxBackground::get_limit_end() const { |
| 150 | return limit_end; |
| 151 | } |
| 152 | |
| 153 | void ParallaxBackground::set_ignore_camera_zoom(bool ignore) { |
| 154 | ignore_camera_zoom = ignore; |
| 155 | } |
| 156 | |
| 157 | bool ParallaxBackground::is_ignore_camera_zoom() { |
| 158 | return ignore_camera_zoom; |
| 159 | } |
| 160 | |
| 161 | Vector2 ParallaxBackground::get_final_offset() const { |
| 162 | return final_offset; |
| 163 | } |
| 164 | |
| 165 | void ParallaxBackground::_bind_methods() { |
| 166 | ClassDB::bind_method(D_METHOD("_camera_moved"), &ParallaxBackground::_camera_moved); |
| 167 | ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &ParallaxBackground::set_scroll_offset); |
| 168 | ClassDB::bind_method(D_METHOD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset); |
| 169 | ClassDB::bind_method(D_METHOD("set_scroll_base_offset", "offset"), &ParallaxBackground::set_scroll_base_offset); |
| 170 | ClassDB::bind_method(D_METHOD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset); |
| 171 | ClassDB::bind_method(D_METHOD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale); |
| 172 | ClassDB::bind_method(D_METHOD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale); |
| 173 | ClassDB::bind_method(D_METHOD("set_limit_begin", "offset"), &ParallaxBackground::set_limit_begin); |
| 174 | ClassDB::bind_method(D_METHOD("get_limit_begin"), &ParallaxBackground::get_limit_begin); |
| 175 | ClassDB::bind_method(D_METHOD("set_limit_end", "offset"), &ParallaxBackground::set_limit_end); |
| 176 | ClassDB::bind_method(D_METHOD("get_limit_end"), &ParallaxBackground::get_limit_end); |
| 177 | ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom); |
| 178 | ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom); |
| 179 | |
| 180 | ADD_GROUP("Scroll", "scroll_"); |
| 181 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset"); |
| 182 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_base_offset", "get_scroll_base_offset"); |
| 183 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale", PROPERTY_HINT_LINK), "set_scroll_base_scale", "get_scroll_base_scale"); |
| 184 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_begin", "get_limit_begin"); |
| 185 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_end", "get_limit_end"); |
| 186 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom"), "set_ignore_camera_zoom", "is_ignore_camera_zoom"); |
| 187 | } |
| 188 | |
| 189 | ParallaxBackground::ParallaxBackground() { |
| 190 | set_layer(-100); //behind all by default |
| 191 | } |