From 9bfb08830b50af8bb06941e1e5bf07f503e9851f Mon Sep 17 00:00:00 2001 From: Carl Olsson Date: Sat, 17 Jan 2015 16:48:21 +1000 Subject: [PATCH] Wrapped duplicated CellOp generation code in a function and added TileMap tile picking with Ctrl+LMB. --- .../editor/plugins/tile_map_editor_plugin.cpp | 79 ++++++++++--------- tools/editor/plugins/tile_map_editor_plugin.h | 6 +- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/tools/editor/plugins/tile_map_editor_plugin.cpp b/tools/editor/plugins/tile_map_editor_plugin.cpp index a7e6a0d1f94..ce5ea581242 100644 --- a/tools/editor/plugins/tile_map_editor_plugin.cpp +++ b/tools/editor/plugins/tile_map_editor_plugin.cpp @@ -73,6 +73,18 @@ int TileMapEditor::get_selected_tile() const { return item->get_metadata(0); } +void TileMapEditor::set_selected_tile(int p_tile) { + TreeItem *item = palette->get_root()->get_children(); + while (item) { + if ((int)item->get_metadata(0) == p_tile) { + item->select(0); + palette->ensure_cursor_is_visible(); + break; + } + item = item->get_next(); + } +} + void TileMapEditor::_set_cell(const Point2i& p_pos,int p_value,bool p_flip_h, bool p_flip_v,bool p_with_undo) { ERR_FAIL_COND(!node); @@ -224,28 +236,25 @@ bool TileMapEditor::forward_input_event(const InputEvent& p_event) { 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)); + 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(); - CellOp op; - op.idx = node->get_cell(local.x,local.y); - if (op.idx>=0) { - if (node->is_cell_x_flipped(local.x,local.y)) - op.xf=true; - if (node->is_cell_y_flipped(local.x,local.y)) - op.yf=true; - } - paint_undo[local]=op; + paint_undo[local]=_get_op_from_cell(local); node->set_cell(local.x,local.y,id,mirror_x->is_pressed(),mirror_y->is_pressed()); return true; } } } else { - if (tool==TOOL_PAINTING || tool == TOOL_SELECTING) { + if (tool==TOOL_PAINTING || tool == TOOL_SELECTING || tool == TOOL_PICKING) { if (tool==TOOL_PAINTING) { @@ -279,15 +288,7 @@ bool TileMapEditor::forward_input_event(const InputEvent& p_event) { tool=TOOL_ERASING; Point2i local =node->world_to_map(xform_inv.xform(Point2(mb.x,mb.y))); paint_undo.clear(); - CellOp op; - op.idx = node->get_cell(local.x,local.y); - if (op.idx>=0) { - if (node->is_cell_x_flipped(local.x,local.y)) - op.xf=true; - if (node->is_cell_y_flipped(local.x,local.y)) - op.yf=true; - } - paint_undo[local]=op; + paint_undo[local]=_get_op_from_cell(local); //node->set_cell(local.x,local.y,id,mirror_x->is_pressed(),mirror_y->is_pressed()); //return true; _set_cell(local,TileMap::INVALID_CELL); @@ -337,15 +338,7 @@ bool TileMapEditor::forward_input_event(const InputEvent& p_event) { if (!paint_undo.has(over_tile)) { - CellOp op; - op.idx = node->get_cell(over_tile.x,over_tile.y); - if (op.idx>=0) { - if (node->is_cell_x_flipped(over_tile.x,over_tile.y)) - op.xf=true; - if (node->is_cell_y_flipped(over_tile.x,over_tile.y)) - op.yf=true; - } - paint_undo[over_tile]=op; + 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()); @@ -374,25 +367,22 @@ bool TileMapEditor::forward_input_event(const InputEvent& p_event) { return true; } + if (tool==TOOL_ERASING) { Point2i local =over_tile; if (!paint_undo.has(over_tile)) { - - CellOp op; - op.idx = node->get_cell(over_tile.x,over_tile.y); - if (op.idx>=0) { - if (node->is_cell_x_flipped(over_tile.x,over_tile.y)) - op.xf=true; - if (node->is_cell_y_flipped(over_tile.x,over_tile.y)) - op.yf=true; - } - paint_undo[over_tile]=op; + 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()); _set_cell(local,TileMap::INVALID_CELL); return true; } + if (tool==TOOL_PICKING) { + set_selected_tile(node->get_cell(over_tile.x, over_tile.y)); + canvas_item_editor->update(); + return true; + } } break; case InputEvent::KEY: { @@ -710,6 +700,19 @@ void TileMapEditor::_bind_methods() { } +TileMapEditor::CellOp TileMapEditor::_get_op_from_cell(const Point2i& p_pos) +{ + CellOp op; + op.idx = node->get_cell(p_pos.x,p_pos.y); + if (op.idx>=0) { + if (node->is_cell_x_flipped(p_pos.x,p_pos.y)) + op.xf=true; + if (node->is_cell_y_flipped(p_pos.x,p_pos.y)) + op.yf=true; + } + return op; +} + TileMapEditor::TileMapEditor(EditorNode *p_editor) { node=NULL; diff --git a/tools/editor/plugins/tile_map_editor_plugin.h b/tools/editor/plugins/tile_map_editor_plugin.h index ef869591bd7..f3c590e228e 100644 --- a/tools/editor/plugins/tile_map_editor_plugin.h +++ b/tools/editor/plugins/tile_map_editor_plugin.h @@ -51,7 +51,8 @@ class TileMapEditor : public VBoxContainer { TOOL_PAINTING, TOOL_SELECTING, TOOL_ERASING, - TOOL_DUPLICATING + TOOL_DUPLICATING, + TOOL_PICKING }; Tool tool; @@ -81,11 +82,13 @@ class TileMapEditor : public VBoxContainer { bool xf; bool yf; CellOp() { idx=-1; xf=false; yf=false; } + CellOp(const CellOp& p_other) : idx(p_other.idx), xf(p_other.xf), yf(p_other.yf) {} }; Map paint_undo; int get_selected_tile() const; + void set_selected_tile(int p_tile); void _update_palette(); void _canvas_draw(); @@ -102,6 +105,7 @@ protected: void _notification(int p_what); void _node_removed(Node *p_node); static void _bind_methods(); + CellOp _get_op_from_cell(const Point2i& p_pos); public: HBoxContainer *get_canvas_item_editor_hb() const { return canvas_item_editor_hb; }