292 lines
7.3 KiB
C++
292 lines
7.3 KiB
C++
/*************************************************************************/
|
|
/* node_2d.cpp */
|
|
/*************************************************************************/
|
|
/* This file is part of: */
|
|
/* GODOT ENGINE */
|
|
/* http://www.godotengine.org */
|
|
/*************************************************************************/
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
/* */
|
|
/* 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 "node_2d.h"
|
|
|
|
#include "servers/visual_server.h"
|
|
#include "scene/gui/control.h"
|
|
#include "scene/main/viewport.h"
|
|
#include "message_queue.h"
|
|
|
|
|
|
void Node2D::edit_set_pivot(const Point2& p_pivot) {
|
|
|
|
|
|
}
|
|
|
|
Point2 Node2D::edit_get_pivot() const {
|
|
|
|
return Point2();
|
|
}
|
|
bool Node2D::edit_has_pivot() const {
|
|
|
|
return false;
|
|
}
|
|
|
|
Variant Node2D::edit_get_state() const {
|
|
|
|
Array state;
|
|
state.push_back(pos);
|
|
state.push_back(angle);
|
|
state.push_back(scale);
|
|
|
|
return state;
|
|
|
|
}
|
|
void Node2D::edit_set_state(const Variant& p_state) {
|
|
|
|
Array state = p_state;
|
|
ERR_FAIL_COND( state.size() != 3);
|
|
|
|
pos = state[0];
|
|
angle = state[1];
|
|
scale = state[2];
|
|
_update_transform();
|
|
_change_notify("transform/rot");
|
|
_change_notify("transform/scale");
|
|
_change_notify("transform/pos");
|
|
|
|
}
|
|
|
|
void Node2D::edit_set_rect(const Rect2& p_edit_rect) {
|
|
|
|
Rect2 r = get_item_rect();
|
|
|
|
Vector2 zero_offset;
|
|
if (r.size.x!=0)
|
|
zero_offset.x = -r.pos.x / r.size.x;
|
|
if (r.size.y!=0)
|
|
zero_offset.y = -r.pos.y / r.size.y;
|
|
|
|
Size2 new_scale(1,1);
|
|
|
|
if (r.size.x!=0)
|
|
new_scale.x = p_edit_rect.size.x / r.size.x;
|
|
if (r.size.y!=0)
|
|
new_scale.y = p_edit_rect.size.y / r.size.y;
|
|
|
|
Point2 new_pos = p_edit_rect.pos + p_edit_rect.size*zero_offset;//p_edit_rect.pos - r.pos;
|
|
|
|
Matrix32 postxf;
|
|
postxf.set_rotation_and_scale(angle,scale);
|
|
new_pos = postxf.xform(new_pos);
|
|
|
|
pos+=new_pos;
|
|
scale*=new_scale;
|
|
|
|
_update_transform();
|
|
_change_notify("transform/scale");
|
|
_change_notify("transform/pos");
|
|
|
|
}
|
|
|
|
|
|
void Node2D::edit_rotate(float p_rot) {
|
|
|
|
angle+=p_rot;
|
|
_update_transform();
|
|
_change_notify("transform/rot");
|
|
}
|
|
|
|
|
|
void Node2D::_update_xform_values() {
|
|
|
|
pos=_mat.elements[2];
|
|
angle=_mat.get_rotation();
|
|
scale=_mat.get_scale();
|
|
_xform_dirty=false;
|
|
}
|
|
|
|
void Node2D::_update_transform() {
|
|
|
|
Matrix32 mat(angle,pos);
|
|
_mat.set_rotation_and_scale(angle,scale);
|
|
_mat.elements[2]=pos;
|
|
|
|
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat);
|
|
|
|
if (!is_inside_scene())
|
|
return;
|
|
|
|
|
|
_notify_transform();
|
|
}
|
|
|
|
void Node2D::set_pos(const Point2& p_pos) {
|
|
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
pos=p_pos;
|
|
_update_transform();
|
|
_change_notify("transform/pos");
|
|
|
|
|
|
}
|
|
|
|
void Node2D::set_rot(float p_angle) {
|
|
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
angle=p_angle;
|
|
_update_transform();
|
|
_change_notify("transform/rot");
|
|
}
|
|
|
|
void Node2D::set_scale(const Size2& p_scale) {
|
|
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
scale=p_scale;
|
|
_update_transform();
|
|
_change_notify("transform/scale");
|
|
|
|
}
|
|
|
|
Point2 Node2D::get_pos() const {
|
|
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
return pos;
|
|
}
|
|
float Node2D::get_rot() const {
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
|
|
return angle;
|
|
}
|
|
Size2 Node2D::get_scale() const {
|
|
if (_xform_dirty)
|
|
((Node2D*)this)->_update_xform_values();
|
|
|
|
return scale;
|
|
}
|
|
|
|
void Node2D::_set_rotd(float p_angle) {
|
|
|
|
set_rot(Math::deg2rad(p_angle));
|
|
}
|
|
|
|
float Node2D::_get_rotd() const {
|
|
|
|
return Math::rad2deg(get_rot());
|
|
}
|
|
|
|
|
|
void Node2D::_notification(int p_what) {
|
|
|
|
switch(p_what) {
|
|
|
|
}
|
|
}
|
|
|
|
Matrix32 Node2D::get_transform() const {
|
|
|
|
return _mat;
|
|
}
|
|
|
|
Rect2 Node2D::get_item_rect() const {
|
|
|
|
if (get_script_instance()) {
|
|
Variant::CallError err;
|
|
Rect2 r = get_script_instance()->call("_get_item_rect",NULL,0,err);
|
|
if (err.error==Variant::CallError::CALL_OK)
|
|
return r;
|
|
}
|
|
return Rect2(Point2(-32,-32),Size2(64,64));
|
|
}
|
|
|
|
Point2 Node2D::get_global_pos() const {
|
|
|
|
return get_global_transform().get_origin();
|
|
}
|
|
|
|
void Node2D::set_transform(const Matrix32& p_transform) {
|
|
|
|
_mat=p_transform;
|
|
_xform_dirty=true;
|
|
|
|
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat);
|
|
|
|
if (!is_inside_scene())
|
|
return;
|
|
|
|
_notify_transform();
|
|
}
|
|
|
|
void Node2D::set_global_transform(const Matrix32& p_transform) {
|
|
|
|
CanvasItem *pi = get_parent_item();
|
|
if (pi)
|
|
set_transform( pi->get_global_transform().affine_inverse() * p_transform);
|
|
else
|
|
set_transform(p_transform);
|
|
|
|
|
|
}
|
|
|
|
void Node2D::_bind_methods() {
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_rotd"),&Node2D::_get_rotd);
|
|
ObjectTypeDB::bind_method(_MD("_set_rotd"),&Node2D::_set_rotd);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_pos","pos"),&Node2D::set_pos);
|
|
ObjectTypeDB::bind_method(_MD("set_rot","rot"),&Node2D::set_rot);
|
|
ObjectTypeDB::bind_method(_MD("set_scale","scale"),&Node2D::set_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_pos"),&Node2D::get_pos);
|
|
ObjectTypeDB::bind_method(_MD("get_rot"),&Node2D::get_rot);
|
|
ObjectTypeDB::bind_method(_MD("get_scale"),&Node2D::get_scale);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_transform","xform"),&Node2D::set_transform);
|
|
ObjectTypeDB::bind_method(_MD("set_global_transform","xform"),&Node2D::set_global_transform);
|
|
|
|
ObjectTypeDB::bind_method(_MD("edit_set_pivot"),&Node2D::edit_set_pivot);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2,"transform/pos"),_SCS("set_pos"),_SCS("get_pos"));
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL,"transform/rot",PROPERTY_HINT_RANGE,"-1440,1440,0.1"),_SCS("_set_rotd"),_SCS("_get_rotd"));
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2,"transform/scale"),_SCS("set_scale"),_SCS("get_scale"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
Node2D::Node2D() {
|
|
|
|
|
|
angle=0;
|
|
scale=Vector2(1,1);
|
|
_xform_dirty=false;
|
|
|
|
}
|
|
|