2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* camera_2d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2014-03-14 02:57:24 +01:00
|
|
|
#include "camera_2d.h"
|
|
|
|
#include "scene/scene_string_names.h"
|
|
|
|
#include "servers/visual_server.h"
|
|
|
|
|
|
|
|
void Camera2D::_update_scroll() {
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (!is_inside_tree())
|
2014-03-14 02:57:24 +01:00
|
|
|
return;
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (get_tree()->is_editor_hint()) {
|
2016-04-19 15:07:06 +02:00
|
|
|
update(); //will just be drawn
|
2014-03-14 02:57:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current) {
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(custom_viewport && !ObjectDB::get_instance(custom_viewport_id));
|
2016-09-14 20:55:14 +02:00
|
|
|
|
2014-03-14 02:57:24 +01:00
|
|
|
Matrix32 xform = get_camera_transform();
|
|
|
|
|
|
|
|
if (viewport) {
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport->set_canvas_transform(xform);
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME, group_name, "_camera_moved", xform);
|
2014-03-14 02:57:24 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_zoom(const Vector2 &p_zoom) {
|
|
|
|
|
|
|
|
zoom = p_zoom;
|
2015-09-04 00:23:39 +02:00
|
|
|
Point2 old_smoothed_camera_pos = smoothed_camera_pos;
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
2015-09-04 00:23:39 +02:00
|
|
|
smoothed_camera_pos = old_smoothed_camera_pos;
|
2014-03-14 02:57:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector2 Camera2D::get_zoom() const {
|
|
|
|
|
|
|
|
return zoom;
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Matrix32 Camera2D::get_camera_transform() {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (!get_tree())
|
2014-03-14 02:57:24 +01:00
|
|
|
return Matrix32();
|
|
|
|
|
|
|
|
Size2 screen_size = get_viewport_rect().size;
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_size = get_viewport_rect().size;
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
Point2 new_camera_pos = get_global_transform().get_origin();
|
|
|
|
Point2 ret_camera_pos;
|
|
|
|
|
|
|
|
if (!first) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2016-04-19 15:07:06 +02:00
|
|
|
if (h_drag_enabled && !get_tree()->is_editor_hint()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
camera_pos.x = MIN(camera_pos.x, (new_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT]));
|
|
|
|
camera_pos.x = MAX(camera_pos.x, (new_camera_pos.x - screen_size.x * 0.5 * drag_margin[MARGIN_LEFT]));
|
2015-06-11 14:55:38 +02:00
|
|
|
} else {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (h_ofs < 0) {
|
2015-06-11 14:55:38 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:07:06 +02:00
|
|
|
if (v_drag_enabled && !get_tree()->is_editor_hint()) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
camera_pos.y = MIN(camera_pos.y, (new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM]));
|
|
|
|
camera_pos.y = MAX(camera_pos.y, (new_camera_pos.y - screen_size.y * 0.5 * drag_margin[MARGIN_TOP]));
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2015-06-11 14:55:38 +02:00
|
|
|
} else {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (v_ofs < 0) {
|
2015-06-11 14:55:38 +02:00
|
|
|
camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs;
|
|
|
|
} else {
|
|
|
|
camera_pos.y = new_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs;
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
camera_pos = new_camera_pos;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:07:06 +02:00
|
|
|
if (smoothing_enabled && !get_tree()->is_editor_hint()) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float c = smoothing * get_fixed_process_delta_time();
|
|
|
|
smoothed_camera_pos = ((camera_pos - smoothed_camera_pos) * c) + smoothed_camera_pos;
|
|
|
|
ret_camera_pos = smoothed_camera_pos;
|
2015-06-11 14:55:38 +02:00
|
|
|
// camera_pos=camera_pos*(1.0-smoothing)+new_camera_pos*smoothing;
|
2014-03-14 02:57:24 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ret_camera_pos = smoothed_camera_pos = camera_pos;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
ret_camera_pos = smoothed_camera_pos = camera_pos = new_camera_pos;
|
|
|
|
first = false;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5 * zoom) : Point2());
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
float angle = get_global_transform().get_rotation();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (rotating) {
|
2014-03-14 02:57:24 +01:00
|
|
|
screen_offset = screen_offset.rotated(angle);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Rect2 screen_rect(-screen_offset + ret_camera_pos, screen_size * zoom);
|
2014-03-14 02:57:24 +01:00
|
|
|
if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
|
|
|
|
screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
|
|
|
|
|
|
|
|
if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
|
|
|
|
screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
|
|
|
|
|
|
|
|
if (screen_rect.pos.x < limit[MARGIN_LEFT])
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_rect.pos.x = limit[MARGIN_LEFT];
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
if (screen_rect.pos.y < limit[MARGIN_TOP])
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_rect.pos.y = limit[MARGIN_TOP];
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (offset != Vector2()) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_rect.pos += offset;
|
2014-03-14 02:57:24 +01:00
|
|
|
if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
|
|
|
|
screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
|
|
|
|
|
|
|
|
if (screen_rect.pos.y + screen_rect.size.y > limit[MARGIN_BOTTOM])
|
|
|
|
screen_rect.pos.y = limit[MARGIN_BOTTOM] - screen_rect.size.y;
|
|
|
|
|
|
|
|
if (screen_rect.pos.x < limit[MARGIN_LEFT])
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_rect.pos.x = limit[MARGIN_LEFT];
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
if (screen_rect.pos.y < limit[MARGIN_TOP])
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_rect.pos.y = limit[MARGIN_TOP];
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
camera_screen_center = screen_rect.pos + screen_rect.size * 0.5;
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
Matrix32 xform;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (rotating) {
|
2014-03-14 02:57:24 +01:00
|
|
|
xform.set_rotation(angle);
|
|
|
|
}
|
|
|
|
xform.scale_basis(zoom);
|
2017-03-19 00:36:26 +01:00
|
|
|
xform.set_origin(screen_rect.pos /*.floor()*/);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
/*
|
2014-03-14 02:57:24 +01: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-19 00:36:26 +01:00
|
|
|
switch (p_what) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_FIXED_PROCESS: {
|
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
|
|
|
if (!is_fixed_processing())
|
|
|
|
_update_scroll();
|
|
|
|
|
|
|
|
} break;
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2016-09-14 20:55:14 +02:00
|
|
|
if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport = custom_viewport;
|
2016-09-14 20:55:14 +02:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport = get_viewport();
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas = get_canvas();
|
|
|
|
|
|
|
|
RID vp = viewport->get_viewport();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
group_name = "__cameras_" + itos(vp.get_id());
|
|
|
|
canvas_group_name = "__cameras_c" + itos(canvas.get_id());
|
2014-03-14 02:57:24 +01:00
|
|
|
add_to_group(group_name);
|
|
|
|
add_to_group(canvas_group_name);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (get_tree()->is_editor_hint()) {
|
2016-04-23 14:27:32 +02:00
|
|
|
set_fixed_process(false);
|
|
|
|
}
|
|
|
|
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
2017-03-19 00:36:26 +01:00
|
|
|
first = true;
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
} break;
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
if (is_current()) {
|
2016-09-14 20:55:14 +02:00
|
|
|
if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport->set_canvas_transform(Matrix32());
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
remove_from_group(group_name);
|
|
|
|
remove_from_group(canvas_group_name);
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport = NULL;
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
} break;
|
2016-04-19 15:07:06 +02:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
|
|
|
if (!is_inside_tree() || !get_tree()->is_editor_hint())
|
|
|
|
break;
|
|
|
|
|
|
|
|
Color area_axis_color(0.5, 0.42, 0.87, 0.63);
|
|
|
|
float area_axis_width = 1;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (current)
|
2016-04-19 15:07:06 +02:00
|
|
|
area_axis_width = 3;
|
|
|
|
|
|
|
|
Matrix32 inv_camera_transform = get_camera_transform().affine_inverse();
|
|
|
|
Size2 screen_size = get_viewport_rect().size;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 screen_endpoints[4] = {
|
2016-04-19 15:07:06 +02:00
|
|
|
inv_camera_transform.xform(Vector2(0, 0)),
|
2017-03-19 00:36:26 +01:00
|
|
|
inv_camera_transform.xform(Vector2(screen_size.width, 0)),
|
2016-04-19 15:07:06 +02:00
|
|
|
inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
|
|
|
|
inv_camera_transform.xform(Vector2(0, screen_size.height))
|
|
|
|
};
|
|
|
|
|
2016-06-21 21:35:04 +02:00
|
|
|
Matrix32 inv_transform = get_global_transform().affine_inverse(); // undo global space
|
2016-04-19 15:07:06 +02:00
|
|
|
|
2017-03-19 00:36:26 +01: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);
|
2016-04-19 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_offset(const Vector2 &p_offset) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
offset = p_offset;
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 Camera2D::get_offset() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_anchor_mode(AnchorMode p_anchor_mode) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
anchor_mode = p_anchor_mode;
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2015-06-11 14:55:38 +02:00
|
|
|
Camera2D::AnchorMode Camera2D::get_anchor_mode() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2015-06-11 14:55:38 +02:00
|
|
|
return anchor_mode;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_rotating(bool p_rotating) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
rotating = p_rotating;
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_rotating() const {
|
|
|
|
|
|
|
|
return rotating;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::_make_current(Object *p_which) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_which == this) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
current = true;
|
2014-03-14 02:57:24 +01:00
|
|
|
_update_scroll();
|
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
current = false;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::_set_current(bool p_current) {
|
|
|
|
|
|
|
|
if (p_current)
|
|
|
|
make_current();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
current = p_current;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_current() const {
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::make_current() {
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
if (!is_inside_tree()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
current = true;
|
2014-03-14 02:57:24 +01:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", this);
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:39:13 +01:00
|
|
|
void Camera2D::clear_current() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
current = false;
|
2015-03-03 18:39:13 +01:00
|
|
|
if (is_inside_tree()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", (Object *)(NULL));
|
2015-03-03 18:39:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_limit(Margin p_margin, int p_limit) {
|
2015-03-03 18:39:13 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_margin, 4);
|
|
|
|
limit[p_margin] = p_limit;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int Camera2D::get_limit(Margin p_margin) const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_margin, 4, 0);
|
2014-03-14 02:57:24 +01:00
|
|
|
return limit[p_margin];
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_drag_margin(Margin p_margin, float p_drag_margin) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_margin, 4);
|
|
|
|
drag_margin[p_margin] = p_drag_margin;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float Camera2D::get_drag_margin(Margin p_margin) const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_margin, 4, 0);
|
2014-03-14 02:57:24 +01:00
|
|
|
return drag_margin[p_margin];
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Camera2D::get_camera_pos() const {
|
|
|
|
|
|
|
|
return camera_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::force_update_scroll() {
|
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2016-06-29 17:39:29 +02:00
|
|
|
void Camera2D::reset_smoothing() {
|
|
|
|
|
|
|
|
smoothed_camera_pos = camera_pos;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
2016-06-29 18:17:11 +02:00
|
|
|
void Camera2D::align() {
|
|
|
|
|
|
|
|
Size2 screen_size = get_viewport_rect().size;
|
2017-03-19 00:36:26 +01:00
|
|
|
screen_size = get_viewport_rect().size;
|
2016-06-29 18:17:11 +02:00
|
|
|
Point2 current_camera_pos = get_global_transform().get_origin();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (anchor_mode == ANCHOR_MODE_DRAG_CENTER) {
|
|
|
|
if (h_ofs < 0) {
|
2016-06-29 18:17:11 +02: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-19 00:36:26 +01:00
|
|
|
if (v_ofs < 0) {
|
2016-06-29 18:17:11 +02: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-19 00:36:26 +01:00
|
|
|
} else if (anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT) {
|
2016-06-29 18:17:11 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
camera_pos = current_camera_pos;
|
2016-06-29 18:17:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
}
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
void Camera2D::set_follow_smoothing(float p_speed) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
smoothing = p_speed;
|
|
|
|
if (smoothing > 0 && !(is_inside_tree() && get_tree()->is_editor_hint()))
|
2014-03-14 02:57:24 +01:00
|
|
|
set_fixed_process(true);
|
|
|
|
else
|
|
|
|
set_fixed_process(false);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float Camera2D::get_follow_smoothing() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
|
|
|
return smoothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 Camera2D::get_camera_screen_center() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return camera_screen_center;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_h_drag_enabled(bool p_enabled) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
h_drag_enabled = p_enabled;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool Camera2D::is_h_drag_enabled() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return h_drag_enabled;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_v_drag_enabled(bool p_enabled) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
v_drag_enabled = p_enabled;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool Camera2D::is_v_drag_enabled() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return v_drag_enabled;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_v_offset(float p_offset) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
v_ofs = p_offset;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float Camera2D::get_v_offset() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return v_ofs;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Camera2D::set_h_offset(float p_offset) {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
h_ofs = p_offset;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
float Camera2D::get_h_offset() const {
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return h_ofs;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
2015-12-12 13:26:13 +01:00
|
|
|
void Camera2D::_set_old_smoothing(float p_val) {
|
|
|
|
//compatibility
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_val > 0) {
|
|
|
|
smoothing_enabled = true;
|
2015-12-12 13:26:13 +01:00
|
|
|
set_follow_smoothing(p_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera2D::set_enable_follow_smoothing(bool p_enabled) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
smoothing_enabled = p_enabled;
|
2015-12-12 13:26:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera2D::is_follow_smoothing_enabled() const {
|
|
|
|
|
|
|
|
return smoothing_enabled;
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:55:14 +02: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-03-19 00:36:26 +01:00
|
|
|
custom_viewport = p_viewport->cast_to<Viewport>();
|
2016-09-14 20:55:14 +02:00
|
|
|
|
|
|
|
if (custom_viewport) {
|
2017-03-19 00:36:26 +01:00
|
|
|
custom_viewport_id = custom_viewport->get_instance_ID();
|
2016-09-14 20:55:14 +02:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
custom_viewport_id = 0;
|
2016-09-14 20:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
|
|
|
|
if (custom_viewport)
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport = custom_viewport;
|
2016-09-14 20:55:14 +02:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
viewport = get_viewport();
|
2016-09-14 20:55:14 +02:00
|
|
|
|
|
|
|
RID vp = viewport->get_viewport();
|
2017-03-19 00:36:26 +01:00
|
|
|
group_name = "__cameras_" + itos(vp.get_id());
|
|
|
|
canvas_group_name = "__cameras_c" + itos(canvas.get_id());
|
2016-09-14 20:55:14 +02:00
|
|
|
add_to_group(group_name);
|
|
|
|
add_to_group(canvas_group_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Node *Camera2D::get_custom_viewport() const {
|
2016-09-14 20:55:14 +02:00
|
|
|
|
|
|
|
return custom_viewport;
|
|
|
|
}
|
|
|
|
|
2014-03-14 02:57:24 +01:00
|
|
|
void Camera2D::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &Camera2D::set_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_offset"), &Camera2D::get_offset);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_anchor_mode", "anchor_mode"), &Camera2D::set_anchor_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_anchor_mode"), &Camera2D::get_anchor_mode);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_rotating", "rotating"), &Camera2D::set_rotating);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_rotating"), &Camera2D::is_rotating);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("make_current"), &Camera2D::make_current);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_current"), &Camera2D::clear_current);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_make_current"), &Camera2D::_make_current);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_update_scroll"), &Camera2D::_update_scroll);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_current", "current"), &Camera2D::_set_current);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_current"), &Camera2D::is_current);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_limit", "margin", "limit"), &Camera2D::set_limit);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_limit", "margin"), &Camera2D::get_limit);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_v_drag_enabled", "enabled"), &Camera2D::set_v_drag_enabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_v_drag_enabled"), &Camera2D::is_v_drag_enabled);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_h_drag_enabled", "enabled"), &Camera2D::set_h_drag_enabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_h_drag_enabled"), &Camera2D::is_h_drag_enabled);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_v_offset", "ofs"), &Camera2D::set_v_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_v_offset"), &Camera2D::get_v_offset);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_h_offset", "ofs"), &Camera2D::set_h_offset);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_h_offset"), &Camera2D::get_h_offset);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_drag_margin", "margin", "drag_margin"), &Camera2D::set_drag_margin);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_drag_margin", "margin"), &Camera2D::get_drag_margin);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_camera_pos"), &Camera2D::get_camera_pos);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_camera_screen_center"), &Camera2D::get_camera_screen_center);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_zoom", "zoom"), &Camera2D::set_zoom);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_zoom"), &Camera2D::get_zoom);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_custom_viewport", "viewport:Viewport"), &Camera2D::set_custom_viewport);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_custom_viewport:Viewport"), &Camera2D::get_custom_viewport);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_follow_smoothing", "follow_smoothing"), &Camera2D::set_follow_smoothing);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_follow_smoothing"), &Camera2D::get_follow_smoothing);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_enable_follow_smoothing", "follow_smoothing"), &Camera2D::set_enable_follow_smoothing);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_follow_smoothing_enabled"), &Camera2D::is_follow_smoothing_enabled);
|
2015-12-12 13:26:13 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("force_update_scroll"), &Camera2D::force_update_scroll);
|
|
|
|
ObjectTypeDB::bind_method(_MD("reset_smoothing"), &Camera2D::reset_smoothing);
|
|
|
|
ObjectTypeDB::bind_method(_MD("align"), &Camera2D::align);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_old_smoothing", "follow_smoothing"), &Camera2D::_set_old_smoothing);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "offset"), _SCS("set_offset"), _SCS("get_offset"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed TopLeft,Drag Center"), _SCS("set_anchor_mode"), _SCS("get_anchor_mode"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotating"), _SCS("set_rotating"), _SCS("is_rotating"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), _SCS("_set_current"), _SCS("is_current"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom"), _SCS("set_zoom"), _SCS("get_zoom"));
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit/left"), _SCS("set_limit"), _SCS("get_limit"), MARGIN_LEFT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit/top"), _SCS("set_limit"), _SCS("get_limit"), MARGIN_TOP);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit/right"), _SCS("set_limit"), _SCS("get_limit"), MARGIN_RIGHT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::INT, "limit/bottom"), _SCS("set_limit"), _SCS("get_limit"), MARGIN_BOTTOM);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin/h_enabled"), _SCS("set_h_drag_enabled"), _SCS("is_h_drag_enabled"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_margin/v_enabled"), _SCS("set_v_drag_enabled"), _SCS("is_v_drag_enabled"));
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smoothing/enable"), _SCS("set_enable_follow_smoothing"), _SCS("is_follow_smoothing_enabled"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "smoothing/speed"), _SCS("set_follow_smoothing"), _SCS("get_follow_smoothing"));
|
2015-12-12 13:26:13 +01:00
|
|
|
|
|
|
|
//compatibility
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "smoothing", PROPERTY_HINT_NONE, "", 0), _SCS("_set_old_smoothing"), _SCS("get_follow_smoothing"));
|
2015-12-12 13:26:13 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin/left", PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_drag_margin"), _SCS("get_drag_margin"), MARGIN_LEFT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin/top", PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_drag_margin"), _SCS("get_drag_margin"), MARGIN_TOP);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin/right", PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_drag_margin"), _SCS("get_drag_margin"), MARGIN_RIGHT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "drag_margin/bottom", PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_drag_margin"), _SCS("get_drag_margin"), MARGIN_BOTTOM);
|
2014-03-14 02:57:24 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
BIND_CONSTANT(ANCHOR_MODE_DRAG_CENTER);
|
|
|
|
BIND_CONSTANT(ANCHOR_MODE_FIXED_TOP_LEFT);
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera2D::Camera2D() {
|
|
|
|
|
2017-03-19 00:36:26 +01: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;
|
|
|
|
custom_viewport = NULL;
|
|
|
|
custom_viewport_id = 0;
|
|
|
|
|
|
|
|
smoothing = 5.0;
|
2014-03-14 02:57:24 +01:00
|
|
|
zoom = Vector2(1, 1);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
h_drag_enabled = true;
|
|
|
|
v_drag_enabled = true;
|
|
|
|
h_ofs = 0;
|
|
|
|
v_ofs = 0;
|
2014-03-14 02:57:24 +01:00
|
|
|
}
|