2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* parallax_layer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2016-01-01 13:50:53 +00:00
|
|
|
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
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_layer.h"
|
|
|
|
#include "parallax_background.h"
|
|
|
|
|
|
|
|
void ParallaxLayer::set_motion_scale(const Size2& p_scale) {
|
|
|
|
|
|
|
|
motion_scale=p_scale;
|
2016-06-26 19:22:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
|
|
|
|
if (is_inside_tree() && pb) {
|
|
|
|
Vector2 ofs = pb->get_final_offset();
|
|
|
|
float scale = pb->get_scroll_scale();
|
|
|
|
set_base_offset_and_scale(ofs,scale);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_scale() const {
|
|
|
|
|
|
|
|
return motion_scale;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:22:20 +00:00
|
|
|
void ParallaxLayer::set_motion_offset(const Size2& p_offset) {
|
|
|
|
|
|
|
|
motion_offset=p_offset;
|
|
|
|
|
|
|
|
ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
|
|
|
|
if (is_inside_tree() && pb) {
|
|
|
|
Vector2 ofs = pb->get_final_offset();
|
|
|
|
float scale = pb->get_scroll_scale();
|
|
|
|
set_base_offset_and_scale(ofs,scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_offset() const {
|
|
|
|
|
|
|
|
return motion_offset;
|
|
|
|
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void ParallaxLayer::_update_mirroring() {
|
|
|
|
|
|
|
|
if (!get_parent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
|
|
|
|
if (pb) {
|
|
|
|
|
|
|
|
RID c = pb->get_world_2d()->get_canvas();
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
VisualServer::get_singleton()->canvas_set_item_mirroring(c,ci,mirroring);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxLayer::set_mirroring(const Size2& p_mirroring) {
|
|
|
|
|
|
|
|
mirroring=p_mirroring;
|
2016-06-26 19:51:13 +00:00
|
|
|
if (mirroring.x<0)
|
|
|
|
mirroring.x=0;
|
|
|
|
if (mirroring.y<0)
|
|
|
|
mirroring.y=0;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_mirroring();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_mirroring() const{
|
|
|
|
|
|
|
|
return mirroring;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ParallaxLayer::_notification(int p_what) {
|
|
|
|
|
|
|
|
switch(p_what) {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
orig_offset=get_pos();
|
|
|
|
orig_scale=get_scale();
|
|
|
|
_update_mirroring();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxLayer::set_base_offset_and_scale(const Point2& p_offset,float p_scale) {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2014-11-06 00:20:42 +00:00
|
|
|
if (get_tree()->is_editor_hint())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2016-06-26 19:22:20 +00:00
|
|
|
Point2 new_ofs = ((orig_offset+p_offset)*motion_scale)*p_scale+motion_offset;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (mirroring.x) {
|
2016-06-26 16:03:15 +00:00
|
|
|
|
|
|
|
while( new_ofs.x>=0) {
|
|
|
|
new_ofs.x -= mirroring.x*p_scale;
|
|
|
|
}
|
|
|
|
while(new_ofs.x < -mirroring.x*p_scale) {
|
|
|
|
new_ofs.x += mirroring.x*p_scale;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mirroring.y) {
|
2016-06-26 16:03:15 +00:00
|
|
|
|
|
|
|
while( new_ofs.y>=0) {
|
|
|
|
new_ofs.y -= mirroring.y*p_scale;
|
|
|
|
}
|
|
|
|
while(new_ofs.y < -mirroring.y*p_scale) {
|
|
|
|
new_ofs.y += mirroring.y*p_scale;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set_pos(new_ofs);
|
|
|
|
set_scale(Vector2(1,1)*p_scale);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:27:15 +00:00
|
|
|
|
|
|
|
String ParallaxLayer::get_configuration_warning() const {
|
|
|
|
|
|
|
|
if (!get_parent() || !get_parent()->cast_to<ParallaxBackground>()) {
|
|
|
|
return TTR("ParallaxLayer node only works when set as child of a ParallaxBackground node.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ParallaxLayer::_bind_methods() {
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_motion_scale","scale"),&ParallaxLayer::set_motion_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_motion_scale"),&ParallaxLayer::get_motion_scale);
|
2016-06-26 19:22:20 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_motion_offset","offset"),&ParallaxLayer::set_motion_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_motion_offset"),&ParallaxLayer::get_motion_offset);
|
2014-02-10 01:10:30 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_mirroring","mirror"),&ParallaxLayer::set_mirroring);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_mirroring"),&ParallaxLayer::get_mirroring);
|
|
|
|
|
|
|
|
ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/scale"),_SCS("set_motion_scale"),_SCS("get_motion_scale"));
|
2016-06-26 19:22:20 +00:00
|
|
|
ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/offset"),_SCS("set_motion_offset"),_SCS("get_motion_offset"));
|
2014-02-10 01:10:30 +00:00
|
|
|
ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/mirroring"),_SCS("set_mirroring"),_SCS("get_mirroring"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ParallaxLayer::ParallaxLayer()
|
|
|
|
{
|
|
|
|
motion_scale=Size2(1,1);
|
|
|
|
}
|