1 /*************************************************************************/
2 /*  interpolated_camera.cpp                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "interpolated_camera.h"
32 
33 #include "core/engine.h"
34 
_notification(int p_what)35 void InterpolatedCamera::_notification(int p_what) {
36 
37 	switch (p_what) {
38 		case NOTIFICATION_ENTER_TREE: {
39 
40 			WARN_DEPRECATED_MSG("InterpolatedCamera has been deprecated and will be removed in Godot 4.0.");
41 
42 			if (Engine::get_singleton()->is_editor_hint() && enabled)
43 				set_process_internal(false);
44 
45 		} break;
46 		case NOTIFICATION_INTERNAL_PROCESS: {
47 
48 			if (!enabled)
49 				break;
50 			if (has_node(target)) {
51 
52 				Spatial *node = Object::cast_to<Spatial>(get_node(target));
53 				if (!node)
54 					break;
55 
56 				float delta = speed * get_process_delta_time();
57 				Transform target_xform = node->get_global_transform();
58 				Transform local_transform = get_global_transform();
59 				local_transform = local_transform.interpolate_with(target_xform, delta);
60 				set_global_transform(local_transform);
61 				Camera *cam = Object::cast_to<Camera>(node);
62 				if (cam) {
63 
64 					if (cam->get_projection() == get_projection()) {
65 
66 						float new_near = Math::lerp(get_znear(), cam->get_znear(), delta);
67 						float new_far = Math::lerp(get_zfar(), cam->get_zfar(), delta);
68 
69 						if (cam->get_projection() == PROJECTION_ORTHOGONAL) {
70 
71 							float size = Math::lerp(get_size(), cam->get_size(), delta);
72 							set_orthogonal(size, new_near, new_far);
73 						} else {
74 
75 							float fov = Math::lerp(get_fov(), cam->get_fov(), delta);
76 							set_perspective(fov, new_near, new_far);
77 						}
78 					}
79 				}
80 			}
81 
82 		} break;
83 	}
84 }
85 
_set_target(const Object * p_target)86 void InterpolatedCamera::_set_target(const Object *p_target) {
87 
88 	ERR_FAIL_NULL(p_target);
89 	set_target(Object::cast_to<Spatial>(p_target));
90 }
91 
set_target(const Spatial * p_target)92 void InterpolatedCamera::set_target(const Spatial *p_target) {
93 
94 	ERR_FAIL_NULL(p_target);
95 	target = get_path_to(p_target);
96 }
97 
set_target_path(const NodePath & p_path)98 void InterpolatedCamera::set_target_path(const NodePath &p_path) {
99 
100 	target = p_path;
101 }
102 
get_target_path() const103 NodePath InterpolatedCamera::get_target_path() const {
104 
105 	return target;
106 }
107 
set_interpolation_enabled(bool p_enable)108 void InterpolatedCamera::set_interpolation_enabled(bool p_enable) {
109 
110 	if (enabled == p_enable)
111 		return;
112 	enabled = p_enable;
113 	if (p_enable) {
114 		if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
115 			return;
116 		set_process_internal(true);
117 	} else
118 		set_process_internal(false);
119 }
120 
is_interpolation_enabled() const121 bool InterpolatedCamera::is_interpolation_enabled() const {
122 
123 	return enabled;
124 }
125 
set_speed(real_t p_speed)126 void InterpolatedCamera::set_speed(real_t p_speed) {
127 
128 	speed = p_speed;
129 }
130 
get_speed() const131 real_t InterpolatedCamera::get_speed() const {
132 
133 	return speed;
134 }
135 
get_configuration_warning() const136 String InterpolatedCamera::get_configuration_warning() const {
137 
138 	return TTR("InterpolatedCamera has been deprecated and will be removed in Godot 4.0.");
139 }
140 
_bind_methods()141 void InterpolatedCamera::_bind_methods() {
142 
143 	ClassDB::bind_method(D_METHOD("set_target_path", "target_path"), &InterpolatedCamera::set_target_path);
144 	ClassDB::bind_method(D_METHOD("get_target_path"), &InterpolatedCamera::get_target_path);
145 	ClassDB::bind_method(D_METHOD("set_target", "target"), &InterpolatedCamera::_set_target);
146 
147 	ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InterpolatedCamera::set_speed);
148 	ClassDB::bind_method(D_METHOD("get_speed"), &InterpolatedCamera::get_speed);
149 
150 	ClassDB::bind_method(D_METHOD("set_interpolation_enabled", "target_path"), &InterpolatedCamera::set_interpolation_enabled);
151 	ClassDB::bind_method(D_METHOD("is_interpolation_enabled"), &InterpolatedCamera::is_interpolation_enabled);
152 
153 	ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target"), "set_target_path", "get_target_path");
154 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed"), "set_speed", "get_speed");
155 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_interpolation_enabled", "is_interpolation_enabled");
156 }
157 
InterpolatedCamera()158 InterpolatedCamera::InterpolatedCamera() {
159 
160 	enabled = false;
161 	speed = 1;
162 }
163