diff options
author | Gilles Roudiere <gilles.roudiere@gmail.com> | 2017-07-11 00:14:22 +0200 |
---|---|---|
committer | Gilles Roudiere <gilles.roudiere@gmail.com> | 2017-08-13 21:20:13 +0200 |
commit | bd0384a9e93e3e8d792cef678dd9f3467a6a088d (patch) | |
tree | d312cdf5a1cbd1441c7c0bdc4d5d5a3838636a73 | |
parent | 0d35d4d53b12197bea3650293bbc342fc0b57139 (diff) |
Add anchor visualization
-rw-r--r-- | editor/icons/2x/icon_editor_control_anchor.png | bin | 0 -> 855 bytes | |||
-rw-r--r-- | editor/icons/icon_editor_control_anchor.png | bin | 0 -> 472 bytes | |||
-rw-r--r-- | editor/icons/source/icon_editor_control_anchor.svg | 98 | ||||
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.cpp | 110 | ||||
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.h | 4 | ||||
-rw-r--r-- | scene/gui/control.cpp | 1 |
6 files changed, 175 insertions, 38 deletions
diff --git a/editor/icons/2x/icon_editor_control_anchor.png b/editor/icons/2x/icon_editor_control_anchor.png Binary files differnew file mode 100644 index 0000000000..838f85b763 --- /dev/null +++ b/editor/icons/2x/icon_editor_control_anchor.png diff --git a/editor/icons/icon_editor_control_anchor.png b/editor/icons/icon_editor_control_anchor.png Binary files differnew file mode 100644 index 0000000000..158dd62a7b --- /dev/null +++ b/editor/icons/icon_editor_control_anchor.png diff --git a/editor/icons/source/icon_editor_control_anchor.svg b/editor/icons/source/icon_editor_control_anchor.svg new file mode 100644 index 0000000000..473b69d060 --- /dev/null +++ b/editor/icons/source/icon_editor_control_anchor.svg @@ -0,0 +1,98 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="16" + height="16" + viewBox="0 0 16 16" + id="svg2" + version="1.1" + inkscape:version="0.92.1 r" + inkscape:export-filename="/home/gilles/godot/editor/icons/icon_editor_control_anchor.png" + inkscape:export-xdpi="96" + inkscape:export-ydpi="96" + sodipodi:docname="icon_editor_control_anchor.svg"> + <defs + id="defs4" /> + <sodipodi:namedview + id="base" + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1.0" + inkscape:pageopacity="0.0" + inkscape:pageshadow="2" + inkscape:zoom="22.627416" + inkscape:cx="0.71312079" + inkscape:cy="11.175116" + inkscape:document-units="px" + inkscape:current-layer="layer1" + showgrid="true" + units="px" + inkscape:snap-bbox="true" + inkscape:bbox-paths="true" + inkscape:bbox-nodes="true" + inkscape:snap-bbox-edge-midpoints="true" + inkscape:snap-bbox-midpoints="true" + inkscape:snap-object-midpoints="true" + inkscape:snap-center="true" + inkscape:window-width="1356" + inkscape:window-height="742" + inkscape:window-x="4" + inkscape:window-y="20" + inkscape:window-maximized="0" + inkscape:pagecheckerboard="true" + showguides="false"> + <inkscape:grid + type="xygrid" + id="grid3336" /> + </sodipodi:namedview> + <metadata + id="metadata7"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title /> + </cc:Work> + </rdf:RDF> + </metadata> + <g + inkscape:label="Layer 1" + inkscape:groupmode="layer" + id="layer1" + transform="translate(0,-1036.3622)"> + <path + style="fill:#a5efac;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1" + d="M 8.8320312 6.1445312 A 4 4.0000234 0 0 1 6.140625 8.8300781 L 16 16 L 8.8320312 6.1445312 z " + transform="translate(0,1036.3622)" + id="path7188" /> + <ellipse + r="2" + style="opacity:1;fill:#6e6e6e;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" + id="ellipse4152" + cx="3" + cy="1039.3622" /> + <ellipse + style="fill:#a5efac;fill-opacity:1;stroke:none;stroke-width:0.53333384;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;paint-order:fill markers stroke" + id="path31" + cx="4" + cy="1040.3622" + rx="4" + ry="4.0000091" /> + <circle + style="fill:#a5efac;fill-opacity:1;stroke:none;stroke-width:0.5333333;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;paint-order:fill markers stroke" + id="path31-3" + cx="5" + cy="1041.3622" + r="0" /> + </g> +</svg> diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 92acd966d0..6516776ed3 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -775,6 +775,15 @@ CanvasItemEditor::DragType CanvasItemEditor::_find_drag_type(const Point2 &p_cli return DRAG_NONE; } +Vector2 CanvasItemEditor::_anchor_to_position(Control *p_control, Vector2 anchor) { + ERR_FAIL_COND_V(!p_control, Vector2()); + + Transform2D parent_transform = p_control->get_transform().affine_inverse(); + Size2 parent_size = p_control->get_parent_area_size(); + + return parent_transform.xform(Vector2(parent_size.x * anchor.x, parent_size.y * anchor.y)); +} + void CanvasItemEditor::_prepare_drag(const Point2 &p_click_pos) { List<Node *> &selection = editor_selection->get_selected_node_list(); @@ -1528,76 +1537,75 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { // Keep the height/width ratio of the item float aspect = local_rect.size.aspect(); switch (drag) { - case DRAG_LEFT: { + case DRAG_LEFT: drag_vector.y = -drag_vector.x / aspect; - } break; - case DRAG_RIGHT: { + break; + case DRAG_RIGHT: drag_vector.y = drag_vector.x / aspect; - } break; - case DRAG_TOP: { + break; + case DRAG_TOP: drag_vector.x = -drag_vector.y * aspect; - } break; - case DRAG_BOTTOM: { + break; + case DRAG_BOTTOM: drag_vector.x = drag_vector.y * aspect; - } break; + break; case DRAG_BOTTOM_LEFT: - case DRAG_TOP_RIGHT: { + case DRAG_TOP_RIGHT: if (aspect > 1.0) { // width > height, take x as reference drag_vector.y = -drag_vector.x / aspect; } else { // height > width, take y as reference drag_vector.x = -drag_vector.y * aspect; } - } break; + break; case DRAG_BOTTOM_RIGHT: - case DRAG_TOP_LEFT: { + case DRAG_TOP_LEFT: if (aspect > 1.0) { // width > height, take x as reference drag_vector.y = drag_vector.x / aspect; } else { // height > width, take y as reference drag_vector.x = drag_vector.y * aspect; } - } break; + break; } } else { switch (drag) { case DRAG_RIGHT: - case DRAG_LEFT: { + case DRAG_LEFT: drag_vector.y = 0; - } break; + break; case DRAG_TOP: - case DRAG_BOTTOM: { + case DRAG_BOTTOM: drag_vector.x = 0; - } break; + break; } } switch (drag) { - case DRAG_ALL: { + case DRAG_ALL: begin += drag_vector; end += drag_vector; - } break; + break; case DRAG_RIGHT: case DRAG_BOTTOM: - case DRAG_BOTTOM_RIGHT: { + case DRAG_BOTTOM_RIGHT: incend(begin.x, end.x, drag_vector.x, minsize.x, symmetric); incend(begin.y, end.y, drag_vector.y, minsize.y, symmetric); - } break; - - case DRAG_TOP_LEFT: { + break; + case DRAG_TOP_LEFT: incbeg(begin.x, end.x, drag_vector.x, minsize.x, symmetric); incbeg(begin.y, end.y, drag_vector.y, minsize.y, symmetric); - } break; - + break; case DRAG_TOP: - case DRAG_TOP_RIGHT: { + case DRAG_TOP_RIGHT: incbeg(begin.y, end.y, drag_vector.y, minsize.y, symmetric); incend(begin.x, end.x, drag_vector.x, minsize.x, symmetric); - } break; + break; case DRAG_LEFT: - case DRAG_BOTTOM_LEFT: { + case DRAG_BOTTOM_LEFT: incbeg(begin.x, end.x, drag_vector.x, minsize.x, symmetric); incend(begin.y, end.y, drag_vector.y, minsize.y, symmetric); - } break; - case DRAG_PIVOT: { + break; + + case DRAG_PIVOT: if (canvas_item->cast_to<Node2D>()) { Node2D *n2d = canvas_item->cast_to<Node2D>(); @@ -1607,15 +1615,13 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { canvas_item->cast_to<Control>()->set_pivot_offset(se->undo_pivot + drag_vector); } continue; - } break; - case DRAG_NODE_2D: { + break; + case DRAG_NODE_2D: ERR_FAIL_COND(!canvas_item->cast_to<Node2D>()); canvas_item->cast_to<Node2D>()->set_global_position(dto); continue; - } break; - - default: {} + break; } if (!dragging_bone) { @@ -1870,13 +1876,32 @@ void CanvasItemEditor::_viewport_draw() { pivot_found = true; } } - if (canvas_item->cast_to<Control>()) { - Vector2 pivot_ofs = canvas_item->cast_to<Control>()->get_pivot_offset(); + + Control *control = canvas_item->cast_to<Control>(); + if (control) { + Vector2 pivot_ofs = control->get_pivot_offset(); if (pivot_ofs != Vector2()) { viewport->draw_texture(pivot, xform.xform(pivot_ofs) + (-pivot->get_size() / 2).floor()); } can_move_pivot = true; pivot_found = true; + + if (tool == TOOL_SELECT) { + // Draw the anchors + Rect2 anchor_rects[4]; + anchor_rects[0] = Rect2(xform.xform(_anchor_to_position(control, Vector2(control->get_anchor(MARGIN_LEFT), control->get_anchor(MARGIN_TOP)))), anchor_handle->get_size()); + anchor_rects[1] = Rect2(xform.xform(_anchor_to_position(control, Vector2(control->get_anchor(MARGIN_RIGHT), control->get_anchor(MARGIN_TOP)))), Point2(-anchor_handle->get_size().x, anchor_handle->get_size().y)); + anchor_rects[2] = Rect2(xform.xform(_anchor_to_position(control, Vector2(control->get_anchor(MARGIN_RIGHT), control->get_anchor(MARGIN_BOTTOM)))), -anchor_handle->get_size()); + anchor_rects[3] = Rect2(xform.xform(_anchor_to_position(control, Vector2(control->get_anchor(MARGIN_LEFT), control->get_anchor(MARGIN_BOTTOM)))), Point2(anchor_handle->get_size().x, -anchor_handle->get_size().y)); + + anchor_rects[0].position -= anchor_handle->get_size(); + anchor_rects[1].position -= Vector2(0.0, anchor_handle->get_size().y); + anchor_rects[3].position -= Vector2(anchor_handle->get_size().x, 0.0); + + for (int i = 0; i < 4; i++) { + anchor_handle->draw_rect(ci, anchor_rects[i]); + } + } } if (tool == TOOL_SELECT) { @@ -2063,19 +2088,27 @@ void CanvasItemEditor::_notification(int p_what) { continue; Rect2 r = canvas_item->get_item_rect(); - Transform2D xform = canvas_item->get_transform(); + float anchors[4]; Vector2 pivot; if (canvas_item->cast_to<Control>()) { pivot = canvas_item->cast_to<Control>()->get_pivot_offset(); + anchors[MARGIN_LEFT] = canvas_item->cast_to<Control>()->get_anchor(MARGIN_LEFT); + anchors[MARGIN_RIGHT] = canvas_item->cast_to<Control>()->get_anchor(MARGIN_RIGHT); + anchors[MARGIN_TOP] = canvas_item->cast_to<Control>()->get_anchor(MARGIN_TOP); + anchors[MARGIN_BOTTOM] = canvas_item->cast_to<Control>()->get_anchor(MARGIN_BOTTOM); } - if (r != se->prev_rect || xform != se->prev_xform || pivot != se->prev_pivot) { + if (r != se->prev_rect || xform != se->prev_xform || pivot != se->prev_pivot || anchors[MARGIN_LEFT] != se->prev_anchors[MARGIN_LEFT] || anchors[MARGIN_RIGHT] != se->prev_anchors[MARGIN_RIGHT] || anchors[MARGIN_TOP] != se->prev_anchors[MARGIN_TOP] || anchors[MARGIN_BOTTOM] != se->prev_anchors[MARGIN_BOTTOM]) { viewport->update(); se->prev_rect = r; se->prev_xform = xform; se->prev_pivot = pivot; + se->prev_anchors[MARGIN_LEFT] = anchors[MARGIN_LEFT]; + se->prev_anchors[MARGIN_RIGHT] = anchors[MARGIN_RIGHT]; + se->prev_anchors[MARGIN_TOP] = anchors[MARGIN_TOP]; + se->prev_anchors[MARGIN_BOTTOM] = anchors[MARGIN_BOTTOM]; } } @@ -2124,6 +2157,7 @@ void CanvasItemEditor::_notification(int p_what) { pan_button->set_icon(get_icon("ToolPan", "EditorIcons")); pivot_button->set_icon(get_icon("EditPivot", "EditorIcons")); select_handle = get_icon("EditorHandle", "EditorIcons"); + anchor_handle = get_icon("EditorControlAnchor", "EditorIcons"); lock_button->set_icon(get_icon("Lock", "EditorIcons")); unlock_button->set_icon(get_icon("Unlock", "EditorIcons")); group_button->set_icon(get_icon("Group", "EditorIcons")); diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index 53cfb88858..bd6591011a 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -57,6 +57,7 @@ public: float prev_rot; Rect2 prev_rect; Vector2 prev_pivot; + float prev_anchors[4]; CanvasItemEditorSelectedItem() { prev_rot = 0; } }; @@ -300,6 +301,7 @@ class CanvasItemEditor : public VBoxContainer { #endif Ref<StyleBoxTexture> select_sb; Ref<Texture> select_handle; + Ref<Texture> anchor_handle; int handle_len; bool _is_part_of_subscene(CanvasItem *p_item); @@ -328,6 +330,8 @@ class CanvasItemEditor : public VBoxContainer { DragType _find_drag_type(const Point2 &p_click, Vector2 &r_point); void _prepare_drag(const Point2 &p_click_pos); + Vector2 _anchor_to_position(Control *p_control, Vector2 anchor); + void _popup_callback(int p_op); bool updating_scroll; void _update_scroll(float); diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 4d0678db74..f7058989a0 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1304,6 +1304,7 @@ void Control::set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin) { _size_changed(); } } + update(); _change_notify(); } |