diff options
30 files changed, 713 insertions, 242 deletions
diff --git a/editor/call_dialog.cpp b/editor/call_dialog.cpp index 8cf9ed6ef4..32b9fc1254 100644 --- a/editor/call_dialog.cpp +++ b/editor/call_dialog.cpp @@ -253,8 +253,8 @@ CallDialog::CallDialog() { call->set_anchor( MARGIN_TOP, ANCHOR_END ); call->set_anchor( MARGIN_RIGHT, ANCHOR_END ); call->set_anchor( MARGIN_BOTTOM, ANCHOR_END ); - call->set_begin( Point2( 70, 29 ) ); - call->set_end( Point2( 15, 15 ) ); + call->set_begin( Point2( -70, -29 ) ); + call->set_end( Point2( -15, -15 ) ); call->set_text(TTR("Call")); add_child(call); @@ -262,8 +262,8 @@ CallDialog::CallDialog() { cancel = memnew( Button ); cancel->set_anchor( MARGIN_TOP, ANCHOR_END ); cancel->set_anchor( MARGIN_BOTTOM, ANCHOR_END ); - cancel->set_begin( Point2( 15, 29 ) ); - cancel->set_end( Point2( 70, 15 ) ); + cancel->set_begin( Point2( -15, 29 ) ); + cancel->set_end( Point2( 70, -15 ) ); cancel->set_text(TTR("Close")); add_child(cancel); @@ -272,7 +272,7 @@ CallDialog::CallDialog() { tree->set_anchor( MARGIN_BOTTOM, ANCHOR_END ); tree->set_begin( Point2( 20,50 ) ); - tree->set_margin(MARGIN_BOTTOM, 44 ); + tree->set_margin(MARGIN_BOTTOM, -44 ); tree->set_margin(MARGIN_RIGHT, 0.5 ); tree->set_select_mode( Tree::SELECT_ROW ); add_child(tree); @@ -282,10 +282,10 @@ CallDialog::CallDialog() { property_editor = memnew( PropertyEditor ); - property_editor->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 15 ); + property_editor->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, -15 ); property_editor->set_anchor_and_margin( MARGIN_TOP, ANCHOR_BEGIN, 50 ); //property_editor->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.55 ); - property_editor->set_anchor_and_margin( MARGIN_BOTTOM, ANCHOR_END, 90 ); + property_editor->set_anchor_and_margin( MARGIN_BOTTOM, ANCHOR_END, -90 ); property_editor->get_scene_tree()->set_hide_root( true ); property_editor->hide_top_label(); @@ -305,15 +305,15 @@ CallDialog::CallDialog() { return_label = memnew( Label ); //return_label->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.53 ); - return_label->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, 85 ); + return_label->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, -85 ); return_label->set_text(TTR("Return:")); add_child(return_label); return_value = memnew( LineEdit ); //return_value->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.55 ); - return_value->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 15 ); - return_value->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, 65 ); + return_value->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, -15 ); + return_value->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, -65 ); add_child(return_value); diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 2e406fb23d..07c0945114 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -70,7 +70,7 @@ GotoLineDialog::GotoLineDialog() { line = memnew(LineEdit); line->set_anchor(MARGIN_RIGHT, ANCHOR_END); line->set_begin(Point2(15, 22)); - line->set_end(Point2(15, 35)); + line->set_end(Point2(-15, 35)); add_child(line); register_text_enter(line); text_editor = NULL; @@ -887,7 +887,7 @@ FindReplaceDialog::FindReplaceDialog() { replace_text = memnew(LineEdit); replace_text->set_anchor(MARGIN_RIGHT, ANCHOR_END); replace_text->set_begin(Point2(15, 132)); - replace_text->set_end(Point2(15, 135)); + replace_text->set_end(Point2(-15, 135)); //replace_text->set_self_opacity(0.7); replace_mc->add_child(replace_text); @@ -937,8 +937,8 @@ FindReplaceDialog::FindReplaceDialog() { skip->set_anchor(MARGIN_TOP, ANCHOR_END); skip->set_anchor(MARGIN_RIGHT, ANCHOR_END); skip->set_anchor(MARGIN_BOTTOM, ANCHOR_END); - skip->set_begin(Point2(70, button_margin)); - skip->set_end(Point2(10, margin)); + skip->set_begin(Point2(-70, -button_margin)); + skip->set_end(Point2(-10, -margin)); skip->set_text(TTR("Skip")); add_child(skip); skip->connect("pressed", this, "_skip_pressed"); diff --git a/editor/editor_name_dialog.cpp b/editor/editor_name_dialog.cpp index 6ebfcbf313..74bb526ccc 100644 --- a/editor/editor_name_dialog.cpp +++ b/editor/editor_name_dialog.cpp @@ -87,6 +87,6 @@ EditorNameDialog::EditorNameDialog() { makevb->add_child(name); name->set_margin(MARGIN_TOP, 5); name->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 5); - name->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 5); + name->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -5); name->connect("gui_input", this, "_line_gui_input"); } 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/output_strings.cpp b/editor/output_strings.cpp index eb2fc62f03..8c9f8386ef 100644 --- a/editor/output_strings.cpp +++ b/editor/output_strings.cpp @@ -38,15 +38,15 @@ void OutputStrings::update_scrollbars() { v_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END); v_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END); - v_scroll->set_begin(Point2(vmin.width, 0)); + v_scroll->set_begin(Point2(-vmin.width, 0)); v_scroll->set_end(Point2(0, 0)); h_scroll->set_anchor(MARGIN_RIGHT, ANCHOR_END); h_scroll->set_anchor(MARGIN_TOP, ANCHOR_END); h_scroll->set_anchor(MARGIN_BOTTOM, ANCHOR_END); - h_scroll->set_begin(Point2(0, hmin.y)); - h_scroll->set_end(Point2(vmin.x, 0)); + h_scroll->set_begin(Point2(0, -hmin.y)); + h_scroll->set_end(Point2(-vmin.x, 0)); margin.y = hmin.y; margin.x = vmin.x; diff --git a/editor/plugins/animation_tree_editor_plugin.cpp b/editor/plugins/animation_tree_editor_plugin.cpp index 156a74144c..0ffd1c79c2 100644 --- a/editor/plugins/animation_tree_editor_plugin.cpp +++ b/editor/plugins/animation_tree_editor_plugin.cpp @@ -1371,7 +1371,7 @@ AnimationTreeEditor::AnimationTreeEditor() { edit_option = memnew(OptionButton); edit_option->set_anchor(MARGIN_RIGHT, ANCHOR_END); - edit_option->set_margin(MARGIN_RIGHT, 10); + edit_option->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(edit_option); edit_option->connect("item_selected", this, "_edit_dialog_changedf"); edit_option->hide(); @@ -1379,7 +1379,7 @@ AnimationTreeEditor::AnimationTreeEditor() { for (int i = 0; i < 2; i++) { edit_scroll[i] = memnew(HSlider); edit_scroll[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END); - edit_scroll[i]->set_margin(MARGIN_RIGHT, 10); + edit_scroll[i]->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(edit_scroll[i]); edit_scroll[i]->hide(); edit_scroll[i]->connect("value_changed", this, "_edit_dialog_changedf"); @@ -1387,7 +1387,7 @@ AnimationTreeEditor::AnimationTreeEditor() { for (int i = 0; i < 4; i++) { edit_line[i] = memnew(LineEdit); edit_line[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END); - edit_line[i]->set_margin(MARGIN_RIGHT, 10); + edit_line[i]->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(edit_line[i]); edit_line[i]->hide(); edit_line[i]->connect("text_changed", this, "_edit_dialog_changeds"); @@ -1399,14 +1399,14 @@ AnimationTreeEditor::AnimationTreeEditor() { edit_button = memnew(Button); edit_button->set_anchor(MARGIN_RIGHT, ANCHOR_END); - edit_button->set_margin(MARGIN_RIGHT, 10); + edit_button->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(edit_button); edit_button->hide(); edit_button->connect("pressed", this, "_edit_oneshot_start"); edit_check = memnew(CheckButton); edit_check->set_anchor(MARGIN_RIGHT, ANCHOR_END); - edit_check->set_margin(MARGIN_RIGHT, 10); + edit_check->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(edit_check); edit_check->hide(); edit_check->connect("pressed", this, "_edit_dialog_changed"); @@ -1428,7 +1428,7 @@ AnimationTreeEditor::AnimationTreeEditor() { filter_button = memnew(Button); filter_button->set_anchor(MARGIN_RIGHT, ANCHOR_END); - filter_button->set_margin(MARGIN_RIGHT, 10); + filter_button->set_margin(MARGIN_RIGHT, -10); edit_dialog->add_child(filter_button); filter_button->hide(); filter_button->set_text(TTR("Filters..")); diff --git a/editor/plugins/camera_editor_plugin.cpp b/editor/plugins/camera_editor_plugin.cpp index b8f8464bae..8e625c4a73 100644 --- a/editor/plugins/camera_editor_plugin.cpp +++ b/editor/plugins/camera_editor_plugin.cpp @@ -88,7 +88,7 @@ CameraEditor::CameraEditor() { preview->set_toggle_mode(true); preview->set_anchor(MARGIN_LEFT, Control::ANCHOR_END); preview->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END); - preview->set_margin(MARGIN_LEFT, 60); + preview->set_margin(MARGIN_LEFT, -60); preview->set_margin(MARGIN_RIGHT, 0); preview->set_margin(MARGIN_TOP, 0); preview->set_margin(MARGIN_BOTTOM, 10); diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index a35d7a9a50..64428e9506 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -709,8 +709,8 @@ CanvasItem *CanvasItemEditor::get_single_item() { return single_item; } -CanvasItemEditor::DragType CanvasItemEditor::_find_drag_type(const Point2 &p_click, Vector2 &r_point) { - +CanvasItemEditor::DragType CanvasItemEditor::_get_resize_handle_drag_type(const Point2 &p_click, Vector2 &r_point) { + // Returns a drag type if a resize handle is clicked CanvasItem *canvas_item = get_single_item(); ERR_FAIL_COND_V(!canvas_item, DRAG_NONE); @@ -775,6 +775,90 @@ CanvasItemEditor::DragType CanvasItemEditor::_find_drag_type(const Point2 &p_cli return DRAG_NONE; } +float CanvasItemEditor::_anchor_snap(float p_anchor, bool *p_snapped, float p_opposite_anchor) { + bool snapped = false; + float dist, dist_min = 0.0; + float radius = 0.05 / zoom; + float basic_anchors[3] = { ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_END }; + for (int i = 0; i < 3; i++) { + if ((dist = fabs(p_anchor - basic_anchors[i])) < radius) { + if (!snapped || dist <= dist_min) { + p_anchor = basic_anchors[i]; + dist_min = dist; + snapped = true; + } + } + } + if (p_opposite_anchor >= 0 && (dist = fabs(p_anchor - p_opposite_anchor)) < radius) { + if (!snapped || dist <= dist_min) { + p_anchor = p_opposite_anchor; + dist_min = dist; + snapped = true; + } + } + if (p_snapped) + *p_snapped = snapped; + return p_anchor; +} + +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)); +} + +Vector2 CanvasItemEditor::_position_to_anchor(Control *p_control, Vector2 position) { + ERR_FAIL_COND_V(!p_control, Vector2()); + Size2 parent_size = p_control->get_parent_area_size(); + + return p_control->get_transform().xform(position) / parent_size; +} + +CanvasItemEditor::DragType CanvasItemEditor::_get_anchor_handle_drag_type(const Point2 &p_click, Vector2 &r_point) { + // Returns a drag type if an anchor handle is clicked + CanvasItem *canvas_item = get_single_item(); + ERR_FAIL_COND_V(!canvas_item, DRAG_NONE); + + Control *control = canvas_item->cast_to<Control>(); + ERR_FAIL_COND_V(!control, DRAG_NONE); + + Vector2 anchor_pos[4]; + anchor_pos[0] = Vector2(control->get_anchor(MARGIN_LEFT), control->get_anchor(MARGIN_TOP)); + anchor_pos[1] = Vector2(control->get_anchor(MARGIN_RIGHT), control->get_anchor(MARGIN_TOP)); + anchor_pos[2] = Vector2(control->get_anchor(MARGIN_RIGHT), control->get_anchor(MARGIN_BOTTOM)); + anchor_pos[3] = Vector2(control->get_anchor(MARGIN_LEFT), control->get_anchor(MARGIN_BOTTOM)); + + Rect2 anchor_rects[4]; + for (int i = 0; i < 4; i++) { + anchor_pos[i] = (transform * control->get_global_transform_with_canvas()).xform(_anchor_to_position(control, anchor_pos[i])); + anchor_rects[i] = Rect2(anchor_pos[i], anchor_handle->get_size()); + anchor_rects[i].position -= anchor_handle->get_size() * Vector2(i == 0 || i == 3, i <= 1); + } + + DragType dragger[] = { + DRAG_ANCHOR_TOP_LEFT, + DRAG_ANCHOR_TOP_RIGHT, + DRAG_ANCHOR_BOTTOM_RIGHT, + DRAG_ANCHOR_BOTTOM_LEFT, + }; + + for (int i = 0; i < 4; i++) { + if (anchor_rects[i].has_point(p_click)) { + r_point = transform.affine_inverse().xform(anchor_pos[i]); + if ((anchor_pos[0] == anchor_pos[2]) && (anchor_pos[0].distance_to(p_click) < anchor_handle->get_size().length() / 3.0)) { + return DRAG_ANCHOR_ALL; + } else { + return dragger[i]; + } + } + } + + return DRAG_NONE; +} + void CanvasItemEditor::_prepare_drag(const Point2 &p_click_pos) { List<Node *> &selection = editor_selection->get_selected_node_list(); @@ -1265,6 +1349,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (b) { bool ik_found = false; + bool first = true; while (b) { @@ -1336,8 +1421,8 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } } - // Drag - drag = _find_drag_type(click, drag_point_from); + // Drag resize handles + drag = _get_resize_handle_drag_type(click, drag_point_from); if (drag != DRAG_NONE) { drag_from = transform.affine_inverse().xform(click); se->undo_state = canvas_item->edit_get_state(); @@ -1347,6 +1432,16 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { se->undo_pivot = canvas_item->cast_to<Control>()->get_pivot_offset(); return; } + + // Drag anchor handles + if (canvas_item->cast_to<Control>()) { + drag = _get_anchor_handle_drag_type(click, drag_point_from); + if (drag != DRAG_NONE) { + drag_from = transform.affine_inverse().xform(click); + se->undo_state = canvas_item->edit_get_state(); + return; + } + } } } @@ -1423,9 +1518,8 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } if (drag == DRAG_NONE) { - if ((m->get_button_mask() & BUTTON_MASK_LEFT && tool == TOOL_PAN) || m->get_button_mask() & BUTTON_MASK_MIDDLE || (m->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE))) { - + // Pan the viewport Point2i relative; if (bool(EditorSettings::get_singleton()->get("editors/2d/warped_mouse_panning"))) { relative = Input::get_singleton()->warp_mouse_motion(m, viewport->get_global_rect()); @@ -1441,7 +1535,6 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } List<Node *> &selection = editor_selection->get_selected_node_list(); - for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { CanvasItem *canvas_item = E->get()->cast_to<CanvasItem>(); @@ -1470,7 +1563,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { continue; if (drag == DRAG_ROTATE) { - + // Rotate the node Vector2 center = canvas_item->get_global_transform_with_canvas().get_origin(); { Node2D *node = canvas_item->cast_to<Node2D>(); @@ -1499,10 +1592,47 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { continue; } + Control *control = canvas_item->cast_to<Control>(); + if (control) { + // Drag and snap the anchor + Vector2 anchor = _position_to_anchor(control, canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dto - drag_from + drag_point_from)); + + switch (drag) { + case DRAG_ANCHOR_TOP_LEFT: + control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_RIGHT)), false, false); + control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_BOTTOM)), false, false); + continue; + break; + case DRAG_ANCHOR_TOP_RIGHT: + control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_LEFT)), false, false); + control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_BOTTOM)), false, false); + continue; + break; + case DRAG_ANCHOR_BOTTOM_RIGHT: + control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_LEFT)), false, false); + control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_TOP)), false, false); + continue; + break; + case DRAG_ANCHOR_BOTTOM_LEFT: + control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_RIGHT)), false, false); + control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_TOP)), false, false); + continue; + break; + case DRAG_ANCHOR_ALL: + control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x)); + control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x)); + control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y)); + control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y)); + continue; + break; + } + } + bool uniform = m->get_shift(); bool symmetric = m->get_alt(); - dto = dto - (drag == DRAG_ALL || drag == DRAG_NODE_2D ? drag_from - drag_point_from : Vector2(0, 0)); + if (drag == DRAG_ALL || drag == DRAG_NODE_2D) + dto -= drag_from - drag_point_from; if (uniform && (drag == DRAG_ALL || drag == DRAG_NODE_2D)) { if (ABS(dto.x - drag_point_from.x) > ABS(dto.y - drag_point_from.y)) { @@ -1528,76 +1658,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 +1736,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) { @@ -1768,6 +1895,30 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } } +void CanvasItemEditor::_draw_percentage_at_position(float p_value, Point2 p_position, Margin p_side) { + if (p_value != 0) { + Color color = Color(0.8, 0.8, 0.8, 0.5); + Ref<Font> font = get_font("font", "Label"); + String str = vformat("%.1f %%", p_value * 100.0); + Size2 text_size = font->get_string_size(str); + switch (p_side) { + case MARGIN_LEFT: + p_position += Vector2(-text_size.x - 5, text_size.y / 2); + break; + case MARGIN_TOP: + p_position += Vector2(-text_size.x / 2, -5); + break; + case MARGIN_RIGHT: + p_position += Vector2(5, text_size.y / 2); + break; + case MARGIN_BOTTOM: + p_position += Vector2(-text_size.x / 2, text_size.y + 5); + break; + } + viewport->draw_string(font, p_position, str, color); + } +} + void CanvasItemEditor::_viewport_draw() { // TODO fetch the viewport? @@ -1870,13 +2021,99 @@ 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) { + float anchors_values[4]; + anchors_values[0] = control->get_anchor(MARGIN_LEFT); + anchors_values[1] = control->get_anchor(MARGIN_TOP); + anchors_values[2] = control->get_anchor(MARGIN_RIGHT); + anchors_values[3] = control->get_anchor(MARGIN_BOTTOM); + + // Draw the anchors + Vector2 anchors[4]; + Vector2 anchors_pos[4]; + for (int i = 0; i < 4; i++) { + anchors[i] = Vector2((i % 2 == 0) ? anchors_values[i] : anchors_values[(i + 1) % 4], (i % 2 == 1) ? anchors_values[i] : anchors_values[(i + 1) % 4]); + anchors_pos[i] = xform.xform(_anchor_to_position(control, anchors[i])); + } + + // Get which anchor is dragged + int dragged_anchor = -1; + switch (drag) { + case DRAG_ANCHOR_ALL: + case DRAG_ANCHOR_TOP_LEFT: + dragged_anchor = 0; + break; + case DRAG_ANCHOR_TOP_RIGHT: + dragged_anchor = 1; + break; + case DRAG_ANCHOR_BOTTOM_RIGHT: + dragged_anchor = 2; + break; + case DRAG_ANCHOR_BOTTOM_LEFT: + dragged_anchor = 3; + break; + } + + if (dragged_anchor >= 0) { + // Draw the 4 lines when dragged + bool snapped; + Color color_snapped = Color(0.64, 0.93, 0.67, 0.5); + Color color_base = Color(0.8, 0.8, 0.8, 0.5); + + Vector2 corners_pos[4]; + for (int i = 0; i < 4; i++) { + corners_pos[i] = xform.xform(_anchor_to_position(control, Vector2((i == 0 || i == 3) ? ANCHOR_BEGIN : ANCHOR_END, (i <= 1) ? ANCHOR_BEGIN : ANCHOR_END))); + } + + Vector2 line_starts[4]; + Vector2 line_ends[4]; + for (int i = 0; i < 4; i++) { + float anchor_val = (i >= 2) ? ANCHOR_END - anchors_values[i] : anchors_values[i]; + line_starts[i] = Vector2::linear_interpolate(corners_pos[i], corners_pos[(i + 1) % 4], anchor_val); + line_ends[i] = Vector2::linear_interpolate(corners_pos[(i + 3) % 4], corners_pos[(i + 2) % 4], anchor_val); + _anchor_snap(anchors_values[i], &snapped); + viewport->draw_line(line_starts[i], line_ends[i], snapped ? color_snapped : color_base, (i == dragged_anchor || (i + 3) % 4 == dragged_anchor) ? 2 : 1); + } + + // Display the percentages next to the lines + float percent_val; + percent_val = anchors_values[(dragged_anchor + 2) % 4] - anchors_values[dragged_anchor]; + percent_val = (dragged_anchor >= 2) ? -percent_val : percent_val; + _draw_percentage_at_position(percent_val, (anchors_pos[dragged_anchor] + anchors_pos[(dragged_anchor + 1) % 4]) / 2, (Margin)((dragged_anchor + 1) % 4)); + + percent_val = anchors_values[(dragged_anchor + 3) % 4] - anchors_values[(dragged_anchor + 1) % 4]; + percent_val = ((dragged_anchor + 1) % 4 >= 2) ? -percent_val : percent_val; + _draw_percentage_at_position(percent_val, (anchors_pos[dragged_anchor] + anchors_pos[(dragged_anchor + 3) % 4]) / 2, (Margin)(dragged_anchor)); + + percent_val = anchors_values[(dragged_anchor + 1) % 4]; + percent_val = ((dragged_anchor + 1) % 4 >= 2) ? ANCHOR_END - percent_val : percent_val; + _draw_percentage_at_position(percent_val, (line_starts[dragged_anchor] + anchors_pos[dragged_anchor]) / 2, (Margin)(dragged_anchor)); + + percent_val = anchors_values[dragged_anchor]; + percent_val = (dragged_anchor >= 2) ? ANCHOR_END - percent_val : percent_val; + _draw_percentage_at_position(percent_val, (line_ends[(dragged_anchor + 1) % 4] + anchors_pos[dragged_anchor]) / 2, (Margin)((dragged_anchor + 1) % 4)); + } + + Rect2 anchor_rects[4]; + anchor_rects[0] = Rect2(anchors_pos[0] - anchor_handle->get_size(), anchor_handle->get_size()); + anchor_rects[1] = Rect2(anchors_pos[1] - Vector2(0.0, anchor_handle->get_size().y), Point2(-anchor_handle->get_size().x, anchor_handle->get_size().y)); + anchor_rects[2] = Rect2(anchors_pos[2], -anchor_handle->get_size()); + anchor_rects[3] = Rect2(anchors_pos[3] - Vector2(anchor_handle->get_size().x, 0.0), Point2(anchor_handle->get_size().x, -anchor_handle->get_size().y)); + + for (int i = 0; i < 4; i++) { + anchor_handle->draw_rect(ci, anchor_rects[i]); + } + } } if (tool == TOOL_SELECT) { @@ -2063,19 +2300,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 +2369,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")); @@ -2338,7 +2584,7 @@ void CanvasItemEditor::_update_scroll(float) { viewport->update(); } -void CanvasItemEditor::_set_anchor(Control::AnchorType p_left, Control::AnchorType p_top, Control::AnchorType p_right, Control::AnchorType p_bottom) { +void CanvasItemEditor::_set_anchors_preset(Control::LayoutPreset p_preset) { List<Node *> &selection = editor_selection->get_selected_node_list(); undo_redo->create_action(TTR("Change Anchors")); @@ -2346,10 +2592,7 @@ void CanvasItemEditor::_set_anchor(Control::AnchorType p_left, Control::AnchorTy Control *c = E->get()->cast_to<Control>(); - undo_redo->add_do_method(c, "set_anchor", MARGIN_LEFT, p_left); - undo_redo->add_do_method(c, "set_anchor", MARGIN_TOP, p_top); - undo_redo->add_do_method(c, "set_anchor", MARGIN_RIGHT, p_right); - undo_redo->add_do_method(c, "set_anchor", MARGIN_BOTTOM, p_bottom); + undo_redo->add_do_method(c, "set_anchors_preset", p_preset); undo_redo->add_undo_method(c, "set_anchor", MARGIN_LEFT, c->get_anchor(MARGIN_LEFT)); undo_redo->add_undo_method(c, "set_anchor", MARGIN_TOP, c->get_anchor(MARGIN_TOP)); undo_redo->add_undo_method(c, "set_anchor", MARGIN_RIGHT, c->get_anchor(MARGIN_RIGHT)); @@ -2367,10 +2610,7 @@ void CanvasItemEditor::_set_full_rect() { Control *c = E->get()->cast_to<Control>(); - undo_redo->add_do_method(c, "set_anchor", MARGIN_LEFT, ANCHOR_BEGIN); - undo_redo->add_do_method(c, "set_anchor", MARGIN_TOP, ANCHOR_BEGIN); - undo_redo->add_do_method(c, "set_anchor", MARGIN_RIGHT, ANCHOR_END); - undo_redo->add_do_method(c, "set_anchor", MARGIN_BOTTOM, ANCHOR_END); + undo_redo->add_do_method(c, "set_anchors_preset", PRESET_WIDE); undo_redo->add_do_method(c, "set_margin", MARGIN_LEFT, 0); undo_redo->add_do_method(c, "set_margin", MARGIN_TOP, 0); undo_redo->add_do_method(c, "set_margin", MARGIN_RIGHT, 0); @@ -2591,53 +2831,52 @@ void CanvasItemEditor::_popup_callback(int p_op) { //space_selected_items< proj_vector2_y, compare_items_y >(); } break; case ANCHOR_ALIGN_TOP_LEFT: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_BEGIN); + _set_anchors_preset(PRESET_TOP_LEFT); } break; case ANCHOR_ALIGN_TOP_RIGHT: { - _set_anchor(ANCHOR_END, ANCHOR_BEGIN, ANCHOR_END, ANCHOR_BEGIN); + _set_anchors_preset(PRESET_TOP_RIGHT); } break; case ANCHOR_ALIGN_BOTTOM_LEFT: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_END, ANCHOR_BEGIN, ANCHOR_END); + _set_anchors_preset(PRESET_BOTTOM_LEFT); } break; case ANCHOR_ALIGN_BOTTOM_RIGHT: { - _set_anchor(ANCHOR_END, ANCHOR_END, ANCHOR_END, ANCHOR_END); + _set_anchors_preset(PRESET_BOTTOM_RIGHT); } break; case ANCHOR_ALIGN_CENTER_LEFT: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_BEGIN, ANCHOR_CENTER); + _set_anchors_preset(PRESET_CENTER_LEFT); } break; case ANCHOR_ALIGN_CENTER_RIGHT: { - - _set_anchor(ANCHOR_END, ANCHOR_CENTER, ANCHOR_END, ANCHOR_CENTER); + _set_anchors_preset(PRESET_CENTER_RIGHT); } break; case ANCHOR_ALIGN_CENTER_TOP: { - _set_anchor(ANCHOR_CENTER, ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_BEGIN); + _set_anchors_preset(PRESET_CENTER_TOP); } break; case ANCHOR_ALIGN_CENTER_BOTTOM: { - _set_anchor(ANCHOR_CENTER, ANCHOR_END, ANCHOR_CENTER, ANCHOR_END); + _set_anchors_preset(PRESET_CENTER_BOTTOM); } break; case ANCHOR_ALIGN_CENTER: { - _set_anchor(ANCHOR_CENTER, ANCHOR_CENTER, ANCHOR_CENTER, ANCHOR_CENTER); + _set_anchors_preset(PRESET_CENTER); } break; case ANCHOR_ALIGN_TOP_WIDE: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_END, ANCHOR_BEGIN); + _set_anchors_preset(PRESET_TOP_WIDE); } break; case ANCHOR_ALIGN_LEFT_WIDE: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_END); + _set_anchors_preset(PRESET_LEFT_WIDE); } break; case ANCHOR_ALIGN_RIGHT_WIDE: { - _set_anchor(ANCHOR_END, ANCHOR_BEGIN, ANCHOR_END, ANCHOR_END); + _set_anchors_preset(PRESET_RIGHT_WIDE); } break; case ANCHOR_ALIGN_BOTTOM_WIDE: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_END, ANCHOR_END, ANCHOR_END); + _set_anchors_preset(PRESET_BOTTOM_WIDE); } break; case ANCHOR_ALIGN_VCENTER_WIDE: { - _set_anchor(ANCHOR_CENTER, ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_END); + _set_anchors_preset(PRESET_VCENTER_WIDE); } break; case ANCHOR_ALIGN_HCENTER_WIDE: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_END, ANCHOR_CENTER); + _set_anchors_preset(PRESET_HCENTER_WIDE); } break; case ANCHOR_ALIGN_WIDE: { - _set_anchor(ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_END, ANCHOR_END); + _set_anchors_preset(PRESET_WIDE); } break; case ANCHOR_ALIGN_WIDE_FIT: { _set_full_rect(); @@ -3372,7 +3611,7 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { dialog_val = memnew(SpinBox); dialog_val->set_anchor(MARGIN_RIGHT, ANCHOR_END); dialog_val->set_begin(Point2(15, 25)); - dialog_val->set_end(Point2(10, 25)); + dialog_val->set_end(Point2(-10, 25)); value_dialog->add_child(dialog_val); dialog_val->connect("value_changed", this, "_dialog_value_changed"); select_sb = Ref<StyleBoxTexture>(memnew(StyleBoxTexture)); diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index 4383be251c..9b027fda60 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; } }; @@ -143,11 +144,15 @@ class CanvasItemEditor : public VBoxContainer { DRAG_BOTTOM_RIGHT, DRAG_BOTTOM, DRAG_BOTTOM_LEFT, + DRAG_ANCHOR_TOP_LEFT, + DRAG_ANCHOR_TOP_RIGHT, + DRAG_ANCHOR_BOTTOM_RIGHT, + DRAG_ANCHOR_BOTTOM_LEFT, + DRAG_ANCHOR_ALL, DRAG_ALL, DRAG_ROTATE, DRAG_PIVOT, DRAG_NODE_2D, - }; enum KeyMoveMODE { @@ -300,6 +305,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); @@ -325,8 +331,13 @@ class CanvasItemEditor : public VBoxContainer { void _key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE p_move_mode); void _list_select(const Ref<InputEventMouseButton> &b); - DragType _find_drag_type(const Point2 &p_click, Vector2 &r_point); + DragType _get_resize_handle_drag_type(const Point2 &p_click, Vector2 &r_point); void _prepare_drag(const Point2 &p_click_pos); + DragType _get_anchor_handle_drag_type(const Point2 &p_click, Vector2 &r_point); + + float _anchor_snap(float anchor, bool *snapped = NULL, float p_opposite_anchor = -1); + Vector2 _anchor_to_position(Control *p_control, Vector2 anchor); + Vector2 _position_to_anchor(Control *p_control, Vector2 position); void _popup_callback(int p_op); bool updating_scroll; @@ -355,12 +366,14 @@ class CanvasItemEditor : public VBoxContainer { void _unhandled_key_input(const Ref<InputEvent> &p_ev); + void _draw_percentage_at_position(float p_value, Point2 p_position, Margin p_side); + void _viewport_gui_input(const Ref<InputEvent> &p_event); void _viewport_draw(); void _focus_selection(int p_op); - void _set_anchor(Control::AnchorType p_left, Control::AnchorType p_top, Control::AnchorType p_right, Control::AnchorType p_bottom); + void _set_anchors_preset(Control::LayoutPreset p_preset); void _set_full_rect(); HSplitContainer *palette_split; diff --git a/editor/plugins/multimesh_editor_plugin.cpp b/editor/plugins/multimesh_editor_plugin.cpp index 9520cc5297..a9689cce56 100644 --- a/editor/plugins/multimesh_editor_plugin.cpp +++ b/editor/plugins/multimesh_editor_plugin.cpp @@ -381,7 +381,7 @@ MultiMeshEditor::MultiMeshEditor() { populate_amount = memnew(SpinBox); populate_amount->set_anchor(MARGIN_RIGHT, ANCHOR_END); populate_amount->set_begin(Point2(20, 232)); - populate_amount->set_end(Point2(5, 237)); + populate_amount->set_end(Point2(-5, 237)); populate_amount->set_min(1); populate_amount->set_max(65536); populate_amount->set_value(128); diff --git a/editor/plugins/sample_editor_plugin.cpp b/editor/plugins/sample_editor_plugin.cpp index 739a8abb53..c4f04f0a80 100644 --- a/editor/plugins/sample_editor_plugin.cpp +++ b/editor/plugins/sample_editor_plugin.cpp @@ -366,14 +366,14 @@ SampleEditor::SampleEditor() { sample_texframe = memnew( TextureRect ); add_child(sample_texframe); sample_texframe->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5); - sample_texframe->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5); + sample_texframe->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,-5); sample_texframe->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,30); - sample_texframe->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,5); + sample_texframe->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,-5); info_label = memnew( Label ); sample_texframe->add_child(info_label); info_label->set_area_as_parent_rect(); - info_label->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,15); + info_label->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,-15); info_label->set_margin(MARGIN_BOTTOM,4); info_label->set_margin(MARGIN_RIGHT,4); info_label->set_align(Label::ALIGN_RIGHT); diff --git a/editor/plugins/sample_library_editor_plugin.cpp b/editor/plugins/sample_library_editor_plugin.cpp index 5ccfde15ff..9e2bc211cf 100644 --- a/editor/plugins/sample_library_editor_plugin.cpp +++ b/editor/plugins/sample_library_editor_plugin.cpp @@ -452,9 +452,9 @@ SampleLibraryEditor::SampleLibraryEditor() { tree->set_columns(6); add_child(tree); tree->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5); - tree->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5); + tree->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,-5); tree->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,30); - tree->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,5); + tree->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,-5); tree->set_column_titles_visible(true); tree->set_column_title(0,TTR("Name")); tree->set_column_title(1,TTR("Preview")); diff --git a/editor/plugins/sample_player_editor_plugin.cpp b/editor/plugins/sample_player_editor_plugin.cpp index 25f1fe9e17..ea9b646a9b 100644 --- a/editor/plugins/sample_player_editor_plugin.cpp +++ b/editor/plugins/sample_player_editor_plugin.cpp @@ -120,8 +120,8 @@ SamplePlayerEditor::SamplePlayerEditor() { play->set_position(Point2( 5, 5 )); play->set_toggle_mode(true); - play->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,250); - play->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,230); + play->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,-250); + play->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,-230); play->set_anchor_and_margin(MARGIN_TOP,Control::ANCHOR_BEGIN,0); play->set_anchor_and_margin(MARGIN_BOTTOM,Control::ANCHOR_BEGIN,0); @@ -131,15 +131,15 @@ SamplePlayerEditor::SamplePlayerEditor() { stop->set_position(Point2( 35, 5 )); stop->set_toggle_mode(true); - stop->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,220); - stop->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,200); + stop->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,-220); + stop->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,-200); stop->set_anchor_and_margin(MARGIN_TOP,Control::ANCHOR_BEGIN,0); stop->set_anchor_and_margin(MARGIN_BOTTOM,Control::ANCHOR_BEGIN,0); add_child(stop); samples = memnew( OptionButton ); - samples->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,190); - samples->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,5); + samples->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_END,-190); + samples->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,-5); samples->set_anchor_and_margin(MARGIN_TOP,Control::ANCHOR_BEGIN,0); samples->set_anchor_and_margin(MARGIN_BOTTOM,Control::ANCHOR_BEGIN,0); add_child(samples); @@ -182,7 +182,7 @@ SamplePlayerEditorPlugin::SamplePlayerEditorPlugin(EditorNode *p_node) { sample_player_editor->set_anchor(MARGIN_LEFT,Control::ANCHOR_END); sample_player_editor->set_anchor(MARGIN_RIGHT,Control::ANCHOR_END); - sample_player_editor->set_margin(MARGIN_LEFT,250); + sample_player_editor->set_margin(MARGIN_LEFT,-250); sample_player_editor->set_margin(MARGIN_RIGHT,0); sample_player_editor->set_margin(MARGIN_TOP,0); sample_player_editor->set_margin(MARGIN_BOTTOM,10); diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 13fdb7ac9d..97f85d9971 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -2485,7 +2485,7 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed preview_camera = memnew(Button); preview_camera->set_toggle_mode(true); - preview_camera->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, 90 * EDSCALE); + preview_camera->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE); preview_camera->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE); preview_camera->set_text("preview"); surface->add_child(preview_camera); diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index dc2c9a2085..0176f606d6 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -434,13 +434,13 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: } else if (hint == PROPERTY_HINT_EXP_EASING) { easing_draw->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 5 * EDSCALE); - easing_draw->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 5 * EDSCALE); + easing_draw->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -5 * EDSCALE); easing_draw->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 5 * EDSCALE); - easing_draw->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 30 * EDSCALE); + easing_draw->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -30 * EDSCALE); type_button->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 3 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 3 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, 25 * EDSCALE); - type_button->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 7 * EDSCALE); + type_button->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -3 * EDSCALE); + type_button->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -25 * EDSCALE); + type_button->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -7 * EDSCALE); type_button->set_text(TTR("Preset..")); type_button->get_popup()->clear(); type_button->get_popup()->add_item(TTR("Linear"), EASING_LINEAR); @@ -523,8 +523,8 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: action_buttons[0]->set_anchor(MARGIN_TOP, ANCHOR_END); action_buttons[0]->set_anchor(MARGIN_RIGHT, ANCHOR_END); action_buttons[0]->set_anchor(MARGIN_BOTTOM, ANCHOR_END); - action_buttons[0]->set_begin(Point2(70 * EDSCALE, button_margin - 5 * EDSCALE)); - action_buttons[0]->set_end(Point2(margin, margin)); + action_buttons[0]->set_begin(Point2(-70 * EDSCALE, -button_margin + 5 * EDSCALE)); + action_buttons[0]->set_end(Point2(-margin, -margin)); action_buttons[0]->set_text(TTR("Close")); action_buttons[0]->show(); diff --git a/main/tests/test_gui.cpp b/main/tests/test_gui.cpp index 3c6a708cd8..44d4464aac 100644 --- a/main/tests/test_gui.cpp +++ b/main/tests/test_gui.cpp @@ -278,7 +278,7 @@ public: richtext->set_position(Point2(600, 210)); richtext->set_size(Point2(180, 250)); - richtext->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, 20); + richtext->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -20); frame->add_child(richtext); @@ -319,8 +319,6 @@ public: ///richtext->add_text("Hello!\n"); //richtext->pop(); - richtext->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END); - TabContainer *tabc = memnew(TabContainer); Control *ctl = memnew(Control); diff --git a/modules/gridmap/grid_map_editor_plugin.cpp b/modules/gridmap/grid_map_editor_plugin.cpp index 954e865bcd..38f58799df 100644 --- a/modules/gridmap/grid_map_editor_plugin.cpp +++ b/modules/gridmap/grid_map_editor_plugin.cpp @@ -1203,7 +1203,7 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) { edit_mode = memnew(OptionButton); edit_mode->set_area_as_parent_rect(); edit_mode->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 24); - edit_mode->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 14); + edit_mode->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -14); edit_mode->add_item("Tiles"); edit_mode->add_item("Areas"); hb->add_child(edit_mode); diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index 8912227692..456036b32a 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -3271,7 +3271,7 @@ VisualScriptEditor::VisualScriptEditor() { graph->set_area_as_parent_rect(); hint_text = memnew(Label); - hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, 100); + hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100); hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); hint_text->set_align(Label::ALIGN_CENTER); diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 01570e28c4..f68132f4ff 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -51,6 +51,12 @@ Variant Control::edit_get_state() const { s["rect"] = get_rect(); s["rot"] = get_rotation(); s["scale"] = get_scale(); + Array anchors; + anchors.push_back(get_anchor(MARGIN_LEFT)); + anchors.push_back(get_anchor(MARGIN_TOP)); + anchors.push_back(get_anchor(MARGIN_RIGHT)); + anchors.push_back(get_anchor(MARGIN_BOTTOM)); + s["anchors"] = anchors; return s; } void Control::edit_set_state(const Variant &p_state) { @@ -62,6 +68,11 @@ void Control::edit_set_state(const Variant &p_state) { set_size(state.size); set_rotation(s["rot"]); set_scale(s["scale"]); + Array anchors = s["anchors"]; + set_anchor(MARGIN_LEFT, anchors[0]); + set_anchor(MARGIN_TOP, anchors[1]); + set_anchor(MARGIN_RIGHT, anchors[2]); + set_anchor(MARGIN_BOTTOM, anchors[3]); } void Control::set_custom_minimum_size(const Size2 &p_custom) { @@ -1212,21 +1223,7 @@ void Control::_size_changed() { for (int i = 0; i < 4; i++) { float area = parent_size[i & 1]; - switch (data.anchor[i]) { - - case ANCHOR_BEGIN: { - - margin_pos[i] = data.margin[i]; - } break; - case ANCHOR_END: { - - margin_pos[i] = area - data.margin[i]; - } break; - case ANCHOR_CENTER: { - - margin_pos[i] = (area / 2) - data.margin[i]; - } break; - } + margin_pos[i] = data.margin[i] + (data.anchor[i] * area); } Point2 new_pos_cache = Point2(margin_pos[0], margin_pos[1]); @@ -1299,59 +1296,38 @@ float Control::_get_range(int p_idx) const { return to - from; } -float Control::_s2a(float p_val, AnchorType p_anchor, float p_range) const { - - switch (p_anchor) { - - case ANCHOR_BEGIN: { - return p_val; - } break; - case ANCHOR_END: { - return p_range - p_val; - } break; - case ANCHOR_CENTER: { - return (p_range / 2) - p_val; - } break; - } - - return 0; +float Control::_s2a(float p_val, float p_anchor, float p_range) const { + return p_val - (p_anchor * p_range); } -float Control::_a2s(float p_val, AnchorType p_anchor, float p_range) const { - - switch (p_anchor) { - - case ANCHOR_BEGIN: { - return Math::floor(p_val); - } break; - case ANCHOR_END: { - return Math::floor(p_range - p_val); - } break; - case ANCHOR_CENTER: { - return Math::floor((p_range / 2) - p_val); - } break; - } - return 0; +float Control::_a2s(float p_val, float p_anchor, float p_range) const { + return Math::floor(p_val + (p_anchor * p_range)); } -void Control::set_anchor(Margin p_margin, AnchorType p_anchor, bool p_keep_margin) { +void Control::set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin, bool p_push_opposite_anchor) { + bool pushed = false; + data.anchor[p_margin] = CLAMP(p_anchor, 0.0, 1.0); - if (!is_inside_tree()) { + if (((p_margin == MARGIN_LEFT || p_margin == MARGIN_TOP) && data.anchor[p_margin] > data.anchor[(p_margin + 2) % 4]) || + ((p_margin == MARGIN_RIGHT || p_margin == MARGIN_BOTTOM) && data.anchor[p_margin] < data.anchor[(p_margin + 2) % 4])) { + if (p_push_opposite_anchor) { + data.anchor[(p_margin + 2) % 4] = data.anchor[p_margin]; + pushed = true; + } else { + data.anchor[p_margin] = data.anchor[(p_margin + 2) % 4]; + } + } - data.anchor[p_margin] = p_anchor; - } else if (!p_keep_margin) { - float pr = _get_parent_range(p_margin); - float s = _a2s(data.margin[p_margin], data.anchor[p_margin], pr); - data.anchor[p_margin] = p_anchor; - data.margin[p_margin] = _s2a(s, p_anchor, pr); - } else { - data.anchor[p_margin] = p_anchor; - _size_changed(); + if (is_inside_tree()) { + if (p_keep_margin) { + _size_changed(); + } } + update(); _change_notify(); } -void Control::_set_anchor(Margin p_margin, AnchorType p_anchor) { +void Control::_set_anchor(Margin p_margin, float p_anchor) { #ifdef TOOLS_ENABLED if (is_inside_tree() && get_tree()->is_editor_hint()) { set_anchor(p_margin, p_anchor, EDITOR_DEF("editors/2d/keep_margins_when_changing_anchors", false)); @@ -1363,13 +1339,127 @@ void Control::_set_anchor(Margin p_margin, AnchorType p_anchor) { #endif } -void Control::set_anchor_and_margin(Margin p_margin, AnchorType p_anchor, float p_pos) { +void Control::set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor) { - set_anchor(p_margin, p_anchor); + set_anchor(p_margin, p_anchor, false, p_push_opposite_anchor); set_margin(p_margin, p_pos); } -Control::AnchorType Control::get_anchor(Margin p_margin) const { +void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin) { + //Left + switch (p_preset) { + case PRESET_TOP_LEFT: + case PRESET_BOTTOM_LEFT: + case PRESET_CENTER_LEFT: + case PRESET_TOP_WIDE: + case PRESET_BOTTOM_WIDE: + case PRESET_LEFT_WIDE: + case PRESET_HCENTER_WIDE: + case PRESET_WIDE: + set_anchor(MARGIN_LEFT, ANCHOR_BEGIN, p_keep_margin); + break; + + case PRESET_CENTER_TOP: + case PRESET_CENTER_BOTTOM: + case PRESET_CENTER: + case PRESET_VCENTER_WIDE: + set_anchor(MARGIN_LEFT, ANCHOR_CENTER, p_keep_margin); + break; + + case PRESET_TOP_RIGHT: + case PRESET_BOTTOM_RIGHT: + case PRESET_CENTER_RIGHT: + case PRESET_RIGHT_WIDE: + set_anchor(MARGIN_LEFT, ANCHOR_END, p_keep_margin); + break; + } + + // Top + switch (p_preset) { + case PRESET_TOP_LEFT: + case PRESET_TOP_RIGHT: + case PRESET_CENTER_TOP: + case PRESET_LEFT_WIDE: + case PRESET_RIGHT_WIDE: + case PRESET_TOP_WIDE: + case PRESET_VCENTER_WIDE: + case PRESET_WIDE: + set_anchor(MARGIN_TOP, ANCHOR_BEGIN, p_keep_margin); + break; + + case PRESET_CENTER_LEFT: + case PRESET_CENTER_RIGHT: + case PRESET_CENTER: + case PRESET_HCENTER_WIDE: + set_anchor(MARGIN_TOP, ANCHOR_CENTER, p_keep_margin); + break; + + case PRESET_BOTTOM_LEFT: + case PRESET_BOTTOM_RIGHT: + case PRESET_CENTER_BOTTOM: + case PRESET_BOTTOM_WIDE: + set_anchor(MARGIN_TOP, ANCHOR_END, p_keep_margin); + break; + } + + // Right + switch (p_preset) { + case PRESET_TOP_LEFT: + case PRESET_BOTTOM_LEFT: + case PRESET_CENTER_LEFT: + case PRESET_LEFT_WIDE: + set_anchor(MARGIN_RIGHT, ANCHOR_BEGIN, p_keep_margin); + break; + + case PRESET_CENTER_TOP: + case PRESET_CENTER_BOTTOM: + case PRESET_CENTER: + case PRESET_VCENTER_WIDE: + set_anchor(MARGIN_RIGHT, ANCHOR_CENTER, p_keep_margin); + break; + + case PRESET_TOP_RIGHT: + case PRESET_BOTTOM_RIGHT: + case PRESET_CENTER_RIGHT: + case PRESET_TOP_WIDE: + case PRESET_RIGHT_WIDE: + case PRESET_BOTTOM_WIDE: + case PRESET_HCENTER_WIDE: + case PRESET_WIDE: + set_anchor(MARGIN_RIGHT, ANCHOR_END, p_keep_margin); + break; + } + + // Bottom + switch (p_preset) { + case PRESET_TOP_LEFT: + case PRESET_TOP_RIGHT: + case PRESET_CENTER_TOP: + case PRESET_TOP_WIDE: + set_anchor(MARGIN_BOTTOM, ANCHOR_BEGIN, p_keep_margin); + break; + + case PRESET_CENTER_LEFT: + case PRESET_CENTER_RIGHT: + case PRESET_CENTER: + case PRESET_HCENTER_WIDE: + set_anchor(MARGIN_BOTTOM, ANCHOR_CENTER, p_keep_margin); + break; + + case PRESET_BOTTOM_LEFT: + case PRESET_BOTTOM_RIGHT: + case PRESET_CENTER_BOTTOM: + case PRESET_LEFT_WIDE: + case PRESET_RIGHT_WIDE: + case PRESET_BOTTOM_WIDE: + case PRESET_VCENTER_WIDE: + case PRESET_WIDE: + set_anchor(MARGIN_BOTTOM, ANCHOR_END, p_keep_margin); + break; + } +} + +float Control::get_anchor(Margin p_margin) const { return data.anchor[p_margin]; } @@ -1523,11 +1613,13 @@ Rect2 Control::get_item_rect() const { void Control::set_area_as_parent_rect(int p_margin) { data.anchor[MARGIN_LEFT] = ANCHOR_BEGIN; + data.margin[MARGIN_LEFT] = p_margin; data.anchor[MARGIN_TOP] = ANCHOR_BEGIN; + data.margin[MARGIN_TOP] = p_margin; data.anchor[MARGIN_RIGHT] = ANCHOR_END; + data.margin[MARGIN_RIGHT] = -p_margin; data.anchor[MARGIN_BOTTOM] = ANCHOR_END; - for (int i = 0; i < 4; i++) - data.margin[i] = p_margin; + data.margin[MARGIN_BOTTOM] = -p_margin; _size_changed(); } @@ -2371,11 +2463,12 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event); ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size); ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size); - ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor_mode", "keep_margin"), &Control::set_anchor, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor_mode"), &Control::_set_anchor); + ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor", "keep_margin", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor"), &Control::_set_anchor); + ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_margin"), &Control::set_anchors_preset, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_anchor", "margin"), &Control::get_anchor); ClassDB::bind_method(D_METHOD("set_margin", "margin", "offset"), &Control::set_margin); - ClassDB::bind_method(D_METHOD("set_anchor_and_margin", "margin", "anchor_mode", "offset"), &Control::set_anchor_and_margin); + ClassDB::bind_method(D_METHOD("set_anchor_and_margin", "margin", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_margin, DEFVAL(false)); ClassDB::bind_method(D_METHOD("set_begin", "pos"), &Control::set_begin); ClassDB::bind_method(D_METHOD("set_end", "pos"), &Control::set_end); ClassDB::bind_method(D_METHOD("set_position", "pos"), &Control::set_position); @@ -2497,10 +2590,10 @@ void Control::_bind_methods() { BIND_VMETHOD(MethodInfo("drop_data", PropertyInfo(Variant::VECTOR2, "pos"), PropertyInfo(Variant::NIL, "data"))); ADD_GROUP("Anchor", "anchor_"); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_left", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_LEFT); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_top", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_TOP); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_right", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_RIGHT); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_bottom", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_BOTTOM); + ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.01"), "_set_anchor", "get_anchor", MARGIN_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.01"), "_set_anchor", "get_anchor", MARGIN_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.01"), "_set_anchor", "get_anchor", MARGIN_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.01"), "_set_anchor", "get_anchor", MARGIN_BOTTOM); ADD_GROUP("Margin", "margin_"); ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "margin_left", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_LEFT); @@ -2541,9 +2634,6 @@ void Control::_bind_methods() { ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme"); ADD_GROUP("", ""); - BIND_CONSTANT(ANCHOR_BEGIN); - BIND_CONSTANT(ANCHOR_END); - BIND_CONSTANT(ANCHOR_CENTER); BIND_CONSTANT(FOCUS_NONE); BIND_CONSTANT(FOCUS_CLICK); BIND_CONSTANT(FOCUS_ALL); @@ -2574,6 +2664,23 @@ void Control::_bind_methods() { BIND_CONSTANT(CURSOR_HSPLIT); BIND_CONSTANT(CURSOR_HELP); + BIND_CONSTANT(PRESET_TOP_LEFT); + BIND_CONSTANT(PRESET_TOP_RIGHT); + BIND_CONSTANT(PRESET_BOTTOM_LEFT); + BIND_CONSTANT(PRESET_BOTTOM_RIGHT); + BIND_CONSTANT(PRESET_CENTER_LEFT); + BIND_CONSTANT(PRESET_CENTER_TOP); + BIND_CONSTANT(PRESET_CENTER_RIGHT); + BIND_CONSTANT(PRESET_CENTER_BOTTOM); + BIND_CONSTANT(PRESET_CENTER); + BIND_CONSTANT(PRESET_LEFT_WIDE); + BIND_CONSTANT(PRESET_TOP_WIDE); + BIND_CONSTANT(PRESET_RIGHT_WIDE); + BIND_CONSTANT(PRESET_BOTTOM_WIDE); + BIND_CONSTANT(PRESET_VCENTER_WIDE); + BIND_CONSTANT(PRESET_HCENTER_WIDE); + BIND_CONSTANT(PRESET_WIDE); + BIND_CONSTANT(SIZE_EXPAND); BIND_CONSTANT(SIZE_FILL); BIND_CONSTANT(SIZE_EXPAND_FILL); diff --git a/scene/gui/control.h b/scene/gui/control.h index 86cf8f6dbd..60d835044a 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -51,11 +51,9 @@ class Control : public CanvasItem { OBJ_CATEGORY("GUI Nodes"); public: - enum AnchorType { - ANCHOR_BEGIN, - ANCHOR_END, - ANCHOR_CENTER, - }; + static const float ANCHOR_BEGIN = 0.0f; + static const float ANCHOR_CENTER = 0.5f; + static const float ANCHOR_END = 1.0f; enum GrowDirection { GROW_DIRECTION_BEGIN, @@ -105,6 +103,25 @@ public: CURSOR_MAX }; + enum LayoutPreset { + PRESET_TOP_LEFT, + PRESET_TOP_RIGHT, + PRESET_BOTTOM_LEFT, + PRESET_BOTTOM_RIGHT, + PRESET_CENTER_LEFT, + PRESET_CENTER_TOP, + PRESET_CENTER_RIGHT, + PRESET_CENTER_BOTTOM, + PRESET_CENTER, + PRESET_LEFT_WIDE, + PRESET_TOP_WIDE, + PRESET_RIGHT_WIDE, + PRESET_BOTTOM_WIDE, + PRESET_VCENTER_WIDE, + PRESET_HCENTER_WIDE, + PRESET_WIDE + }; + private: struct CComparator { @@ -122,7 +139,7 @@ private: Size2 size_cache; float margin[4]; - AnchorType anchor[4]; + float anchor[4]; FocusMode focus_mode; GrowDirection h_grow; GrowDirection v_grow; @@ -182,12 +199,12 @@ private: void _window_find_focus_neighbour(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, float p_min, float &r_closest_dist, Control **r_closest); Control *_get_focus_neighbour(Margin p_margin, int p_count = 0); - void _set_anchor(Margin p_margin, AnchorType p_anchor); + void _set_anchor(Margin p_margin, float p_anchor); float _get_parent_range(int p_idx) const; float _get_range(int p_idx) const; - float _s2a(float p_val, AnchorType p_anchor, float p_range) const; - float _a2s(float p_val, AnchorType p_anchor, float p_range) const; + float _s2a(float p_val, float p_anchor, float p_range) const; + float _a2s(float p_val, float p_anchor, float p_range) const; void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign = true); void _theme_changed(); @@ -275,10 +292,11 @@ public: /* POSITIONING */ - void set_anchor(Margin p_margin, AnchorType p_anchor, bool p_keep_margin = false); - void set_anchor_and_margin(Margin p_margin, AnchorType p_anchor, float p_pos); + void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = false, bool p_push_opposite_anchor = true); + void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true); + void set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin = false); - AnchorType get_anchor(Margin p_margin) const; + float get_anchor(Margin p_margin) const; void set_margin(Margin p_margin, float p_value); @@ -425,10 +443,10 @@ public: ~Control(); }; -VARIANT_ENUM_CAST(Control::AnchorType); VARIANT_ENUM_CAST(Control::FocusMode); VARIANT_ENUM_CAST(Control::SizeFlags); VARIANT_ENUM_CAST(Control::CursorShape); +VARIANT_ENUM_CAST(Control::LayoutPreset); VARIANT_ENUM_CAST(Control::MouseFilter); VARIANT_ENUM_CAST(Control::GrowDirection); diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp index dedf44d407..d576ff1dbc 100644 --- a/scene/gui/dialogs.cpp +++ b/scene/gui/dialogs.cpp @@ -215,7 +215,7 @@ void WindowDialog::_notification(int p_what) { close_button->set_pressed_texture(get_icon("close", "WindowDialog")); close_button->set_hover_texture(get_icon("close_highlight", "WindowDialog")); close_button->set_anchor(MARGIN_LEFT, ANCHOR_END); - close_button->set_begin(Point2(get_constant("close_h_ofs", "WindowDialog"), -get_constant("close_v_ofs", "WindowDialog"))); + close_button->set_begin(Point2(-get_constant("close_h_ofs", "WindowDialog"), -get_constant("close_v_ofs", "WindowDialog"))); } break; case NOTIFICATION_MOUSE_EXIT: { @@ -546,7 +546,7 @@ AcceptDialog::AcceptDialog() { label->set_anchor(MARGIN_RIGHT, ANCHOR_END); label->set_anchor(MARGIN_BOTTOM, ANCHOR_END); label->set_begin(Point2(margin, margin)); - label->set_end(Point2(margin, button_margin + 10)); + label->set_end(Point2(-margin, -button_margin - 10)); //label->set_autowrap(true); add_child(label); diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index e85ef73f4e..ec1932ed5a 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -269,14 +269,14 @@ void GraphEdit::_notification(int p_what) { Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); - v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, vmin.width); + v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width); v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0); v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0); h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); - h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, hmin.height); + h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height); h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); zoom_minus->set_icon(get_icon("minus")); diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 484051f546..d90f0c7898 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -736,10 +736,10 @@ void ItemList::_notification(int p_what) { Ref<StyleBox> bg = get_stylebox("bg"); int mw = scroll_bar->get_minimum_size().x; - scroll_bar->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, mw + bg->get_margin(MARGIN_RIGHT)); - scroll_bar->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, bg->get_margin(MARGIN_RIGHT)); + scroll_bar->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -mw + bg->get_margin(MARGIN_RIGHT)); + scroll_bar->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -bg->get_margin(MARGIN_RIGHT)); scroll_bar->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, bg->get_margin(MARGIN_TOP)); - scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, bg->get_margin(MARGIN_BOTTOM)); + scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -bg->get_margin(MARGIN_BOTTOM)); Size2 size = get_size(); diff --git a/scene/gui/popup.cpp b/scene/gui/popup.cpp index 8979e0f111..bdd0e8e597 100644 --- a/scene/gui/popup.cpp +++ b/scene/gui/popup.cpp @@ -108,13 +108,10 @@ void Popup::set_as_minsize() { float margin_begin = c->get_margin(m_beg); float margin_end = c->get_margin(m_end); - AnchorType anchor_begin = c->get_anchor(m_beg); - AnchorType anchor_end = c->get_anchor(m_end); + float anchor_begin = c->get_anchor(m_beg); + float anchor_end = c->get_anchor(m_end); - if (anchor_begin == ANCHOR_BEGIN) - minsize[j] += margin_begin; - if (anchor_end == ANCHOR_END) - minsize[j] += margin_end; + minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end; } total_minsize.width = MAX(total_minsize.width, minsize.width); @@ -145,13 +142,10 @@ void Popup::popup_centered_minsize(const Size2 &p_minsize) { float margin_begin = c->get_margin(m_beg); float margin_end = c->get_margin(m_end); - AnchorType anchor_begin = c->get_anchor(m_beg); - AnchorType anchor_end = c->get_anchor(m_end); + float anchor_begin = c->get_anchor(m_beg); + float anchor_end = c->get_anchor(m_end); - if (anchor_begin == ANCHOR_BEGIN) - minsize[j] += margin_begin; - if (anchor_end == ANCHOR_END) - minsize[j] += margin_end; + minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end; } total_minsize.width = MAX(total_minsize.width, minsize.width); diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index 0e4004c27b..42084ade25 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -594,7 +594,7 @@ void RichTextLabel::_update_scroll() { main->first_invalid_line = 0; scroll_w = vscroll->get_combined_minimum_size().width; vscroll->show(); - vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, scroll_w); + vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -scroll_w); _validate_line_caches(main); } else { diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp index cc1a637d2b..939bdd8d0c 100644 --- a/scene/gui/scroll_container.cpp +++ b/scene/gui/scroll_container.cpp @@ -187,14 +187,14 @@ void ScrollContainer::_update_scrollbar_pos() { Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); - v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, vmin.width); + v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width); v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0); v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0); h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); - h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, hmin.height); + h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height); h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); h_scroll->raise(); diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 4b8b180b0e..61a409d43e 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -371,8 +371,10 @@ void TabContainer::add_child_notify(Node *p_child) { if (tabs_visible) c->set_margin(MARGIN_TOP, _get_top_margin()); Ref<StyleBox> sb = get_stylebox("panel"); - for (int i = 0; i < 4; i++) - c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i))); + c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP))); + c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT))); + c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT))); + c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM))); update(); p_child->connect("renamed", this, "_child_renamed_callback"); @@ -402,8 +404,10 @@ void TabContainer::set_current_tab(int p_current) { c->set_area_as_parent_rect(); if (tabs_visible) c->set_margin(MARGIN_TOP, _get_top_margin()); - for (int i = 0; i < 4; i++) - c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i))); + c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP))); + c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT))); + c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT))); + c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM))); } else c->hide(); diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 4f27231050..a22d897669 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1473,8 +1473,8 @@ void Viewport::_gui_show_tooltip() { gui.tooltip_label->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_LEFT)); gui.tooltip_label->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_TOP)); - gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, ttp->get_margin(MARGIN_RIGHT)); - gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, ttp->get_margin(MARGIN_BOTTOM)); + gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -ttp->get_margin(MARGIN_RIGHT)); + gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -ttp->get_margin(MARGIN_BOTTOM)); gui.tooltip_label->set_text(tooltip); Rect2 r(gui.tooltip_pos + Point2(10, 10), gui.tooltip_label->get_combined_minimum_size() + ttp->get_minimum_size()); Rect2 vr = gui.tooltip_label->get_viewport_rect(); |