Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A rotating Camera2D #11

Merged
merged 2 commits into from
Feb 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions scene/2d/camera_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ Matrix32 Camera2D::get_camera_transform() {
Point2 screen_offset = (centered ? (screen_size * 0.5 * zoom) : Point2());;
screen_offset+=offset;

float angle = get_global_transform().get_rotation();
if(rotating){
screen_offset = screen_offset.rotated(angle);
}

Rect2 screen_rect(-screen_offset+ret_camera_pos,screen_size);
if (screen_rect.pos.x + screen_rect.size.x > limit[MARGIN_RIGHT])
screen_rect.pos.x = limit[MARGIN_RIGHT] - screen_rect.size.x;
Expand All @@ -151,6 +156,9 @@ Matrix32 Camera2D::get_camera_transform() {
camera_screen_center=screen_rect.pos+screen_rect.size*0.5;

Matrix32 xform;
if(rotating){
xform.set_rotation(angle);
}
xform.scale_basis(zoom);
xform.set_origin(screen_rect.pos/*.floor()*/);

Expand Down Expand Up @@ -251,6 +259,17 @@ bool Camera2D::is_centered() const {
return centered;
}

void Camera2D::set_rotating(bool p_rotating){

rotating=p_rotating;
_update_scroll();
}

bool Camera2D::is_rotating() const {

return rotating;
}


void Camera2D::_make_current(Object *p_which) {

Expand Down Expand Up @@ -394,6 +413,9 @@ void Camera2D::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_centered","centered"),&Camera2D::set_centered);
ObjectTypeDB::bind_method(_MD("is_centered"),&Camera2D::is_centered);

ObjectTypeDB::bind_method(_MD("set_rotating","rotating"),&Camera2D::set_rotating);
ObjectTypeDB::bind_method(_MD("is_rotating"),&Camera2D::is_rotating);

ObjectTypeDB::bind_method(_MD("make_current"),&Camera2D::make_current);
ObjectTypeDB::bind_method(_MD("_make_current"),&Camera2D::_make_current);

Expand Down Expand Up @@ -436,6 +458,7 @@ void Camera2D::_bind_methods() {

ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"offset"),_SCS("set_offset"),_SCS("get_offset"));
ADD_PROPERTY( PropertyInfo(Variant::BOOL,"centered"),_SCS("set_centered"),_SCS("is_centered"));
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::REAL,"smoothing"),_SCS("set_follow_smoothing"),_SCS("get_follow_smoothing") );
ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"zoom"),_SCS("set_zoom"),_SCS("get_zoom") );
Expand All @@ -462,6 +485,7 @@ Camera2D::Camera2D() {


centered=true;
rotating=false;
current=false;
limit[MARGIN_LEFT]=-10000000;
limit[MARGIN_TOP]=-10000000;
Expand Down
4 changes: 4 additions & 0 deletions scene/2d/camera_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Camera2D : public Node2D {
Vector2 offset;
Vector2 zoom;
bool centered;
bool rotating;
bool current;
float smoothing;
int limit[4];
Expand Down Expand Up @@ -79,6 +80,9 @@ class Camera2D : public Node2D {
void set_centered(bool p_centered);
bool is_centered() const;

void set_rotating(bool p_rotating);
bool is_rotating() const;

void set_limit(Margin p_margin,int p_limit);
int get_limit(Margin p_margin) const;

Expand Down