void TileMapEditor::_update_palette() { if (!node) return; int selected = get_selected_tile(); palette->clear(); Ref<TileSet> tileset=node->get_tileset(); if (tileset.is_null()) return; List<int> tiles; tileset->get_tile_list(&tiles); if (tiles.empty()) return; palette->set_max_columns(0); palette->set_icon_mode(ItemList::ICON_MODE_TOP); palette->set_max_text_lines(2); String filter = search_box->get_text().strip_edges(); for (List<int>::Element *E=tiles.front();E;E=E->next()) { String name; if (tileset->tile_get_name(E->get())!="") { name = tileset->tile_get_name(E->get()); } else { name = "#"+itos(E->get()); } if (filter != "" && name.findn(filter) == -1) continue; palette->add_item(name); Ref<Texture> tex = tileset->tile_get_texture(E->get()); if (tex.is_valid()) { Rect2 region = tileset->tile_get_region(E->get()); if (!region.has_no_area()) palette->set_item_icon_region(palette->get_item_count()-1, region); palette->set_item_icon(palette->get_item_count()-1, tex); } palette->set_item_metadata(palette->get_item_count()-1, E->get()); } if (selected != -1) set_selected_tile(selected); else palette->select(0, true); }
DVector<Vector2> TileMapEditor::_bucket_fill(const Point2i& p_start) { if (node->get_cell(p_start.x, p_start.y) != TileMap::INVALID_CELL) return DVector<Vector2>(); int id = get_selected_tile(); if (id == TileMap::INVALID_CELL) return DVector<Vector2>(); Rect2 r = node->get_item_rect(); r.pos = r.pos/node->get_cell_size(); r.size = r.size/node->get_cell_size(); DVector<Vector2> points; List<Point2i> queue; queue.push_back(p_start); while (queue.size()) { Point2i n = queue.front()->get(); queue.pop_front(); if (!r.has_point(n)) continue; if (node->get_cell(n.x, n.y) == TileMap::INVALID_CELL) { node->set_cellv(n, id, flip_h, flip_v, transpose); points.push_back(n); queue.push_back(n + Point2i(0, 1)); queue.push_back(n + Point2i(0, -1)); queue.push_back(n + Point2i(1, 0)); queue.push_back(n + Point2i(-1, 0)); } } return points; }
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); } } }
bool TileMapEditor::forward_input_event(const InputEvent& p_event) { if (!node || !node->get_tileset().is_valid() || !node->is_visible()) return false; Matrix32 xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform(); Matrix32 xform_inv = xform.affine_inverse(); switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &mb=p_event.mouse_button; if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) return false; //drag if (tool==TOOL_NONE) { if (mb.mod.shift) { if (mb.mod.control) tool=TOOL_RECTANGLE_PAINT; else tool=TOOL_LINE_PAINT; selection_active=false; rectangle_begin=over_tile; return true; } if (mb.mod.control) { tool=TOOL_PICKING; _pick_tile(over_tile); return true; } tool=TOOL_PAINTING; } if (tool==TOOL_PAINTING) { int id = get_selected_tile(); if (id!=TileMap::INVALID_CELL) { tool=TOOL_PAINTING; paint_undo.clear(); paint_undo[over_tile]=_get_op_from_cell(over_tile); _set_cell(over_tile, id, flip_h, flip_v, transpose); } } else if (tool==TOOL_PICKING) { _pick_tile(over_tile); } else if (tool==TOOL_SELECTING) { selection_active=true; rectangle_begin=over_tile; } return true; } else { if (tool!=TOOL_NONE) { if (tool==TOOL_PAINTING) { int id=get_selected_tile(); if (id!=TileMap::INVALID_CELL && paint_undo.size()) { undo_redo->create_action(TTR("Paint TileMap")); for (Map<Point2i,CellOp>::Element *E=paint_undo.front();E;E=E->next()) { Point2 p=E->key(); undo_redo->add_do_method(node,"set_cellv",p,id,flip_h,flip_v,transpose); undo_redo->add_undo_method(node,"set_cellv",p,E->get().idx,E->get().xf,E->get().yf,E->get().tr); } undo_redo->commit_action(); paint_undo.clear(); } } else if (tool==TOOL_LINE_PAINT) { int id=get_selected_tile(); if (id!=TileMap::INVALID_CELL) { undo_redo->create_action("Line Draw"); for (Map<Point2i,CellOp>::Element *E=paint_undo.front();E;E=E->next()) { _set_cell(E->key(), id, flip_h, flip_v, transpose, true); } undo_redo->commit_action(); paint_undo.clear(); canvas_item_editor->update(); } } else if (tool==TOOL_RECTANGLE_PAINT) { int id=get_selected_tile(); if (id!=TileMap::INVALID_CELL) { undo_redo->create_action("Rectangle Paint"); 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++) { _set_cell(Point2i(j, i), id, flip_h, flip_v, transpose, true); } } undo_redo->commit_action(); canvas_item_editor->update(); } } else if (tool==TOOL_DUPLICATING) { Point2 ofs = over_tile-rectangle.pos; undo_redo->create_action(TTR("Duplicate")); for (List<TileData>::Element *E=copydata.front();E;E=E->next()) { _set_cell(E->get().pos+ofs,E->get().cell,E->get().flip_h,E->get().flip_v,E->get().transpose,true); } undo_redo->commit_action(); copydata.clear(); canvas_item_editor->update(); } else if (tool==TOOL_SELECTING) { canvas_item_editor->update(); } else if (tool==TOOL_BUCKET) { DVector<Vector2> points = _bucket_fill(over_tile); if (points.size() == 0) return false; Dictionary op; op["id"] = get_selected_tile(); op["flip_h"] = flip_h; op["flip_v"] = flip_v; op["transpose"] = transpose; undo_redo->create_action("Bucket Fill"); undo_redo->add_do_method(this, "_fill_points", points, op); undo_redo->add_undo_method(this, "_erase_points", points); undo_redo->commit_action(); } tool=TOOL_NONE; return true; } } } else if (mb.button_index==BUTTON_RIGHT) { if (mb.pressed) { if (tool==TOOL_SELECTING || selection_active) { tool=TOOL_NONE; selection_active=false; canvas_item_editor->update(); return true; } if (tool==TOOL_DUPLICATING) { tool=TOOL_NONE; copydata.clear(); canvas_item_editor->update(); return true; } if (tool==TOOL_NONE) { paint_undo.clear(); Point2 local = node->world_to_map(xform_inv.xform(Point2(mb.x, mb.y))); if (mb.mod.shift) { if (mb.mod.control) tool=TOOL_RECTANGLE_ERASE; else tool=TOOL_LINE_ERASE; selection_active=false; rectangle_begin=local; } else { tool=TOOL_ERASING; paint_undo[local]=_get_op_from_cell(local); _set_cell(local, TileMap::INVALID_CELL); } return true; } } else { if (tool==TOOL_ERASING || tool==TOOL_RECTANGLE_ERASE || tool==TOOL_LINE_ERASE) { if (paint_undo.size()) { undo_redo->create_action(TTR("Erase TileMap")); for (Map<Point2i,CellOp>::Element *E=paint_undo.front();E;E=E->next()) { Point2 p=E->key(); undo_redo->add_do_method(node,"set_cellv",p,TileMap::INVALID_CELL,false,false,false); undo_redo->add_undo_method(node,"set_cellv",p,E->get().idx,E->get().xf,E->get().yf,E->get().tr); } undo_redo->commit_action(); paint_undo.clear(); } if (tool==TOOL_RECTANGLE_ERASE || tool==TOOL_LINE_ERASE) { canvas_item_editor->update(); } tool=TOOL_NONE; return true; } } } } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion &mm=p_event.mouse_motion; Point2i new_over_tile = node->world_to_map(xform_inv.xform(Point2(mm.x,mm.y))); if (new_over_tile!=over_tile) { over_tile=new_over_tile; canvas_item_editor->update(); } if (tool==TOOL_PAINTING) { int id = get_selected_tile(); if (id!=TileMap::INVALID_CELL) { if (!paint_undo.has(over_tile)) { paint_undo[over_tile]=_get_op_from_cell(over_tile); } _set_cell(over_tile, id, flip_h, flip_v, transpose); return true; } } if (tool==TOOL_SELECTING) { _select(rectangle_begin, over_tile); return true; } if (tool==TOOL_LINE_PAINT || tool==TOOL_LINE_ERASE) { int id = get_selected_tile(); bool erasing = (tool==TOOL_LINE_ERASE); if (erasing && paint_undo.size()) { for (Map<Point2i, CellOp>::Element *E=paint_undo.front();E;E=E->next()) { _set_cell(E->key(), E->get().idx, E->get().xf, E->get().yf, E->get().tr); } } paint_undo.clear(); if (id!=TileMap::INVALID_CELL) { Vector<Point2i> points = line(rectangle_begin.x, over_tile.x, rectangle_begin.y, over_tile.y); for (int i=0;i<points.size();i++) { paint_undo[points[i]]=_get_op_from_cell(points[i]); if (erasing) _set_cell(points[i], TileMap::INVALID_CELL); } canvas_item_editor->update(); } return true; } if (tool==TOOL_RECTANGLE_PAINT || tool==TOOL_RECTANGLE_ERASE) { _select(rectangle_begin, over_tile); if (tool==TOOL_RECTANGLE_ERASE) { if (paint_undo.size()) { for (Map<Point2i, CellOp>::Element *E=paint_undo.front();E;E=E->next()) { _set_cell(E->key(), E->get().idx, E->get().xf, E->get().yf, E->get().tr); } } paint_undo.clear(); 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++) { Point2i tile = Point2i(j, i); paint_undo[tile]=_get_op_from_cell(tile); _set_cell(tile, TileMap::INVALID_CELL); } } } return true; } if (tool==TOOL_ERASING) { if (!paint_undo.has(over_tile)) { paint_undo[over_tile]=_get_op_from_cell(over_tile); } _set_cell(over_tile, TileMap::INVALID_CELL); return true; } if (tool==TOOL_PICKING && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) { _pick_tile(over_tile); return true; } } break; case InputEvent::KEY: { const InputEventKey &k = p_event.key; if (!k.pressed) break; if (k.scancode==KEY_ESCAPE) { if (tool==TOOL_DUPLICATING) copydata.clear(); else if (tool==TOOL_SELECTING || selection_active) selection_active=false; tool=TOOL_NONE; canvas_item_editor->update(); return true; } if (tool!=TOOL_NONE || !mouse_over) return false; if (k.scancode==KEY_DELETE) { _menu_option(OPTION_ERASE_SELECTION); return true; } if (k.mod.command) { if (k.scancode==KEY_F) { search_box->select_all(); search_box->grab_focus(); return true; } if (k.scancode==KEY_B) { tool=TOOL_SELECTING; selection_active=false; canvas_item_editor->update(); return true; } if (k.scancode==KEY_D) { _update_copydata(); if (selection_active) { tool=TOOL_DUPLICATING; canvas_item_editor->update(); return true; } } } else { if (k.scancode==KEY_A) { flip_h=!flip_h; mirror_x->set_pressed(flip_h); canvas_item_editor->update(); return true; } if (k.scancode==KEY_S) { flip_v=!flip_v; mirror_y->set_pressed(flip_v); canvas_item_editor->update(); return true; } } } break; } return false; }
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()); } } } } } } }
bool TileMapEditor::forward_input_event(const InputEvent& p_event) { if (!node || !node->get_tileset().is_valid()) return false; Matrix32 xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * node->get_global_transform(); Matrix32 xform_inv = xform.affine_inverse(); Vector2 snap = node->get_cell_size(); switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &mb=p_event.mouse_button; if (mb.button_index==BUTTON_LEFT) { if (mb.pressed && tool==TOOL_DUPLICATING) { List<_TileMapEditorCopyData> dupdata; Point2 ofs = over_tile-selection.pos; for(int i=selection.pos.y;i<=selection.pos.y+selection.size.y;i++) { for(int j=selection.pos.x;j<=selection.pos.x+selection.size.x;j++) { _TileMapEditorCopyData tcd; tcd.pos=Point2i(j,i); tcd.cell=node->get_cell(j,i); tcd.flip_h=node->is_cell_x_flipped(j,i); tcd.flip_v=node->is_cell_y_flipped(j,i); tcd.transpose=node->is_cell_transposed(j,i); dupdata.push_back(tcd); } } undo_redo->create_action("Duplicate"); for (List<_TileMapEditorCopyData>::Element *E=dupdata.front();E;E=E->next()) { _set_cell(E->get().pos+ofs,E->get().cell,E->get().flip_h,E->get().flip_v,E->get().transpose,true); } undo_redo->commit_action(); tool=TOOL_NONE; canvas_item_editor->update(); selection.pos=over_tile; } else if (mb.pressed && tool==TOOL_NONE) { if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) return false; //drag if (mb.mod.shift) { tool=TOOL_SELECTING; selection_begin =node->world_to_map(xform_inv.xform(Point2(mb.x,mb.y))); selection.pos=selection_begin; selection.size=Point2(0,0); selection_active=true; canvas_item_editor->update(); return true; } else if (mb.mod.control) { tool=TOOL_PICKING; set_selected_tile(node->get_cell(over_tile.x, over_tile.y)); mirror_x->set_pressed(node->is_cell_x_flipped(over_tile.x, over_tile.y)); mirror_y->set_pressed(node->is_cell_y_flipped(over_tile.x, over_tile.y)); transpose->set_pressed(node->is_cell_transposed(over_tile.x, over_tile.y)); _update_transform_buttons(); canvas_item_editor->update(); return true; } else { int id = get_selected_tile(); if (id!=TileMap::INVALID_CELL) { tool=TOOL_PAINTING; Point2i local =node->world_to_map((xform_inv.xform(Point2(mb.x,mb.y)))); paint_undo.clear(); paint_undo[local]=_get_op_from_cell(local); node->set_cell(local.x,local.y,id,mirror_x->is_pressed(),mirror_y->is_pressed(),transpose->is_pressed()); return true; } } } else { if (tool==TOOL_PAINTING || tool == TOOL_SELECTING || tool == TOOL_PICKING) { if (tool==TOOL_PAINTING) { if (paint_undo.size()) { undo_redo->create_action("Paint TileMap"); for(Map<Point2i,CellOp>::Element *E=paint_undo.front();E;E=E->next()) { Point2i p=E->key(); undo_redo->add_do_method(this,"_set_cell_shortened",Point2(p),node->get_cell(p.x,p.y),node->is_cell_x_flipped(p.x,p.y),node->is_cell_y_flipped(p.x,p.y),node->is_cell_transposed(p.x,p.y)); undo_redo->add_undo_method(this,"_set_cell_shortened",Point2(p),E->get().idx,E->get().xf,E->get().yf,E->get().tr); } undo_redo->commit_action(); paint_undo.clear(); } } tool=TOOL_NONE; return true; } } } if (mb.button_index==BUTTON_RIGHT) { if (mb.pressed && tool==TOOL_DUPLICATING) { tool=TOOL_NONE; canvas_item_editor->update(); } else if (mb.pressed && tool==TOOL_NONE) { tool=TOOL_ERASING; Point2i local =node->world_to_map(xform_inv.xform(Point2(mb.x,mb.y))); paint_undo.clear(); paint_undo[local]=_get_op_from_cell(local); //node->set_cell(local.x,local.y,id,mirror_x->is_pressed(),mirror_y->is_pressed(),transpose->is_pressed()); //return true; _set_cell(local,TileMap::INVALID_CELL); return true; } else { if (tool==TOOL_ERASING) { if (paint_undo.size()) { undo_redo->create_action("Erase TileMap"); for(Map<Point2i,CellOp>::Element *E=paint_undo.front();E;E=E->next()) { Point2i p=E->key(); //undo_redo->add_do_method(node,"set_cell",p.x,p.y,node->get_cell(p.x,p.y),node->is_cell_x_flipped(p.x,p.y),node->is_cell_y_flipped(p.x,p.y),node->is_cell_transposed(p.x,p.y)); _set_cell(p,TileMap::INVALID_CELL,false,false,false,true); undo_redo->add_undo_method(this,"_set_cell_shortened",Point2(p),E->get().idx,E->get().xf,E->get().yf,E->get().tr); } undo_redo->commit_action(); paint_undo.clear(); } tool=TOOL_NONE; return true; } } } } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion &mm=p_event.mouse_motion; Point2i new_over_tile = node->world_to_map(xform_inv.xform(Point2(mm.x,mm.y)));//(xform_inv.xform(Point2(mm.x,mm.y))/snap).floor(); if (new_over_tile!=over_tile) { over_tile=new_over_tile; canvas_item_editor->update(); } if (tool==TOOL_PAINTING) { int id = get_selected_tile(); if (id!=TileMap::INVALID_CELL) { if (!paint_undo.has(over_tile)) { paint_undo[over_tile]=_get_op_from_cell(over_tile); } node->set_cell(over_tile.x,over_tile.y,id,mirror_x->is_pressed(),mirror_y->is_pressed(),transpose->is_pressed()); return true; } } if (tool==TOOL_SELECTING) { Point2i begin=selection_begin; Point2i end =over_tile; if (begin.x > end.x) { SWAP( begin.x, end.x); } if (begin.y > end.y) { SWAP( begin.y, end.y); } selection.pos=begin; selection.size=end-begin; canvas_item_editor->update(); return true; } if (tool==TOOL_ERASING) { Point2i local =over_tile; if (!paint_undo.has(over_tile)) { paint_undo[over_tile]=_get_op_from_cell(over_tile); } //node->set_cell(over_tile.x,over_tile.y,id,mirror_x->is_pressed(),mirror_y->is_pressed(),transpose->is_pressed()); _set_cell(local,TileMap::INVALID_CELL); return true; } if (tool==TOOL_PICKING) { set_selected_tile(node->get_cell(over_tile.x, over_tile.y)); mirror_x->set_pressed(node->is_cell_x_flipped(over_tile.x, over_tile.y)); mirror_y->set_pressed(node->is_cell_y_flipped(over_tile.x, over_tile.y)); transpose->set_pressed(node->is_cell_transposed(over_tile.x, over_tile.y)); _update_transform_buttons(); canvas_item_editor->update(); return true; } } break; case InputEvent::KEY: { const InputEventKey &k = p_event.key; if (!node) break; if (k.pressed && k.scancode==KEY_DELETE && selection_active && tool==TOOL_NONE) { undo_redo->create_action("Delete"); for(int i=selection.pos.y;i<=selection.pos.y+selection.size.y;i++) { for(int j=selection.pos.x;j<=selection.pos.x+selection.size.x;j++) { _set_cell(Point2i(j,i),TileMap::INVALID_CELL); } } undo_redo->commit_action(); selection_active=false; canvas_item_editor->update(); return true; } if (mouse_over && k.pressed && k.scancode==KEY_A && tool==TOOL_NONE && !k.mod.command) { /*int cell = node->get_cell(over_tile.x,over_tile.y); if (cell!=TileMap::INVALID_CELL) { bool flip_h = node->is_cell_x_flipped(over_tile.x,over_tile.y); bool flip_v = node->is_cell_y_flipped(over_tile.x,over_tile.y); _set_cell(over_tile,cell,!flip_h,flip_v); }*/ mirror_x->set_pressed( ! mirror_x->is_pressed() ); canvas_item_editor->update(); return true; } if (mouse_over && k.pressed && k.scancode==KEY_S && tool==TOOL_NONE && !k.mod.command) { /* int cell = node->get_cell(over_tile.x,over_tile.y); if (cell!=TileMap::INVALID_CELL) { bool flip_h = node->is_cell_x_flipped(over_tile.x,over_tile.y); bool flip_v = node->is_cell_y_flipped(over_tile.x,over_tile.y); _set_cell(over_tile,cell,flip_h,!flip_v); }*/ mirror_y->set_pressed( ! mirror_y->is_pressed() ); canvas_item_editor->update(); return true; } if (mouse_over && selection_active && k.pressed && k.mod.command && k.scancode==KEY_D && tool==TOOL_NONE) { tool=TOOL_DUPLICATING; canvas_item_editor->update(); return true; } } break; } return false; }
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)); } } } } } } }