| 1 | /**************************************************************************/ |
| 2 | /* parallax_layer.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_layer.h" |
| 32 | |
| 33 | #include "parallax_background.h" |
| 34 | |
| 35 | void ParallaxLayer::set_motion_scale(const Size2 &p_scale) { |
| 36 | motion_scale = p_scale; |
| 37 | |
| 38 | ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent()); |
| 39 | if (pb && is_inside_tree()) { |
| 40 | Vector2 final_ofs = pb->get_final_offset(); |
| 41 | real_t scroll_scale = pb->get_scroll_scale(); |
| 42 | set_base_offset_and_scale(final_ofs, scroll_scale); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | Size2 ParallaxLayer::get_motion_scale() const { |
| 47 | return motion_scale; |
| 48 | } |
| 49 | |
| 50 | void ParallaxLayer::set_motion_offset(const Size2 &p_offset) { |
| 51 | motion_offset = p_offset; |
| 52 | |
| 53 | ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent()); |
| 54 | if (pb && is_inside_tree()) { |
| 55 | Vector2 final_ofs = pb->get_final_offset(); |
| 56 | real_t scroll_scale = pb->get_scroll_scale(); |
| 57 | set_base_offset_and_scale(final_ofs, scroll_scale); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Size2 ParallaxLayer::get_motion_offset() const { |
| 62 | return motion_offset; |
| 63 | } |
| 64 | |
| 65 | void ParallaxLayer::_update_mirroring() { |
| 66 | if (!is_inside_tree()) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent()); |
| 71 | if (pb) { |
| 72 | RID c = pb->get_canvas(); |
| 73 | RID ci = get_canvas_item(); |
| 74 | Point2 mirrorScale = mirroring * get_scale(); |
| 75 | RenderingServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirrorScale); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) { |
| 80 | mirroring = p_mirroring; |
| 81 | if (mirroring.x < 0) { |
| 82 | mirroring.x = 0; |
| 83 | } |
| 84 | if (mirroring.y < 0) { |
| 85 | mirroring.y = 0; |
| 86 | } |
| 87 | |
| 88 | _update_mirroring(); |
| 89 | } |
| 90 | |
| 91 | Size2 ParallaxLayer::get_mirroring() const { |
| 92 | return mirroring; |
| 93 | } |
| 94 | |
| 95 | void ParallaxLayer::_notification(int p_what) { |
| 96 | switch (p_what) { |
| 97 | case NOTIFICATION_ENTER_TREE: { |
| 98 | orig_offset = get_position(); |
| 99 | orig_scale = get_scale(); |
| 100 | _update_mirroring(); |
| 101 | } break; |
| 102 | |
| 103 | case NOTIFICATION_EXIT_TREE: { |
| 104 | if (Engine::get_singleton()->is_editor_hint()) { |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | set_position(orig_offset); |
| 109 | set_scale(orig_scale); |
| 110 | } break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, real_t p_scale) { |
| 115 | if (!is_inside_tree()) { |
| 116 | return; |
| 117 | } |
| 118 | if (Engine::get_singleton()->is_editor_hint()) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | Point2 new_ofs = p_offset * motion_scale + motion_offset * p_scale + orig_offset * p_scale; |
| 123 | |
| 124 | if (mirroring.x) { |
| 125 | real_t den = mirroring.x * p_scale; |
| 126 | new_ofs.x -= den * ceil(new_ofs.x / den); |
| 127 | } |
| 128 | |
| 129 | if (mirroring.y) { |
| 130 | real_t den = mirroring.y * p_scale; |
| 131 | new_ofs.y -= den * ceil(new_ofs.y / den); |
| 132 | } |
| 133 | |
| 134 | set_position(new_ofs); |
| 135 | set_scale(Vector2(1, 1) * p_scale * orig_scale); |
| 136 | |
| 137 | _update_mirroring(); |
| 138 | } |
| 139 | |
| 140 | PackedStringArray ParallaxLayer::get_configuration_warnings() const { |
| 141 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 142 | |
| 143 | if (!Object::cast_to<ParallaxBackground>(get_parent())) { |
| 144 | warnings.push_back(RTR("ParallaxLayer node only works when set as child of a ParallaxBackground node." )); |
| 145 | } |
| 146 | |
| 147 | return warnings; |
| 148 | } |
| 149 | |
| 150 | void ParallaxLayer::_bind_methods() { |
| 151 | ClassDB::bind_method(D_METHOD("set_motion_scale" , "scale" ), &ParallaxLayer::set_motion_scale); |
| 152 | ClassDB::bind_method(D_METHOD("get_motion_scale" ), &ParallaxLayer::get_motion_scale); |
| 153 | ClassDB::bind_method(D_METHOD("set_motion_offset" , "offset" ), &ParallaxLayer::set_motion_offset); |
| 154 | ClassDB::bind_method(D_METHOD("get_motion_offset" ), &ParallaxLayer::get_motion_offset); |
| 155 | ClassDB::bind_method(D_METHOD("set_mirroring" , "mirror" ), &ParallaxLayer::set_mirroring); |
| 156 | ClassDB::bind_method(D_METHOD("get_mirroring" ), &ParallaxLayer::get_mirroring); |
| 157 | |
| 158 | ADD_GROUP("Motion" , "motion_" ); |
| 159 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_scale" , PROPERTY_HINT_LINK), "set_motion_scale" , "get_motion_scale" ); |
| 160 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_motion_offset" , "get_motion_offset" ); |
| 161 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring" ), "set_mirroring" , "get_mirroring" ); |
| 162 | } |
| 163 | |
| 164 | ParallaxLayer::ParallaxLayer() { |
| 165 | } |
| 166 | |