2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* camera_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
#include "camera_2d.h"
|
2019-06-12 20:35:35 +00:00
|
|
|
|
|
|
|
#include "core/engine.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
2014-03-14 01:57:24 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "servers/rendering_server.h"
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
void Camera2D::_update_scroll() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2014-03-14 01:57:24 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-08-18 23:02:56 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2016-04-19 13:07:06 +00:00
|
|
|
update(); //will just be drawn
|
2014-03-14 01:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!viewport) {
|
2019-06-03 19:52:50 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-06-03 19:52:50 +00:00
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
if (current) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
|
2016-09-14 18:55:14 +00:00
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D xform = get_camera_transform();
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2019-06-03 19:52:50 +00:00
|
|
|
viewport->set_canvas_transform(xform);
|
2017-10-26 19:08:40 +00:00
|
|
|
|
|
|
|
Size2 screen_size = viewport->get_visible_rect().size;
|
|
|
|
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
|
|
|
|
|
|
|
|
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_camera_moved", xform, screen_offset);
|
2014-03-14 01:57:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:57:41 +00:00
|
|
|
void Camera2D::_update_process_mode() {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
set_process_internal(false);
|
|
|
|
set_physics_process_internal(false);
|
|
|
|
} else if (process_mode == CAMERA2D_PROCESS_IDLE) {
|
|
|
|
set_process_internal(true);
|
|
|
|
set_physics_process_internal(false);
|
|
|
|
} else {
|
|
|
|
set_process_internal(false);
|
|
|
|
set_physics_process_internal(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
void Camera2D::set_zoom(const Vector2 &p_zoom) {
|
|
|
|
zoom = p_zoom;
|
2015-09-03 22:23:39 +00:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
2015-09-03 22:23:39 +00:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector2 Camera2D::get_zoom() const {
|
|
|
|
return zoom;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Transform2D Camera2D::get_camera_transform() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!get_tree()) {
|
2017-01-11 03:52:51 +00:00
|
|
|
return Transform2D();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(custom_viewport && !ObjectDB::get_instance(custom_viewport_id), Transform2D());
|
2016-09-15 11:17:23 +00:00
|
|
|
|
|
|
|
Size2 screen_size = viewport->get_visible_rect().size;
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
Point2 new_camera_pos = get_global_transform().get_origin();
|
|
|
|
Point2 ret_camera_pos;
|
|
|
|
|
|
|
|
if (!first) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
|
2019-03-14 12:58:29 +00:00
|
|
|
if (h_drag_enabled && !Engine::get_singleton()->is_editor_hint() && !h_offset_changed) {
|
2018-05-15 23:53:38 +00:00
|
|
|
camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * zoom.x * drag_margin[MARGIN_LEFT]));
|
|
|
|
camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * zoom.x * drag_margin[MARGIN_RIGHT]));
|
2015-06-11 12:55:38 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (h_ofs < 0) {
|
2015-06-11 12:55:38 +00:00
|
|
|
camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
|
|
|
|
} else {
|
|
|
|
camera_pos.x = new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
|
|
|
|
}
|
2019-03-14 12:58:29 +00:00
|
|
|
|
|
|
|
h_offset_changed = false;
|
2015-06-11 12:55:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 12:58:29 +00:00
|
|
|
if (v_drag_enabled && !Engine::get_singleton()->is_editor_hint() && !v_offset_changed) {
|
2018-05-15 23:53:38 +00:00
|
|
|
camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * zoom.y * drag_margin[MARGIN_TOP]));
|
|
|
|
camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * zoom.y * drag_margin[MARGIN_BOTTOM]));
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2015-06-11 12:55:38 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (v_ofs < 0) {
|
2015-06-11 12:55:38 +00:00
|
|
|
camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
|
2020-02-29 23:50:33 +00:00
|
|
|
} else {
|
|
|
|
camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
|
2015-06-11 12:55:38 +00:00
|
|
|
}
|
2019-03-14 12:58:29 +00:00
|
|
|
|
|
|
|
v_offset_changed = false;
|
2015-06-11 12:55:38 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
|
|
|
|
camera_pos = new_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
|
|
|
|
Rect2 screen_rect(-screen_offset + camera_pos, screen_size * zoom);
|
|
|
|
|
2016-07-13 09:48:50 +00:00
|
|
|
if (limit_smoothing_enabled) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.x < limit[MARGIN_LEFT]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
camera_pos.x -= screen_rect.position.x - limit[MARGIN_LEFT];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
camera_pos.x -= screen_rect.position.x + screen_rect.size.x - limit[MARGIN_RIGHT];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-07-13 09:48:50 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
camera_pos.y -= screen_rect.position.y + screen_rect.size.y - limit[MARGIN_BOTTOM];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-07-13 09:48:50 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.y < limit[MARGIN_TOP]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
camera_pos.y -= screen_rect.position.y - limit[MARGIN_TOP];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-07-13 09:48:50 +00:00
|
|
|
}
|
2015-06-11 12:55:38 +00:00
|
|
|
|
2017-08-18 23:02:56 +00:00
|
|
|
if (smoothing_enabled && !Engine::get_singleton()->is_editor_hint()) {
|
2019-01-06 13:57:41 +00:00
|
|
|
float c = smoothing * (process_mode == CAMERA2D_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
|
2017-03-05 15:44:50 +00:00
|
|
|
smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
|
|
|
|
ret_camera_pos = smoothed_camera_pos;
|
2017-01-14 11:26:56 +00:00
|
|
|
//camera_pos=camera_pos*(1.0-smoothing)+new_camera_pos*smoothing;
|
2014-03-14 01:57:24 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ret_camera_pos = smoothed_camera_pos = camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
|
|
|
|
first = false;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
float angle = get_global_transform().get_rotation();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (rotating) {
|
2014-03-14 01:57:24 +00:00
|
|
|
screen_offset = screen_offset.rotated(angle);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.x < limit[MARGIN_LEFT]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
screen_rect.position.x = limit[MARGIN_LEFT];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.x + screen_rect.size.x > limit[MARGIN_RIGHT]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
screen_rect.position.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.y + screen_rect.size.y > limit[MARGIN_BOTTOM]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
screen_rect.position.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen_rect.position.y < limit[MARGIN_TOP]) {
|
2017-06-03 22:25:13 +00:00
|
|
|
screen_rect.position.y = limit[MARGIN_TOP];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (offset != Vector2()) {
|
2017-06-03 22:25:13 +00:00
|
|
|
screen_rect.position += offset;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-06-03 22:25:13 +00:00
|
|
|
camera_screen_center = screen_rect.position + screen_rect.size * 0.5;
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D xform;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (rotating) {
|
2014-03-14 01:57:24 +00:00
|
|
|
xform.set_rotation(angle);
|
|
|
|
}
|
|
|
|
xform.scale_basis(zoom);
|
2017-06-03 22:25:13 +00:00
|
|
|
xform.set_origin(screen_rect.position /*.floor()*/);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2014-03-14 01:57:24 +00:00
|
|
|
if (0) {
|
|
|
|
xform = get_global_transform() * xform;
|
|
|
|
} else {
|
|
|
|
xform.elements[2]+=get_global_transform().get_origin();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (xform).affine_inverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2019-01-06 13:57:41 +00:00
|
|
|
case NOTIFICATION_INTERNAL_PROCESS:
|
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_processing_internal() && !is_physics_processing_internal()) {
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2016-09-14 18:55:14 +00:00
|
|
|
if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
viewport = custom_viewport;
|
2016-09-14 18:55:14 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
viewport = get_viewport();
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas = get_canvas();
|
|
|
|
|
2017-01-14 14:07:57 +00:00
|
|
|
RID vp = viewport->get_viewport_rid();
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
group_name = "__cameras_" + itos(vp.get_id());
|
|
|
|
canvas_group_name = "__cameras_c" + itos(canvas.get_id());
|
2014-03-14 01:57:24 +00:00
|
|
|
add_to_group(group_name);
|
|
|
|
add_to_group(canvas_group_name);
|
|
|
|
|
2019-01-06 13:57:41 +00:00
|
|
|
_update_process_mode();
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
2017-03-05 15:44:50 +00:00
|
|
|
first = true;
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (is_current()) {
|
2016-09-14 18:55:14 +00:00
|
|
|
if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
|
2017-03-05 15:44:50 +00:00
|
|
|
viewport->set_canvas_transform(Transform2D());
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
remove_from_group(group_name);
|
|
|
|
remove_from_group(canvas_group_name);
|
2020-04-01 23:20:12 +00:00
|
|
|
viewport = nullptr;
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
} break;
|
2016-04-19 13:07:06 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree() || !Engine::get_singleton()->is_editor_hint()) {
|
2016-04-19 13:07:06 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-04-19 13:07:06 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
if (screen_drawing_enabled) {
|
|
|
|
Color area_axis_color(0.5, 0.42, 0.87, 0.63);
|
|
|
|
float area_axis_width = 1;
|
|
|
|
if (is_current()) {
|
|
|
|
area_axis_width = 3;
|
|
|
|
area_axis_color.a = 0.83;
|
|
|
|
}
|
2016-04-19 13:07:06 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
|
|
|
|
Size2 screen_size = get_viewport_rect().size;
|
2016-04-19 13:07:06 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
Vector2 screen_endpoints[4] = {
|
|
|
|
inv_camera_transform.xform(Vector2(0, 0)),
|
|
|
|
inv_camera_transform.xform(Vector2(screen_size.width, 0)),
|
|
|
|
inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
|
|
|
|
inv_camera_transform.xform(Vector2(0, screen_size.height))
|
|
|
|
};
|
2016-04-19 13:07:06 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
|
2016-04-19 13:07:06 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit_drawing_enabled) {
|
|
|
|
Color limit_drawing_color(1, 1, 0, 0.63);
|
|
|
|
float limit_drawing_width = 1;
|
|
|
|
if (is_current()) {
|
|
|
|
limit_drawing_color.a = 0.83;
|
|
|
|
limit_drawing_width = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 camera_origin = get_global_transform().get_origin();
|
|
|
|
Vector2 camera_scale = get_global_transform().get_scale().abs();
|
|
|
|
Vector2 limit_points[4] = {
|
|
|
|
(Vector2(limit[MARGIN_LEFT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
|
|
|
|
(Vector2(limit[MARGIN_RIGHT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
|
|
|
|
(Vector2(limit[MARGIN_RIGHT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale,
|
|
|
|
(Vector2(limit[MARGIN_LEFT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (margin_drawing_enabled) {
|
|
|
|
Color margin_drawing_color(0, 1, 1, 0.63);
|
|
|
|
float margin_drawing_width = 1;
|
|
|
|
if (is_current()) {
|
|
|
|
margin_drawing_width = 3;
|
|
|
|
margin_drawing_color.a = 0.83;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
|
|
|
|
Size2 screen_size = get_viewport_rect().size;
|
|
|
|
|
|
|
|
Vector2 margin_endpoints[4] = {
|
|
|
|
inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
|
|
|
|
inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
|
|
|
|
inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM]))),
|
|
|
|
inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM])))
|
|
|
|
};
|
|
|
|
|
|
|
|
Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
|
|
|
|
}
|
2016-04-19 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_offset(const Vector2 &p_offset) {
|
|
|
|
offset = p_offset;
|
2020-06-15 14:27:59 +00:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
2020-06-15 14:27:59 +00:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 Camera2D::get_offset() const {
|
2014-03-14 01:57:24 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
|
|
|
|
anchor_mode = p_anchor_mode;
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2015-06-11 12:55:38 +00:00
|
|
|
Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
|
|
|
|
return anchor_mode;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_rotating(bool p_rotating) {
|
|
|
|
rotating = p_rotating;
|
2020-06-15 14:27:59 +00:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
_update_scroll();
|
2020-06-15 14:27:59 +00:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_rotating() const {
|
|
|
|
return rotating;
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:57:41 +00:00
|
|
|
void Camera2D::set_process_mode(Camera2DProcessMode p_mode) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (process_mode == p_mode) {
|
2019-01-06 13:57:41 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-01-06 13:57:41 +00:00
|
|
|
|
|
|
|
process_mode = p_mode;
|
|
|
|
_update_process_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera2D::Camera2DProcessMode Camera2D::get_process_mode() const {
|
|
|
|
return process_mode;
|
|
|
|
}
|
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
void Camera2D::_make_current(Object *p_which) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_which == this) {
|
|
|
|
current = true;
|
2014-03-14 01:57:24 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
current = false;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::_set_current(bool p_current) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_current) {
|
2014-03-14 01:57:24 +00:00
|
|
|
make_current();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current = p_current;
|
2017-06-20 21:00:46 +00:00
|
|
|
update();
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_current() const {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::make_current() {
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
current = true;
|
2014-03-14 01:57:24 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", this);
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
2019-01-01 17:47:34 +00:00
|
|
|
_update_scroll();
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 17:39:13 +00:00
|
|
|
void Camera2D::clear_current() {
|
2017-03-05 15:44:50 +00:00
|
|
|
current = false;
|
2015-03-03 17:39:13 +00:00
|
|
|
if (is_inside_tree()) {
|
2020-04-01 23:20:12 +00:00
|
|
|
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", (Object *)nullptr);
|
2015-03-03 17:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_limit(Margin p_margin, int p_limit) {
|
2018-10-04 07:17:59 +00:00
|
|
|
ERR_FAIL_INDEX((int)p_margin, 4);
|
2017-03-05 15:44:50 +00:00
|
|
|
limit[p_margin] = p_limit;
|
2017-06-20 21:00:46 +00:00
|
|
|
update();
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int Camera2D::get_limit(Margin p_margin) const {
|
2018-10-04 07:17:59 +00:00
|
|
|
ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
|
2014-03-14 01:57:24 +00:00
|
|
|
return limit[p_margin];
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:48:50 +00:00
|
|
|
void Camera2D::set_limit_smoothing_enabled(bool enable) {
|
|
|
|
limit_smoothing_enabled = enable;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera2D::is_limit_smoothing_enabled() const {
|
2016-07-13 09:48:50 +00:00
|
|
|
return limit_smoothing_enabled;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_drag_margin(Margin p_margin, float p_drag_margin) {
|
2018-10-04 07:17:59 +00:00
|
|
|
ERR_FAIL_INDEX((int)p_margin, 4);
|
2017-03-05 15:44:50 +00:00
|
|
|
drag_margin[p_margin] = p_drag_margin;
|
2017-06-20 21:00:46 +00:00
|
|
|
update();
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float Camera2D::get_drag_margin(Margin p_margin) const {
|
2018-10-04 07:17:59 +00:00
|
|
|
ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
|
2014-03-14 01:57:24 +00:00
|
|
|
return drag_margin[p_margin];
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
Vector2 Camera2D::get_camera_position() const {
|
2014-03-14 01:57:24 +00:00
|
|
|
return camera_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::force_update_scroll() {
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2016-06-29 15:39:29 +00:00
|
|
|
void Camera2D::reset_smoothing() {
|
|
|
|
smoothed_camera_pos = camera_pos;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2016-06-29 16:17:11 +00:00
|
|
|
void Camera2D::align() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
|
2016-09-15 11:17:23 +00:00
|
|
|
|
|
|
|
Size2 screen_size = viewport->get_visible_rect().size;
|
|
|
|
|
2016-06-29 16:17:11 +00:00
|
|
|
Point2 current_camera_pos = get_global_transform().get_origin();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
|
|
|
|
if (h_ofs < 0) {
|
2016-06-29 16:17:11 +00:00
|
|
|
camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs;
|
|
|
|
} else {
|
|
|
|
camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (v_ofs < 0) {
|
2016-06-29 16:17:11 +00:00
|
|
|
camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
|
|
|
|
} else {
|
|
|
|
camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
|
|
|
|
camera_pos = current_camera_pos;
|
2016-06-29 16:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
|
|
|
|
void Camera2D::set_follow_smoothing(float p_speed) {
|
2017-03-05 15:44:50 +00:00
|
|
|
smoothing = p_speed;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (smoothing > 0 && !(is_inside_tree() && Engine::get_singleton()->is_editor_hint())) {
|
2017-12-26 05:57:23 +00:00
|
|
|
set_process_internal(true);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-12-26 05:57:23 +00:00
|
|
|
set_process_internal(false);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float Camera2D::get_follow_smoothing() const {
|
2014-03-14 01:57:24 +00:00
|
|
|
return smoothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Camera2D::get_camera_screen_center() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return camera_screen_center;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_h_drag_enabled(bool p_enabled) {
|
2017-03-05 15:44:50 +00:00
|
|
|
h_drag_enabled = p_enabled;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera2D::is_h_drag_enabled() const {
|
|
|
|
return h_drag_enabled;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_v_drag_enabled(bool p_enabled) {
|
|
|
|
v_drag_enabled = p_enabled;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera2D::is_v_drag_enabled() const {
|
|
|
|
return v_drag_enabled;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_v_offset(float p_offset) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v_ofs = p_offset;
|
2019-03-14 12:58:29 +00:00
|
|
|
v_offset_changed = true;
|
2020-06-15 14:27:59 +00:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2018-01-11 22:35:12 +00:00
|
|
|
_update_scroll();
|
2020-06-15 14:27:59 +00:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float Camera2D::get_v_offset() const {
|
|
|
|
return v_ofs;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera2D::set_h_offset(float p_offset) {
|
|
|
|
h_ofs = p_offset;
|
2019-03-14 12:58:29 +00:00
|
|
|
h_offset_changed = true;
|
2020-06-15 14:27:59 +00:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2018-01-11 22:35:12 +00:00
|
|
|
_update_scroll();
|
2020-06-15 14:27:59 +00:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float Camera2D::get_h_offset() const {
|
|
|
|
return h_ofs;
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
void Camera2D::_set_old_smoothing(float p_enable) {
|
2015-12-12 12:26:13 +00:00
|
|
|
//compatibility
|
2017-08-11 19:10:05 +00:00
|
|
|
if (p_enable > 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
smoothing_enabled = true;
|
2017-08-11 19:10:05 +00:00
|
|
|
set_follow_smoothing(p_enable);
|
2015-12-12 12:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_enable_follow_smoothing(bool p_enabled) {
|
2017-03-05 15:44:50 +00:00
|
|
|
smoothing_enabled = p_enabled;
|
2015-12-12 12:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_follow_smoothing_enabled() const {
|
|
|
|
return smoothing_enabled;
|
|
|
|
}
|
|
|
|
|
2016-09-14 18:55:14 +00:00
|
|
|
void Camera2D::set_custom_viewport(Node *p_viewport) {
|
|
|
|
ERR_FAIL_NULL(p_viewport);
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
remove_from_group(group_name);
|
|
|
|
remove_from_group(canvas_group_name);
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
custom_viewport = Object::cast_to<Viewport>(p_viewport);
|
2016-09-14 18:55:14 +00:00
|
|
|
|
|
|
|
if (custom_viewport) {
|
2017-08-07 10:17:31 +00:00
|
|
|
custom_viewport_id = custom_viewport->get_instance_id();
|
2016-09-14 18:55:14 +00:00
|
|
|
} else {
|
2020-02-12 17:24:06 +00:00
|
|
|
custom_viewport_id = ObjectID();
|
2016-09-14 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_inside_tree()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (custom_viewport) {
|
2017-03-05 15:44:50 +00:00
|
|
|
viewport = custom_viewport;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
viewport = get_viewport();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-09-14 18:55:14 +00:00
|
|
|
|
2017-01-14 14:07:57 +00:00
|
|
|
RID vp = viewport->get_viewport_rid();
|
2017-03-05 15:44:50 +00:00
|
|
|
group_name = "__cameras_" + itos(vp.get_id());
|
|
|
|
canvas_group_name = "__cameras_c" + itos(canvas.get_id());
|
2016-09-14 18:55:14 +00:00
|
|
|
add_to_group(group_name);
|
|
|
|
add_to_group(canvas_group_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Node *Camera2D::get_custom_viewport() const {
|
2016-09-14 18:55:14 +00:00
|
|
|
return custom_viewport;
|
|
|
|
}
|
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
void Camera2D::set_screen_drawing_enabled(bool enable) {
|
|
|
|
screen_drawing_enabled = enable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_screen_drawing_enabled() const {
|
|
|
|
return screen_drawing_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_limit_drawing_enabled(bool enable) {
|
|
|
|
limit_drawing_enabled = enable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_limit_drawing_enabled() const {
|
|
|
|
return limit_drawing_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_margin_drawing_enabled(bool enable) {
|
|
|
|
margin_drawing_enabled = enable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_margin_drawing_enabled() const {
|
|
|
|
return margin_drawing_enabled;
|
|
|
|
}
|
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
void Camera2D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Camera2D::set_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offset"), &Camera2D::get_offset);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_anchor_mode"), &Camera2D::get_anchor_mode);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_rotating", "rotating"), &Camera2D::set_rotating);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_rotating"), &Camera2D::is_rotating);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_current"), &Camera2D::clear_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2019-01-06 13:57:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Camera2D::set_process_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_process_mode"), &Camera2D::get_process_mode);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_current", "current"), &Camera2D::_set_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_limit_smoothing_enabled", "limit_smoothing_enabled"), &Camera2D::set_limit_smoothing_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_limit_smoothing_enabled"), &Camera2D::is_limit_smoothing_enabled);
|
2016-07-13 09:48:50 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_v_drag_enabled", "enabled"), &Camera2D::set_v_drag_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_v_drag_enabled"), &Camera2D::is_v_drag_enabled);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_h_drag_enabled", "enabled"), &Camera2D::set_h_drag_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_h_drag_enabled"), &Camera2D::is_h_drag_enabled);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera2D::set_v_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera2D::get_v_offset);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera2D::set_h_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera2D::get_h_offset);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_camera_position"), &Camera2D::get_camera_position);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_camera_screen_center"), &Camera2D::get_camera_screen_center);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_viewport", "viewport"), &Camera2D::set_custom_viewport);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_viewport"), &Camera2D::get_custom_viewport);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_follow_smoothing", "follow_smoothing"), &Camera2D::set_follow_smoothing);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_follow_smoothing"), &Camera2D::get_follow_smoothing);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_follow_smoothing", "follow_smoothing"), &Camera2D::set_enable_follow_smoothing);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_follow_smoothing_enabled"), &Camera2D::is_follow_smoothing_enabled);
|
2015-12-12 12:26:13 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("force_update_scroll"), &Camera2D::force_update_scroll);
|
|
|
|
ClassDB::bind_method(D_METHOD("reset_smoothing"), &Camera2D::reset_smoothing);
|
|
|
|
ClassDB::bind_method(D_METHOD("align"), &Camera2D::align);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_screen_drawing_enabled", "screen_drawing_enabled"), &Camera2D::set_screen_drawing_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_screen_drawing_enabled"), &Camera2D::is_screen_drawing_enabled);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_limit_drawing_enabled", "limit_drawing_enabled"), &Camera2D::set_limit_drawing_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_limit_drawing_enabled"), &Camera2D::is_limit_drawing_enabled);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_margin_drawing_enabled", "margin_drawing_enabled"), &Camera2D::set_margin_drawing_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_margin_drawing_enabled"), &Camera2D::is_margin_drawing_enabled);
|
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed TopLeft,Drag Center"), "set_anchor_mode", "get_anchor_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotating"), "set_rotating", "is_rotating");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "_set_current", "is_current");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom"), "set_zoom", "get_zoom");
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", 0), "set_custom_viewport", "get_custom_viewport");
|
2019-01-06 13:57:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_GROUP("Limit", "limit_");
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_left"), "set_limit", "get_limit", MARGIN_LEFT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_top"), "set_limit", "get_limit", MARGIN_TOP);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_right"), "set_limit", "get_limit", MARGIN_RIGHT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit_bottom"), "set_limit", "get_limit", MARGIN_BOTTOM);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "limit_smoothed"), "set_limit_smoothing_enabled", "is_limit_smoothing_enabled");
|
2017-01-04 04:16:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_GROUP("Draw Margin", "draw_margin_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin_h_enabled"), "set_h_drag_enabled", "is_h_drag_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin_v_enabled"), "set_v_drag_enabled", "is_v_drag_enabled");
|
2017-01-04 04:16:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_GROUP("Smoothing", "smoothing_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smoothing_enabled"), "set_enable_follow_smoothing", "is_follow_smoothing_enabled");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "smoothing_speed"), "set_follow_smoothing", "get_follow_smoothing");
|
2017-01-04 04:16:14 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_GROUP("Offset", "offset_");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "offset_h", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_h_offset", "get_h_offset");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "offset_v", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_v_offset", "get_v_offset");
|
2018-01-11 22:35:12 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_GROUP("Drag Margin", "drag_margin_");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_margin_left", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_LEFT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_margin_top", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_TOP);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_margin_right", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_RIGHT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "drag_margin_bottom", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drag_margin", "get_drag_margin", MARGIN_BOTTOM);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
ADD_GROUP("Editor", "editor_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_screen"), "set_screen_drawing_enabled", "is_screen_drawing_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_limits"), "set_limit_drawing_enabled", "is_limit_drawing_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_draw_drag_margin"), "set_margin_drawing_enabled", "is_margin_drawing_enabled");
|
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
|
2017-10-21 18:58:02 +00:00
|
|
|
BIND_ENUM_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
|
2019-01-06 13:57:41 +00:00
|
|
|
BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_PHYSICS);
|
|
|
|
BIND_ENUM_CONSTANT(CAMERA2D_PROCESS_IDLE);
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera2D::Camera2D() {
|
2017-03-05 15:44:50 +00:00
|
|
|
anchor_mode = ANCHOR_MODE_DRAG_CENTER;
|
|
|
|
rotating = false;
|
|
|
|
current = false;
|
|
|
|
limit[MARGIN_LEFT] = -10000000;
|
|
|
|
limit[MARGIN_TOP] = -10000000;
|
|
|
|
limit[MARGIN_RIGHT] = 10000000;
|
|
|
|
limit[MARGIN_BOTTOM] = 10000000;
|
|
|
|
|
|
|
|
drag_margin[MARGIN_LEFT] = 0.2;
|
|
|
|
drag_margin[MARGIN_TOP] = 0.2;
|
|
|
|
drag_margin[MARGIN_RIGHT] = 0.2;
|
|
|
|
drag_margin[MARGIN_BOTTOM] = 0.2;
|
|
|
|
camera_pos = Vector2();
|
|
|
|
first = true;
|
|
|
|
smoothing_enabled = false;
|
|
|
|
limit_smoothing_enabled = false;
|
2020-04-01 23:20:12 +00:00
|
|
|
custom_viewport = nullptr;
|
2020-02-12 17:24:06 +00:00
|
|
|
|
2019-01-06 13:57:41 +00:00
|
|
|
process_mode = CAMERA2D_PROCESS_IDLE;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
smoothing = 5.0;
|
2014-03-14 01:57:24 +00:00
|
|
|
zoom = Vector2(1, 1);
|
|
|
|
|
2017-06-20 21:00:46 +00:00
|
|
|
screen_drawing_enabled = true;
|
|
|
|
limit_drawing_enabled = false;
|
|
|
|
margin_drawing_enabled = false;
|
|
|
|
|
2019-03-14 12:58:29 +00:00
|
|
|
h_drag_enabled = false;
|
|
|
|
v_drag_enabled = false;
|
2017-03-05 15:44:50 +00:00
|
|
|
h_ofs = 0;
|
|
|
|
v_ofs = 0;
|
2019-03-14 12:58:29 +00:00
|
|
|
h_offset_changed = false;
|
|
|
|
v_offset_changed = false;
|
|
|
|
|
2017-01-12 23:35:46 +00:00
|
|
|
set_notify_transform(true);
|
2014-03-14 01:57:24 +00:00
|
|
|
}
|