Rect2 LineShape2D::get_rect() const { Vector2 point = get_d() * get_normal(); Vector2 l1[2] = { point - get_normal().tangent() * 100, point + get_normal().tangent() * 100 }; Vector2 l2[2] = { point, point + get_normal() * 30 }; Rect2 rect; rect.pos = l1[0]; rect.expand_to(l1[1]); rect.expand_to(l2[0]); rect.expand_to(l2[1]); return rect; }
void GraphEdit::_draw_cos_line(const Vector2& p_from, const Vector2& p_to,const Color& p_color) { static const int steps = 20; Rect2 r; r.pos=p_from; r.expand_to(p_to); Vector2 sign=Vector2((p_from.x < p_to.x) ? 1 : -1,(p_from.y < p_to.y) ? 1 : -1); bool flip = sign.x * sign.y < 0; Vector2 prev; for(int i=0;i<=steps;i++) { float d = i/float(steps); float c=-Math::cos(d*Math_PI) * 0.5+0.5; if (flip) c=1.0-c; Vector2 p = r.pos+Vector2(d*r.size.width,c*r.size.height); if (i>0) { top_layer->draw_line(prev,p,p_color,2); } prev=p; } }
void ConvexPolygonShape2DSW::set_data(const Variant& p_data) { ERR_FAIL_COND(p_data.get_type()!=Variant::VECTOR2_ARRAY && p_data.get_type()!=Variant::REAL_ARRAY); if (points) memdelete_arr(points); points=NULL; point_count=0; if (p_data.get_type()==Variant::VECTOR2_ARRAY) { DVector<Vector2> arr=p_data; ERR_FAIL_COND(arr.size()==0); point_count=arr.size(); points = memnew_arr(Point,point_count); DVector<Vector2>::Read r = arr.read(); for(int i=0;i<point_count;i++) { points[i].pos=r[i]; } for(int i=0;i<point_count;i++) { Vector2 p = points[i].pos; Vector2 pn = points[(i+1)%point_count].pos; points[i].normal=(pn-p).tangent().normalized(); } } else { DVector<real_t> dvr = p_data; point_count=dvr.size()/4; ERR_FAIL_COND(point_count==0); points = memnew_arr(Point,point_count); DVector<real_t>::Read r = dvr.read(); for(int i=0;i<point_count;i++) { int idx=i<<2; points[i].pos.x=r[idx+0]; points[i].pos.y=r[idx+1]; points[i].normal.x=r[idx+2]; points[i].normal.y=r[idx+3]; } } ERR_FAIL_COND(point_count==0); Rect2 aabb; aabb.pos=points[0].pos; for(int i=1;i<point_count;i++) aabb.expand_to(points[i].pos); configure(aabb); }
real_t ConvexPolygonShape2DSW::get_moment_of_inertia(float p_mass) const { Rect2 aabb; aabb.pos=points[0].pos; for(int i=0;i<point_count;i++) { aabb.expand_to(points[i].pos); } return p_mass*aabb.size.dot(aabb.size)/12.0f; }
real_t ConvexPolygonShape2DSW::get_moment_of_inertia(float p_mass,const Vector2& p_scale) const { Rect2 aabb; aabb.pos=points[0].pos*p_scale; for(int i=0;i<point_count;i++) { aabb.expand_to(points[i].pos*p_scale); } return p_mass*aabb.size.dot(aabb.size)/12.0f + p_mass * (aabb.pos+aabb.size*0.5).length_squared(); }
Rect2 Line2D::_edit_get_rect() const { if (_points.size() == 0) return Rect2(0, 0, 0, 0); Vector2 d = Vector2(_width, _width); Rect2 aabb = Rect2(_points[0] - d, 2 * d); for (int i = 1; i < _points.size(); i++) { aabb.expand_to(_points[i] - d); aabb.expand_to(_points[i] + d); } return aabb; }
Rect2 ConvexPolygonShape2D::get_rect() const { Rect2 rect; for (int i = 0; i < points.size(); i++) { if (i == 0) rect.position = points[i]; else rect.expand_to(points[i]); } return rect; }
void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color) { #if 1 //cubic bezier code float diff = p_to.x - p_from.x; float cp_offset; int cp_len = get_constant("bezier_len_pos"); int cp_neg_len = get_constant("bezier_len_neg"); if (diff > 0) { cp_offset = MAX(cp_len, diff * 0.5); } else { cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5); } Vector2 c1 = Vector2(cp_offset * zoom, 0); Vector2 c2 = Vector2(-cp_offset * zoom, 0); int lines = 0; _bake_segment2d(p_where, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 8, p_color, p_to_color, lines); #else static const int steps = 20; //old cosine code Rect2 r; r.pos = p_from; r.expand_to(p_to); Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1); bool flip = sign.x * sign.y < 0; Vector2 prev; for (int i = 0; i <= steps; i++) { float d = i / float(steps); float c = -Math::cos(d * Math_PI) * 0.5 + 0.5; if (flip) c = 1.0 - c; Vector2 p = r.pos + Vector2(d * r.size.width, c * r.size.height); if (i > 0) { p_where->draw_line(prev, p, p_color.linear_interpolate(p_to_color, d), 2); } prev = p; } #endif }
void TileMap::_recompute_rect_cache() { #ifdef DEBUG_ENABLED if (!rect_cache_dirty) return; Rect2 r_total; for (Map<PosKey,Quadrant>::Element *E=quadrant_map.front();E;E=E->next()) { Rect2 r; r.pos=_map_to_world(E->key().x*_get_quadrant_size(), E->key().y*_get_quadrant_size()); r.expand_to( _map_to_world(E->key().x*_get_quadrant_size()+_get_quadrant_size(), E->key().y*_get_quadrant_size()) ); r.expand_to( _map_to_world(E->key().x*_get_quadrant_size()+_get_quadrant_size(), E->key().y*_get_quadrant_size()+_get_quadrant_size()) ); r.expand_to( _map_to_world(E->key().x*_get_quadrant_size(), E->key().y*_get_quadrant_size()+_get_quadrant_size()) ); if (E==quadrant_map.front()) r_total=r; else r_total=r_total.merge(r); } if (r_total==Rect2()) { rect_cache=Rect2(-10,-10,20,20); } else { rect_cache=r_total.grow(MAX(cell_size.x,cell_size.y)*_get_quadrant_size()); } item_rect_changed(); rect_cache_dirty=false; #endif }
void SegmentShape2DSW::set_data(const Variant& p_data) { ERR_FAIL_COND(p_data.get_type()!=Variant::RECT2); Rect2 r = p_data; a=r.pos; b=r.size; n=(b-a).tangent(); Rect2 aabb; aabb.pos=a; aabb.expand_to(b); if (aabb.size.x==0) aabb.size.x=0.001; if (aabb.size.y==0) aabb.size.y=0.001; configure(aabb); }
Rect2 ConcavePolygonShape2D::get_rect() const { PoolVector<Vector2> s = get_segments(); int len = s.size(); if (len == 0) return Rect2(); Rect2 rect; PoolVector<Vector2>::Read r = s.read(); for (int i = 0; i < len; i++) { if (i == 0) rect.position = r[i]; else rect.expand_to(r[i]); } return rect; }
Rect2 Path2D::_edit_get_rect() const { if (!curve.is_valid() || curve->get_point_count() == 0) return Rect2(0, 0, 0, 0); Rect2 aabb = Rect2(curve->get_point_position(0), Vector2(0, 0)); for (int i = 0; i < curve->get_point_count(); i++) { for (int j = 0; j <= 8; j++) { real_t frac = j / 8.0; Vector2 p = curve->interpolate(i, frac); aabb.expand_to(p); } } return aabb; }
void Polygon2D::_notification(int p_what) { switch(p_what) { case NOTIFICATION_DRAW: { if (polygon.size()<3) return; Vector<Vector2> points; Vector<Vector2> uvs; points.resize(polygon.size()); int len = points.size(); { DVector<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; 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; } int ni=(i+1)%len; sum+=(points[ni].x-points[i].x)*(points[ni].y+points[i].y); } 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(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), }; 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[i]=points[i-7]; } for(int i=0;i<7;i++) { points[highest_idx+i+1]=ep[i]; } len=points.size(); } if (texture.is_valid()) { Matrix32 texmat(tex_rot,tex_ofs); texmat.scale(tex_scale); Size2 tex_size=Vector2(1,1); tex_size=texture->get_size(); uvs.resize(points.size()); if (points.size()==uv.size()) { DVector<Vector2>::Read uvr = uv.read(); 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; } } } Vector<Color> colors; colors.push_back(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()); } break; } }
void TileMapEditor::_canvas_draw() { if (!node) return; Matrix32 cell_xf = node->get_cell_transform(); Matrix32 xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform(); Matrix32 xform_inv = xform.affine_inverse(); Size2 screen_size=canvas_item_editor->get_size(); { Rect2 aabb; aabb.pos=node->world_to_map(xform_inv.xform(Vector2())); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(0,screen_size.height)))); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(screen_size.width,0)))); aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size))); Rect2i si=aabb.grow(1.0); if (node->get_half_offset()!=TileMap::HALF_OFFSET_X) { int max_lines=2000; //avoid crash if size too smal for (int i=(si.pos.x)-1;i<=(si.pos.x+si.size.x);i++) { Vector2 from = xform.xform(node->map_to_world(Vector2(i,si.pos.y))); Vector2 to = xform.xform(node->map_to_world(Vector2(i,si.pos.y+si.size.y+1))); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); if (max_lines--==0) break; } } else { int max_lines=10000; //avoid crash if size too smal for (int i=(si.pos.x)-1;i<=(si.pos.x+si.size.x);i++) { for (int j=(si.pos.y)-1;j<=(si.pos.y+si.size.y);j++) { Vector2 ofs; if (ABS(j)&1) { ofs=cell_xf[0]*0.5; } Vector2 from = xform.xform(node->map_to_world(Vector2(i,j),true)+ofs); Vector2 to = xform.xform(node->map_to_world(Vector2(i,j+1),true)+ofs); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); if (max_lines--==0) break; } } } int max_lines=10000; //avoid crash if size too smal if (node->get_half_offset()!=TileMap::HALF_OFFSET_Y) { for (int i=(si.pos.y)-1;i<=(si.pos.y+si.size.y);i++) { Vector2 from = xform.xform(node->map_to_world(Vector2(si.pos.x,i))); Vector2 to = xform.xform(node->map_to_world(Vector2(si.pos.x+si.size.x+1,i))); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); if (max_lines--==0) break; } } else { for (int i=(si.pos.y)-1;i<=(si.pos.y+si.size.y);i++) { for (int j=(si.pos.x)-1;j<=(si.pos.x+si.size.x);j++) { Vector2 ofs; if (ABS(j)&1) { ofs=cell_xf[1]*0.5; } Vector2 from = xform.xform(node->map_to_world(Vector2(j,i),true)+ofs); Vector2 to = xform.xform(node->map_to_world(Vector2(j+1,i),true)+ofs); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); if (max_lines--==0) break; } } } } if (selection_active) { Vector<Vector2> points; points.push_back( xform.xform( node->map_to_world(( rectangle.pos ) ))); points.push_back( xform.xform( node->map_to_world((rectangle.pos+Point2(rectangle.size.x+1,0)) ) )); points.push_back( xform.xform( node->map_to_world((rectangle.pos+Point2(rectangle.size.x+1,rectangle.size.y+1)) ) )); points.push_back( xform.xform( node->map_to_world((rectangle.pos+Point2(0,rectangle.size.y+1)) ) )); canvas_item_editor->draw_colored_polygon(points, Color(0.2,0.8,1,0.4)); } if (mouse_over){ Vector2 endpoints[4]={ node->map_to_world(over_tile, true), node->map_to_world((over_tile+Point2(1,0)), true), node->map_to_world((over_tile+Point2(1,1)), true), node->map_to_world((over_tile+Point2(0,1)), true) }; for (int i=0;i<4;i++) { if (node->get_half_offset()==TileMap::HALF_OFFSET_X && ABS(over_tile.y)&1) endpoints[i]+=cell_xf[0]*0.5; if (node->get_half_offset()==TileMap::HALF_OFFSET_Y && ABS(over_tile.x)&1) endpoints[i]+=cell_xf[1]*0.5; endpoints[i]=xform.xform(endpoints[i]); } Color col; if (node->get_cell(over_tile.x,over_tile.y)!=TileMap::INVALID_CELL) col=Color(0.2,0.8,1.0,0.8); else col=Color(1.0,0.4,0.2,0.8); for (int i=0;i<4;i++) canvas_item_editor->draw_line(endpoints[i],endpoints[(i+1)%4],col,2); if (tool==TOOL_SELECTING || tool==TOOL_PICKING || tool==TOOL_BUCKET) { return; } if (tool==TOOL_LINE_PAINT) { if (paint_undo.empty()) return; int id = get_selected_tile(); if (id==TileMap::INVALID_CELL) return; for (Map<Point2i, CellOp>::Element *E=paint_undo.front();E;E=E->next()) { _draw_cell(id, E->key(), flip_h, flip_v, transpose, xform); } } else if (tool==TOOL_RECTANGLE_PAINT) { int id = get_selected_tile(); if (id==TileMap::INVALID_CELL) return; for (int i=rectangle.pos.y;i<=rectangle.pos.y+rectangle.size.y;i++) { for (int j=rectangle.pos.x;j<=rectangle.pos.x+rectangle.size.x;j++) { _draw_cell(id, Point2i(j, i), flip_h, flip_v, transpose, xform); } } } else if (tool==TOOL_DUPLICATING) { if (copydata.empty()) return; Ref<TileSet> ts = node->get_tileset(); if (ts.is_null()) return; Point2 ofs = over_tile-rectangle.pos; for (List<TileData>::Element *E=copydata.front();E;E=E->next()) { if (!ts->has_tile(E->get().cell)) continue; TileData tcd = E->get(); _draw_cell(tcd.cell, tcd.pos+ofs, tcd.flip_h, tcd.flip_v, tcd.transpose, xform); } Rect2i duplicate=rectangle; duplicate.pos=over_tile; Vector<Vector2> points; points.push_back( xform.xform( node->map_to_world(duplicate.pos ) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(duplicate.size.x+1,0)) ) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(duplicate.size.x+1,duplicate.size.y+1))) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(0,duplicate.size.y+1))) )); canvas_item_editor->draw_colored_polygon(points, Color(0.2,1.0,0.8,0.2)); } else { int st = get_selected_tile(); if (st==TileMap::INVALID_CELL) return; _draw_cell(st, over_tile, flip_h, flip_v, transpose, xform); } } }
void TileMapEditor::_canvas_draw() { if (!node) return; Size2 cell_size=node->get_cell_size(); Matrix32 cell_xf = node->get_cell_transform(); Matrix32 xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform(); Matrix32 xform_inv = xform.affine_inverse(); Size2 screen_size=canvas_item_editor->get_size(); { Rect2 aabb; aabb.pos=node->world_to_map(xform_inv.xform(Vector2())); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(0,screen_size.height)))); aabb.expand_to(node->world_to_map(xform_inv.xform(Vector2(screen_size.width,0)))); aabb.expand_to(node->world_to_map(xform_inv.xform(screen_size))); Rect2i si=aabb.grow(1.0); if (node->get_half_offset()!=TileMap::HALF_OFFSET_X) { for(int i=(si.pos.x)-1;i<=(si.pos.x+si.size.x);i++) { Vector2 from = xform.xform(node->map_to_world(Vector2(i,si.pos.y))); Vector2 to = xform.xform(node->map_to_world(Vector2(i,si.pos.y+si.size.y+1))); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); } } else { for(int i=(si.pos.x)-1;i<=(si.pos.x+si.size.x);i++) { for(int j=(si.pos.y)-1;j<=(si.pos.y+si.size.y);j++) { Vector2 ofs; if (ABS(j)&1) { ofs=cell_xf[0]*0.5; } Vector2 from = xform.xform(node->map_to_world(Vector2(i,j),true)+ofs); Vector2 to = xform.xform(node->map_to_world(Vector2(i,j+1),true)+ofs); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); } } } if (node->get_half_offset()!=TileMap::HALF_OFFSET_Y) { for(int i=(si.pos.y)-1;i<=(si.pos.y+si.size.y);i++) { Vector2 from = xform.xform(node->map_to_world(Vector2(si.pos.x,i))); Vector2 to = xform.xform(node->map_to_world(Vector2(si.pos.x+si.size.x+1,i))); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); } } else { for(int i=(si.pos.y)-1;i<=(si.pos.y+si.size.y);i++) { for(int j=(si.pos.x)-1;j<=(si.pos.x+si.size.x);j++) { Vector2 ofs; if (ABS(j)&1) { ofs=cell_xf[1]*0.5; } Vector2 from = xform.xform(node->map_to_world(Vector2(j,i),true)+ofs); Vector2 to = xform.xform(node->map_to_world(Vector2(j+1,i),true)+ofs); Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(from,to,col,1); } } } /* for(int i=(si.pos.y/cell_size.y)-1;i<=(si.pos.y+si.size.y)/cell_size.y;i++) { int ofs = i*cell_size.y; Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(xform.xform(Point2(si.pos.x,ofs)),xform.xform(Point2(si.pos.x+si.size.x,ofs)),col,1);*/ } if (selection_active) { Vector<Vector2> points; points.push_back( xform.xform( node->map_to_world(( selection.pos ) ))); points.push_back( xform.xform( node->map_to_world((selection.pos+Point2(selection.size.x+1,0)) ) )); points.push_back( xform.xform( node->map_to_world((selection.pos+Point2(selection.size.x+1,selection.size.y+1)) ) )); points.push_back( xform.xform( node->map_to_world((selection.pos+Point2(0,selection.size.y+1)) ) )); Color col=Color(0.2,0.8,1,0.4); canvas_item_editor->draw_colored_polygon(points,col); } if (mouse_over){ Vector2 endpoints[4]={ ( node->map_to_world(over_tile,true) ) , ( node->map_to_world((over_tile+Point2(1,0)),true ) ), ( node->map_to_world((over_tile+Point2(1,1)),true ) ), ( node->map_to_world((over_tile+Point2(0,1)),true ) ) }; for(int i=0;i<4;i++) { if (node->get_half_offset()==TileMap::HALF_OFFSET_X && ABS(over_tile.y)&1) endpoints[i]+=cell_xf[0]*0.5; if (node->get_half_offset()==TileMap::HALF_OFFSET_Y && ABS(over_tile.x)&1) endpoints[i]+=cell_xf[1]*0.5; endpoints[i]=xform.xform(endpoints[i]); } Color col; if (node->get_cell(over_tile.x,over_tile.y)!=TileMap::INVALID_CELL) col=Color(0.2,0.8,1.0,0.8); else col=Color(1.0,0.4,0.2,0.8); for(int i=0;i<4;i++) canvas_item_editor->draw_line(endpoints[i],endpoints[(i+1)%4],col,2); if (tool==TOOL_DUPLICATING) { Rect2i duplicate=selection; duplicate.pos=over_tile; Vector<Vector2> points; points.push_back( xform.xform( node->map_to_world(duplicate.pos ) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(duplicate.size.x+1,0)) ) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(duplicate.size.x+1,duplicate.size.y+1))) )); points.push_back( xform.xform( node->map_to_world((duplicate.pos+Point2(0,duplicate.size.y+1))) )); Color col=Color(0.2,1.0,0.8,0.4); canvas_item_editor->draw_colored_polygon(points,col); } else { Ref<TileSet> ts = node->get_tileset(); if (ts.is_valid()) { int st = get_selected_tile(); if (ts->has_tile(st)) { Ref<Texture> t = ts->tile_get_texture(st); if (t.is_valid()) { Vector2 from = node->map_to_world(over_tile)+node->get_cell_draw_offset(); Rect2 r = ts->tile_get_region(st); Size2 sc = xform.get_scale(); if (mirror_x->is_pressed()) sc.x*=-1.0; if (mirror_y->is_pressed()) sc.y*=-1.0; Rect2 rect; if (r==Rect2()) { rect=Rect2(from,t->get_size()); } else { rect=Rect2(from,r.get_size()); } if (node->get_tile_origin()==TileMap::TILE_ORIGIN_TOP_LEFT) { rect.pos+=ts->tile_get_texture_offset(st); } else if (node->get_tile_origin()==TileMap::TILE_ORIGIN_CENTER) { rect.pos+=node->get_cell_size()/2; Vector2 s = r.size; Vector2 center = (s/2) - ts->tile_get_texture_offset(st); if (mirror_x->is_pressed()) rect.pos.x-=s.x-center.x; else rect.pos.x-=center.x; if (mirror_y->is_pressed()) rect.pos.y-=s.y-center.y; else rect.pos.y-=center.y; } rect.pos=xform.xform(rect.pos); rect.size*=sc; if (r==Rect2()) { canvas_item_editor->draw_texture_rect(t,rect,false,Color(1,1,1,0.5),transpose->is_pressed()); } else { canvas_item_editor->draw_texture_rect_region(t,rect,r,Color(1,1,1,0.5),transpose->is_pressed()); } } } } } } }
void CollisionPolygonEditor::_polygon_draw() { if (!node) return; Vector<Vector2> poly; if (wip_active) poly=wip; else poly=node->get_polygon(); int len = poly.size(); float depth = node->get_depth()*0.5; imgeom->clear(); imgeom->set_material_override(line_material); imgeom->begin(Mesh::PRIMITIVE_LINES,Ref<Texture>()); Rect2 rect; for(int i=0;i<poly.size();i++) { Vector2 p,p2; p = i==edited_point ? edited_point_pos : poly[i]; if ((wip_active && i==poly.size()-1) || (((i+1)%poly.size())==edited_point)) p2=edited_point_pos; else p2 = poly[(i+1)%poly.size()]; if (i==0) rect.pos=p; else rect.expand_to(p); Vector3 point = Vector3(p.x,p.y,depth); Vector3 next_point = Vector3(p2.x,p2.y,depth); imgeom->set_color(Color(1,0.3,0.1,0.8)); imgeom->add_vertex(point); imgeom->set_color(Color(1,0.3,0.1,0.8)); imgeom->add_vertex(next_point); //Color col=Color(1,0.3,0.1,0.8); //vpc->draw_line(point,next_point,col,2); //vpc->draw_texture(handle,point-handle->get_size()*0.5); } rect=rect.grow(1); AABB r; r.pos.x=rect.pos.x; r.pos.y=rect.pos.y; r.pos.z=depth; r.size.x=rect.size.x; r.size.y=rect.size.y; r.size.z=0; imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0.3,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0.0,0.3,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(r.size.x,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(r.size.x,0,0)-Vector3(0.3,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(r.size.x,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(r.size.x,0,0)+Vector3(0,0.3,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0,r.size.y,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0,r.size.y,0)-Vector3(0,0.3,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0,r.size.y,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+Vector3(0,r.size.y,0)+Vector3(0.3,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+r.size); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+r.size-Vector3(0.3,0,0)); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+r.size); imgeom->set_color(Color(0.8,0.8,0.8,0.2)); imgeom->add_vertex(r.pos+r.size-Vector3(0.0,0.3,0)); imgeom->end(); while(m->get_surface_count()) { m->surface_remove(0); } if (poly.size()==0) return; Array a; a.resize(Mesh::ARRAY_MAX); DVector<Vector3> va; { va.resize(poly.size()); DVector<Vector3>::Write w=va.write(); for(int i=0;i<poly.size();i++) { Vector2 p,p2; p = i==edited_point ? edited_point_pos : poly[i]; Vector3 point = Vector3(p.x,p.y,depth); w[i]=point; } } a[Mesh::ARRAY_VERTEX]=va; m->add_surface(Mesh::PRIMITIVE_POINTS,a); m->surface_set_material(0,handle_material); }
void ConcavePolygonShape2DSW::set_data(const Variant& p_data) { ERR_FAIL_COND(p_data.get_type()!=Variant::VECTOR2_ARRAY && p_data.get_type()!=Variant::REAL_ARRAY); segments.clear();; points.clear();; bvh.clear();; bvh_depth=1; Rect2 aabb; if (p_data.get_type()==Variant::VECTOR2_ARRAY) { DVector<Vector2> p2arr = p_data; int len = p2arr.size(); DVector<Vector2>::Read arr = p2arr.read(); Map<Point2,int> pointmap; for(int i=0;i<len;i+=2) { Point2 p1 =arr[i]; Point2 p2 =arr[i+1]; int idx_p1,idx_p2; if (p1==p2) continue; //don't want it if (pointmap.has(p1)) { idx_p1=pointmap[p1]; } else { idx_p1=pointmap.size(); pointmap[p1]=idx_p1; } if (pointmap.has(p2)) { idx_p2=pointmap[p2]; } else { idx_p2=pointmap.size(); pointmap[p2]=idx_p2; } Segment s; s.points[0]=idx_p1; s.points[1]=idx_p2; segments.push_back(s); } points.resize(pointmap.size()); aabb.pos=pointmap.front()->key(); for(Map<Point2,int>::Element *E=pointmap.front();E;E=E->next()) { aabb.expand_to(E->key()); points[E->get()]=E->key(); } Vector<BVH> main_vbh; main_vbh.resize(segments.size()); for(int i=0;i<main_vbh.size();i++) { main_vbh[i].aabb.pos=points[segments[i].points[0]]; main_vbh[i].aabb.expand_to(points[segments[i].points[1]]); main_vbh[i].left=-1; main_vbh[i].right=i; } _generate_bvh(&main_vbh[0],main_vbh.size(),1); } else { //dictionary with arrays } configure(aabb); }
void Polygon2D::_notification(int p_what) { switch (p_what) { case NOTIFICATION_DRAW: { if (polygon.size() < 3) return; Skeleton2D *skeleton_node = NULL; if (has_node(skeleton)) { skeleton_node = Object::cast_to<Skeleton2D>(get_node(skeleton)); } ObjectID new_skeleton_id = 0; if (skeleton_node) { VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton()); new_skeleton_id = skeleton_node->get_instance_id(); } else { VS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID()); } if (new_skeleton_id != current_skeleton_id) { Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id); if (old_skeleton) { old_skeleton->disconnect("bone_setup_changed", this, "_skeleton_bone_setup_changed"); } if (skeleton_node) { skeleton_node->connect("bone_setup_changed", this, "_skeleton_bone_setup_changed"); } current_skeleton_id = new_skeleton_id; } Vector<Vector2> points; Vector<Vector2> uvs; Vector<int> bones; Vector<float> weights; int len = polygon.size(); if ((invert || polygons.size() == 0) && internal_vertices > 0) { //if no polygons are around, internal vertices must not be drawn, else let them be len -= internal_vertices; } if (len <= 0) { return; } points.resize(len); { PoolVector<Vector2>::Read polyr = polygon.read(); for (int i = 0; i < len; i++) { points.write[i] = polyr[i] + offset; } } if (invert) { Rect2 bounds; int highest_idx = -1; float highest_y = -1e20; float sum = 0; for (int i = 0; i < len; i++) { if (i == 0) bounds.position = points[i]; else bounds.expand_to(points[i]); 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); } bounds = bounds.grow(invert_border); Vector2 ep[7] = { Vector2(points[highest_idx].x, points[highest_idx].y + invert_border), Vector2(bounds.position + bounds.size), Vector2(bounds.position + Vector2(bounds.size.x, 0)), Vector2(bounds.position), Vector2(bounds.position + 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.write[highest_idx]); } points.resize(points.size() + 7); for (int i = points.size() - 1; i >= highest_idx + 7; i--) { points.write[i] = points[i - 7]; } for (int i = 0; i < 7; i++) { points.write[highest_idx + i + 1] = ep[i]; } len = points.size(); } if (texture.is_valid()) { Transform2D texmat(tex_rot, tex_ofs); texmat.scale(tex_scale); Size2 tex_size = texture->get_size(); uvs.resize(len); if (points.size() == uv.size()) { PoolVector<Vector2>::Read uvr = uv.read(); for (int i = 0; i < len; i++) { uvs.write[i] = texmat.xform(uvr[i]) / tex_size; } } else { for (int i = 0; i < len; i++) { uvs.write[i] = texmat.xform(points[i]) / tex_size; } } } if (skeleton_node && !invert && bone_weights.size()) { //a skeleton is set! fill indices and weights int vc = len; bones.resize(vc * 4); weights.resize(vc * 4); int *bonesw = bones.ptrw(); float *weightsw = weights.ptrw(); for (int i = 0; i < vc * 4; i++) { bonesw[i] = 0; weightsw[i] = 0; } for (int i = 0; i < bone_weights.size(); i++) { if (bone_weights[i].weights.size() != points.size()) { continue; //different number of vertices, sorry not using. } if (!skeleton_node->has_node(bone_weights[i].path)) { continue; //node does not exist } Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path)); if (!bone) { continue; } int bone_index = bone->get_index_in_skeleton(); PoolVector<float>::Read r = bone_weights[i].weights.read(); for (int j = 0; j < vc; j++) { if (r[j] == 0.0) continue; //weight is unpainted, skip //find an index with a weight for (int k = 0; k < 4; k++) { if (weightsw[j * 4 + k] < r[j]) { //this is less than this weight, insert weight! for (int l = 3; l > k; l--) { weightsw[j * 4 + l] = weightsw[j * 4 + l - 1]; bonesw[j * 4 + l] = bonesw[j * 4 + l - 1]; } weightsw[j * 4 + k] = r[j]; bonesw[j * 4 + k] = bone_index; break; } } } } //normalize the weights for (int i = 0; i < vc; i++) { float tw = 0; for (int j = 0; j < 4; j++) { tw += weightsw[i * 4 + j]; } if (tw == 0) continue; //unpainted, do nothing //normalize for (int j = 0; j < 4; j++) { weightsw[i * 4 + j] /= tw; } } } Vector<Color> colors; if (vertex_colors.size() == points.size()) { colors.resize(len); PoolVector<Color>::Read color_r = vertex_colors.read(); for (int i = 0; i < len; i++) { colors.write[i] = color_r[i]; } } else { colors.push_back(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()); if (invert || polygons.size() == 0) { Vector<int> indices = Geometry::triangulate_polygon(points); VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); } else { //draw individual polygons Vector<int> total_indices; for (int i = 0; i < polygons.size(); i++) { PoolVector<int> src_indices = polygons[i]; int ic = src_indices.size(); if (ic < 3) continue; PoolVector<int>::Read r = src_indices.read(); Vector<Vector2> tmp_points; tmp_points.resize(ic); for (int j = 0; j < ic; j++) { int idx = r[j]; ERR_CONTINUE(idx < 0 || idx >= points.size()); tmp_points.write[j] = points[r[j]]; } Vector<int> indices = Geometry::triangulate_polygon(tmp_points); int ic2 = indices.size(); const int *r2 = indices.ptr(); int bic = total_indices.size(); total_indices.resize(bic + ic2); int *w2 = total_indices.ptrw(); for (int j = 0; j < ic2; j++) { w2[j + bic] = r[r2[j]]; } } if (total_indices.size()) { VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), total_indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); } #if 0 //use splits Vector<int> loop; int sc = splits.size(); PoolVector<int>::Read r = splits.read(); print_line("has splits, amount " + itos(splits.size())); Vector<Vector<int> > loops; // find a point that can be used to begin, must not be in a split, and have to the left and right the same one // like this one -> x---o // \ / \ . // o---o int base_point = -1; { int current_point = -1; int base_point_prev_split = -1; for (int i = 0; i < points.size(); i++) { //find if this point is in a split int split_index = -1; bool has_prev_split = false; int min_dist_to_end = 0x7FFFFFFF; for (int j = 0; j < sc; j += 2) { int split_pos = -1; int split_end = -1; if (r[j + 0] == i) { //found split in first point split_pos = r[j + 0]; split_end = r[j + 1]; } else if (r[j + 1] == i) { //found split in second point split_pos = r[j + 1]; split_end = r[j + 0]; } if (split_pos == split_end) { continue; //either nothing found or begin == end, this not a split in either case } if (j == base_point_prev_split) { has_prev_split = true; } //compute distance from split pos to split end in current traversal direction int dist_to_end = split_end > split_pos ? split_end - split_pos : (last - split_pos + split_end); if (dist_to_end < min_dist_to_end) { //always keep the valid split with the least distance to the loop min_dist_to_end = dist_to_end; split_index = j; } } if (split_index == -1) { current_point = i; //no split here, we are testing this point } else if (has_prev_split) { base_point = current_point; // there is a split and it contains the previous visited split, success break; } else { //invalidate current point and keep split current_point = -1; base_point_prev_split = split_index; } } } print_line("found base point: " + itos(base_point)); if (base_point != -1) { int point = base_point; int last = base_point; //go through all the points, find splits do { int split; int last_dist_to_end = -1; //maximum valid distance to end do { loop.push_back(point); //push current point split = -1; int end = -1; int max_dist_to_end = 0; //find if this point is in a split for (int j = 0; j < sc; j += 2) { int split_pos = -1; int split_end = -1; if (r[j + 0] == point) { //match first split index split_pos = r[j + 0]; split_end = r[j + 1]; } else if (r[j + 1] == point) { //match second split index split_pos = r[j + 1]; split_end = r[j + 0]; } if (split_pos == split_end) { continue; //either nothing found or begin == end, this not a split in either case } //compute distance from split pos to split end int dist_to_end = split_end > split_pos ? split_end - split_pos : (points.size() - split_pos + split_end); if (last_dist_to_end != -1 && dist_to_end >= last_dist_to_end) { //distance must be shorter than in last iteration, means we've tested this before so ignore continue; } else if (dist_to_end > max_dist_to_end) { //always keep the valid point with the most distance (as long as it's valid) max_dist_to_end = dist_to_end; split = split_pos; end = split_end; } } if (split != -1) { //found a split! int from = end; //add points until last is reached while (true) { //find if point is in a split loop.push_back(from); if (from == last) { break; } from++; if (from >= points.size()) { //wrap if reached end from = 0; } if (from == loop[0]) { break; //end because we reached split source } } loops.push_back(loop); //done with this loop loop.clear(); last_dist_to_end = max_dist_to_end; last = end; //algorithm can safely finish in this split point } } while (split != -1); } while (point != last); } if (loop.size() >=2 ) { //points remained //points remain loop.push_back(last); //no splits found, use last loops.push_back(loop); } print_line("total loops: " + itos(loops.size())); if (loops.size()) { //loops found Vector<int> indices; for (int i = 0; i < loops.size(); i++) { Vector<int> loop = loops[i]; Vector<Vector2> vertices; vertices.resize(loop.size()); for (int j = 0; j < vertices.size(); j++) { vertices.write[j] = points[loop[j]]; } Vector<int> sub_indices = Geometry::triangulate_polygon(vertices); int from = indices.size(); indices.resize(from + sub_indices.size()); for (int j = 0; j < sub_indices.size(); j++) { indices.write[from + j] = loop[sub_indices[j]]; } } VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); } #endif } } break; } }
void TileMapEditor::_canvas_draw() { if (!node) return; int cell_size=node->get_cell_size(); Matrix32 xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform(); Matrix32 xform_inv = xform.affine_inverse(); Size2 screen_size=canvas_item_editor->get_size(); Rect2 aabb; aabb.pos=xform_inv.xform(Vector2()); aabb.expand_to(xform_inv.xform(Vector2(0,screen_size.height))); aabb.expand_to(xform_inv.xform(Vector2(screen_size.width,0))); aabb.expand_to(xform_inv.xform(screen_size)); Rect2i si=aabb; for(int i=(si.pos.x/cell_size)-1;i<=(si.pos.x+si.size.x)/cell_size;i++) { int ofs = i*cell_size; Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(xform.xform(Point2(ofs,si.pos.y)),xform.xform(Point2(ofs,si.pos.y+si.size.y)),col,1); } for(int i=(si.pos.y/cell_size)-1;i<=(si.pos.y+si.size.y)/cell_size;i++) { int ofs = i*cell_size; Color col=i==0?Color(1,0.8,0.2,0.5):Color(1,0.3,0.1,0.2); canvas_item_editor->draw_line(xform.xform(Point2(si.pos.x,ofs)),xform.xform(Point2(si.pos.x+si.size.x,ofs)),col,1); } if (selection_active) { Vector<Vector2> points; points.push_back( xform.xform( selection.pos * cell_size) ); points.push_back( xform.xform( (selection.pos+Point2(selection.size.x+1,0)) * cell_size) ); points.push_back( xform.xform( (selection.pos+Point2(selection.size.x+1,selection.size.y+1)) * cell_size) ); points.push_back( xform.xform( (selection.pos+Point2(0,selection.size.y+1)) * cell_size) ); Color col=Color(0.2,0.8,1,0.4); canvas_item_editor->draw_colored_polygon(points,col); } if (mouse_over){ const Vector2 endpoints[4]={ xform.xform( over_tile * cell_size) , xform.xform( (over_tile+Point2(1,0)) * cell_size) , xform.xform( (over_tile+Point2(1,1)) * cell_size) , xform.xform( (over_tile+Point2(0,1)) * cell_size) , }; Color col; if (node->get_cell(over_tile.x,over_tile.y)!=TileMap::INVALID_CELL) col=Color(0.2,0.8,1.0,0.8); else col=Color(1.0,0.4,0.2,0.8); for(int i=0;i<4;i++) canvas_item_editor->draw_line(endpoints[i],endpoints[(i+1)%4],col,2); if (tool==TOOL_DUPLICATING) { Rect2i duplicate=selection; duplicate.pos=over_tile; Vector<Vector2> points; points.push_back( xform.xform( duplicate.pos * cell_size) ); points.push_back( xform.xform( (duplicate.pos+Point2(duplicate.size.x+1,0)) * cell_size) ); points.push_back( xform.xform( (duplicate.pos+Point2(duplicate.size.x+1,duplicate.size.y+1)) * cell_size) ); points.push_back( xform.xform( (duplicate.pos+Point2(0,duplicate.size.y+1)) * cell_size) ); Color col=Color(0.2,1.0,0.8,0.4); canvas_item_editor->draw_colored_polygon(points,col); } else { Ref<TileSet> ts = node->get_tileset(); if (ts.is_valid()) { int st = get_selected_tile(); if (ts->has_tile(st)) { Ref<Texture> t = ts->tile_get_texture(st); if (t.is_valid()) { Rect2 r = ts->tile_get_region(st); Size2 sc = (endpoints[2]-endpoints[0])/cell_size; if (mirror_x->is_pressed()) sc.x*=-1.0; if (mirror_y->is_pressed()) sc.y*=-1.0; if (r==Rect2()) { canvas_item_editor->draw_texture_rect(t,Rect2(endpoints[0],t->get_size()*sc),false,Color(1,1,1,0.5)); } else { canvas_item_editor->draw_texture_rect_region(t,Rect2(endpoints[0],r.get_size()*sc),r,Color(1,1,1,0.5)); } } } } } } }