From 61fd1ad703d60c413c2ace7abc5bd2e6ebc06678 Mon Sep 17 00:00:00 2001 From: marynate Date: Sun, 4 May 2014 01:36:59 +0800 Subject: Press F key in 2d editor to center view to selected node Press Ctrl+F to frame selected nodes in 2d editor Change Frame Selection hotkey in 2d editor from Ctrl to CMD for Mac compatability --- tools/editor/plugins/canvas_item_editor_plugin.cpp | 54 ++++++++++++++++++++++ tools/editor/plugins/canvas_item_editor_plugin.h | 6 ++- 2 files changed, 58 insertions(+), 2 deletions(-) (limited to 'tools/editor') diff --git a/tools/editor/plugins/canvas_item_editor_plugin.cpp b/tools/editor/plugins/canvas_item_editor_plugin.cpp index 7914ecfd95..4bed6ee4e7 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.cpp +++ b/tools/editor/plugins/canvas_item_editor_plugin.cpp @@ -1956,7 +1956,59 @@ void CanvasItemEditor::_popup_callback(int p_op) { } break; + case VIEW_CENTER_TO_SELECTION: + case VIEW_FRAME_TO_SELECTION: { + Vector2 center(0.f, 0.f); + Rect2 rect; + int count = 0; + + Map &selection = editor_selection->get_selection(); + for(Map::Element *E=selection.front();E;E=E->next()) { + CanvasItem *canvas_item = E->key()->cast_to(); + if (!canvas_item) continue; + + // counting invisible items, for now + //if (!canvas_item->is_visible()) continue; + ++count; + + Rect2 item_rect = canvas_item->get_item_rect(); + + Vector2 pos = canvas_item->get_global_transform().get_origin(); + Vector2 scale = canvas_item->get_global_transform().get_scale(); + real_t angle = canvas_item->get_global_transform().get_rotation(); + + Matrix32 t(angle, Vector2(0.f,0.f)); + item_rect = t.xform(item_rect); + Rect2 canvas_item_rect(pos + scale*item_rect.pos, scale*item_rect.size); + if (count == 1) { + rect = canvas_item_rect; + } else { + rect = rect.merge(canvas_item_rect); + } + }; + if (count==0) break; + + if (p_op == VIEW_CENTER_TO_SELECTION) { + + center = rect.pos + rect.size/2; + Vector2 offset = viewport->get_size()/2 - editor->get_scene_root()->get_global_canvas_transform().xform(center); + h_scroll->set_val(h_scroll->get_val() - offset.x/zoom); + v_scroll->set_val(v_scroll->get_val() - offset.y/zoom); + + } else { // VIEW_FRAME_TO_SELECTION + + if (rect.size.x > CMP_EPSILON && rect.size.y > CMP_EPSILON) { + float scale_x = viewport->get_size().x/rect.size.x; + float scale_y = viewport->get_size().y/rect.size.y; + zoom = scale_x < scale_y? scale_x:scale_y; + zoom *= 0.90; + _update_scroll(0); + call_deferred("_popup_callback", VIEW_CENTER_TO_SELECTION); + } + } + + } break; } } #if 0 @@ -2201,6 +2253,8 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { p->add_item("Zoom Out",ZOOM_OUT); p->add_item("Zoom Reset",ZOOM_RESET); p->add_item("Zoom Set..",ZOOM_SET); + p->add_item("Center Selection", VIEW_CENTER_TO_SELECTION, KEY_F); + p->add_item("Frame Selection", VIEW_FRAME_TO_SELECTION, KEY_MASK_CMD|KEY_F); animation_menu = memnew( MenuButton ); animation_menu->set_text("Animation"); diff --git a/tools/editor/plugins/canvas_item_editor_plugin.h b/tools/editor/plugins/canvas_item_editor_plugin.h index 64c5d523c8..488fe67584 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.h +++ b/tools/editor/plugins/canvas_item_editor_plugin.h @@ -97,10 +97,12 @@ class CanvasItemEditor : public VBoxContainer { ANIM_INSERT_POS_ROT, ANIM_INSERT_POS_SCALE, ANIM_INSERT_ROT_SCALE, - ANIM_INSERT_POS_ROT_SCALE, + ANIM_INSERT_POS_ROT_SCALE, ANIM_COPY_POSE, ANIM_PASTE_POSE, - ANIM_CLEAR_POSE + ANIM_CLEAR_POSE, + VIEW_CENTER_TO_SELECTION, + VIEW_FRAME_TO_SELECTION, }; -- cgit v1.2.3