From fcd4b1a421e9c3ed25201cfd428cfbe789b12204 Mon Sep 17 00:00:00 2001 From: reduz Date: Mon, 28 Dec 2015 09:38:15 -0300 Subject: -added a visual tool to change the pivot of a sprite/asprite/polygon/etc. Closes #2670 --- tools/editor/icons/icon_edit_pivot.png | Bin 0 -> 407 bytes tools/editor/plugins/canvas_item_editor_plugin.cpp | 100 ++++++++++++++------- tools/editor/plugins/canvas_item_editor_plugin.h | 4 + 3 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 tools/editor/icons/icon_edit_pivot.png (limited to 'tools') diff --git a/tools/editor/icons/icon_edit_pivot.png b/tools/editor/icons/icon_edit_pivot.png new file mode 100644 index 0000000000..d68f7bbf25 Binary files /dev/null and b/tools/editor/icons/icon_edit_pivot.png differ diff --git a/tools/editor/plugins/canvas_item_editor_plugin.cpp b/tools/editor/plugins/canvas_item_editor_plugin.cpp index 0946383c8d..6df0a653fa 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.cpp +++ b/tools/editor/plugins/canvas_item_editor_plugin.cpp @@ -152,6 +152,44 @@ public: } }; +void CanvasItemEditor::_edit_set_pivot(const Vector2& mouse_pos) { + List &selection = editor_selection->get_selected_node_list(); + + undo_redo->create_action("Move Pivot"); + + for(List::Element *E=selection.front();E;E=E->next()) { + + Node2D *n2d = E->get()->cast_to(); + + if (n2d && n2d->edit_has_pivot()) { + + Vector2 offset = n2d->edit_get_pivot(); + Vector2 gpos = n2d->get_global_pos(); + + Vector2 motion_ofs = gpos-mouse_pos; + + undo_redo->add_do_method(n2d,"set_global_pos",mouse_pos); + undo_redo->add_do_method(n2d,"edit_set_pivot",offset+n2d->get_global_transform().affine_inverse().basis_xform(motion_ofs)); + undo_redo->add_undo_method(n2d,"set_global_pos",gpos); + undo_redo->add_undo_method(n2d,"edit_set_pivot",offset); + for(int i=0;iget_child_count();i++) { + Node2D *n2dc = n2d->get_child(i)->cast_to(); + if (!n2dc) + continue; + + undo_redo->add_do_method(n2dc,"set_global_pos",n2dc->get_global_pos()); + undo_redo->add_undo_method(n2dc,"set_global_pos",n2dc->get_global_pos()); + + } + + } + + } + + undo_redo->commit_action(); + +} + void CanvasItemEditor::_unhandled_key_input(const InputEvent& p_ev) { if (!is_visible()) @@ -179,38 +217,7 @@ void CanvasItemEditor::_unhandled_key_input(const InputEvent& p_ev) { mouse_pos=transform.affine_inverse().xform(mouse_pos); mouse_pos=snap_point(mouse_pos); - undo_redo->create_action("Move Pivot"); - - for(List::Element *E=selection.front();E;E=E->next()) { - - Node2D *n2d = E->get()->cast_to(); - - if (n2d && n2d->edit_has_pivot()) { - - Vector2 offset = n2d->edit_get_pivot(); - Vector2 gpos = n2d->get_global_pos(); - - Vector2 motion_ofs = gpos-mouse_pos; - - undo_redo->add_do_method(n2d,"set_global_pos",mouse_pos); - undo_redo->add_do_method(n2d,"edit_set_pivot",offset+n2d->get_global_transform().affine_inverse().basis_xform(motion_ofs)); - undo_redo->add_undo_method(n2d,"set_global_pos",gpos); - undo_redo->add_undo_method(n2d,"edit_set_pivot",offset); - for(int i=0;iget_child_count();i++) { - Node2D *n2dc = n2d->get_child(i)->cast_to(); - if (!n2dc) - continue; - - undo_redo->add_do_method(n2dc,"set_global_pos",n2dc->get_global_pos()); - undo_redo->add_undo_method(n2dc,"set_global_pos",n2dc->get_global_pos()); - - } - - } - - } - - undo_redo->commit_action(); + _edit_set_pivot(mouse_pos); } } @@ -221,7 +228,7 @@ void CanvasItemEditor::_unhandled_key_input(const InputEvent& p_ev) { void CanvasItemEditor::_tool_select(int p_index) { - ToolButton *tb[TOOL_MAX]={select_button,list_select_button,move_button,rotate_button,pan_button}; + ToolButton *tb[TOOL_MAX]={select_button,list_select_button,move_button,rotate_button,pivot_button,pan_button}; for(int i=0;iset_pressed(i==p_index); @@ -1130,6 +1137,20 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { return; } + + if (b.button_index==BUTTON_LEFT && tool==TOOL_EDIT_PIVOT) { + if (b.pressed) { + + Point2 mouse_pos(b.x,b.y); + mouse_pos=transform.affine_inverse().xform(mouse_pos); + mouse_pos=snap_point(mouse_pos); + _edit_set_pivot(mouse_pos); + } + return; + } + + + if (tool==TOOL_PAN || b.button_index!=BUTTON_LEFT || Input::get_singleton()->is_key_pressed(KEY_SPACE)) return; @@ -1858,6 +1879,8 @@ void CanvasItemEditor::_viewport_draw() { CanvasItem *single_item=NULL; + bool pivot_found=false; + for(Map::Element *E=selection.front();E;E=E->next()) { @@ -1899,7 +1922,7 @@ void CanvasItemEditor::_viewport_draw() { viewport->draw_line(endpoints[i],endpoints[(i+1)%4],c,2); } - if (single && (tool==TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_ROTATE)) { //kind of sucks + if (single && (tool==TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_ROTATE || tool==TOOL_EDIT_PIVOT)) { //kind of sucks if (canvas_item->cast_to()) { @@ -1907,6 +1930,7 @@ void CanvasItemEditor::_viewport_draw() { if (canvas_item->cast_to()->edit_has_pivot()) { viewport->draw_texture(pivot,xform.get_origin()+(-pivot->get_size()/2).floor()); can_move_pivot=true; + pivot_found=true; } } @@ -1941,6 +1965,7 @@ void CanvasItemEditor::_viewport_draw() { //E->get().last_rect = rect; } + pivot_button->set_disabled(!pivot_found); VisualServer::get_singleton()->canvas_item_add_set_transform(ci,Matrix32()); @@ -2149,6 +2174,7 @@ void CanvasItemEditor::_notification(int p_what) { move_button->set_icon( get_icon("ToolMove","EditorIcons")); rotate_button->set_icon( get_icon("ToolRotate","EditorIcons")); pan_button->set_icon( get_icon("ToolPan", "EditorIcons")); + pivot_button->set_icon( get_icon("EditPivot", "EditorIcons")); select_handle=get_icon("EditorHandle","EditorIcons"); lock_button->set_icon(get_icon("Lock","EditorIcons")); unlock_button->set_icon(get_icon("Unlock","EditorIcons")); @@ -3206,6 +3232,12 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { list_select_button->connect("pressed",this,"_tool_select",make_binds(TOOL_LIST_SELECT)); list_select_button->set_tooltip("Show a list of all objects at the position clicked\n(same as Alt+RMB in selet mode)."); + pivot_button = memnew( ToolButton ); + pivot_button->set_toggle_mode(true); + hb->add_child(pivot_button); + pivot_button->connect("pressed",this,"_tool_select",make_binds(TOOL_EDIT_PIVOT)); + pivot_button->set_tooltip("Click to change object's rotation pivot"); + pan_button = memnew( ToolButton ); pan_button->set_toggle_mode(true); hb->add_child(pan_button); diff --git a/tools/editor/plugins/canvas_item_editor_plugin.h b/tools/editor/plugins/canvas_item_editor_plugin.h index 2376e9f842..c731e8c4c6 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.h +++ b/tools/editor/plugins/canvas_item_editor_plugin.h @@ -70,6 +70,7 @@ class CanvasItemEditor : public VBoxContainer { TOOL_LIST_SELECT, TOOL_MOVE, TOOL_ROTATE, + TOOL_EDIT_PIVOT, TOOL_PAN, TOOL_MAX }; @@ -245,6 +246,7 @@ class CanvasItemEditor : public VBoxContainer { ToolButton *move_button; ToolButton *rotate_button; + ToolButton *pivot_button; ToolButton *pan_button; ToolButton *lock_button; @@ -266,6 +268,7 @@ class CanvasItemEditor : public VBoxContainer { PopupMenu *selection_menu; + //PopupMenu *popup; DragType drag; Point2 drag_from; @@ -306,6 +309,7 @@ class CanvasItemEditor : public VBoxContainer { CanvasItem *ref_item; + void _edit_set_pivot(const Vector2& mouse_pos); void _add_canvas_item(CanvasItem *p_canvas_item); void _remove_canvas_item(CanvasItem *p_canvas_item); void _clear_canvas_items(); -- cgit v1.2.3