2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* parallax_background.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "parallax_background.h"
|
|
|
|
#include "parallax_layer.h"
|
|
|
|
|
|
|
|
void ParallaxBackground::_notification(int p_what) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
group_name = "__cameras_" + itos(get_viewport().get_id());
|
2014-02-10 01:10:30 +00:00
|
|
|
add_to_group(group_name);
|
|
|
|
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
remove_from_group(group_name);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 19:30:21 +00:00
|
|
|
void ParallaxBackground::_camera_moved(const Matrix32 &p_transform, const Point2 &p_screen_offset) {
|
|
|
|
|
|
|
|
screen_offset = p_screen_offset;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
|
2017-11-06 19:30:21 +00:00
|
|
|
set_scroll_offset(p_transform.get_origin());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_scroll_scale(float p_scale) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
scale = p_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float ParallaxBackground::get_scroll_scale() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
offset = p_ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::_update_scroll() {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 ofs = base_offset + offset * base_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 vps = get_viewport_size();
|
|
|
|
|
|
|
|
ofs = -ofs;
|
|
|
|
if (limit_begin.x < limit_end.x) {
|
|
|
|
|
|
|
|
if (ofs.x < limit_begin.x)
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs.x = limit_begin.x;
|
|
|
|
else if (ofs.x + vps.x > limit_end.x)
|
|
|
|
ofs.x = limit_end.x - vps.x;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit_begin.y < limit_end.y) {
|
|
|
|
|
|
|
|
if (ofs.y < limit_begin.y)
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs.y = limit_begin.y;
|
|
|
|
else if (ofs.y + vps.y > limit_end.y)
|
|
|
|
ofs.y = limit_end.y - vps.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
ofs = -ofs;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
final_offset = ofs;
|
2016-06-26 19:22:20 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ParallaxLayer *l = get_child(i)->cast_to<ParallaxLayer>();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!l)
|
|
|
|
continue;
|
|
|
|
|
2016-06-26 19:22:20 +00:00
|
|
|
if (ignore_camera_zoom)
|
2017-11-06 19:30:21 +00:00
|
|
|
l->set_base_offset_and_scale(ofs, 1.0, screen_offset);
|
2016-06-26 19:22:20 +00:00
|
|
|
else
|
2017-11-06 19:30:21 +00:00
|
|
|
l->set_base_offset_and_scale(ofs, scale, screen_offset);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_scroll_offset() const {
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
base_offset = p_ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Point2 ParallaxBackground::get_scroll_base_offset() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return base_offset;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
base_scale = p_ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Point2 ParallaxBackground::get_scroll_base_scale() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return base_scale;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
limit_begin = p_ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_limit_begin() const {
|
|
|
|
|
|
|
|
return limit_begin;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_limit_end(const Point2 &p_ofs) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
limit_end = p_ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_limit_end() const {
|
|
|
|
|
|
|
|
return limit_end;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ParallaxBackground::set_ignore_camera_zoom(bool ignore) {
|
2015-08-04 04:44:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ignore_camera_zoom = ignore;
|
2015-08-04 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ParallaxBackground::is_ignore_camera_zoom() {
|
2015-08-04 04:44:38 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ignore_camera_zoom;
|
2015-08-04 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 19:22:20 +00:00
|
|
|
Vector2 ParallaxBackground::get_final_offset() const {
|
|
|
|
|
|
|
|
return final_offset;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ParallaxBackground::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_camera_moved"), &ParallaxBackground::_camera_moved);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_scroll_offset", "ofs"), &ParallaxBackground::set_scroll_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_scroll_base_offset", "ofs"), &ParallaxBackground::set_scroll_base_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_limit_begin", "ofs"), &ParallaxBackground::set_limit_begin);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_limit_begin"), &ParallaxBackground::get_limit_begin);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_limit_end", "ofs"), &ParallaxBackground::set_limit_end);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_limit_end"), &ParallaxBackground::get_limit_end);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll/offset"), _SCS("set_scroll_offset"), _SCS("get_scroll_offset"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll/base_offset"), _SCS("set_scroll_base_offset"), _SCS("get_scroll_base_offset"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll/base_scale"), _SCS("set_scroll_base_scale"), _SCS("get_scroll_base_scale"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll/limit_begin"), _SCS("set_limit_begin"), _SCS("get_limit_begin"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll/limit_end"), _SCS("set_limit_end"), _SCS("get_limit_end"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll/ignore_camera_zoom"), _SCS("set_ignore_camera_zoom"), _SCS("is_ignore_camera_zoom"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ParallaxBackground::ParallaxBackground() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
base_scale = Vector2(1, 1);
|
|
|
|
scale = 1.0;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_layer(-1); //behind all by default
|
|
|
|
}
|