static inline Vector2 interpolate(const Rect2 &r, const Vector2 &v) { return Vector2( Math::lerp(r.get_pos().x, r.get_pos().x + r.get_size().x, v.x), Math::lerp(r.get_pos().y, r.get_pos().y + r.get_size().y, v.y)); }
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 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)); } } } } } } }