diff options
Diffstat (limited to 'scene/2d/polygon_2d.cpp')
-rw-r--r-- | scene/2d/polygon_2d.cpp | 300 |
1 files changed, 140 insertions, 160 deletions
diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index ec6729844b..1d54fd1538 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -30,29 +30,25 @@ Rect2 Polygon2D::get_item_rect() const { - - if (rect_cache_dirty){ - int l =polygon.size(); + if (rect_cache_dirty) { + int l = polygon.size(); PoolVector<Vector2>::Read r = polygon.read(); - item_rect=Rect2(); - for(int i=0;i<l;i++) { + item_rect = Rect2(); + for (int i = 0; i < l; i++) { Vector2 pos = r[i] + offset; - if (i==0) - item_rect.pos=pos; + if (i == 0) + item_rect.pos = pos; else item_rect.expand_to(pos); } - item_rect=item_rect.grow(20); - rect_cache_dirty=false; + item_rect = item_rect.grow(20); + rect_cache_dirty = false; } - return item_rect; - - } -void Polygon2D::edit_set_pivot(const Point2& p_pivot) { +void Polygon2D::edit_set_pivot(const Point2 &p_pivot) { set_offset(p_pivot); } @@ -68,12 +64,11 @@ bool Polygon2D::edit_has_pivot() const { void Polygon2D::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_DRAW: { - if (polygon.size()<3) + if (polygon.size() < 3) return; Vector<Vector2> points; @@ -84,162 +79,155 @@ void Polygon2D::_notification(int p_what) { int len = points.size(); { - PoolVector<Vector2>::Read polyr =polygon.read(); - for(int i=0;i<len;i++) { - points[i]=polyr[i]+offset; + PoolVector<Vector2>::Read polyr = polygon.read(); + for (int i = 0; i < len; i++) { + points[i] = polyr[i] + offset; } } if (invert) { Rect2 bounds; - int highest_idx=-1; - float highest_y=-1e20; - float sum=0; + int highest_idx = -1; + float highest_y = -1e20; + float sum = 0; - for(int i=0;i<len;i++) { - if (i==0) - bounds.pos=points[i]; + for (int i = 0; i < len; i++) { + if (i == 0) + bounds.pos = points[i]; else bounds.expand_to(points[i]); - if (points[i].y>highest_y) { - highest_idx=i; - highest_y=points[i].y; + if (points[i].y > highest_y) { + highest_idx = i; + highest_y = points[i].y; } - int ni=(i+1)%len; - sum+=(points[ni].x-points[i].x)*(points[ni].y+points[i].y); + int ni = (i + 1) % len; + sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y); } - bounds=bounds.grow(invert_border); + bounds = bounds.grow(invert_border); - Vector2 ep[7]={ - Vector2(points[highest_idx].x,points[highest_idx].y+invert_border), - Vector2(bounds.pos+bounds.size), - Vector2(bounds.pos+Vector2(bounds.size.x,0)), + Vector2 ep[7] = { + Vector2(points[highest_idx].x, points[highest_idx].y + invert_border), + Vector2(bounds.pos + bounds.size), + Vector2(bounds.pos + Vector2(bounds.size.x, 0)), Vector2(bounds.pos), - Vector2(bounds.pos+Vector2(0,bounds.size.y)), - Vector2(points[highest_idx].x-CMP_EPSILON,points[highest_idx].y+invert_border), - Vector2(points[highest_idx].x-CMP_EPSILON,points[highest_idx].y), + Vector2(bounds.pos + Vector2(0, bounds.size.y)), + Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border), + Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y), }; - - if (sum>0) { - SWAP(ep[1],ep[4]); - SWAP(ep[2],ep[3]); - SWAP(ep[5],ep[0]); - SWAP(ep[6],points[highest_idx]); + if (sum > 0) { + SWAP(ep[1], ep[4]); + SWAP(ep[2], ep[3]); + SWAP(ep[5], ep[0]); + SWAP(ep[6], points[highest_idx]); } - points.resize(points.size()+7); - for(int i=points.size()-1;i>=highest_idx+7;i--) { + points.resize(points.size() + 7); + for (int i = points.size() - 1; i >= highest_idx + 7; i--) { - points[i]=points[i-7]; + points[i] = points[i - 7]; } - for(int i=0;i<7;i++) { + for (int i = 0; i < 7; i++) { - points[highest_idx+i+1]=ep[i]; + points[highest_idx + i + 1] = ep[i]; } - - len=points.size(); - + len = points.size(); } if (texture.is_valid()) { - Transform2D texmat(tex_rot,tex_ofs); + Transform2D texmat(tex_rot, tex_ofs); texmat.scale(tex_scale); - Size2 tex_size=Vector2(1,1); + Size2 tex_size = Vector2(1, 1); - tex_size=texture->get_size(); + tex_size = texture->get_size(); uvs.resize(points.size()); - if (points.size()==uv.size()) { + if (points.size() == uv.size()) { PoolVector<Vector2>::Read uvr = uv.read(); - for(int i=0;i<len;i++) { - uvs[i]=texmat.xform(uvr[i])/tex_size; + for (int i = 0; i < len; i++) { + uvs[i] = texmat.xform(uvr[i]) / tex_size; } } else { - for(int i=0;i<len;i++) { - uvs[i]=texmat.xform(points[i])/tex_size; + for (int i = 0; i < len; i++) { + uvs[i] = texmat.xform(points[i]) / tex_size; } } - } - Vector<Color> colors; - int color_len=vertex_colors.size(); + int color_len = vertex_colors.size(); colors.resize(len); { - PoolVector<Color>::Read color_r=vertex_colors.read(); - for(int i=0;i<color_len && i<len;i++){ - colors[i]=color_r[i]; + PoolVector<Color>::Read color_r = vertex_colors.read(); + for (int i = 0; i < color_len && i < len; i++) { + colors[i] = color_r[i]; } - for(int i=color_len;i<len;i++){ - colors[i]=color; + for (int i = color_len; i < len; i++) { + colors[i] = color; } } Vector<int> indices = Geometry::triangulate_polygon(points); - VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(),indices,points,colors,uvs,texture.is_valid()?texture->get_rid():RID()); + VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID()); } break; } } - -void Polygon2D::set_polygon(const PoolVector<Vector2>& p_polygon) { - polygon=p_polygon; - rect_cache_dirty=true; +void Polygon2D::set_polygon(const PoolVector<Vector2> &p_polygon) { + polygon = p_polygon; + rect_cache_dirty = true; update(); } -PoolVector<Vector2> Polygon2D::get_polygon() const{ +PoolVector<Vector2> Polygon2D::get_polygon() const { return polygon; } +void Polygon2D::set_uv(const PoolVector<Vector2> &p_uv) { -void Polygon2D::set_uv(const PoolVector<Vector2>& p_uv) { - - uv=p_uv; + uv = p_uv; update(); } -PoolVector<Vector2> Polygon2D::get_uv() const{ +PoolVector<Vector2> Polygon2D::get_uv() const { return uv; } -void Polygon2D::set_color(const Color& p_color){ +void Polygon2D::set_color(const Color &p_color) { - color=p_color; + color = p_color; update(); } -Color Polygon2D::get_color() const{ +Color Polygon2D::get_color() const { return color; } -void Polygon2D::set_vertex_colors(const PoolVector<Color>& p_colors){ +void Polygon2D::set_vertex_colors(const PoolVector<Color> &p_colors) { - vertex_colors=p_colors; + vertex_colors = p_colors; update(); } -PoolVector<Color> Polygon2D::get_vertex_colors() const{ +PoolVector<Color> Polygon2D::get_vertex_colors() const { return vertex_colors; } -void Polygon2D::set_texture(const Ref<Texture>& p_texture){ +void Polygon2D::set_texture(const Ref<Texture> &p_texture) { - texture=p_texture; + texture = p_texture; /*if (texture.is_valid()) { uint32_t flags=texture->get_flags(); @@ -251,77 +239,74 @@ void Polygon2D::set_texture(const Ref<Texture>& p_texture){ }*/ update(); } -Ref<Texture> Polygon2D::get_texture() const{ +Ref<Texture> Polygon2D::get_texture() const { return texture; } +void Polygon2D::set_texture_offset(const Vector2 &p_offset) { -void Polygon2D::set_texture_offset(const Vector2& p_offset){ - - tex_ofs=p_offset; + tex_ofs = p_offset; update(); } -Vector2 Polygon2D::get_texture_offset() const{ +Vector2 Polygon2D::get_texture_offset() const { return tex_ofs; } -void Polygon2D::set_texture_rotation(float p_rot){ +void Polygon2D::set_texture_rotation(float p_rot) { - tex_rot=p_rot; + tex_rot = p_rot; update(); } -float Polygon2D::get_texture_rotation() const{ +float Polygon2D::get_texture_rotation() const { return tex_rot; } - -void Polygon2D::_set_texture_rotationd(float p_rot){ +void Polygon2D::_set_texture_rotationd(float p_rot) { set_texture_rotation(Math::deg2rad(p_rot)); } -float Polygon2D::_get_texture_rotationd() const{ +float Polygon2D::_get_texture_rotationd() const { return Math::rad2deg(get_texture_rotation()); } +void Polygon2D::set_texture_scale(const Size2 &p_scale) { -void Polygon2D::set_texture_scale(const Size2& p_scale){ - - tex_scale=p_scale; + tex_scale = p_scale; update(); } -Size2 Polygon2D::get_texture_scale() const{ +Size2 Polygon2D::get_texture_scale() const { return tex_scale; } -void Polygon2D::set_invert(bool p_invert){ +void Polygon2D::set_invert(bool p_invert) { - invert=p_invert; + invert = p_invert; update(); } -bool Polygon2D::get_invert() const{ +bool Polygon2D::get_invert() const { return invert; } -void Polygon2D::set_invert_border(float p_invert_border){ +void Polygon2D::set_invert_border(float p_invert_border) { - invert_border=p_invert_border; + invert_border = p_invert_border; update(); } -float Polygon2D::get_invert_border() const{ +float Polygon2D::get_invert_border() const { return invert_border; } -void Polygon2D::set_offset(const Vector2& p_offset) { +void Polygon2D::set_offset(const Vector2 &p_offset) { - offset=p_offset; - rect_cache_dirty=true; + offset = p_offset; + rect_cache_dirty = true; update(); } @@ -330,73 +315,68 @@ Vector2 Polygon2D::get_offset() const { return offset; } - void Polygon2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_polygon","polygon"),&Polygon2D::set_polygon); - ClassDB::bind_method(D_METHOD("get_polygon"),&Polygon2D::get_polygon); - - ClassDB::bind_method(D_METHOD("set_uv","uv"),&Polygon2D::set_uv); - ClassDB::bind_method(D_METHOD("get_uv"),&Polygon2D::get_uv); - - ClassDB::bind_method(D_METHOD("set_color","color"),&Polygon2D::set_color); - ClassDB::bind_method(D_METHOD("get_color"),&Polygon2D::get_color); - - ClassDB::bind_method(D_METHOD("set_vertex_colors","vertex_colors"),&Polygon2D::set_vertex_colors); - ClassDB::bind_method(D_METHOD("get_vertex_colors"),&Polygon2D::get_vertex_colors); - - ClassDB::bind_method(D_METHOD("set_texture","texture"),&Polygon2D::set_texture); - ClassDB::bind_method(D_METHOD("get_texture"),&Polygon2D::get_texture); + ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon); + ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon); - ClassDB::bind_method(D_METHOD("set_texture_offset","texture_offset"),&Polygon2D::set_texture_offset); - ClassDB::bind_method(D_METHOD("get_texture_offset"),&Polygon2D::get_texture_offset); + ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv); + ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv); - ClassDB::bind_method(D_METHOD("set_texture_rotation","texture_rotation"),&Polygon2D::set_texture_rotation); - ClassDB::bind_method(D_METHOD("get_texture_rotation"),&Polygon2D::get_texture_rotation); + ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color); + ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color); - ClassDB::bind_method(D_METHOD("_set_texture_rotationd","texture_rotation"),&Polygon2D::_set_texture_rotationd); - ClassDB::bind_method(D_METHOD("_get_texture_rotationd"),&Polygon2D::_get_texture_rotationd); + ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors); + ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors); - ClassDB::bind_method(D_METHOD("set_texture_scale","texture_scale"),&Polygon2D::set_texture_scale); - ClassDB::bind_method(D_METHOD("get_texture_scale"),&Polygon2D::get_texture_scale); + ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture); + ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture); + ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset); + ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset); - ClassDB::bind_method(D_METHOD("set_invert","invert"),&Polygon2D::set_invert); - ClassDB::bind_method(D_METHOD("get_invert"),&Polygon2D::get_invert); + ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation); + ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation); - ClassDB::bind_method(D_METHOD("set_invert_border","invert_border"),&Polygon2D::set_invert_border); - ClassDB::bind_method(D_METHOD("get_invert_border"),&Polygon2D::get_invert_border); + ClassDB::bind_method(D_METHOD("_set_texture_rotationd", "texture_rotation"), &Polygon2D::_set_texture_rotationd); + ClassDB::bind_method(D_METHOD("_get_texture_rotationd"), &Polygon2D::_get_texture_rotationd); - ClassDB::bind_method(D_METHOD("set_offset","offset"),&Polygon2D::set_offset); - ClassDB::bind_method(D_METHOD("get_offset"),&Polygon2D::get_offset); + ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale); + ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale); + ClassDB::bind_method(D_METHOD("set_invert", "invert"), &Polygon2D::set_invert); + ClassDB::bind_method(D_METHOD("get_invert"), &Polygon2D::get_invert); + ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border); + ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border); - ADD_PROPERTY( PropertyInfo(Variant::POOL_VECTOR2_ARRAY,"polygon"),"set_polygon","get_polygon"); - ADD_PROPERTY( PropertyInfo(Variant::POOL_VECTOR2_ARRAY,"uv"),"set_uv","get_uv"); - ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),"set_color","get_color"); - ADD_PROPERTY( PropertyInfo(Variant::POOL_COLOR_ARRAY,"vertex_colors"),"set_vertex_colors","get_vertex_colors"); - ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"offset"),"set_offset","get_offset"); - ADD_GROUP("Texture",""); - ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),"set_texture","get_texture"); - ADD_GROUP("Texture","texture_"); - ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"texture_offset"),"set_texture_offset","get_texture_offset"); - ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"texture_scale"),"set_texture_scale","get_texture_scale"); - ADD_PROPERTY( PropertyInfo(Variant::REAL,"texture_rotation",PROPERTY_HINT_RANGE,"-1440,1440,0.1"),"_set_texture_rotationd","_get_texture_rotationd"); + ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset); + ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset); - ADD_GROUP("Invert","invert_"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"invert_enable"),"set_invert","get_invert"); - ADD_PROPERTY( PropertyInfo(Variant::REAL,"invert_border",PROPERTY_HINT_RANGE,"0.1,16384,0.1"),"set_invert_border","get_invert_border"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); + ADD_GROUP("Texture", ""); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + ADD_GROUP("Texture", "texture_"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset"), "set_texture_offset", "get_texture_offset"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale"), "set_texture_scale", "get_texture_scale"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_rotation", PROPERTY_HINT_RANGE, "-1440,1440,0.1"), "_set_texture_rotationd", "_get_texture_rotationd"); + ADD_GROUP("Invert", "invert_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enable"), "set_invert", "get_invert"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1"), "set_invert_border", "get_invert_border"); } Polygon2D::Polygon2D() { - invert=0; - invert_border=100; - tex_rot=0; - tex_tile=true; - tex_scale=Vector2(1,1); - color=Color(1,1,1); - rect_cache_dirty=true; + invert = 0; + invert_border = 100; + tex_rot = 0; + tex_tile = true; + tex_scale = Vector2(1, 1); + color = Color(1, 1, 1); + rect_cache_dirty = true; } |