diff options
Diffstat (limited to 'editor/plugins')
24 files changed, 2291 insertions, 843 deletions
diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp index cc7fc57cde..de905d80ef 100644 --- a/editor/plugins/animation_player_editor_plugin.cpp +++ b/editor/plugins/animation_player_editor_plugin.cpp @@ -55,79 +55,85 @@ void AnimationPlayerEditor::_gui_input(Ref<InputEvent> p_event) { } void AnimationPlayerEditor::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_PROCESS: { - if (p_what == NOTIFICATION_PROCESS) { - - if (!player) - return; + if (!player) + return; - updating = true; + updating = true; - if (player->is_playing()) { + if (player->is_playing()) { - { - String animname = player->get_current_animation(); + { + String animname = player->get_current_animation(); - if (player->has_animation(animname)) { - Ref<Animation> anim = player->get_animation(animname); - if (!anim.is_null()) { + if (player->has_animation(animname)) { + Ref<Animation> anim = player->get_animation(animname); + if (!anim.is_null()) { - frame->set_max(anim->get_length()); + frame->set_max(anim->get_length()); + } } } + frame->set_value(player->get_current_animation_position()); + key_editor->set_anim_pos(player->get_current_animation_position()); + EditorNode::get_singleton()->get_property_editor()->refresh(); + + } else if (last_active) { + //need the last frame after it stopped + + frame->set_value(player->get_current_animation_position()); } - frame->set_value(player->get_current_animation_pos()); - key_editor->set_anim_pos(player->get_current_animation_pos()); - EditorNode::get_singleton()->get_property_editor()->refresh(); - } else if (last_active) { - //need the last frame after it stopped + last_active = player->is_playing(); + //seek->set_val(player->get_position()); + updating = false; - frame->set_value(player->get_current_animation_pos()); - } + } break; - last_active = player->is_playing(); - //seek->set_val(player->get_position()); - updating = false; - } + case NOTIFICATION_ENTER_TREE: { + + save_anim->get_popup()->connect("id_pressed", this, "_animation_save_menu"); + + tool_anim->get_popup()->connect("id_pressed", this, "_animation_tool_menu"); + + blend_editor.next->connect("item_selected", this, "_blend_editor_next_changed"); - if (p_what == NOTIFICATION_ENTER_TREE) { - - //editor->connect("hide_animation_player_editors",this,"_hide_anim_editors"); - add_anim->set_icon(get_icon("New", "EditorIcons")); - rename_anim->set_icon(get_icon("Rename", "EditorIcons")); - duplicate_anim->set_icon(get_icon("Duplicate", "EditorIcons")); - autoplay->set_icon(get_icon("AutoPlay", "EditorIcons")); - load_anim->set_icon(get_icon("Folder", "EditorIcons")); - save_anim->set_icon(get_icon("Save", "EditorIcons")); - save_anim->get_popup()->connect("id_pressed", this, "_animation_save_menu"); - remove_anim->set_icon(get_icon("Remove", "EditorIcons")); - - blend_anim->set_icon(get_icon("Blend", "EditorIcons")); - play->set_icon(get_icon("PlayStart", "EditorIcons")); - play_from->set_icon(get_icon("Play", "EditorIcons")); - play_bw->set_icon(get_icon("PlayStartBackwards", "EditorIcons")); - play_bw_from->set_icon(get_icon("PlayBackwards", "EditorIcons")); - - autoplay_icon = get_icon("AutoPlay", "EditorIcons"); - stop->set_icon(get_icon("Stop", "EditorIcons")); - resource_edit_anim->set_icon(get_icon("EditResource", "EditorIcons")); - pin->set_icon(get_icon("Pin", "EditorIcons")); - tool_anim->set_icon(get_icon("Tools", "EditorIcons")); - tool_anim->get_popup()->connect("id_pressed", this, "_animation_tool_menu"); - - blend_editor.next->connect("item_selected", this, "_blend_editor_next_changed"); - - /* - anim_editor_load->set_normal_texture( get_icon("AnimGet","EditorIcons")); - anim_editor_store->set_normal_texture( get_icon("AnimSet","EditorIcons")); - anim_editor_load->set_pressed_texture( get_icon("AnimGet","EditorIcons")); - anim_editor_store->set_pressed_texture( get_icon("AnimSet","EditorIcons")); - anim_editor_load->set_hover_texture( get_icon("AnimGetHl","EditorIcons")); - anim_editor_store->set_hover_texture( get_icon("AnimSetHl","EditorIcons")); -*/ - - get_tree()->connect("node_removed", this, "_node_removed"); + get_tree()->connect("node_removed", this, "_node_removed"); + + add_style_override("panel", editor->get_gui_base()->get_stylebox("panel", "Panel")); + } break; + + case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + + add_style_override("panel", editor->get_gui_base()->get_stylebox("panel", "Panel")); + } break; + + case NOTIFICATION_THEME_CHANGED: { + + add_anim->set_icon(get_icon("New", "EditorIcons")); + rename_anim->set_icon(get_icon("Rename", "EditorIcons")); + duplicate_anim->set_icon(get_icon("Duplicate", "EditorIcons")); + autoplay->set_icon(get_icon("AutoPlay", "EditorIcons")); + load_anim->set_icon(get_icon("Folder", "EditorIcons")); + save_anim->set_icon(get_icon("Save", "EditorIcons")); + + remove_anim->set_icon(get_icon("Remove", "EditorIcons")); + + blend_anim->set_icon(get_icon("Blend", "EditorIcons")); + play->set_icon(get_icon("PlayStart", "EditorIcons")); + play_from->set_icon(get_icon("Play", "EditorIcons")); + play_bw->set_icon(get_icon("PlayStartBackwards", "EditorIcons")); + play_bw_from->set_icon(get_icon("PlayBackwards", "EditorIcons")); + + autoplay_icon = get_icon("AutoPlay", "EditorIcons"); + stop->set_icon(get_icon("Stop", "EditorIcons")); + resource_edit_anim->set_icon(get_icon("EditResource", "EditorIcons")); + pin->set_icon(get_icon("Pin", "EditorIcons")); + tool_anim->set_icon(get_icon("Tools", "EditorIcons")); + + } break; } } @@ -191,7 +197,7 @@ void AnimationPlayerEditor::_play_from_pressed() { if (current != "") { - float time = player->get_current_animation_pos(); + float time = player->get_current_animation_position(); if (current == player->get_current_animation() && player->is_playing()) { @@ -239,7 +245,7 @@ void AnimationPlayerEditor::_play_bw_from_pressed() { if (current != "") { - float time = player->get_current_animation_pos(); + float time = player->get_current_animation_position(); if (current == player->get_current_animation()) player->stop(); //so it wont blend with itself @@ -938,7 +944,7 @@ void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set) { } if (player->is_valid() && !p_set) { - float cpos = player->get_current_animation_pos(); + float cpos = player->get_current_animation_position(); player->seek_delta(pos, pos - cpos); } else { @@ -1176,7 +1182,6 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) { set_focus_mode(FOCUS_ALL); player = NULL; - add_style_override("panel", editor->get_gui_base()->get_stylebox("panel", "Panel")); Label *l; @@ -1376,7 +1381,6 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) { key_editor = memnew(AnimationKeyEditor); add_child(key_editor); - add_constant_override("separation", get_constant("separation", "VBoxContainer")); key_editor->set_v_size_flags(SIZE_EXPAND_FILL); key_editor->connect("timeline_changed", this, "_animation_key_editor_seek"); key_editor->connect("animation_len_changed", this, "_animation_key_editor_anim_len_changed"); diff --git a/editor/plugins/animation_tree_editor_plugin.cpp b/editor/plugins/animation_tree_editor_plugin.cpp index 054124da8f..4dd877a6ee 100644 --- a/editor/plugins/animation_tree_editor_plugin.cpp +++ b/editor/plugins/animation_tree_editor_plugin.cpp @@ -63,7 +63,7 @@ Size2 AnimationTreeEditor::_get_maximum_size() { for (List<StringName>::Element *E = order.front(); E; E = E->next()) { - Point2 pos = anim_tree->node_get_pos(E->get()); + Point2 pos = anim_tree->node_get_position(E->get()); if (click_type == CLICK_NODE && click_node == E->get()) { @@ -257,7 +257,7 @@ void AnimationTreeEditor::_popup_edit_dialog() { filter_button->hide(); edit_check->hide(); - Point2 pos = anim_tree->node_get_pos(edited_node) - Point2(h_scroll->get_value(), v_scroll->get_value()); + Point2 pos = anim_tree->node_get_position(edited_node) - Point2(h_scroll->get_value(), v_scroll->get_value()); Ref<StyleBox> style = get_stylebox("panel", "PopupMenu"); Size2 size = get_node_size(edited_node); Point2 popup_pos(pos.x + style->get_margin(MARGIN_LEFT), pos.y + size.y - style->get_margin(MARGIN_BOTTOM)); @@ -476,10 +476,10 @@ void AnimationTreeEditor::_draw_node(const StringName &p_node) { Color font_color = get_color("font_color", "PopupMenu"); Color font_color_title = get_color("font_color_hover", "PopupMenu"); font_color_title.a *= 0.8; - Ref<Texture> slot_icon = get_icon("NodeRealSlot", "EditorIcons"); + Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons"); Size2 size = get_node_size(p_node); - Point2 pos = anim_tree->node_get_pos(p_node); + Point2 pos = anim_tree->node_get_position(p_node); if (click_type == CLICK_NODE && click_node == p_node) { pos += click_motion - click_pos; @@ -599,7 +599,7 @@ void AnimationTreeEditor::_draw_node(const StringName &p_node) { if (editable) { - Ref<Texture> arrow = get_icon("arrow", "Tree"); + Ref<Texture> arrow = get_icon("GuiDropdown", "EditorIcons"); Point2 arrow_ofs(w - arrow->get_width(), Math::floor((h - arrow->get_height()) / 2)); arrow->draw(ci, ofs + arrow_ofs); } @@ -618,7 +618,7 @@ AnimationTreeEditor::ClickType AnimationTreeEditor::_locate_click(const Point2 & AnimationTreePlayer::NodeType type = anim_tree->node_get_type(node); - Point2 pos = anim_tree->node_get_pos(node); + Point2 pos = anim_tree->node_get_position(node); Size2 size = get_node_size(node); pos -= Point2(h_scroll->get_value(), v_scroll->get_value()); @@ -671,10 +671,10 @@ Point2 AnimationTreeEditor::_get_slot_pos(const StringName &p_node_id, bool p_in Ref<StyleBox> style = get_stylebox("panel", "PopupMenu"); Ref<Font> font = get_font("font", "PopupMenu"); - Ref<Texture> slot_icon = get_icon("NodeRealSlot", "EditorIcons"); + Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons"); Size2 size = get_node_size(p_node_id); - Point2 pos = anim_tree->node_get_pos(p_node_id); + Point2 pos = anim_tree->node_get_position(p_node_id); if (click_type == CLICK_NODE && click_node == p_node_id) { @@ -806,12 +806,12 @@ void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) { } break; case CLICK_NODE: { - Point2 new_pos = anim_tree->node_get_pos(click_node) + (click_motion - click_pos); + Point2 new_pos = anim_tree->node_get_position(click_node) + (click_motion - click_pos); if (new_pos.x < 5) new_pos.x = 5; if (new_pos.y < 5) new_pos.y = 5; - anim_tree->node_set_pos(click_node, new_pos); + anim_tree->node_set_position(click_node, new_pos); } break; default: {} @@ -1081,7 +1081,7 @@ StringName AnimationTreeEditor::_add_node(int p_item) { } anim_tree->add_node((AnimationTreePlayer::NodeType)p_item, name); - anim_tree->node_set_pos(name, Point2(last_x, last_y)); + anim_tree->node_set_position(name, Point2(last_x, last_y)); order.push_back(name); last_x += 10; last_y += 10; diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 3d5732c749..bb25576007 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -53,6 +53,8 @@ #define MIN_ZOOM 0.01 #define MAX_ZOOM 100 +#define RULER_WIDTH 15 * EDSCALE + class SnapDialog : public ConfirmationDialog { GDCLASS(SnapDialog, ConfirmationDialog); @@ -109,13 +111,13 @@ public: label->set_h_size_flags(SIZE_EXPAND_FILL); grid_step_x = memnew(SpinBox); - grid_step_x->set_min(-SPIN_BOX_GRID_RANGE); + grid_step_x->set_min(0.01); grid_step_x->set_max(SPIN_BOX_GRID_RANGE); grid_step_x->set_suffix("px"); child_container->add_child(grid_step_x); grid_step_y = memnew(SpinBox); - grid_step_y->set_min(-SPIN_BOX_GRID_RANGE); + grid_step_y->set_min(0.01); grid_step_y->set_max(SPIN_BOX_GRID_RANGE); grid_step_y->set_suffix("px"); child_container->add_child(grid_step_y); @@ -149,7 +151,7 @@ public: child_container->add_child(rotation_step); } - void set_fields(const Point2 p_grid_offset, const Size2 p_grid_step, const float p_rotation_offset, const float p_rotation_step) { + void set_fields(const Point2 p_grid_offset, const Point2 p_grid_step, const float p_rotation_offset, const float p_rotation_step) { grid_offset_x->set_value(p_grid_offset.x); grid_offset_y->set_value(p_grid_offset.y); grid_step_x->set_value(p_grid_step.x); @@ -158,11 +160,9 @@ public: rotation_step->set_value(p_rotation_step * (180 / Math_PI)); } - void get_fields(Point2 &p_grid_offset, Size2 &p_grid_step, float &p_rotation_offset, float &p_rotation_step) { - p_grid_offset.x = grid_offset_x->get_value(); - p_grid_offset.y = grid_offset_y->get_value(); - p_grid_step.x = grid_step_x->get_value(); - p_grid_step.y = grid_step_y->get_value(); + void get_fields(Point2 &p_grid_offset, Point2 &p_grid_step, float &p_rotation_offset, float &p_rotation_step) { + p_grid_offset = Point2(grid_offset_x->get_value(), grid_offset_y->get_value()); + p_grid_step = Point2(grid_step_x->get_value(), grid_step_y->get_value()); p_rotation_offset = rotation_offset->get_value() / (180 / Math_PI); p_rotation_step = rotation_step->get_value() / (180 / Math_PI); } @@ -222,6 +222,135 @@ void CanvasItemEditor::_edit_set_pivot(const Vector2 &mouse_pos) { undo_redo->commit_action(); } +void CanvasItemEditor::_snap_if_closer(Point2 p_value, Point2 p_target_snap, Point2 &r_current_snap, bool (&r_snapped)[2], real_t rotation, float p_radius) { + float radius = p_radius / zoom; + float dist; + + Transform2D rot_trans = Transform2D(rotation, Point2()); + p_value = rot_trans.inverse().xform(p_value); + p_target_snap = rot_trans.inverse().xform(p_target_snap); + r_current_snap = rot_trans.inverse().xform(r_current_snap); + + dist = Math::abs(p_value.x - p_target_snap.x); + if (p_radius < 0 || dist < radius && (!r_snapped[0] || dist < Math::abs(r_current_snap.x - p_value.x))) { + r_current_snap.x = p_target_snap.x; + r_snapped[0] = true; + } + + dist = Math::abs(p_value.y - p_target_snap.y); + if (p_radius < 0 || dist < radius && (!r_snapped[1] || dist < Math::abs(r_current_snap.y - p_value.y))) { + r_current_snap.y = p_target_snap.y; + r_snapped[1] = true; + } + + r_current_snap = rot_trans.xform(r_current_snap); +} + +void CanvasItemEditor::_snap_other_nodes(Point2 p_value, Point2 &r_current_snap, bool (&r_snapped)[2], const Node *p_current, const CanvasItem *p_to_snap) { + const CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_current); + if (canvas_item && p_current != p_to_snap) { + Transform2D ci_transform = canvas_item->get_global_transform_with_canvas(); + Transform2D to_snap_transform = p_to_snap->get_global_transform_with_canvas(); + if (ci_transform.get_rotation() == to_snap_transform.get_rotation()) { + Point2 begin = ci_transform.xform(canvas_item->get_item_rect().get_position()); + Point2 end = ci_transform.xform(canvas_item->get_item_rect().get_position() + canvas_item->get_item_rect().get_size()); + + _snap_if_closer(p_value, begin, r_current_snap, r_snapped, ci_transform.get_rotation()); + _snap_if_closer(p_value, end, r_current_snap, r_snapped, ci_transform.get_rotation()); + } + } + for (int i = 0; i < p_current->get_child_count(); i++) { + _snap_other_nodes(p_value, r_current_snap, r_snapped, p_current->get_child(i), p_to_snap); + } +} + +Point2 CanvasItemEditor::snap_point(Point2 p_target, unsigned int p_modes, const CanvasItem *p_canvas_item, unsigned int p_forced_modes) { + Point2 dist[2]; + bool snapped[2] = { false, false }; + + // Smart snap using the canvas position + Vector2 output = p_target; + real_t rotation = 0.0; + + if (p_canvas_item) { + Point2 begin; + Point2 end; + rotation = p_canvas_item->get_global_transform_with_canvas().get_rotation(); + + if ((snap_active && snap_node_parent && (p_modes & SNAP_NODE_PARENT)) || (p_forced_modes & SNAP_NODE_PARENT)) { + // Parent sides and center + bool can_snap = false; + if (const Control *c = Object::cast_to<Control>(p_canvas_item)) { + begin = p_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(0, 0))); + end = p_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(1, 1))); + can_snap = true; + } else if (const CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_canvas_item->get_parent())) { + begin = p_canvas_item->get_transform().affine_inverse().xform(parent_ci->get_item_rect().get_position()); + end = p_canvas_item->get_transform().affine_inverse().xform(parent_ci->get_item_rect().get_position() + parent_ci->get_item_rect().get_size()); + can_snap = true; + } + + if (can_snap) { + _snap_if_closer(p_target, begin, output, snapped, rotation); + _snap_if_closer(p_target, (begin + end) / 2.0, output, snapped, rotation); + _snap_if_closer(p_target, end, output, snapped, rotation); + } + } + + // Self anchors (for sides) + if ((snap_active && snap_node_anchors && (p_modes & SNAP_NODE_ANCHORS)) || (p_forced_modes & SNAP_NODE_ANCHORS)) { + if (const Control *c = Object::cast_to<Control>(p_canvas_item)) { + begin = p_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(c->get_anchor(MARGIN_LEFT), c->get_anchor(MARGIN_TOP)))); + end = p_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(c->get_anchor(MARGIN_RIGHT), c->get_anchor(MARGIN_BOTTOM)))); + _snap_if_closer(p_target, begin, output, snapped, rotation); + _snap_if_closer(p_target, end, output, snapped, rotation); + } + } + + // Self sides (for anchors) + if ((snap_active && snap_node_sides && (p_modes & SNAP_NODE_SIDES)) || (p_forced_modes & SNAP_NODE_SIDES)) { + begin = p_canvas_item->get_global_transform_with_canvas().xform(p_canvas_item->get_item_rect().get_position()); + end = p_canvas_item->get_global_transform_with_canvas().xform(p_canvas_item->get_item_rect().get_position() + p_canvas_item->get_item_rect().get_size()); + _snap_if_closer(p_target, begin, output, snapped, rotation); + _snap_if_closer(p_target, end, output, snapped, rotation); + } + + // Other nodes sides + if ((snap_active && snap_other_nodes && (p_modes & SNAP_OTHER_NODES)) || (p_forced_modes & SNAP_OTHER_NODES)) { + _snap_other_nodes(p_target, output, snapped, get_tree()->get_edited_scene_root(), p_canvas_item); + } + } + + if (((snap_active && snap_grid && (p_modes & SNAP_GRID)) || (p_forced_modes & SNAP_GRID)) && rotation == 0.0) { + // Grid + Point2 offset = grid_offset; + if (snap_relative) { + List<Node *> &selection = editor_selection->get_selected_node_list(); + if (selection.size() == 1 && Object::cast_to<Node2D>(selection[0])) { + offset = Object::cast_to<Node2D>(selection[0])->get_global_position(); + } else { + offset = _find_topleftmost_point(); + } + } + Point2 grid_output; + grid_output.x = Math::stepify(p_target.x - offset.x, grid_step.x * Math::pow(2.0, grid_step_multiplier)) + offset.x; + grid_output.y = Math::stepify(p_target.y - offset.y, grid_step.y * Math::pow(2.0, grid_step_multiplier)) + offset.y; + _snap_if_closer(p_target, grid_output, output, snapped, 0.0, -1.0); + } + + if (((snap_pixel && (p_modes & SNAP_PIXEL)) || (p_forced_modes & SNAP_PIXEL)) && rotation == 0.0) { + // Pixel + output = output.snapped(Size2(1, 1)); + } + + return output; +} + +float CanvasItemEditor::snap_angle(float p_target, float p_start) const { + float offset = snap_relative ? p_start : p_target; + return (snap_rotation && snap_rotation_step != 0) ? Math::stepify(p_target - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset : p_target; +} + void CanvasItemEditor::_unhandled_key_input(const Ref<InputEvent> &p_ev) { Ref<InputEventKey> k = p_ev; @@ -232,22 +361,34 @@ void CanvasItemEditor::_unhandled_key_input(const Ref<InputEvent> &p_ev) { if (k->get_control()) return; - if (k->is_pressed() && !k->is_echo() && k->get_scancode() == KEY_V && drag == DRAG_NONE && can_move_pivot) { - - if (k->get_shift()) { + if (k->is_pressed() && !k->is_echo()) { + if (drag_pivot_shortcut.is_valid() && drag_pivot_shortcut->is_shortcut(p_ev) && drag == DRAG_NONE && can_move_pivot) { //move drag pivot drag = DRAG_PIVOT; - } else if (!Input::get_singleton()->is_mouse_button_pressed(0)) { - - List<Node *> &selection = editor_selection->get_selected_node_list(); - Vector2 mouse_pos = viewport->get_local_mouse_pos(); - if (selection.size() && viewport->get_rect().has_point(mouse_pos)) { - //just in case, make it work if over viewport - mouse_pos = transform.affine_inverse().xform(mouse_pos); - mouse_pos = snap_point(mouse_pos); - - _edit_set_pivot(mouse_pos); + } else if (set_pivot_shortcut.is_valid() && set_pivot_shortcut->is_shortcut(p_ev) && drag == DRAG_NONE && can_move_pivot) { + if (!Input::get_singleton()->is_mouse_button_pressed(0)) { + List<Node *> &selection = editor_selection->get_selected_node_list(); + Vector2 mouse_pos = viewport->get_local_mouse_position(); + if (selection.size() && viewport->get_rect().has_point(mouse_pos)) { + //just in case, make it work if over viewport + mouse_pos = transform.affine_inverse().xform(mouse_pos); + mouse_pos = snap_point(mouse_pos, SNAP_DEFAULT, _get_single_item()); + + _edit_set_pivot(mouse_pos); + } } + } else if ((snap_grid || show_grid) && multiply_grid_step_shortcut.is_valid() && multiply_grid_step_shortcut->is_shortcut(p_ev)) { + // Multiply the grid size + grid_step_multiplier = MIN(grid_step_multiplier + 1, 12); + viewport_base->update(); + viewport->update(); + } else if ((snap_grid || show_grid) && divide_grid_step_shortcut.is_valid() && divide_grid_step_shortcut->is_shortcut(p_ev)) { + // Divide the grid size + Point2 new_grid_step = grid_step * Math::pow(2.0, grid_step_multiplier - 1); + if (new_grid_step.x >= 1.0 && new_grid_step.y >= 1.0) + grid_step_multiplier--; + viewport_base->update(); + viewport->update(); } } } @@ -272,38 +413,25 @@ Object *CanvasItemEditor::_get_editor_data(Object *p_what) { return memnew(CanvasItemEditorSelectedItem); } -inline float _snap_scalar(float p_offset, float p_step, bool p_snap_relative, float p_target, float p_start) { - float offset = p_snap_relative ? p_start : p_offset; - return p_step != 0 ? Math::stepify(p_target - offset, p_step) + offset : p_target; -} - -Vector2 CanvasItemEditor::snap_point(Vector2 p_target, Vector2 p_start) const { - if (snap_grid) { - p_target.x = _snap_scalar(snap_offset.x, snap_step.x, snap_relative, p_target.x, p_start.x); - p_target.y = _snap_scalar(snap_offset.y, snap_step.y, snap_relative, p_target.y, p_start.y); - } - if (snap_pixel) - p_target = p_target.snapped(Size2(1, 1)); - - return p_target; -} - -float CanvasItemEditor::snap_angle(float p_target, float p_start) const { - return snap_rotation ? _snap_scalar(snap_rotation_offset, snap_rotation_step, snap_relative, p_target, p_start) : p_target; -} - Dictionary CanvasItemEditor::get_state() const { Dictionary state; state["zoom"] = zoom; state["ofs"] = Point2(h_scroll->get_value(), v_scroll->get_value()); //state["ofs"]=-transform.get_origin(); - state["snap_offset"] = snap_offset; - state["snap_step"] = snap_step; + state["grid_offset"] = grid_offset; + state["grid_step"] = grid_step; state["snap_rotation_offset"] = snap_rotation_offset; state["snap_rotation_step"] = snap_rotation_step; + state["snap_active"] = snap_active; + state["snap_node_parent"] = snap_node_parent; + state["snap_node_anchors"] = snap_node_anchors; + state["snap_node_sides"] = snap_node_sides; + state["snap_other_nodes"] = snap_other_nodes; state["snap_grid"] = snap_grid; - state["snap_show_grid"] = snap_show_grid; + state["show_grid"] = show_grid; + state["show_rulers"] = show_rulers; + state["show_helpers"] = show_helpers; state["snap_rotation"] = snap_rotation; state["snap_relative"] = snap_relative; state["snap_pixel"] = snap_pixel; @@ -324,12 +452,12 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) { v_scroll->set_value(ofs.y); } - if (state.has("snap_step")) { - snap_step = state["snap_step"]; + if (state.has("grid_offset")) { + grid_offset = state["grid_offset"]; } - if (state.has("snap_offset")) { - snap_offset = state["snap_offset"]; + if (state.has("grid_step")) { + grid_step = state["grid_step"]; } if (state.has("snap_rotation_step")) { @@ -340,40 +468,81 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) { snap_rotation_offset = state["snap_rotation_offset"]; } + if (state.has("snap_active")) { + snap_active = state["snap_active"]; + snap_button->set_pressed(snap_active); + } + + if (state.has("snap_node_parent")) { + snap_node_parent = state["snap_node_parent"]; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT); + smartsnap_config_popup->set_item_checked(idx, snap_node_parent); + } + + if (state.has("snap_node_anchors")) { + snap_node_anchors = state["snap_node_anchors"]; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS); + smartsnap_config_popup->set_item_checked(idx, snap_node_anchors); + } + + if (state.has("snap_node_sides")) { + snap_node_sides = state["snap_node_sides"]; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES); + smartsnap_config_popup->set_item_checked(idx, snap_node_sides); + } + + if (state.has("snap_other_nodes")) { + snap_other_nodes = state["snap_other_nodes"]; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES); + smartsnap_config_popup->set_item_checked(idx, snap_other_nodes); + } + if (state.has("snap_grid")) { snap_grid = state["snap_grid"]; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE); - edit_menu->get_popup()->set_item_checked(idx, snap_grid); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_GRID); + snap_config_menu->get_popup()->set_item_checked(idx, snap_grid); } - if (state.has("snap_show_grid")) { - snap_show_grid = state["snap_show_grid"]; - int idx = edit_menu->get_popup()->get_item_index(SNAP_SHOW_GRID); - edit_menu->get_popup()->set_item_checked(idx, snap_show_grid); + if (state.has("show_grid")) { + show_grid = state["show_grid"]; + int idx = view_menu->get_popup()->get_item_index(SHOW_GRID); + view_menu->get_popup()->set_item_checked(idx, show_grid); + } + + if (state.has("show_rulers")) { + show_rulers = state["show_rulers"]; + int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS); + view_menu->get_popup()->set_item_checked(idx, show_rulers); + } + + if (state.has("show_helpers")) { + show_helpers = state["show_helpers"]; + int idx = view_menu->get_popup()->get_item_index(SHOW_HELPERS); + view_menu->get_popup()->set_item_checked(idx, show_helpers); } if (state.has("snap_rotation")) { snap_rotation = state["snap_rotation"]; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE_ROTATION); - edit_menu->get_popup()->set_item_checked(idx, snap_rotation); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION); + snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation); } if (state.has("snap_relative")) { snap_relative = state["snap_relative"]; - int idx = edit_menu->get_popup()->get_item_index(SNAP_RELATIVE); - edit_menu->get_popup()->set_item_checked(idx, snap_relative); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE); + snap_config_menu->get_popup()->set_item_checked(idx, snap_relative); } if (state.has("snap_pixel")) { snap_pixel = state["snap_pixel"]; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE_PIXEL); - edit_menu->get_popup()->set_item_checked(idx, snap_pixel); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL); + snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel); } if (state.has("skeleton_show_bones")) { skeleton_show_bones = state["skeleton_show_bones"]; - int idx = skeleton_menu->get_item_index(SKELETON_SHOW_BONES); - skeleton_menu->set_item_checked(idx, skeleton_show_bones); + int idx = skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES); + skeleton_menu->get_popup()->set_item_checked(idx, skeleton_show_bones); } viewport->update(); @@ -495,6 +664,7 @@ void CanvasItemEditor::_select_click_on_empty_area(Point2 p_click_pos, bool p_ap if (!p_append) { editor_selection->clear(); viewport->update(); + viewport_base->update(); }; if (p_box_selection) { @@ -534,6 +704,7 @@ bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_po } viewport->update(); + viewport_base->update(); return still_selected; } @@ -566,7 +737,7 @@ void CanvasItemEditor::_key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE Vector2 drag = p_dir; if (p_snap) - drag *= snap_step; + drag *= grid_step * Math::pow(2.0, grid_step_multiplier); undo_redo->add_undo_method(canvas_item, "edit_set_state", canvas_item->edit_get_state()); @@ -580,7 +751,8 @@ void CanvasItemEditor::_key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE } else { // p_move_mode==MOVE_LOCAL_BASE || p_move_mode==MOVE_LOCAL_WITH_ROT - if (Node2D *node_2d = Object::cast_to<Node2D>(canvas_item)) { + Node2D *node_2d = Object::cast_to<Node2D>(canvas_item); + if (node_2d) { if (p_move_mode == MOVE_LOCAL_WITH_ROT) { Transform2D m; @@ -589,9 +761,10 @@ void CanvasItemEditor::_key_move(const Vector2 &p_dir, bool p_snap, KeyMoveMODE } node_2d->set_position(node_2d->get_position() + drag); - } else if (Control *control = Object::cast_to<Control>(canvas_item)) { - - control->set_position(control->get_position() + drag); + } else { + Control *control = Object::cast_to<Control>(canvas_item); + if (control) + control->set_position(control->get_position() + drag); } } } @@ -647,7 +820,7 @@ int CanvasItemEditor::get_item_count() { return ic; } -CanvasItem *CanvasItemEditor::get_single_item() { +CanvasItem *CanvasItemEditor::_get_single_item() { Map<Node *, Object *> &selection = editor_selection->get_selection(); @@ -672,7 +845,7 @@ CanvasItem *CanvasItemEditor::get_single_item() { 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(); + CanvasItem *canvas_item = _get_single_item(); ERR_FAIL_COND_V(!canvas_item, DRAG_NONE); @@ -736,33 +909,7 @@ CanvasItemEditor::DragType CanvasItemEditor::_get_resize_handle_drag_type(const 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] = { 0.0, 0.5, 1.0 }; - 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) { +Vector2 CanvasItemEditor::_anchor_to_position(const Control *p_control, Vector2 anchor) { ERR_FAIL_COND_V(!p_control, Vector2()); Transform2D parent_transform = p_control->get_transform().affine_inverse(); @@ -771,7 +918,7 @@ Vector2 CanvasItemEditor::_anchor_to_position(Control *p_control, Vector2 anchor 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) { +Vector2 CanvasItemEditor::_position_to_anchor(const Control *p_control, Vector2 position) { ERR_FAIL_COND_V(!p_control, Vector2()); Size2 parent_size = p_control->get_parent_area_size(); @@ -780,7 +927,7 @@ Vector2 CanvasItemEditor::_position_to_anchor(Control *p_control, Vector2 positi 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(); + CanvasItem *canvas_item = _get_single_item(); ERR_FAIL_COND_V(!canvas_item, DRAG_NONE); Control *control = Object::cast_to<Control>(canvas_item); @@ -841,6 +988,8 @@ void CanvasItemEditor::_prepare_drag(const Point2 &p_click_pos) { se->undo_pivot = Object::cast_to<Node2D>(canvas_item)->edit_get_pivot(); if (Object::cast_to<Control>(canvas_item)) se->undo_pivot = Object::cast_to<Control>(canvas_item)->get_pivot_offset(); + + se->pre_drag_xform = canvas_item->get_global_transform_with_canvas(); } if (selection.size() == 1 && Object::cast_to<Node2D>(selection[0])) { @@ -914,28 +1063,12 @@ void CanvasItemEditor::_append_canvas_item(CanvasItem *p_item) { } void CanvasItemEditor::_snap_changed() { - ((SnapDialog *)snap_dialog)->get_fields(snap_offset, snap_step, snap_rotation_offset, snap_rotation_step); + ((SnapDialog *)snap_dialog)->get_fields(grid_offset, grid_step, snap_rotation_offset, snap_rotation_step); + grid_step_multiplier = 0; + viewport_base->update(); viewport->update(); } -void CanvasItemEditor::_dialog_value_changed(double) { - - if (updating_value_dialog) - return; - - switch (last_option) { - - case ZOOM_SET: { - - zoom = dialog_val->get_value() / 100.0; - _update_scroll(0); - viewport->update(); - - } break; - default: {} - } -} - void CanvasItemEditor::_selection_result_pressed(int p_result) { if (selection_results.size() <= p_result) @@ -956,7 +1089,7 @@ void CanvasItemEditor::_selection_menu_hide() { void CanvasItemEditor::_list_select(const Ref<InputEventMouseButton> &b) { - Point2 click = b->get_position(); + Point2 click = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); Node *scene = editor->get_edited_scene(); if (!scene) @@ -1064,7 +1197,7 @@ void CanvasItemEditor::_update_cursor() { viewport->set_default_cursor_shape(c); } -void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { +void CanvasItemEditor::_viewport_base_gui_input(const Ref<InputEvent> &p_event) { { EditorNode *en = editor; @@ -1097,7 +1230,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { float prev_zoom = zoom; zoom = zoom * (1 - (0.05 * b->get_factor())); { - Point2 ofs = b->get_position(); + Point2 ofs = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); ofs = ofs / prev_zoom - ofs / zoom; h_scroll->set_value(h_scroll->get_value() + ofs.x); v_scroll->set_value(v_scroll->get_value() + ofs.y); @@ -1121,7 +1254,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { float prev_zoom = zoom; zoom = zoom * ((0.95 + (0.05 * b->get_factor())) / 0.95); { - Point2 ofs = b->get_position(); + Point2 ofs = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); ofs = ofs / prev_zoom - ofs / zoom; h_scroll->set_value(h_scroll->get_value() + ofs.x); v_scroll->set_value(v_scroll->get_value() + ofs.y); @@ -1210,9 +1343,9 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { if (b->get_button_index() == BUTTON_LEFT && tool == TOOL_EDIT_PIVOT) { if (b->is_pressed()) { // Set the pivot point - Point2 mouse_pos = b->get_position(); + Point2 mouse_pos = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); mouse_pos = transform.affine_inverse().xform(mouse_pos); - mouse_pos = snap_point(mouse_pos); + mouse_pos = snap_point(mouse_pos, SNAP_DEFAULT, _get_single_item()); _edit_set_pivot(mouse_pos); } return; @@ -1333,8 +1466,8 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { E->get().to }; - Vector2 p = Geometry::get_closest_point_to_segment_2d(b->get_position(), s); - float d = p.distance_to(b->get_position()); + Vector2 p = Geometry::get_closest_point_to_segment_2d(viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()), s); + float d = p.distance_to(viewport_scrollable->get_transform().affine_inverse().xform(b->get_position())); if (d < bone_width && d < closest_dist) { Cbone = E; closest_dist = d; @@ -1391,12 +1524,12 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } // Single selected item - CanvasItem *canvas_item = get_single_item(); + CanvasItem *canvas_item = _get_single_item(); if (canvas_item) { CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item); ERR_FAIL_COND(!se); - Point2 click = b->get_position(); + Point2 click = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); // Rotation if ((b->get_control() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) { @@ -1407,6 +1540,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { se->undo_pivot = Object::cast_to<Node2D>(canvas_item)->edit_get_pivot(); if (Object::cast_to<Control>(canvas_item)) se->undo_pivot = Object::cast_to<Control>(canvas_item)->get_pivot_offset(); + se->pre_drag_xform = canvas_item->get_global_transform_with_canvas(); return; } @@ -1428,15 +1562,18 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { se->undo_pivot = Object::cast_to<Node2D>(canvas_item)->edit_get_pivot(); if (Object::cast_to<Control>(canvas_item)) se->undo_pivot = Object::cast_to<Control>(canvas_item)->get_pivot_offset(); + se->pre_drag_xform = canvas_item->get_global_transform_with_canvas(); return; } // Drag anchor handles - if (Object::cast_to<Control>(canvas_item)) { + Control *control = Object::cast_to<Control>(canvas_item); + if (control && show_helpers && !Object::cast_to<Container>(control->get_parent())) { 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(); + se->pre_drag_xform = canvas_item->get_global_transform_with_canvas(); return; } } @@ -1444,7 +1581,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } // Multiple selected items - Point2 click = b->get_position(); + Point2 click = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); if ((b->get_alt() || tool == TOOL_MOVE) && get_item_count()) { // Drag the nodes @@ -1504,18 +1641,18 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { // Mouse motion event _update_cursor(); - if (!viewport->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field())) - viewport->call_deferred("grab_focus"); + if (!viewport_base->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field())) + viewport_base->call_deferred("grab_focus"); if (box_selecting) { // Update box selection - box_selecting_to = transform.affine_inverse().xform(m->get_position()); + box_selecting_to = transform.affine_inverse().xform(viewport_scrollable->get_transform().affine_inverse().xform(m->get_position())); viewport->update(); return; } 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))) { + 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"))) { @@ -1555,7 +1692,7 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } Vector2 dfrom = drag_from; - Vector2 dto = transform.affine_inverse().xform(m->get_position()); + Vector2 dto = transform.affine_inverse().xform(viewport_scrollable->get_transform().affine_inverse().xform(m->get_position())); if (canvas_item->has_meta("_edit_lock_")) continue; @@ -1589,60 +1726,74 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { continue; } + bool uniform = m->get_shift(); + bool symmetric = m->get_alt(); + + Vector2 drag_vector = + canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dto) - + canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dfrom); + + switch (drag) { + case DRAG_ALL: + case DRAG_NODE_2D: + dto -= drag_from - drag_point_from; + if (uniform) { + if (ABS(dto.x - drag_point_from.x) > ABS(dto.y - drag_point_from.y)) { + dto.y = drag_point_from.y; + } else { + dto.x = drag_point_from.x; + } + } + break; + } + Control *control = Object::cast_to<Control>(canvas_item); 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)); + Transform2D c_trans_rev = canvas_item->get_global_transform_with_canvas().affine_inverse(); + + Vector2 anchor = c_trans_rev.xform(dto - drag_from + drag_point_from); + anchor = _position_to_anchor(control, anchor); + + Vector2 anchor_snapped = c_trans_rev.xform(snap_point(dto - drag_from + drag_point_from, SNAP_GRID | SNAP_OTHER_NODES, _get_single_item(), SNAP_NODE_PARENT | SNAP_NODE_SIDES)); + anchor_snapped = _position_to_anchor(control, anchor_snapped).snapped(Vector2(0.00001, 0.00001)); + + bool use_y = Math::abs(drag_vector.y) > Math::abs(drag_vector.x); 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); + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_LEFT, anchor_snapped.x); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_TOP, anchor_snapped.y); 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); + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_RIGHT, anchor_snapped.x); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_TOP, anchor_snapped.y); 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; + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_RIGHT, anchor_snapped.x); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_BOTTOM, anchor_snapped.y); 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); + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_LEFT, anchor_snapped.x); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_BOTTOM, anchor_snapped.y); 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)); + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_LEFT, anchor_snapped.x); + if (!uniform || (uniform && !use_y)) control->set_anchor(MARGIN_RIGHT, anchor_snapped.x); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_TOP, anchor_snapped.y); + if (!uniform || (uniform && use_y)) control->set_anchor(MARGIN_BOTTOM, anchor_snapped.y); continue; break; } } - bool uniform = m->get_shift(); - bool symmetric = m->get_alt(); - - 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)) { - dto.y = drag_point_from.y; - } else { - dto.x = drag_point_from.x; - } - } - dfrom = drag_point_from; - dto = snap_point(dto, drag_point_from); + dto = snap_point(dto, SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, _get_single_item()); - Vector2 drag_vector = + drag_vector = canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dto) - canvas_item->get_global_transform_with_canvas().affine_inverse().xform(dfrom); @@ -1892,56 +2043,148 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) { } } +void CanvasItemEditor::_draw_text_at_position(Point2 p_position, String p_string, Margin p_side) { + Color color = Color(0.8, 0.8, 0.8, 0.5); + Ref<Font> font = get_font("font", "Label"); + Size2 text_size = font->get_string_size(p_string); + 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, p_string, color); +} + +void CanvasItemEditor::_draw_margin_at_position(int p_value, Point2 p_position, Margin p_side) { + String str = vformat("%d px", p_value); + if (p_value != 0) { + _draw_text_at_position(p_position, str, p_side); + } +} + void CanvasItemEditor::_draw_percentage_at_position(float p_value, Point2 p_position, Margin p_side) { + String str = vformat("%.1f %%", p_value * 100.0); 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); + _draw_text_at_position(p_position, str, p_side); } } -void CanvasItemEditor::_viewport_draw() { +void CanvasItemEditor::_draw_rulers() { + Color graduation_color = Color(0.5, 0.5, 0.5, 1); + Color bg_color = get_color("dark_color_2", "Editor"); + Color font_color = Color(0.8, 0.8, 0.8, 1); + Ref<Font> font = get_font("rulers", "EditorFonts"); + + // The rule transform + Transform2D ruler_transform; + if (show_grid || snap_grid) { + ruler_transform = Transform2D(); + if (snap_relative && get_item_count() > 0) { + ruler_transform.translate(_find_topleftmost_point()); + ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier)); + } else { + ruler_transform.translate(grid_offset); + ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier)); + } + while ((transform * ruler_transform).get_scale().x < 50 || (transform * ruler_transform).get_scale().y < 50) { - // TODO fetch the viewport? + ruler_transform.scale_basis(Point2(2, 2)); + } + } else { + float basic_rule = 100; + for (int i = 0; basic_rule * zoom > 100; i++) { + basic_rule /= (i % 2) ? 5.0 : 2.0; + } + for (int i = 0; basic_rule * zoom < 100; i++) { + basic_rule *= (i % 2) ? 2.0 : 5.0; + } + ruler_transform = Transform2D(); + ruler_transform.scale(Size2(basic_rule, basic_rule)); + } - Ref<Texture> pivot = get_icon("EditorPivot", "EditorIcons"); - _update_scrollbars(); - RID ci = viewport->get_canvas_item(); + // Subdivisions + int major_subdivision = 2; + Transform2D major_subdivide = Transform2D(); + major_subdivide.scale(Size2(1.0 / major_subdivision, 1.0 / major_subdivision)); + + int minor_subdivision = 5; + Transform2D minor_subdivide = Transform2D(); + minor_subdivide.scale(Size2(1.0 / minor_subdivision, 1.0 / minor_subdivision)); + + // First and last graduations to draw (in the ruler space) + Point2 first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Point2()); + Point2 last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(viewport->get_size()); + + // Draw top ruler + viewport_base->draw_rect(Rect2(Point2(RULER_WIDTH, 0), Size2(viewport->get_size().x, RULER_WIDTH)), bg_color); + for (int i = Math::ceil(first.x); i < last.x; i++) { + Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)); + if (i % (major_subdivision * minor_subdivision) == 0) { + viewport_base->draw_line(Point2(position.x + RULER_WIDTH, 0), Point2(position.x + RULER_WIDTH, RULER_WIDTH), graduation_color); + float val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)).x; + viewport_base->draw_string(font, Point2(position.x + RULER_WIDTH + 2, font->get_height()), vformat(((int)val == val) ? "%d" : "%.1f", val), font_color); + } else { + if (i % minor_subdivision == 0) { + viewport_base->draw_line(Point2(position.x + RULER_WIDTH, RULER_WIDTH * 0.33), Point2(position.x + RULER_WIDTH, RULER_WIDTH), graduation_color); + } else { + viewport_base->draw_line(Point2(position.x + RULER_WIDTH, RULER_WIDTH * 0.66), Point2(position.x + RULER_WIDTH, RULER_WIDTH), graduation_color); + } + } + } - if (snap_show_grid) { + // Draw left ruler + viewport_base->draw_rect(Rect2(Point2(0, RULER_WIDTH), Size2(RULER_WIDTH, viewport->get_size().y)), bg_color); + for (int i = Math::ceil(first.y); i < last.y; i++) { + Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)); + if (i % (major_subdivision * minor_subdivision) == 0) { + viewport_base->draw_line(Point2(0, position.y + RULER_WIDTH), Point2(RULER_WIDTH, position.y + RULER_WIDTH), graduation_color); + float val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)).y; + viewport_base->draw_string(font, Point2(2, position.y + RULER_WIDTH + 2 + font->get_height()), vformat(((int)val == val) ? "%d" : "%.1f", val), font_color); + } else { + if (i % minor_subdivision == 0) { + viewport_base->draw_line(Point2(RULER_WIDTH * 0.33, position.y + RULER_WIDTH), Point2(RULER_WIDTH, position.y + RULER_WIDTH), graduation_color); + } else { + viewport_base->draw_line(Point2(RULER_WIDTH * 0.66, position.y + RULER_WIDTH), Point2(RULER_WIDTH, position.y + RULER_WIDTH), graduation_color); + } + } + } + viewport_base->draw_rect(Rect2(Point2(), Size2(RULER_WIDTH, RULER_WIDTH)), graduation_color); +} + +void CanvasItemEditor::_draw_focus() { + if (viewport_base->has_focus()) { + get_stylebox("Focus", "EditorStyles")->draw(viewport_base->get_canvas_item(), Rect2(Point2(), viewport_base->get_size())); + } +} + +void CanvasItemEditor::_draw_grid() { + if (show_grid) { //Draw the grid Size2 s = viewport->get_size(); int last_cell = 0; Transform2D xform = transform.affine_inverse(); - Vector2 grid_offset; - if (snap_relative && snap_grid && get_item_count() > 0) { + Vector2 real_grid_offset; + if (snap_relative && get_item_count() > 0) { Vector2 topleft = _find_topleftmost_point(); - grid_offset.x = fmod(topleft.x, snap_step.x); - grid_offset.y = fmod(topleft.y, snap_step.y); + real_grid_offset.x = fmod(topleft.x, grid_step.x * Math::pow(2.0, grid_step_multiplier)); + real_grid_offset.y = fmod(topleft.y, grid_step.y * Math::pow(2.0, grid_step_multiplier)); } else { - grid_offset = snap_offset; + real_grid_offset = grid_offset; } - if (snap_step.x != 0) { + if (grid_step.x != 0) { for (int i = 0; i < s.width; i++) { - int cell = Math::fast_ftoi(Math::floor((xform.xform(Vector2(i, 0)).x - grid_offset.x) / snap_step.x)); + int cell = Math::fast_ftoi(Math::floor((xform.xform(Vector2(i, 0)).x - real_grid_offset.x) / (grid_step.x * Math::pow(2.0, grid_step_multiplier)))); if (i == 0) last_cell = cell; if (last_cell != cell) @@ -1950,9 +2193,9 @@ void CanvasItemEditor::_viewport_draw() { } } - if (snap_step.y != 0) { + if (grid_step.y != 0) { for (int i = 0; i < s.height; i++) { - int cell = Math::fast_ftoi(Math::floor((xform.xform(Vector2(0, i)).y - grid_offset.y) / snap_step.y)); + int cell = Math::fast_ftoi(Math::floor((xform.xform(Vector2(0, i)).y - real_grid_offset.y) / (grid_step.y * Math::pow(2.0, grid_step_multiplier)))); if (i == 0) last_cell = cell; if (last_cell != cell) @@ -1961,21 +2204,15 @@ void CanvasItemEditor::_viewport_draw() { } } } +} - if (viewport->has_focus()) { - Size2 size = viewport->get_size(); - get_stylebox("Focus", "EditorStyles")->draw(ci, Rect2(Point2(), size)); - } - - Ref<Texture> lock = get_icon("Lock", "EditorIcons"); - Ref<Texture> group = get_icon("Group", "EditorIcons"); - - bool single = get_single_item() != NULL; - - Map<Node *, Object *> &selection = editor_selection->get_selection(); - +void CanvasItemEditor::_draw_selection() { bool pivot_found = false; + Ref<Texture> pivot_icon = get_icon("EditorPivot", "EditorIcons"); + bool single = _get_single_item() != NULL; + RID ci = viewport->get_canvas_item(); + Map<Node *, Object *> &selection = editor_selection->get_selection(); for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) { CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E->key()); @@ -1989,6 +2226,23 @@ void CanvasItemEditor::_viewport_draw() { Rect2 rect = canvas_item->get_item_rect(); + if (drag != DRAG_NONE && drag != DRAG_PIVOT) { + const Transform2D pre_drag_xform = transform * se->pre_drag_xform; + const Color pre_drag_color = Color(0.4, 0.6, 1, 0.7); + + Vector2 pre_drag_endpoints[4] = { + + pre_drag_xform.xform(rect.position), + pre_drag_xform.xform(rect.position + Vector2(rect.size.x, 0)), + pre_drag_xform.xform(rect.position + rect.size), + pre_drag_xform.xform(rect.position + Vector2(0, rect.size.y)) + }; + + for (int i = 0; i < 4; i++) { + viewport->draw_line(pre_drag_endpoints[i], pre_drag_endpoints[(i + 1) % 4], pre_drag_color, 2); + } + } + Transform2D xform = transform * canvas_item->get_global_transform_with_canvas(); VisualServer::get_singleton()->canvas_item_add_set_transform(ci, xform); @@ -2010,10 +2264,10 @@ void CanvasItemEditor::_viewport_draw() { if (single && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_ROTATE || tool == TOOL_EDIT_PIVOT)) { //kind of sucks - if (Object::cast_to<Node2D>(canvas_item)) { - - if (Object::cast_to<Node2D>(canvas_item)->edit_has_pivot()) { - viewport->draw_texture(pivot, xform.get_origin() + (-pivot->get_size() / 2).floor()); + Node2D *node2d = Object::cast_to<Node2D>(canvas_item); + if (node2d) { + if (node2d->edit_has_pivot()) { + viewport->draw_texture(pivot_icon, xform.get_origin() + (-pivot_icon->get_size() / 2).floor()); can_move_pivot = true; pivot_found = true; } @@ -2023,12 +2277,15 @@ void CanvasItemEditor::_viewport_draw() { 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()); + viewport->draw_texture(pivot_icon, xform.xform(pivot_ofs) + (-pivot_icon->get_size() / 2).floor()); } can_move_pivot = true; pivot_found = true; - if (tool == TOOL_SELECT) { + if (tool == TOOL_SELECT && show_helpers && !Object::cast_to<Container>(control->get_parent())) { + // Draw the helpers + Color color_base = Color(0.8, 0.8, 0.8, 0.5); + float anchors_values[4]; anchors_values[0] = control->get_anchor(MARGIN_LEFT); anchors_values[1] = control->get_anchor(MARGIN_TOP); @@ -2043,6 +2300,7 @@ void CanvasItemEditor::_viewport_draw() { anchors_pos[i] = xform.xform(_anchor_to_position(control, anchors[i])); } + Map<Node *, Object *> &selection = editor_selection->get_selection(); // Get which anchor is dragged int dragged_anchor = -1; switch (drag) { @@ -2065,7 +2323,6 @@ void CanvasItemEditor::_viewport_draw() { // 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++) { @@ -2078,7 +2335,7 @@ void CanvasItemEditor::_viewport_draw() { 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); + snapped = anchors_values[i] == 0.0 || anchors_values[i] == 0.5 || anchors_values[i] == 1.0; viewport->draw_line(line_starts[i], line_ends[i], snapped ? color_snapped : color_base, (i == dragged_anchor || (i + 3) % 4 == dragged_anchor) ? 2 : 1); } @@ -2110,6 +2367,83 @@ void CanvasItemEditor::_viewport_draw() { for (int i = 0; i < 4; i++) { anchor_handle->draw_rect(ci, anchor_rects[i]); } + + // Draw the margin values and the node width/height when dragging control side + float ratio = 0.33; + Transform2D parent_transform = xform * control->get_transform().affine_inverse(); + float node_pos_in_parent[4]; + + node_pos_in_parent[0] = control->get_anchor(MARGIN_LEFT) * control->get_parent_area_size().width + control->get_margin(MARGIN_LEFT); + node_pos_in_parent[1] = control->get_anchor(MARGIN_TOP) * control->get_parent_area_size().height + control->get_margin(MARGIN_TOP); + node_pos_in_parent[2] = control->get_anchor(MARGIN_RIGHT) * control->get_parent_area_size().width + control->get_margin(MARGIN_RIGHT); + node_pos_in_parent[3] = control->get_anchor(MARGIN_BOTTOM) * control->get_parent_area_size().height + control->get_margin(MARGIN_BOTTOM); + + switch (drag) { + case DRAG_LEFT: + case DRAG_TOP_LEFT: + case DRAG_BOTTOM_LEFT: + _draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), MARGIN_BOTTOM); + case DRAG_ALL: + Point2 start = Vector2(node_pos_in_parent[0], Math::lerp(node_pos_in_parent[1], node_pos_in_parent[3], ratio)); + Point2 end = start - Vector2(control->get_margin(MARGIN_LEFT), 0); + _draw_margin_at_position(control->get_margin(MARGIN_LEFT), parent_transform.xform((start + end) / 2), MARGIN_TOP); + viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, 1); + break; + } + switch (drag) { + case DRAG_RIGHT: + case DRAG_TOP_RIGHT: + case DRAG_BOTTOM_RIGHT: + _draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), MARGIN_BOTTOM); + case DRAG_ALL: + Point2 start = Vector2(node_pos_in_parent[2], Math::lerp(node_pos_in_parent[3], node_pos_in_parent[1], ratio)); + Point2 end = start - Vector2(control->get_margin(MARGIN_RIGHT), 0); + _draw_margin_at_position(control->get_margin(MARGIN_RIGHT), parent_transform.xform((start + end) / 2), MARGIN_BOTTOM); + viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, 1); + break; + } + switch (drag) { + case DRAG_TOP: + case DRAG_TOP_LEFT: + case DRAG_TOP_RIGHT: + _draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2)) + Vector2(5, 0), MARGIN_RIGHT); + case DRAG_ALL: + Point2 start = Vector2(Math::lerp(node_pos_in_parent[0], node_pos_in_parent[2], ratio), node_pos_in_parent[1]); + Point2 end = start - Vector2(0, control->get_margin(MARGIN_TOP)); + _draw_margin_at_position(control->get_margin(MARGIN_TOP), parent_transform.xform((start + end) / 2), MARGIN_LEFT); + viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, 1); + break; + } + switch (drag) { + case DRAG_BOTTOM: + case DRAG_BOTTOM_LEFT: + case DRAG_BOTTOM_RIGHT: + _draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2) + Vector2(5, 0)), MARGIN_RIGHT); + case DRAG_ALL: + Point2 start = Vector2(Math::lerp(node_pos_in_parent[2], node_pos_in_parent[0], ratio), node_pos_in_parent[3]); + Point2 end = start - Vector2(0, control->get_margin(MARGIN_BOTTOM)); + _draw_margin_at_position(control->get_margin(MARGIN_BOTTOM), parent_transform.xform((start + end) / 2), MARGIN_RIGHT); + viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, 1); + break; + } + + switch (drag) { + //Draw the ghost rect if the node if rotated/scaled + case DRAG_LEFT: + case DRAG_TOP_LEFT: + case DRAG_TOP: + case DRAG_TOP_RIGHT: + case DRAG_RIGHT: + case DRAG_BOTTOM_RIGHT: + case DRAG_BOTTOM: + case DRAG_BOTTOM_LEFT: + case DRAG_ALL: + if (control->get_rotation() != 0.0 || control->get_scale() != Vector2(1, 1)) { + Rect2 rect = Rect2(Vector2(node_pos_in_parent[0], node_pos_in_parent[1]), control->get_size()); + viewport->draw_rect(parent_transform.xform(rect), color_base, false); + } + break; + } } } @@ -2132,33 +2466,32 @@ void CanvasItemEditor::_viewport_draw() { } } } - - //DRAW_EMPTY_RECT( Rect2( current_window->get_scroll()-Point2(1,1), get_size()+Size2(2,2)), Color(0.8,0.8,1.0,0.8) ); - //E->get().last_rect = rect; } - pivot_button->set_disabled(!pivot_found); - VisualServer::get_singleton()->canvas_item_add_set_transform(ci, Transform2D()); - - Color x_axis_color(1.0, 0.4, 0.4, 0.6); - Color y_axis_color(0.4, 1.0, 0.4, 0.6); - Color area_axis_color(0.4, 0.4, 1.0, 0.4); - Color rotate_color(0.4, 0.7, 1.0, 0.8); - - VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(h_scroll->get_min(), 0) + transform.get_origin(), Point2(h_scroll->get_max(), 0) + transform.get_origin(), x_axis_color); - VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(0, v_scroll->get_min()) + transform.get_origin(), Point2(0, v_scroll->get_max()) + transform.get_origin(), y_axis_color); if (box_selecting) { - Point2 bsfrom = transform.xform(drag_from); Point2 bsto = transform.xform(box_selecting_to); VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(bsfrom, bsto - bsfrom), Color(0.7, 0.7, 1.0, 0.3)); } + Color rotate_color(0.4, 0.7, 1.0, 0.8); if (drag == DRAG_ROTATE) { VisualServer::get_singleton()->canvas_item_add_line(ci, transform.xform(display_rotate_from), transform.xform(display_rotate_to), rotate_color); } +} + +void CanvasItemEditor::_draw_axis() { + RID ci = viewport->get_canvas_item(); + + Color x_axis_color(1.0, 0.4, 0.4, 0.6); + Color y_axis_color(0.4, 1.0, 0.4, 0.6); + Color area_axis_color(0.4, 0.4, 1.0, 0.4); + + Point2 origin = transform.get_origin(); + VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(0, origin.y), Point2(viewport->get_size().x, origin.y), x_axis_color); + VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(origin.x, 0), Point2(origin.x, viewport->get_size().y), y_axis_color); Size2 screen_size = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height")); @@ -2170,34 +2503,12 @@ void CanvasItemEditor::_viewport_draw() { }; for (int i = 0; i < 4; i++) { - VisualServer::get_singleton()->canvas_item_add_line(ci, screen_endpoints[i], screen_endpoints[(i + 1) % 4], area_axis_color); } +} - for (List<LockList>::Element *E = lock_list.front(); E; E = E->next()) { - - Vector2 ofs = transform.xform(E->get().pos); - if (E->get().lock) { - - lock->draw(ci, ofs); - ofs.x += lock->get_width(); - } - if (E->get().group) { - - group->draw(ci, ofs); - } - } - - { - - EditorNode *en = editor; - EditorPluginList *over_plugin_list = en->get_editor_plugins_over(); - - if (!over_plugin_list->empty()) { - - over_plugin_list->forward_draw_over_canvas(transform, viewport); - } - } +void CanvasItemEditor::_draw_bones() { + RID ci = viewport->get_canvas_item(); if (skeleton_show_bones) { int bone_width = EditorSettings::get_singleton()->get("editors/2d/bone_width"); @@ -2265,11 +2576,135 @@ void CanvasItemEditor::_viewport_draw() { } } +void CanvasItemEditor::_draw_locks_and_groups(Node *p_node, const Transform2D &p_xform) { + ERR_FAIL_COND(!p_node); + + RID viewport_ci = viewport->get_canvas_item(); + + Transform2D transform_ci = p_xform; + CanvasItem *ci = Object::cast_to<CanvasItem>(p_node); + if (ci) + transform_ci = transform_ci * ci->get_transform(); + + for (int i = p_node->get_child_count() - 1; i >= 0; i--) { + _draw_locks_and_groups(p_node->get_child(i), transform_ci); + } + + if (ci) { + Ref<Texture> lock = get_icon("Lock", "EditorIcons"); + if (p_node->has_meta("_edit_lock_")) { + lock->draw(viewport_ci, transform_ci.xform(Point2(0, 0))); + } + + Ref<Texture> group = get_icon("Group", "EditorIcons"); + if (ci->has_meta("_edit_group_")) { + Vector2 ofs = transform_ci.xform(Point2(0, 0)); + if (ci->has_meta("_edit_lock_")) + ofs = Point2(ofs.x + lock->get_size().x, ofs.y); + group->draw(viewport_ci, ofs); + } + } +} + +void CanvasItemEditor::_build_bones_list(Node *p_node) { + ERR_FAIL_COND(!p_node); + + for (int i = 0; i < p_node->get_child_count(); i++) { + _build_bones_list(p_node->get_child(i)); + } + + CanvasItem *c = Object::cast_to<CanvasItem>(p_node); + if (c && c->is_visible_in_tree()) { + if (c->has_meta("_edit_bone_")) { + + ObjectID id = c->get_instance_id(); + if (!bone_list.has(id)) { + BoneList bone; + bone.bone = id; + bone_list[id] = bone; + } + + bone_list[id].last_pass = bone_last_frame; + } + } +} + +void CanvasItemEditor::_get_encompassing_rect(Node *p_node, Rect2 &r_rect, const Transform2D &p_xform) { + ERR_FAIL_COND(!p_node); + + for (int i = 0; i < p_node->get_child_count(); i++) { + _get_encompassing_rect(p_node->get_child(i), r_rect, p_xform); + } + + CanvasItem *c = Object::cast_to<CanvasItem>(p_node); + if (c && c->is_visible_in_tree()) { + Rect2 rect = c->get_item_rect(); + Transform2D xform = p_xform * c->get_transform(); + r_rect.expand_to(xform.xform(rect.position)); + r_rect.expand_to(xform.xform(rect.position + Point2(rect.size.x, 0))); + r_rect.expand_to(xform.xform(rect.position + Point2(0, rect.size.y))); + r_rect.expand_to(xform.xform(rect.position + rect.size)); + } +} + +void CanvasItemEditor::_draw_viewport_base() { + if (show_rulers) + _draw_rulers(); + _draw_focus(); +} + +void CanvasItemEditor::_draw_viewport() { + + // hide/show buttons depending on the selection + bool all_locked = true; + bool all_group = true; + List<Node *> &selection = editor_selection->get_selected_node_list(); + if (selection.empty()) { + all_locked = false; + all_group = false; + } else { + for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { + if (Object::cast_to<Control>(E->get()) && !Object::cast_to<Control>(E->get())->has_meta("_edit_lock_")) { + all_locked = false; + break; + } + } + for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { + if (Object::cast_to<Control>(E->get()) && !Object::cast_to<Control>(E->get())->has_meta("_edit_group_")) { + all_group = false; + break; + } + } + } + + lock_button->set_visible(!all_locked); + lock_button->set_disabled(selection.empty()); + unlock_button->set_visible(all_locked); + group_button->set_visible(!all_group); + group_button->set_disabled(selection.empty()); + ungroup_button->set_visible(all_group); + + _update_scrollbars(); + + _draw_grid(); + _draw_selection(); + _draw_axis(); + _draw_locks_and_groups(editor->get_edited_scene(), transform); + + RID ci = viewport->get_canvas_item(); + VisualServer::get_singleton()->canvas_item_add_set_transform(ci, Transform2D()); + + EditorPluginList *over_plugin_list = editor->get_editor_plugins_over(); + if (!over_plugin_list->empty()) { + over_plugin_list->forward_draw_over_canvas(transform, viewport); + } + _draw_focus(); +} + void CanvasItemEditor::_notification(int p_what) { if (p_what == NOTIFICATION_FIXED_PROCESS) { - EditorNode::get_singleton()->get_scene_root()->set_snap_controls_to_pixels(GLOBAL_GET("gui/common/snap_controls_to_pixels")); List<Node *> &selection = editor_selection->get_selected_node_list(); @@ -2298,19 +2733,25 @@ void CanvasItemEditor::_notification(int p_what) { Rect2 r = canvas_item->get_item_rect(); Transform2D xform = canvas_item->get_transform(); - float anchors[4]; - Vector2 pivot; + if (r != se->prev_rect || xform != se->prev_xform) { + viewport->update(); + se->prev_rect = r; + se->prev_xform = xform; + } + if (Object::cast_to<Control>(canvas_item)) { + float anchors[4]; + Vector2 pivot; + pivot = Object::cast_to<Control>(canvas_item)->get_pivot_offset(); anchors[MARGIN_LEFT] = Object::cast_to<Control>(canvas_item)->get_anchor(MARGIN_LEFT); anchors[MARGIN_RIGHT] = Object::cast_to<Control>(canvas_item)->get_anchor(MARGIN_RIGHT); anchors[MARGIN_TOP] = Object::cast_to<Control>(canvas_item)->get_anchor(MARGIN_TOP); anchors[MARGIN_BOTTOM] = Object::cast_to<Control>(canvas_item)->get_anchor(MARGIN_BOTTOM); - 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]) { + if (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; + viewport_base->update(); se->prev_pivot = pivot; se->prev_anchors[MARGIN_LEFT] = anchors[MARGIN_LEFT]; se->prev_anchors[MARGIN_RIGHT] = anchors[MARGIN_RIGHT]; @@ -2359,6 +2800,9 @@ void CanvasItemEditor::_notification(int p_what) { list_select_button->set_icon(get_icon("ListSelect", "EditorIcons")); move_button->set_icon(get_icon("ToolMove", "EditorIcons")); rotate_button->set_icon(get_icon("ToolRotate", "EditorIcons")); + snap_button->set_icon(get_icon("Snap", "EditorIcons")); + snap_config_menu->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons")); + skeleton_menu->set_icon(get_icon("Bone", "EditorIcons")); pan_button->set_icon(get_icon("ToolPan", "EditorIcons")); pivot_button->set_icon(get_icon("EditPivot", "EditorIcons")); select_handle = get_icon("EditorHandle", "EditorIcons"); @@ -2369,6 +2813,10 @@ void CanvasItemEditor::_notification(int p_what) { ungroup_button->set_icon(get_icon("Ungroup", "EditorIcons")); key_insert_button->set_icon(get_icon("Key", "EditorIcons")); + zoom_minus->set_icon(get_icon("ZoomLess", "EditorIcons")); + zoom_reset->set_icon(get_icon("ZoomReset", "EditorIcons")); + zoom_plus->set_icon(get_icon("ZoomMore", "EditorIcons")); + anchor_menu->set_icon(get_icon("Anchor", "EditorIcons")); PopupMenu *p = anchor_menu->get_popup(); @@ -2403,6 +2851,9 @@ void CanvasItemEditor::_notification(int p_what) { list_select_button->set_icon(get_icon("ListSelect", "EditorIcons")); move_button->set_icon(get_icon("ToolMove", "EditorIcons")); rotate_button->set_icon(get_icon("ToolRotate", "EditorIcons")); + snap_button->set_icon(get_icon("Snap", "EditorIcons")); + snap_config_menu->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons")); + skeleton_menu->set_icon(get_icon("Bone", "EditorIcons")); pan_button->set_icon(get_icon("ToolPan", "EditorIcons")); pivot_button->set_icon(get_icon("EditPivot", "EditorIcons")); select_handle = get_icon("EditorHandle", "EditorIcons"); @@ -2444,66 +2895,23 @@ void CanvasItemEditor::edit(CanvasItem *p_canvas_item) { drag = DRAG_NONE; + // Clear the selection editor_selection->clear(); //_clear_canvas_items(); editor_selection->add_node(p_canvas_item); //_add_canvas_item(p_canvas_item); viewport->update(); -} - -void CanvasItemEditor::_find_canvas_items_span(Node *p_node, Rect2 &r_rect, const Transform2D &p_xform) { - - if (!p_node) - return; - - CanvasItem *c = Object::cast_to<CanvasItem>(p_node); - - for (int i = p_node->get_child_count() - 1; i >= 0; i--) { - - //CanvasItem *r=NULL; - - if (c && !c->is_set_as_toplevel()) - _find_canvas_items_span(p_node->get_child(i), r_rect, p_xform * c->get_transform()); - else - _find_canvas_items_span(p_node->get_child(i), r_rect, Transform2D()); - } - - if (c && c->is_visible_in_tree()) { - - Rect2 rect = c->get_item_rect(); - Transform2D xform = p_xform * c->get_transform(); - - LockList lock; - lock.lock = c->has_meta("_edit_lock_"); - lock.group = c->has_meta("_edit_group_"); - - if (lock.group || lock.lock) { - lock.pos = xform.xform(rect.position); - lock_list.push_back(lock); - } - - if (c->has_meta("_edit_bone_")) { - - ObjectID id = c->get_instance_id(); - if (!bone_list.has(id)) { - BoneList bone; - bone.bone = id; - bone_list[id] = bone; - } - - bone_list[id].last_pass = bone_last_frame; - } - - r_rect.expand_to(xform.xform(rect.position)); - r_rect.expand_to(xform.xform(rect.position + Point2(rect.size.x, 0))); - r_rect.expand_to(xform.xform(rect.position + Point2(0, rect.size.y))); - r_rect.expand_to(xform.xform(rect.position + rect.size)); - } + viewport_base->update(); } void CanvasItemEditor::_update_scrollbars() { updating_scroll = true; + if (show_rulers) + viewport_scrollable->set_begin(Point2(RULER_WIDTH, RULER_WIDTH)); + else + viewport_scrollable->set_begin(Point2()); + Size2 size = viewport->get_size(); Size2 hmin = h_scroll->get_minimum_size(); Size2 vmin = v_scroll->get_minimum_size(); @@ -2520,11 +2928,12 @@ void CanvasItemEditor::_update_scrollbars() { Rect2 canvas_item_rect = Rect2(Point2(), screen_rect); - lock_list.clear(); bone_last_frame++; - if (editor->get_edited_scene()) - _find_canvas_items_span(editor->get_edited_scene(), canvas_item_rect, Transform2D()); + if (editor->get_edited_scene()) { + _build_bones_list(editor->get_edited_scene()); + _get_encompassing_rect(editor->get_edited_scene(), canvas_item_rect, Transform2D()); + } List<Map<ObjectID, BoneList>::Element *> bone_to_erase; @@ -2547,7 +2956,6 @@ void CanvasItemEditor::_update_scrollbars() { Point2 ofs; if (canvas_item_rect.size.height <= (local_rect.size.y / zoom)) { - v_scroll->hide(); ofs.y = canvas_item_rect.position.y; } else { @@ -2608,6 +3016,7 @@ void CanvasItemEditor::_update_scroll(float) { editor->get_scene_root()->set_global_canvas_transform(transform); viewport->update(); + viewport_base->update(); } void CanvasItemEditor::_set_anchors_preset(Control::LayoutPreset p_preset) { @@ -2654,88 +3063,117 @@ void CanvasItemEditor::_set_full_rect() { undo_redo->commit_action(); } +void CanvasItemEditor::_zoom_minus() { + if (zoom < MIN_ZOOM) + return; + zoom /= 2.0; + + _update_scroll(0); + viewport->update(); + viewport_base->update(); +} + +void CanvasItemEditor::_zoom_reset() { + zoom = 1; + _update_scroll(0); + viewport->update(); + viewport_base->update(); +} + +void CanvasItemEditor::_zoom_plus() { + if (zoom > MAX_ZOOM) + return; + + zoom *= 2.0; + _update_scroll(0); + viewport->update(); + viewport_base->update(); +} + +void CanvasItemEditor::_toggle_snap(bool p_status) { + snap_active = p_status; + viewport->update(); + viewport_base->update(); +} + void CanvasItemEditor::_popup_callback(int p_op) { last_option = MenuOption(p_op); switch (p_op) { - case SNAP_USE: { - snap_grid = !snap_grid; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE); - edit_menu->get_popup()->set_item_checked(idx, snap_grid); + case SHOW_GRID: { + show_grid = !show_grid; + int idx = view_menu->get_popup()->get_item_index(SHOW_GRID); + view_menu->get_popup()->set_item_checked(idx, show_grid); viewport->update(); + viewport_base->update(); } break; - case SNAP_SHOW_GRID: { - snap_show_grid = !snap_show_grid; - int idx = edit_menu->get_popup()->get_item_index(SNAP_SHOW_GRID); - edit_menu->get_popup()->set_item_checked(idx, snap_show_grid); - viewport->update(); + case SNAP_USE_NODE_PARENT: { + snap_node_parent = !snap_node_parent; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT); + smartsnap_config_popup->set_item_checked(idx, snap_node_parent); + } break; + case SNAP_USE_NODE_ANCHORS: { + snap_node_anchors = !snap_node_anchors; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS); + smartsnap_config_popup->set_item_checked(idx, snap_node_anchors); + } break; + case SNAP_USE_NODE_SIDES: { + snap_node_sides = !snap_node_sides; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES); + smartsnap_config_popup->set_item_checked(idx, snap_node_sides); + } break; + case SNAP_USE_OTHER_NODES: { + snap_other_nodes = !snap_other_nodes; + int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES); + smartsnap_config_popup->set_item_checked(idx, snap_other_nodes); + } break; + case SNAP_USE_GRID: { + snap_grid = !snap_grid; + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_GRID); + snap_config_menu->get_popup()->set_item_checked(idx, snap_grid); } break; case SNAP_USE_ROTATION: { snap_rotation = !snap_rotation; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE_ROTATION); - edit_menu->get_popup()->set_item_checked(idx, snap_rotation); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION); + snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation); } break; case SNAP_RELATIVE: { snap_relative = !snap_relative; - int idx = edit_menu->get_popup()->get_item_index(SNAP_RELATIVE); - edit_menu->get_popup()->set_item_checked(idx, snap_relative); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE); + snap_config_menu->get_popup()->set_item_checked(idx, snap_relative); viewport->update(); + viewport_base->update(); } break; case SNAP_USE_PIXEL: { snap_pixel = !snap_pixel; - int idx = edit_menu->get_popup()->get_item_index(SNAP_USE_PIXEL); - edit_menu->get_popup()->set_item_checked(idx, snap_pixel); + int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL); + snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel); } break; case SNAP_CONFIGURE: { - ((SnapDialog *)snap_dialog)->set_fields(snap_offset, snap_step, snap_rotation_offset, snap_rotation_step); + ((SnapDialog *)snap_dialog)->set_fields(grid_offset, grid_step, snap_rotation_offset, snap_rotation_step); snap_dialog->popup_centered(Size2(220, 160)); } break; case SKELETON_SHOW_BONES: { skeleton_show_bones = !skeleton_show_bones; - int idx = skeleton_menu->get_item_index(SKELETON_SHOW_BONES); - skeleton_menu->set_item_checked(idx, skeleton_show_bones); + int idx = skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES); + skeleton_menu->get_popup()->set_item_checked(idx, skeleton_show_bones); viewport->update(); } break; - case ZOOM_IN: { - if (zoom > MAX_ZOOM) - return; - zoom = zoom * (1.0 / 0.5); - _update_scroll(0); + case SHOW_HELPERS: { + show_helpers = !show_helpers; + int idx = view_menu->get_popup()->get_item_index(SHOW_HELPERS); + view_menu->get_popup()->set_item_checked(idx, show_helpers); viewport->update(); - return; } break; - case ZOOM_OUT: { - if (zoom < MIN_ZOOM) - return; - - zoom = zoom * 0.5; - _update_scroll(0); + case SHOW_RULERS: { + show_rulers = !show_rulers; + int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS); + view_menu->get_popup()->set_item_checked(idx, show_rulers); viewport->update(); - return; - + viewport_base->update(); } break; - case ZOOM_RESET: { - zoom = 1; - _update_scroll(0); - viewport->update(); - return; - - } break; - case ZOOM_SET: { - - updating_value_dialog = true; - - dialog_label->set_text(TTR("Zoom (%):")); - dialog_val->set_min(0.1); - dialog_val->set_step(0.1); - dialog_val->set_max(800); - dialog_val->set_value(zoom * 100); - value_dialog->popup_centered(Size2(200, 85)); - updating_value_dialog = false; - - } break; case LOCK_SELECTED: { List<Node *> &selection = editor_selection->get_selected_node_list(); @@ -3082,7 +3520,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { n2d->set_meta("_edit_bone_", true); if (!skeleton_show_bones) - skeleton_menu->activate_item(skeleton_menu->get_item_index(SKELETON_SHOW_BONES)); + skeleton_menu->get_popup()->activate_item(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES)); } viewport->update(); @@ -3101,7 +3539,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { n2d->set_meta("_edit_bone_", Variant()); if (!skeleton_show_bones) - skeleton_menu->activate_item(skeleton_menu->get_item_index(SKELETON_SHOW_BONES)); + skeleton_menu->get_popup()->activate_item(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES)); } viewport->update(); @@ -3121,7 +3559,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { canvas_item->set_meta("_edit_ik_", true); if (!skeleton_show_bones) - skeleton_menu->activate_item(skeleton_menu->get_item_index(SKELETON_SHOW_BONES)); + skeleton_menu->get_popup()->activate_item(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES)); } viewport->update(); @@ -3141,7 +3579,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { n2d->set_meta("_edit_ik_", Variant()); if (!skeleton_show_bones) - skeleton_menu->activate_item(skeleton_menu->get_item_index(SKELETON_SHOW_BONES)); + skeleton_menu->get_popup()->activate_item(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES)); } viewport->update(); @@ -3204,15 +3642,19 @@ void CanvasItemEditor::_focus_selection(int p_op) { void CanvasItemEditor::_bind_methods() { + ClassDB::bind_method("_zoom_minus", &CanvasItemEditor::_zoom_minus); + ClassDB::bind_method("_zoom_reset", &CanvasItemEditor::_zoom_reset); + ClassDB::bind_method("_zoom_plus", &CanvasItemEditor::_zoom_plus); + ClassDB::bind_method("_toggle_snap", &CanvasItemEditor::_toggle_snap); ClassDB::bind_method("_update_scroll", &CanvasItemEditor::_update_scroll); ClassDB::bind_method("_popup_callback", &CanvasItemEditor::_popup_callback); - ClassDB::bind_method("_dialog_value_changed", &CanvasItemEditor::_dialog_value_changed); ClassDB::bind_method("_get_editor_data", &CanvasItemEditor::_get_editor_data); ClassDB::bind_method("_tool_select", &CanvasItemEditor::_tool_select); ClassDB::bind_method("_keying_changed", &CanvasItemEditor::_keying_changed); ClassDB::bind_method("_unhandled_key_input", &CanvasItemEditor::_unhandled_key_input); - ClassDB::bind_method("_viewport_draw", &CanvasItemEditor::_viewport_draw); - ClassDB::bind_method("_viewport_gui_input", &CanvasItemEditor::_viewport_gui_input); + ClassDB::bind_method("_draw_viewport", &CanvasItemEditor::_draw_viewport); + ClassDB::bind_method("_draw_viewport_base", &CanvasItemEditor::_draw_viewport_base); + ClassDB::bind_method("_viewport_base_gui_input", &CanvasItemEditor::_viewport_base_gui_input); ClassDB::bind_method("_snap_changed", &CanvasItemEditor::_snap_changed); ClassDB::bind_method(D_METHOD("_selection_result_pressed"), &CanvasItemEditor::_selection_result_pressed); ClassDB::bind_method(D_METHOD("_selection_menu_hide"), &CanvasItemEditor::_selection_menu_hide); @@ -3254,64 +3696,92 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { hb->set_area_as_parent_rect(); bottom_split = memnew(VSplitContainer); - bottom_split->set_v_size_flags(SIZE_EXPAND_FILL); add_child(bottom_split); + bottom_split->set_v_size_flags(SIZE_EXPAND_FILL); palette_split = memnew(HSplitContainer); - palette_split->set_v_size_flags(SIZE_EXPAND_FILL); bottom_split->add_child(palette_split); + palette_split->set_v_size_flags(SIZE_EXPAND_FILL); - Control *vp_base = memnew(Control); - vp_base->set_v_size_flags(SIZE_EXPAND_FILL); - palette_split->add_child(vp_base); - - ViewportContainer *vp = memnew(ViewportContainer); - vp->set_stretch(true); - vp_base->add_child(vp); - vp->set_area_as_parent_rect(); - vp->add_child(p_editor->get_scene_root()); + viewport_base = memnew(Control); + palette_split->add_child(viewport_base); + viewport_base->connect("draw", this, "_draw_viewport_base"); + viewport_base->connect("gui_input", this, "_viewport_base_gui_input"); + viewport_base->set_focus_mode(FOCUS_ALL); + viewport_base->set_v_size_flags(SIZE_EXPAND_FILL); + viewport_base->set_h_size_flags(SIZE_EXPAND_FILL); + + viewport_scrollable = memnew(Control); + viewport_base->add_child(viewport_scrollable); + viewport_scrollable->set_mouse_filter(MOUSE_FILTER_PASS); + viewport_scrollable->set_draw_behind_parent(true); + viewport_scrollable->set_area_as_parent_rect(); + viewport_scrollable->set_begin(Point2(RULER_WIDTH, RULER_WIDTH)); + + ViewportContainer *scene_tree = memnew(ViewportContainer); + viewport_scrollable->add_child(scene_tree); + scene_tree->set_stretch(true); + scene_tree->set_area_as_parent_rect(); + scene_tree->add_child(p_editor->get_scene_root()); viewport = memnew(CanvasItemEditorViewport(p_editor, this)); - vp_base->add_child(viewport); + viewport_scrollable->add_child(viewport); + viewport->set_mouse_filter(MOUSE_FILTER_PASS); viewport->set_area_as_parent_rect(); viewport->set_clip_contents(true); + viewport->connect("draw", this, "_draw_viewport"); h_scroll = memnew(HScrollBar); - v_scroll = memnew(VScrollBar); - viewport->add_child(h_scroll); - viewport->add_child(v_scroll); - viewport->connect("draw", this, "_viewport_draw"); - viewport->connect("gui_input", this, "_viewport_gui_input"); - h_scroll->connect("value_changed", this, "_update_scroll", Vector<Variant>(), Object::CONNECT_DEFERRED); - v_scroll->connect("value_changed", this, "_update_scroll", Vector<Variant>(), Object::CONNECT_DEFERRED); - h_scroll->hide(); + + v_scroll = memnew(VScrollBar); + viewport->add_child(v_scroll); + v_scroll->connect("value_changed", this, "_update_scroll", Vector<Variant>(), Object::CONNECT_DEFERRED); v_scroll->hide(); + + HBoxContainer *zoom_hb = memnew(HBoxContainer); + viewport->add_child(zoom_hb); + zoom_hb->set_begin(Point2(5, 5)); + + zoom_minus = memnew(ToolButton); + zoom_hb->add_child(zoom_minus); + zoom_minus->connect("pressed", this, "_zoom_minus"); + zoom_minus->set_focus_mode(FOCUS_NONE); + + zoom_reset = memnew(ToolButton); + zoom_hb->add_child(zoom_reset); + zoom_reset->connect("pressed", this, "_zoom_reset"); + zoom_reset->set_focus_mode(FOCUS_NONE); + + zoom_plus = memnew(ToolButton); + zoom_hb->add_child(zoom_plus); + zoom_plus->connect("pressed", this, "_zoom_plus"); + zoom_plus->set_focus_mode(FOCUS_NONE); + updating_scroll = false; - viewport->set_focus_mode(FOCUS_ALL); handle_len = 10; first_update = true; select_button = memnew(ToolButton); - select_button->set_toggle_mode(true); hb->add_child(select_button); + select_button->set_toggle_mode(true); select_button->connect("pressed", this, "_tool_select", make_binds(TOOL_SELECT)); select_button->set_pressed(true); select_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/select_mode", TTR("Select Mode"), KEY_Q)); select_button->set_tooltip(TTR("Select Mode") + " $sc\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Drag: Rotate") + "\n" + TTR("Alt+Drag: Move") + "\n" + TTR("Press 'v' to Change Pivot, 'Shift+v' to Drag Pivot (while moving).") + "\n" + TTR("Alt+RMB: Depth list selection")); move_button = memnew(ToolButton); - move_button->set_toggle_mode(true); hb->add_child(move_button); + move_button->set_toggle_mode(true); move_button->connect("pressed", this, "_tool_select", make_binds(TOOL_MOVE)); move_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/move_mode", TTR("Move Mode"), KEY_W)); move_button->set_tooltip(TTR("Move Mode")); rotate_button = memnew(ToolButton); - rotate_button->set_toggle_mode(true); hb->add_child(rotate_button); + rotate_button->set_toggle_mode(true); rotate_button->connect("pressed", this, "_tool_select", make_binds(TOOL_ROTATE)); rotate_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/rotate_mode", TTR("Rotate Mode"), KEY_E)); rotate_button->set_tooltip(TTR("Rotate Mode")); @@ -3319,25 +3789,60 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { hb->add_child(memnew(VSeparator)); list_select_button = memnew(ToolButton); - list_select_button->set_toggle_mode(true); hb->add_child(list_select_button); + list_select_button->set_toggle_mode(true); list_select_button->connect("pressed", this, "_tool_select", make_binds(TOOL_LIST_SELECT)); list_select_button->set_tooltip(TTR("Show a list of all objects at the position clicked\n(same as Alt+RMB in select mode).")); pivot_button = memnew(ToolButton); - pivot_button->set_toggle_mode(true); hb->add_child(pivot_button); + pivot_button->set_toggle_mode(true); pivot_button->connect("pressed", this, "_tool_select", make_binds(TOOL_EDIT_PIVOT)); pivot_button->set_tooltip(TTR("Click to change object's rotation pivot.")); pan_button = memnew(ToolButton); - pan_button->set_toggle_mode(true); hb->add_child(pan_button); + pan_button->set_toggle_mode(true); pan_button->connect("pressed", this, "_tool_select", make_binds(TOOL_PAN)); pan_button->set_tooltip(TTR("Pan Mode")); hb->add_child(memnew(VSeparator)); + snap_button = memnew(ToolButton); + hb->add_child(snap_button); + snap_button->set_toggle_mode(true); + snap_button->connect("toggled", this, "_toggle_snap"); + snap_button->set_tooltip(TTR("Toggles snapping")); + snap_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/use_snap", TTR("Use Snap"), KEY_S)); + + snap_config_menu = memnew(MenuButton); + hb->add_child(snap_config_menu); + snap_config_menu->get_popup()->connect("id_pressed", this, "_popup_callback"); + snap_config_menu->set_h_size_flags(SIZE_SHRINK_END); + snap_config_menu->set_tooltip(TTR("Snapping options")); + + PopupMenu *p = snap_config_menu->get_popup(); + p->connect("id_pressed", this, "_popup_callback"); + p->set_hide_on_checkable_item_selection(false); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_grid", TTR("Snap to grid")), SNAP_USE_GRID); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_rotation_snap", TTR("Use Rotation Snap")), SNAP_USE_ROTATION); + p->add_shortcut(ED_SHORTCUT("canvas_item_editor/configure_snap", TTR("Configure Snap...")), SNAP_CONFIGURE); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_relative", TTR("Snap Relative")), SNAP_RELATIVE); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_pixel_snap", TTR("Use Pixel Snap")), SNAP_USE_PIXEL); + p->add_submenu_item(TTR("Smart snapping"), "SmartSnapping"); + + smartsnap_config_popup = memnew(PopupMenu); + p->add_child(smartsnap_config_popup); + smartsnap_config_popup->set_name("SmartSnapping"); + smartsnap_config_popup->connect("id_pressed", this, "_popup_callback"); + smartsnap_config_popup->set_hide_on_checkable_item_selection(false); + smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_parent", TTR("Snap to parent")), SNAP_USE_NODE_PARENT); + smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_anchors", TTR("Snap to node anchor")), SNAP_USE_NODE_ANCHORS); + smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_sides", TTR("Snap to node sides")), SNAP_USE_NODE_SIDES); + smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_other_nodes", TTR("Snap to other nodes")), SNAP_USE_OTHER_NODES); + + hb->add_child(memnew(VSeparator)); + lock_button = memnew(ToolButton); hb->add_child(lock_button); @@ -3361,35 +3866,23 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { hb->add_child(memnew(VSeparator)); - edit_menu = memnew(MenuButton); - edit_menu->set_text(TTR("Edit")); - hb->add_child(edit_menu); - edit_menu->get_popup()->connect("id_pressed", this, "_popup_callback"); + skeleton_menu = memnew(MenuButton); + hb->add_child(skeleton_menu); - PopupMenu *p; - p = edit_menu->get_popup(); + p = skeleton_menu->get_popup(); p->set_hide_on_checkable_item_selection(false); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_snap", TTR("Use Snap")), SNAP_USE); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_grid", TTR("Show Grid")), SNAP_SHOW_GRID); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_rotation_snap", TTR("Use Rotation Snap")), SNAP_USE_ROTATION); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_relative", TTR("Snap Relative")), SNAP_RELATIVE); - p->add_shortcut(ED_SHORTCUT("canvas_item_editor/configure_snap", TTR("Configure Snap..")), SNAP_CONFIGURE); + p->connect("id_pressed", this, "_popup_callback"); + p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_make_bones", TTR("Make Bones"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B), SKELETON_MAKE_BONES); + p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_clear_bones", TTR("Clear Bones")), SKELETON_CLEAR_BONES); p->add_separator(); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_pixel_snap", TTR("Use Pixel Snap")), SNAP_USE_PIXEL); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_show_bones", TTR("Show Bones")), SKELETON_SHOW_BONES); p->add_separator(); - p->add_submenu_item(TTR("Skeleton.."), "skeleton"); - skeleton_menu = memnew(PopupMenu); - p->add_child(skeleton_menu); - skeleton_menu->set_name("skeleton"); - skeleton_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_make_bones", TTR("Make Bones"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B), SKELETON_MAKE_BONES); - skeleton_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_clear_bones", TTR("Clear Bones")), SKELETON_CLEAR_BONES); - skeleton_menu->add_separator(); - skeleton_menu->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_show_bones", TTR("Show Bones")), SKELETON_SHOW_BONES); - skeleton_menu->add_separator(); - skeleton_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_set_ik_chain", TTR("Make IK Chain")), SKELETON_SET_IK_CHAIN); - skeleton_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_clear_ik_chain", TTR("Clear IK Chain")), SKELETON_CLEAR_IK_CHAIN); - skeleton_menu->set_hide_on_checkable_item_selection(false); - skeleton_menu->connect("id_pressed", this, "_popup_callback"); + p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_set_ik_chain", TTR("Make IK Chain")), SKELETON_SET_IK_CHAIN); + p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_clear_ik_chain", TTR("Clear IK Chain")), SKELETON_CLEAR_IK_CHAIN); + p->set_hide_on_checkable_item_selection(false); + p->connect("id_pressed", this, "_popup_callback"); + + hb->add_child(memnew(VSeparator)); view_menu = memnew(MenuButton); view_menu->set_text(TTR("View")); @@ -3397,11 +3890,9 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { view_menu->get_popup()->connect("id_pressed", this, "_popup_callback"); p = view_menu->get_popup(); - - p->add_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_in", TTR("Zoom In")), ZOOM_IN); - p->add_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_out", TTR("Zoom Out")), ZOOM_OUT); - p->add_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_reset", TTR("Zoom Reset")), ZOOM_RESET); - p->add_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_set", TTR("Zoom Set..")), ZOOM_SET); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_grid", TTR("Show Grid"), KEY_G), SHOW_GRID); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_helpers", TTR("Show helpers"), KEY_H), SHOW_HELPERS); + p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_rulers", TTR("Show rulers"), KEY_R), SHOW_RULERS); p->add_separator(); p->add_shortcut(ED_SHORTCUT("canvas_item_editor/center_selection", TTR("Center Selection"), KEY_F), VIEW_CENTER_TO_SELECTION); p->add_shortcut(ED_SHORTCUT("canvas_item_editor/frame_selection", TTR("Frame Selection"), KEY_MASK_SHIFT | KEY_F), VIEW_FRAME_TO_SELECTION); @@ -3472,23 +3963,6 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { snap_dialog->connect("confirmed", this, "_snap_changed"); add_child(snap_dialog); - value_dialog = memnew(AcceptDialog); - value_dialog->set_title(TTR("Set a Value")); - value_dialog->get_ok()->set_text(TTR("Close")); - add_child(value_dialog); - - Label *l = memnew(Label); - l->set_text(TTR("Snap (Pixels):")); - l->set_position(Point2(5, 5)); - value_dialog->add_child(l); - dialog_label = l; - - 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)); - value_dialog->add_child(dialog_val); - dialog_val->connect("value_changed", this, "_dialog_value_changed"); select_sb = Ref<StyleBoxTexture>(memnew(StyleBoxTexture)); selection_menu = memnew(PopupMenu); @@ -3497,22 +3971,35 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { selection_menu->connect("id_pressed", this, "_selection_result_pressed"); selection_menu->connect("popup_hide", this, "_selection_menu_hide"); + drag_pivot_shortcut = ED_SHORTCUT("canvas_item_editor/drag_pivot", TTR("Drag pivot from mouse position"), KEY_MASK_SHIFT | KEY_V); + set_pivot_shortcut = ED_SHORTCUT("canvas_item_editor/set_pivot", TTR("Set pivot at mouse position"), KEY_V); + + multiply_grid_step_shortcut = ED_SHORTCUT("canvas_item_editor/multiply_grid_step", TTR("Multiply grid step by 2"), KEY_KP_MULTIPLY); + divide_grid_step_shortcut = ED_SHORTCUT("canvas_item_editor/divide_grid_step", TTR("Divide grid step by 2"), KEY_KP_DIVIDE); + key_pos = true; key_rot = true; key_scale = false; + show_grid = false; + show_helpers = false; + show_rulers = false; zoom = 1; - snap_offset = Vector2(0, 0); - snap_step = Vector2(10, 10); + grid_offset = Point2(); + grid_step = Point2(10, 10); + grid_step_multiplier = 0; snap_rotation_offset = 0; snap_rotation_step = 15 / (180 / Math_PI); - snap_grid = false; - snap_show_grid = false; + snap_active = false; + snap_node_parent = true; + snap_node_anchors = true; + snap_node_sides = true; + snap_other_nodes = true; + snap_grid = true; snap_rotation = false; snap_pixel = false; skeleton_show_bones = true; - skeleton_menu->set_item_checked(skeleton_menu->get_item_index(SKELETON_SHOW_BONES), true); - updating_value_dialog = false; + skeleton_menu->get_popup()->set_item_checked(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES), true); box_selecting = false; //zoom=0.5; singleton = this; @@ -3543,7 +4030,7 @@ void CanvasItemEditorPlugin::make_visible(bool p_visible) { canvas_item_editor->show(); canvas_item_editor->set_fixed_process(true); VisualServer::get_singleton()->viewport_set_hide_canvas(editor->get_scene_root()->get_viewport_rid(), false); - canvas_item_editor->viewport->grab_focus(); + canvas_item_editor->viewport_base->grab_focus(); } else { @@ -3707,13 +4194,13 @@ void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String & pos = parent->call("get_global_position"); } Transform2D trans = canvas->get_canvas_transform(); - Point2 target_pos = (p_point - trans.get_origin()) / trans.get_scale().x - pos; + Point2 target_position = (p_point - trans.get_origin()) / trans.get_scale().x - pos; if (default_type == "Polygon2D" || default_type == "TouchScreenButton" || default_type == "TextureRect" || default_type == "Patch9Rect") { - target_pos -= texture_size / 2; + target_position -= texture_size / 2; } // there's nothing to be used as source position so snapping will work as absolute if enabled - target_pos = canvas->snap_point(target_pos, Vector2()); - editor_data->get_undo_redo().add_do_method(child, "set_position", target_pos); + target_position = canvas->snap_point(target_position); + editor_data->get_undo_redo().add_do_method(child, "set_position", target_position); } bool CanvasItemEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) { @@ -3746,22 +4233,13 @@ bool CanvasItemEditorViewport::_create_instance(Node *parent, String &path, cons editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", editor->get_edited_scene()->get_path_to(parent), path, new_name); editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name)); - Point2 pos; - Node2D *parent_node2d = Object::cast_to<Node2D>(parent); - if (parent_node2d) { - pos = parent_node2d->get_global_position(); - } else { - Control *parent_control = Object::cast_to<Control>(parent); - if (parent_control) { - pos = parent_control->get_global_position(); - } + CanvasItem *parent_ci = Object::cast_to<CanvasItem>(parent); + if (parent_ci) { + Vector2 target_pos = canvas->get_canvas_transform().affine_inverse().xform(p_point); + target_pos = canvas->snap_point(target_pos); + target_pos = parent_ci->get_global_transform_with_canvas().affine_inverse().xform(target_pos); + editor_data->get_undo_redo().add_do_method(instanced_scene, "set_position", target_pos); } - Transform2D trans = canvas->get_canvas_transform(); - Vector2 target_pos = (p_point - trans.get_origin()) / trans.get_scale().x - pos; - // in relative snapping it may be useful for the user to take the original node position into account - Vector2 start_pos = Object::cast_to<Node2D>(instanced_scene) ? Object::cast_to<Node2D>(instanced_scene)->get_position() : target_pos; - target_pos = canvas->snap_point(target_pos, start_pos); - editor_data->get_undo_redo().add_do_method(instanced_scene, "set_position", target_pos); return true; } @@ -3840,6 +4318,20 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian continue; } memdelete(instanced_scene); + } else if (type == "Texture" || + type == "ImageTexture" || + type == "ViewportTexture" || + type == "CurveTexture" || + type == "GradientTexture" || + type == "StreamTexture" || + type == "AtlasTexture" || + type == "LargeTexture") { + Ref<Texture> texture = ResourceLoader::load(files[i]); + if (texture.is_valid() == false) { + continue; + } + } else { + continue; } can_instance = true; break; @@ -3948,41 +4440,39 @@ CanvasItemEditorViewport::CanvasItemEditorViewport(EditorNode *p_node, CanvasIte editor_data = editor->get_scene_tree_dock()->get_editor_data(); canvas = p_canvas; preview_node = memnew(Node2D); + accept = memnew(AcceptDialog); editor->get_gui_base()->add_child(accept); selector = memnew(AcceptDialog); + editor->get_gui_base()->add_child(selector); selector->set_title(TTR("Change default type")); + selector->connect("confirmed", this, "_on_change_type"); VBoxContainer *vbc = memnew(VBoxContainer); + selector->add_child(vbc); vbc->set_h_size_flags(SIZE_EXPAND_FILL); vbc->set_v_size_flags(SIZE_EXPAND_FILL); vbc->set_custom_minimum_size(Size2(200, 260) * EDSCALE); selector_label = memnew(Label); + vbc->add_child(selector_label); selector_label->set_align(Label::ALIGN_CENTER); selector_label->set_valign(Label::VALIGN_BOTTOM); selector_label->set_custom_minimum_size(Size2(0, 30) * EDSCALE); - vbc->add_child(selector_label); - - button_group.instance(); btn_group = memnew(VBoxContainer); + vbc->add_child(btn_group); btn_group->set_h_size_flags(0); + button_group.instance(); for (int i = 0; i < types.size(); i++) { CheckBox *check = memnew(CheckBox); + btn_group->add_child(check); check->set_text(types[i]); check->connect("button_down", this, "_on_select_type", varray(check)); - btn_group->add_child(check); check->set_button_group(button_group); } - vbc->add_child(btn_group); - - selector->connect("confirmed", this, "_on_change_type"); - - selector->add_child(vbc); - editor->get_gui_base()->add_child(selector); label = memnew(Label); label->add_color_override("font_color_shadow", Color(0, 0, 0, 1)); diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index 2e1a2eac5f..af834507e0 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -58,6 +58,8 @@ public: Vector2 prev_pivot; float prev_anchors[4]; + Transform2D pre_drag_xform; + CanvasItemEditorSelectedItem() { prev_rot = 0; } }; @@ -68,7 +70,6 @@ class CanvasItemEditor : public VBoxContainer { EditorNode *editor; enum Tool { - TOOL_SELECT, TOOL_LIST_SELECT, TOOL_MOVE, @@ -80,15 +81,18 @@ class CanvasItemEditor : public VBoxContainer { enum MenuOption { SNAP_USE, - SNAP_SHOW_GRID, + SNAP_USE_NODE_PARENT, + SNAP_USE_NODE_ANCHORS, + SNAP_USE_NODE_SIDES, + SNAP_USE_OTHER_NODES, + SNAP_USE_GRID, SNAP_USE_ROTATION, SNAP_RELATIVE, SNAP_CONFIGURE, SNAP_USE_PIXEL, - ZOOM_IN, - ZOOM_OUT, - ZOOM_RESET, - ZOOM_SET, + SHOW_GRID, + SHOW_HELPERS, + SHOW_RULERS, LOCK_SELECTED, UNLOCK_SELECTED, GROUP_SELECTED, @@ -161,6 +165,8 @@ class CanvasItemEditor : public VBoxContainer { Tool tool; bool first_update; Control *viewport; + Control *viewport_base; + Control *viewport_scrollable; bool can_move_pivot; @@ -168,14 +174,28 @@ class CanvasItemEditor : public VBoxContainer { VScrollBar *v_scroll; HBoxContainer *hb; + ToolButton *zoom_minus; + ToolButton *zoom_reset; + ToolButton *zoom_plus; + Transform2D transform; + bool show_grid; + bool show_rulers; + bool show_helpers; float zoom; - Vector2 snap_offset; - Vector2 snap_step; + + Point2 grid_offset; + Point2 grid_step; + int grid_step_multiplier; + float snap_rotation_step; float snap_rotation_offset; + bool snap_active; + bool snap_node_parent; + bool snap_node_anchors; + bool snap_node_sides; + bool snap_other_nodes; bool snap_grid; - bool snap_show_grid; bool snap_rotation; bool snap_relative; bool snap_pixel; @@ -202,18 +222,6 @@ class CanvasItemEditor : public VBoxContainer { Vector<_SelectResult> selection_results; - struct LockList { - Point2 pos; - bool lock; - bool group; - LockList() { - lock = false; - group = false; - } - }; - - List<LockList> lock_list; - struct BoneList { Transform2D xform; @@ -253,6 +261,10 @@ class CanvasItemEditor : public VBoxContainer { ToolButton *move_button; ToolButton *rotate_button; + ToolButton *snap_button; + MenuButton *snap_config_menu; + PopupMenu *smartsnap_config_popup; + ToolButton *pivot_button; ToolButton *pan_button; @@ -262,8 +274,7 @@ class CanvasItemEditor : public VBoxContainer { ToolButton *group_button; ToolButton *ungroup_button; - MenuButton *edit_menu; - PopupMenu *skeleton_menu; + MenuButton *skeleton_menu; MenuButton *view_menu; HBoxContainer *animation_hb; MenuButton *animation_menu; @@ -276,6 +287,9 @@ class CanvasItemEditor : public VBoxContainer { PopupMenu *selection_menu; + Control *top_ruler; + Control *left_ruler; + //PopupMenu *popup; DragType drag; Point2 drag_from; @@ -288,6 +302,11 @@ class CanvasItemEditor : public VBoxContainer { Ref<Texture> select_handle; Ref<Texture> anchor_handle; + Ref<ShortCut> drag_pivot_shortcut; + Ref<ShortCut> set_pivot_shortcut; + Ref<ShortCut> multiply_grid_step_shortcut; + Ref<ShortCut> divide_grid_step_shortcut; + int handle_len; bool _is_part_of_subscene(CanvasItem *p_item); void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform, Vector<_SelectResult> &r_items, int limit = 0); @@ -298,10 +317,6 @@ class CanvasItemEditor : public VBoxContainer { ConfirmationDialog *snap_dialog; - AcceptDialog *value_dialog; - Label *dialog_label; - SpinBox *dialog_val; - CanvasItem *ref_item; void _edit_set_pivot(const Vector2 &mouse_pos); @@ -315,9 +330,8 @@ class CanvasItemEditor : public VBoxContainer { 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); + Vector2 _anchor_to_position(const Control *p_control, Vector2 anchor); + Vector2 _position_to_anchor(const Control *p_control, Vector2 position); void _popup_callback(int p_op); bool updating_scroll; @@ -328,7 +342,6 @@ class CanvasItemEditor : public VBoxContainer { void incend(float &beg, float &end, float inc, float minsize, bool p_symmetric); void _append_canvas_item(CanvasItem *p_item); - void _dialog_value_changed(double); void _snap_changed(); void _selection_result_pressed(int); void _selection_menu_hide(); @@ -337,26 +350,49 @@ class CanvasItemEditor : public VBoxContainer { Point2 _find_topleftmost_point(); - void _find_canvas_items_span(Node *p_node, Rect2 &r_rect, const Transform2D &p_xform); + void _build_bones_list(Node *p_node); + + void _get_encompassing_rect(Node *p_node, Rect2 &r_rect, const Transform2D &p_xform); Object *_get_editor_data(Object *p_what); - CanvasItem *get_single_item(); + CanvasItem *_get_single_item(); int get_item_count(); void _keying_changed(); void _unhandled_key_input(const Ref<InputEvent> &p_ev); + void _draw_text_at_position(Point2 p_position, String p_string, Margin p_side); + void _draw_margin_at_position(int p_value, Point2 p_position, Margin p_side); 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 _draw_rulers(); + void _draw_focus(); + void _draw_grid(); + void _draw_selection(); + void _draw_axis(); + void _draw_bones(); + void _draw_locks_and_groups(Node *p_node, const Transform2D &p_xform); + + void _draw_viewport(); + + void _viewport_base_gui_input(const Ref<InputEvent> &p_event); + void _draw_viewport_base(); void _focus_selection(int p_op); + void _snap_if_closer(Point2 p_value, Point2 p_target_snap, Point2 &r_current_snap, bool (&r_snapped)[2], real_t rotation = 0.0, float p_radius = 10.0); + void _snap_other_nodes(Point2 p_value, Point2 &r_current_snap, bool (&r_snapped)[2], const Node *p_current, const CanvasItem *p_to_snap); + void _set_anchors_preset(Control::LayoutPreset p_preset); void _set_full_rect(); + void _zoom_minus(); + void _zoom_reset(); + void _zoom_plus(); + + void _toggle_snap(bool p_status); + HSplitContainer *palette_split; VSplitContainer *bottom_split; @@ -400,7 +436,18 @@ protected: static CanvasItemEditor *singleton; public: - Vector2 snap_point(Vector2 p_target, Vector2 p_start = Vector2(0, 0)) const; + enum SnapMode { + SNAP_GRID = 1 << 0, + SNAP_PIXEL = 1 << 1, + SNAP_NODE_PARENT = 1 << 2, + SNAP_NODE_ANCHORS = 1 << 3, + SNAP_NODE_SIDES = 1 << 4, + SNAP_OTHER_NODES = 1 << 5, + + SNAP_DEFAULT = 0x03, + }; + + Point2 snap_point(Point2 p_target, unsigned int p_modes = SNAP_DEFAULT, const CanvasItem *p_canvas_item = NULL, unsigned int p_forced_modes = 0); float snap_angle(float p_target, float p_start = 0) const; Transform2D get_canvas_transform() const { return transform; } diff --git a/editor/plugins/curve_editor_plugin.cpp b/editor/plugins/curve_editor_plugin.cpp index 615cf85aa4..70d771afc8 100644 --- a/editor/plugins/curve_editor_plugin.cpp +++ b/editor/plugins/curve_editor_plugin.cpp @@ -188,7 +188,7 @@ void CurveEditor::on_gui_input(const Ref<InputEvent> &p_event) { } else { // Drag tangent - Vector2 point_pos = curve.get_point_pos(_selected_point); + Vector2 point_pos = curve.get_point_position(_selected_point); Vector2 control_pos = get_world_pos(mpos); Vector2 dir = (control_pos - point_pos).normalized(); @@ -378,7 +378,7 @@ int CurveEditor::get_point_at(Vector2 pos) const { const float r = _hover_radius * _hover_radius; for (int i = 0; i < curve.get_point_count(); ++i) { - Vector2 p = get_view_pos(curve.get_point_pos(i)); + Vector2 p = get_view_pos(curve.get_point_position(i)); if (p.distance_squared_to(pos) <= r) { return i; } @@ -525,8 +525,8 @@ Vector2 CurveEditor::get_tangent_view_pos(int i, TangentIndex tangent) const { else dir = Vector2(1, _curve_ref->get_point_right_tangent(i)); - Vector2 point_pos = get_view_pos(_curve_ref->get_point_pos(i)); - Vector2 control_pos = get_view_pos(_curve_ref->get_point_pos(i) + dir); + Vector2 point_pos = get_view_pos(_curve_ref->get_point_position(i)); + Vector2 control_pos = get_view_pos(_curve_ref->get_point_position(i) + dir); return point_pos + _tangents_length * (control_pos - point_pos).normalized(); } @@ -549,8 +549,8 @@ static void plot_curve_accurate(const Curve &curve, float step, T plot_func) { plot_func(Vector2(0, y), Vector2(1.f, y), true); } else { - Vector2 first_point = curve.get_point_pos(0); - Vector2 last_point = curve.get_point_pos(curve.get_point_count() - 1); + Vector2 first_point = curve.get_point_position(0); + Vector2 last_point = curve.get_point_position(curve.get_point_count() - 1); // Edge lines plot_func(Vector2(0, first_point.y), first_point, false); @@ -559,8 +559,8 @@ static void plot_curve_accurate(const Curve &curve, float step, T plot_func) { // Draw section by section, so that we get maximum precision near points. // It's an accurate representation, but slower than using the baked one. for (int i = 1; i < curve.get_point_count(); ++i) { - Vector2 a = curve.get_point_pos(i - 1); - Vector2 b = curve.get_point_pos(i); + Vector2 a = curve.get_point_position(i - 1); + Vector2 b = curve.get_point_position(i); Vector2 pos = a; Vector2 prev_pos = a; @@ -667,7 +667,7 @@ void CurveEditor::_draw() { const Color tangent_color(0.5, 0.5, 1, 1); int i = _selected_point; - Vector2 pos = curve.get_point_pos(i); + Vector2 pos = curve.get_point_position(i); if (i != 0) { Vector2 control_pos = get_tangent_view_pos(i, TANGENT_LEFT); @@ -718,7 +718,7 @@ void CurveEditor::_draw() { const Color selected_point_color(1, 0.5, 0.5); for (int i = 0; i < curve.get_point_count(); ++i) { - Vector2 pos = curve.get_point_pos(i); + Vector2 pos = curve.get_point_position(i); draw_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(3), i == _selected_point ? selected_point_color : point_color); // TODO Circles are prettier. Needs a fix! Or a texture //draw_circle(pos, 2, point_color); @@ -728,7 +728,7 @@ void CurveEditor::_draw() { if (_hover_point != -1) { const Color hover_color = line_color; - Vector2 pos = curve.get_point_pos(_hover_point); + Vector2 pos = curve.get_point_position(_hover_point); stroke_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(_hover_radius), hover_color); } diff --git a/editor/plugins/line_2d_editor_plugin.cpp b/editor/plugins/line_2d_editor_plugin.cpp index 84620a75a5..ef3ee6a78f 100644 --- a/editor/plugins/line_2d_editor_plugin.cpp +++ b/editor/plugins/line_2d_editor_plugin.cpp @@ -66,7 +66,7 @@ int Line2DEditor::get_point_index_at(Vector2 gpos) { Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); for (int i = 0; i < node->get_point_count(); ++i) { - Point2 p = xform.xform(node->get_point_pos(i)); + Point2 p = xform.xform(node->get_point_position(i)); if (gpos.distance_to(p) < grab_threshold) { return i; } @@ -96,12 +96,12 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (mb->get_button_index() == BUTTON_LEFT && !mb->get_shift() && mode == MODE_EDIT) { _dragging = true; action_point = i; - moving_from = node->get_point_pos(i); + moving_from = node->get_point_position(i); moving_screen_from = gpoint; } else if ((mb->get_button_index() == BUTTON_RIGHT && mode == MODE_EDIT) || (mb->get_button_index() == BUTTON_LEFT && mode == MODE_DELETE)) { undo_redo->create_action(TTR("Remove Point from Line2D")); undo_redo->add_do_method(node, "remove_point", i); - undo_redo->add_undo_method(node, "add_point", node->get_point_pos(i), i); + undo_redo->add_undo_method(node, "add_point", node->get_point_position(i), i); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->commit_action(); @@ -121,7 +121,7 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { _dragging = true; action_point = node->get_point_count() - 1; - moving_from = node->get_point_pos(action_point); + moving_from = node->get_point_position(action_point); moving_screen_from = gpoint; canvas_item_editor->get_viewport_control()->update(); @@ -131,8 +131,8 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && _dragging) { undo_redo->create_action(TTR("Move Point in Line2D")); - undo_redo->add_do_method(node, "set_point_pos", action_point, cpoint); - undo_redo->add_undo_method(node, "set_point_pos", action_point, moving_from); + undo_redo->add_do_method(node, "set_point_position", action_point, cpoint); + undo_redo->add_undo_method(node, "set_point_position", action_point, moving_from); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->commit_action(); @@ -147,7 +147,7 @@ bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (_dragging) { Vector2 cpoint = mouse_to_local_pos(mm->get_position(), mm->get_alt()); - node->set_point_pos(action_point, cpoint); + node->set_point_position(action_point, cpoint); canvas_item_editor->get_viewport_control()->update(); return true; } @@ -172,7 +172,7 @@ void Line2DEditor::_canvas_draw() { Control *vpc = canvas_item_editor->get_viewport_control(); for (int i = 0; i < len; ++i) { - Vector2 point = xform.xform(node->get_point_pos(i)); + Vector2 point = xform.xform(node->get_point_position(i)); vpc->draw_texture_rect(handle, Rect2(point - handle_size * 0.5, handle_size), false); } } diff --git a/editor/plugins/navigation_mesh_editor_plugin.cpp b/editor/plugins/navigation_mesh_editor_plugin.cpp new file mode 100644 index 0000000000..f0f5a62494 --- /dev/null +++ b/editor/plugins/navigation_mesh_editor_plugin.cpp @@ -0,0 +1,165 @@ +/*************************************************************************/ +/* navigation_mesh_editor_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "navigation_mesh_editor_plugin.h" +#include "io/marshalls.h" +#include "io/resource_saver.h" +#include "scene/3d/mesh_instance.h" +#include "scene/gui/box_container.h" + +#ifdef RECAST_ENABLED + +void NavigationMeshEditor::_node_removed(Node *p_node) { + + if (p_node == node) { + node = NULL; + + hide(); + } +} + +void NavigationMeshEditor::_notification(int p_option) { + + if (p_option == NOTIFICATION_ENTER_TREE) { + + button_bake->set_icon(get_icon("Bake", "EditorIcons")); + button_reset->set_icon(get_icon("Reload", "EditorIcons")); + } +} + +void NavigationMeshEditor::_bake_pressed() { + + ERR_FAIL_COND(!node); + const String conf_warning = node->get_configuration_warning(); + if (!conf_warning.empty()) { + err_dialog->set_text(conf_warning); + err_dialog->popup_centered_minsize(); + button_bake->set_pressed(false); + return; + } + + NavigationMeshGenerator::clear(node->get_navigation_mesh()); + NavigationMeshGenerator::bake(node->get_navigation_mesh(), node); + + if (node) { + node->update_gizmo(); + } +} + +void NavigationMeshEditor::_clear_pressed() { + + if (node) + NavigationMeshGenerator::clear(node->get_navigation_mesh()); + + button_bake->set_pressed(false); + bake_info->set_text(""); + + if (node) { + node->update_gizmo(); + } +} + +void NavigationMeshEditor::edit(NavigationMeshInstance *p_nav_mesh_instance) { + + if (p_nav_mesh_instance == NULL || node == p_nav_mesh_instance) { + return; + } + + node = p_nav_mesh_instance; +} + +void NavigationMeshEditor::_bind_methods() { + + ClassDB::bind_method("_bake_pressed", &NavigationMeshEditor::_bake_pressed); + ClassDB::bind_method("_clear_pressed", &NavigationMeshEditor::_clear_pressed); +} + +NavigationMeshEditor::NavigationMeshEditor() { + + bake_hbox = memnew(HBoxContainer); + button_bake = memnew(ToolButton); + button_bake->set_text(TTR("Bake!")); + button_bake->set_toggle_mode(true); + button_reset = memnew(Button); + button_bake->set_tooltip(TTR("Bake the navigation mesh.\n")); + + bake_info = memnew(Label); + bake_hbox->add_child(button_bake); + bake_hbox->add_child(button_reset); + bake_hbox->add_child(bake_info); + + err_dialog = memnew(AcceptDialog); + add_child(err_dialog); + node = NULL; + + button_bake->connect("pressed", this, "_bake_pressed"); + button_reset->connect("pressed", this, "_clear_pressed"); + button_reset->set_tooltip(TTR("Clear the navigation mesh.")); +} + +NavigationMeshEditor::~NavigationMeshEditor() { +} + +void NavigationMeshEditorPlugin::edit(Object *p_object) { + + navigation_mesh_editor->edit(Object::cast_to<NavigationMeshInstance>(p_object)); +} + +bool NavigationMeshEditorPlugin::handles(Object *p_object) const { + + return p_object->is_class("NavigationMeshInstance"); +} + +void NavigationMeshEditorPlugin::make_visible(bool p_visible) { + + if (p_visible) { + navigation_mesh_editor->show(); + navigation_mesh_editor->bake_hbox->show(); + } else { + + navigation_mesh_editor->hide(); + navigation_mesh_editor->bake_hbox->hide(); + navigation_mesh_editor->edit(NULL); + } +} + +NavigationMeshEditorPlugin::NavigationMeshEditorPlugin(EditorNode *p_node) { + + editor = p_node; + navigation_mesh_editor = memnew(NavigationMeshEditor); + editor->get_viewport()->add_child(navigation_mesh_editor); + add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox); + navigation_mesh_editor->hide(); + navigation_mesh_editor->bake_hbox->hide(); +} + +NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() { +} + +#endif // RECAST_ENABLED diff --git a/editor/plugins/navigation_mesh_editor_plugin.h b/editor/plugins/navigation_mesh_editor_plugin.h new file mode 100644 index 0000000000..bac7f608ab --- /dev/null +++ b/editor/plugins/navigation_mesh_editor_plugin.h @@ -0,0 +1,86 @@ +/*************************************************************************/ +/* navigation_mesh_editor_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef NAVIGATION_MESH_GENERATOR_PLUGIN_H +#define NAVIGATION_MESH_GENERATOR_PLUGIN_H + +#ifdef RECAST_ENABLED + +#include "editor/editor_node.h" +#include "editor/editor_plugin.h" +#include "navigation_mesh_generator.h" + +class NavigationMeshEditor : public Control { + friend class NavigationMeshEditorPlugin; + + GDCLASS(NavigationMeshEditor, Control); + + AcceptDialog *err_dialog; + + HBoxContainer *bake_hbox; + Button *button_bake; + Button *button_reset; + Label *bake_info; + + NavigationMeshInstance *node; + + void _bake_pressed(); + void _clear_pressed(); + +protected: + void _node_removed(Node *p_node); + static void _bind_methods(); + void _notification(int p_option); + +public: + void edit(NavigationMeshInstance *p_nav_mesh_instance); + NavigationMeshEditor(); + ~NavigationMeshEditor(); +}; + +class NavigationMeshEditorPlugin : public EditorPlugin { + + GDCLASS(NavigationMeshEditorPlugin, EditorPlugin); + + NavigationMeshEditor *navigation_mesh_editor; + EditorNode *editor; + +public: + virtual String get_name() const { return "NavigationMesh"; } + bool has_main_screen() const { return false; } + virtual void edit(Object *p_object); + virtual bool handles(Object *p_object) const; + virtual void make_visible(bool p_visible); + + NavigationMeshEditorPlugin(EditorNode *p_node); + ~NavigationMeshEditorPlugin(); +}; + +#endif // RECAST_ENABLED +#endif // NAVIGATION_MESH_GENERATOR_PLUGIN_H diff --git a/editor/plugins/navigation_mesh_generator.cpp b/editor/plugins/navigation_mesh_generator.cpp new file mode 100644 index 0000000000..526db3a582 --- /dev/null +++ b/editor/plugins/navigation_mesh_generator.cpp @@ -0,0 +1,311 @@ +/*************************************************************************/ +/* navigation_mesh_generator.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "navigation_mesh_generator.h" + +#ifdef RECAST_ENABLED + +void NavigationMeshGenerator::_add_vertex(const Vector3 &p_vec3, Vector<float> &p_verticies) { + p_verticies.push_back(p_vec3.x); + p_verticies.push_back(p_vec3.y); + p_verticies.push_back(p_vec3.z); +} + +void NavigationMeshGenerator::_add_mesh(const Ref<Mesh> &p_mesh, const Transform &p_xform, Vector<float> &p_verticies, Vector<int> &p_indices) { + int current_vertex_count = p_verticies.size() / 3; + + for (int i = 0; i < p_mesh->get_surface_count(); i++) { + if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) + continue; + + int index_count = 0; + if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) { + index_count = p_mesh->surface_get_array_index_len(i); + } else { + index_count = p_mesh->surface_get_array_len(i); + } + + ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0)); + + int face_count = index_count / 3; + + Array a = p_mesh->surface_get_arrays(i); + + PoolVector<Vector3> mesh_vertices = a[Mesh::ARRAY_VERTEX]; + PoolVector<Vector3>::Read vr = mesh_vertices.read(); + + if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) { + + PoolVector<int> mesh_indices = a[Mesh::ARRAY_INDEX]; + PoolVector<int>::Read ir = mesh_indices.read(); + + for (int i = 0; i < mesh_vertices.size(); i++) { + _add_vertex(p_xform.xform(vr[i]), p_verticies); + } + + for (int i = 0; i < face_count; i++) { + // CCW + p_indices.push_back(current_vertex_count + (ir[i * 3 + 0])); + p_indices.push_back(current_vertex_count + (ir[i * 3 + 2])); + p_indices.push_back(current_vertex_count + (ir[i * 3 + 1])); + } + } else { + face_count = mesh_vertices.size() / 3; + for (int i = 0; i < face_count; i++) { + _add_vertex(p_xform.xform(vr[i * 3 + 0]), p_verticies); + _add_vertex(p_xform.xform(vr[i * 3 + 2]), p_verticies); + _add_vertex(p_xform.xform(vr[i * 3 + 1]), p_verticies); + + p_indices.push_back(current_vertex_count + (i * 3 + 0)); + p_indices.push_back(current_vertex_count + (i * 3 + 1)); + p_indices.push_back(current_vertex_count + (i * 3 + 2)); + } + } + } +} + +void NavigationMeshGenerator::_parse_geometry(const Transform &p_base_inverse, Node *p_node, Vector<float> &p_verticies, Vector<int> &p_indices) { + + if (Object::cast_to<MeshInstance>(p_node)) { + + MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(p_node); + Ref<Mesh> mesh = mesh_instance->get_mesh(); + if (mesh.is_valid()) { + _add_mesh(mesh, p_base_inverse * mesh_instance->get_global_transform(), p_verticies, p_indices); + } + } + + for (int i = 0; i < p_node->get_child_count(); i++) { + _parse_geometry(p_base_inverse, p_node->get_child(i), p_verticies, p_indices); + } +} + +void NavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_nav_mesh) { + + PoolVector<Vector3> nav_vertices; + + for (int i = 0; i < p_detail_mesh->nverts; i++) { + const float *v = &p_detail_mesh->verts[i * 3]; + nav_vertices.append(Vector3(v[0], v[1], v[2])); + } + p_nav_mesh->set_vertices(nav_vertices); + + for (int i = 0; i < p_detail_mesh->nmeshes; i++) { + const unsigned int *m = &p_detail_mesh->meshes[i * 4]; + const unsigned int bverts = m[0]; + const unsigned int btris = m[2]; + const unsigned int ntris = m[3]; + const unsigned char *tris = &p_detail_mesh->tris[btris * 4]; + for (unsigned int j = 0; j < ntris; j++) { + Vector<int> nav_indices; + nav_indices.resize(3); + nav_indices[0] = ((int)(bverts + tris[j * 4 + 0])); + nav_indices[1] = ((int)(bverts + tris[j * 4 + 1])); + nav_indices[2] = ((int)(bverts + tris[j * 4 + 2])); + p_nav_mesh->add_polygon(nav_indices); + } + } +} + +void NavigationMeshGenerator::_build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh, EditorProgress *ep, + rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh, rcPolyMeshDetail *detail_mesh, + Vector<float> &verticies, Vector<int> &indices) { + rcContext ctx; + ep->step(TTR("Setting up Configuration..."), 1); + + const float *verts = verticies.ptr(); + const int nverts = verticies.size() / 3; + const int *tris = indices.ptr(); + const int ntris = indices.size() / 3; + + float bmin[3], bmax[3]; + rcCalcBounds(verts, nverts, bmin, bmax); + + rcConfig cfg; + memset(&cfg, 0, sizeof(cfg)); + + cfg.cs = p_nav_mesh->get_cell_size(); + cfg.ch = p_nav_mesh->get_cell_height(); + cfg.walkableSlopeAngle = p_nav_mesh->get_agent_max_slope(); + cfg.walkableHeight = (int)Math::ceil(p_nav_mesh->get_agent_height() / cfg.ch); + cfg.walkableClimb = (int)Math::floor(p_nav_mesh->get_agent_max_climb() / cfg.ch); + cfg.walkableRadius = (int)Math::ceil(p_nav_mesh->get_agent_radius() / cfg.cs); + cfg.maxEdgeLen = (int)(p_nav_mesh->get_edge_max_length() / p_nav_mesh->get_cell_size()); + cfg.maxSimplificationError = p_nav_mesh->get_edge_max_error(); + cfg.minRegionArea = (int)(p_nav_mesh->get_region_min_size() * p_nav_mesh->get_region_min_size()); + cfg.mergeRegionArea = (int)(p_nav_mesh->get_region_merge_size() * p_nav_mesh->get_region_merge_size()); + cfg.maxVertsPerPoly = (int)p_nav_mesh->get_verts_per_poly(); + cfg.detailSampleDist = p_nav_mesh->get_detail_sample_distance() < 0.9f ? 0 : p_nav_mesh->get_cell_size() * p_nav_mesh->get_detail_sample_distance(); + cfg.detailSampleMaxError = p_nav_mesh->get_cell_height() * p_nav_mesh->get_detail_sample_max_error(); + + cfg.bmin[0] = bmin[0]; + cfg.bmin[1] = bmin[1]; + cfg.bmin[2] = bmin[2]; + cfg.bmax[0] = bmax[0]; + cfg.bmax[1] = bmax[1]; + cfg.bmax[2] = bmax[2]; + + ep->step(TTR("Calculating grid size..."), 2); + rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height); + + ep->step(TTR("Creating heightfield..."), 3); + hf = rcAllocHeightfield(); + + ERR_FAIL_COND(!hf); + ERR_FAIL_COND(!rcCreateHeightfield(&ctx, *hf, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch)); + + ep->step(TTR("Marking walkable triangles..."), 4); + { + Vector<unsigned char> tri_areas; + tri_areas.resize(ntris); + + ERR_FAIL_COND(tri_areas.size() == 0); + + memset(tri_areas.ptr(), 0, ntris * sizeof(unsigned char)); + rcMarkWalkableTriangles(&ctx, cfg.walkableSlopeAngle, verts, nverts, tris, ntris, tri_areas.ptr()); + + ERR_FAIL_COND(!rcRasterizeTriangles(&ctx, verts, nverts, tris, tri_areas.ptr(), ntris, *hf, cfg.walkableClimb)); + } + + if (p_nav_mesh->get_filter_low_hanging_obstacles()) + rcFilterLowHangingWalkableObstacles(&ctx, cfg.walkableClimb, *hf); + if (p_nav_mesh->get_filter_ledge_spans()) + rcFilterLedgeSpans(&ctx, cfg.walkableHeight, cfg.walkableClimb, *hf); + if (p_nav_mesh->get_filter_walkable_low_height_spans()) + rcFilterWalkableLowHeightSpans(&ctx, cfg.walkableHeight, *hf); + + ep->step(TTR("Constructing compact heightfield..."), 5); + + chf = rcAllocCompactHeightfield(); + + ERR_FAIL_COND(!chf); + ERR_FAIL_COND(!rcBuildCompactHeightfield(&ctx, cfg.walkableHeight, cfg.walkableClimb, *hf, *chf)); + + rcFreeHeightField(hf); + hf = 0; + + ep->step(TTR("Eroding walkable area..."), 6); + ERR_FAIL_COND(!rcErodeWalkableArea(&ctx, cfg.walkableRadius, *chf)); + + ep->step(TTR("Partioning..."), 7); + if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_WATERSHED) { + ERR_FAIL_COND(!rcBuildDistanceField(&ctx, *chf)); + ERR_FAIL_COND(!rcBuildRegions(&ctx, *chf, 0, cfg.minRegionArea, cfg.mergeRegionArea)); + } else if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_MONOTONE) { + ERR_FAIL_COND(!rcBuildRegionsMonotone(&ctx, *chf, 0, cfg.minRegionArea, cfg.mergeRegionArea)); + } else { + ERR_FAIL_COND(!rcBuildLayerRegions(&ctx, *chf, 0, cfg.minRegionArea)); + } + + ep->step(TTR("Creating contours..."), 8); + + cset = rcAllocContourSet(); + + ERR_FAIL_COND(!cset); + ERR_FAIL_COND(!rcBuildContours(&ctx, *chf, cfg.maxSimplificationError, cfg.maxEdgeLen, *cset)); + + ep->step(TTR("Creating polymesh..."), 9); + + poly_mesh = rcAllocPolyMesh(); + ERR_FAIL_COND(!poly_mesh); + ERR_FAIL_COND(!rcBuildPolyMesh(&ctx, *cset, cfg.maxVertsPerPoly, *poly_mesh)); + + detail_mesh = rcAllocPolyMeshDetail(); + ERR_FAIL_COND(!detail_mesh); + ERR_FAIL_COND(!rcBuildPolyMeshDetail(&ctx, *poly_mesh, *chf, cfg.detailSampleDist, cfg.detailSampleMaxError, *detail_mesh)); + + rcFreeCompactHeightfield(chf); + chf = 0; + rcFreeContourSet(cset); + cset = 0; + + ep->step(TTR("Converting to native navigation mesh..."), 10); + + _convert_detail_mesh_to_native_navigation_mesh(detail_mesh, p_nav_mesh); + + rcFreePolyMesh(poly_mesh); + poly_mesh = 0; + rcFreePolyMeshDetail(detail_mesh); + detail_mesh = 0; +} + +void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) { + + ERR_FAIL_COND(!p_nav_mesh.is_valid()); + + EditorProgress ep("bake", TTR("Navigation Mesh Generator Setup:"), 11); + ep.step(TTR("Parsing Geometry..."), 0); + + Vector<float> verticies; + Vector<int> indices; + + _parse_geometry(Object::cast_to<Spatial>(p_node)->get_global_transform().affine_inverse(), p_node, verticies, indices); + + if (verticies.size() > 0 && indices.size() > 0) { + + rcHeightfield *hf = NULL; + rcCompactHeightfield *chf = NULL; + rcContourSet *cset = NULL; + rcPolyMesh *poly_mesh = NULL; + rcPolyMeshDetail *detail_mesh = NULL; + + _build_recast_navigation_mesh(p_nav_mesh, &ep, hf, chf, cset, poly_mesh, detail_mesh, verticies, indices); + + if (hf) { + rcFreeHeightField(hf); + hf = 0; + } + if (chf) { + rcFreeCompactHeightfield(chf); + chf = 0; + } + if (cset) { + rcFreeContourSet(cset); + cset = 0; + } + if (poly_mesh) { + rcFreePolyMesh(poly_mesh); + poly_mesh = 0; + } + if (detail_mesh) { + rcFreePolyMeshDetail(detail_mesh); + detail_mesh = 0; + } + } + ep.step(TTR("Done!"), 11); +} + +void NavigationMeshGenerator::clear(Ref<NavigationMesh> p_nav_mesh) { + if (p_nav_mesh.is_valid()) { + p_nav_mesh->clear_polygons(); + p_nav_mesh->set_vertices(PoolVector<Vector3>()); + } +} + +#endif //RECAST_ENABLED diff --git a/editor/plugins/navigation_mesh_generator.h b/editor/plugins/navigation_mesh_generator.h new file mode 100644 index 0000000000..0a1c497f8f --- /dev/null +++ b/editor/plugins/navigation_mesh_generator.h @@ -0,0 +1,65 @@ +/*************************************************************************/ +/* navigation_mesh_generator.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef NAVIGATION_MESH_GENERATOR_H +#define NAVIGATION_MESH_GENERATOR_H + +#ifdef RECAST_ENABLED + +#include "editor/editor_node.h" +#include "editor/editor_settings.h" + +#include "scene/3d/mesh_instance.h" + +#include "scene/3d/navigation_mesh.h" + +#include "os/thread.h" +#include "scene/resources/shape.h" + +#include <Recast.h> + +class NavigationMeshGenerator { +protected: + static void _add_vertex(const Vector3 &p_vec3, Vector<float> &p_verticies); + static void _add_mesh(const Ref<Mesh> &p_mesh, const Transform &p_xform, Vector<float> &p_verticies, Vector<int> &p_indices); + static void _parse_geometry(const Transform &p_base_inverse, Node *p_node, Vector<float> &p_verticies, Vector<int> &p_indices); + + static void _convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_nav_mesh); + static void _build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh, EditorProgress *ep, + rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh, + rcPolyMeshDetail *detail_mesh, Vector<float> &verticies, Vector<int> &indices); + +public: + static void bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node); + static void clear(Ref<NavigationMesh> p_nav_mesh); +}; + +#endif // RECAST_ENABLED + +#endif // NAVIGATION_MESH_GENERATOR_H diff --git a/editor/plugins/path_2d_editor_plugin.cpp b/editor/plugins/path_2d_editor_plugin.cpp index adc8d4f091..1160e90384 100644 --- a/editor/plugins/path_2d_editor_plugin.cpp +++ b/editor/plugins/path_2d_editor_plugin.cpp @@ -89,9 +89,9 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { for (int i = 0; i < curve->get_point_count(); i++) { - real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_pos(i))); - real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_pos(i) + curve->get_point_out(i))); - real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_pos(i) + curve->get_point_in(i))); + real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i))); + real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i))); + real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i))); // Check for point movement start (for point + in/out controls). if (mb->get_button_index() == BUTTON_LEFT) { @@ -100,7 +100,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { action = ACTION_MOVING_POINT; action_point = i; - moving_from = curve->get_point_pos(i); + moving_from = curve->get_point_position(i); moving_screen_from = gpoint; return true; } else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) { @@ -129,7 +129,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { undo_redo->create_action(TTR("Remove Point from Curve")); undo_redo->add_do_method(curve.ptr(), "remove_point", i); - undo_redo->add_undo_method(curve.ptr(), "add_point", curve->get_point_pos(i), curve->get_point_in(i), curve->get_point_out(i), i); + undo_redo->add_undo_method(curve.ptr(), "add_point", curve->get_point_position(i), curve->get_point_in(i), curve->get_point_out(i), i); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->commit_action(); @@ -171,7 +171,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { action = ACTION_MOVING_POINT; action_point = curve->get_point_count() - 1; - moving_from = curve->get_point_pos(action_point); + moving_from = curve->get_point_position(action_point); moving_screen_from = gpoint; canvas_item_editor->get_viewport_control()->update(); @@ -194,8 +194,8 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { case ACTION_MOVING_POINT: { undo_redo->create_action(TTR("Move Point in Curve")); - undo_redo->add_do_method(curve.ptr(), "set_point_pos", action_point, cpoint); - undo_redo->add_undo_method(curve.ptr(), "set_point_pos", action_point, moving_from); + undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint); + undo_redo->add_undo_method(curve.ptr(), "set_point_position", action_point, moving_from); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update"); undo_redo->commit_action(); @@ -255,7 +255,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { break; case ACTION_MOVING_POINT: { - curve->set_point_pos(action_point, cpoint); + curve->set_point_position(action_point, cpoint); } break; case ACTION_MOVING_IN: { @@ -296,17 +296,17 @@ void Path2DEditor::_canvas_draw() { for (int i = 0; i < len; i++) { - Vector2 point = xform.xform(curve->get_point_pos(i)); + Vector2 point = xform.xform(curve->get_point_position(i)); vpc->draw_texture_rect(handle, Rect2(point - handle_size * 0.5, handle_size), false, Color(1, 1, 1, 1)); if (i < len - 1) { - Vector2 pointout = xform.xform(curve->get_point_pos(i) + curve->get_point_out(i)); + Vector2 pointout = xform.xform(curve->get_point_position(i) + curve->get_point_out(i)); vpc->draw_line(point, pointout, Color(0.5, 0.5, 1.0, 0.8), 1.0); vpc->draw_texture_rect(handle, Rect2(pointout - handle_size * 0.5, handle_size), false, Color(1, 0.5, 1, 0.3)); } if (i > 0) { - Vector2 pointin = xform.xform(curve->get_point_pos(i) + curve->get_point_in(i)); + Vector2 pointin = xform.xform(curve->get_point_position(i) + curve->get_point_in(i)); vpc->draw_line(point, pointin, Color(0.5, 0.5, 1.0, 0.8), 1.0); vpc->draw_texture_rect(handle, Rect2(pointin - handle_size * 0.5, handle_size), false, Color(1, 0.5, 1, 0.3)); } @@ -389,8 +389,8 @@ void Path2DEditor::_mode_selected(int p_mode) { if (node->get_curve()->get_point_count() < 3) return; - Vector2 begin = node->get_curve()->get_point_pos(0); - Vector2 end = node->get_curve()->get_point_pos(node->get_curve()->get_point_count() - 1); + Vector2 begin = node->get_curve()->get_point_position(0); + Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1); if (begin.distance_to(end) < CMP_EPSILON) return; diff --git a/editor/plugins/path_editor_plugin.cpp b/editor/plugins/path_editor_plugin.cpp index d0f2b19ed3..fa97c96614 100644 --- a/editor/plugins/path_editor_plugin.cpp +++ b/editor/plugins/path_editor_plugin.cpp @@ -64,7 +64,7 @@ Variant PathSpatialGizmo::get_handle_value(int p_idx) const { if (p_idx < c->get_point_count()) { - original = c->get_point_pos(p_idx); + original = c->get_point_position(p_idx); return original; } @@ -79,7 +79,7 @@ Variant PathSpatialGizmo::get_handle_value(int p_idx) const { else ofs = c->get_point_out(idx); - original = ofs + c->get_point_pos(idx); + original = ofs + c->get_point_position(idx); return ofs; } @@ -108,7 +108,7 @@ void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_p } Vector3 local = gi.xform(inters); - c->set_point_pos(p_idx, local); + c->set_point_position(p_idx, local); } return; @@ -119,7 +119,7 @@ void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_p int idx = p_idx / 2; int t = p_idx % 2; - Vector3 base = c->get_point_pos(idx); + Vector3 base = c->get_point_position(idx); Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2)); @@ -148,12 +148,12 @@ void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p if (p_cancel) { - c->set_point_pos(p_idx, p_restore); + c->set_point_position(p_idx, p_restore); return; } - ur->create_action(TTR("Set Curve Point Pos")); - ur->add_do_method(c.ptr(), "set_point_pos", p_idx, c->get_point_pos(p_idx)); - ur->add_undo_method(c.ptr(), "set_point_pos", p_idx, p_restore); + ur->create_action(TTR("Set Curve Point Position")); + ur->add_do_method(c.ptr(), "set_point_position", p_idx, c->get_point_position(p_idx)); + ur->add_undo_method(c.ptr(), "set_point_position", p_idx, p_restore); ur->commit_action(); return; @@ -178,7 +178,7 @@ void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p c->set_point_in(p_idx, p_restore); return; } - ur->create_action(TTR("Set Curve In Pos")); + ur->create_action(TTR("Set Curve In Position")); ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx)); ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore); ur->commit_action(); @@ -189,7 +189,7 @@ void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p c->set_point_out(idx, p_restore); return; } - ur->create_action(TTR("Set Curve Out Pos")); + ur->create_action(TTR("Set Curve Out Position")); ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx)); ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore); ur->commit_action(); @@ -234,7 +234,7 @@ void PathSpatialGizmo::redraw() { for (int i = 0; i < c->get_point_count(); i++) { - Vector3 p = c->get_point_pos(i); + Vector3 p = c->get_point_position(i); handles.push_back(p); if (i > 0) { v3p.push_back(p); @@ -307,16 +307,16 @@ bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp if (rc >= 2) { PoolVector<Vector3>::Read r = v3a.read(); - if (p_camera->unproject_position(gt.xform(c->get_point_pos(0))).distance_to(mbpos) < click_dist) + if (p_camera->unproject_position(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) return false; //nope, existing for (int i = 0; i < c->get_point_count() - 1; i++) { //find the offset and point index of the place to break up int j = idx; - if (p_camera->unproject_position(gt.xform(c->get_point_pos(i + 1))).distance_to(mbpos) < click_dist) + if (p_camera->unproject_position(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) return false; //nope, existing - while (j < rc && c->get_point_pos(i + 1) != r[j]) { + while (j < rc && c->get_point_position(i + 1) != r[j]) { Vector3 from = r[j]; Vector3 to = r[j + 1]; @@ -371,7 +371,7 @@ bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp if (c->get_point_count() == 0) org = path->get_transform().get_origin(); else - org = gt.xform(c->get_point_pos(c->get_point_count() - 1)); + org = gt.xform(c->get_point_position(c->get_point_count() - 1)); Plane p(org, p_camera->get_transform().basis.get_axis(2)); Vector3 ray_from = p_camera->project_ray_origin(mbpos); Vector3 ray_dir = p_camera->project_ray_normal(mbpos); @@ -392,9 +392,9 @@ bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp } else if (mb->is_pressed() && ((mb->get_button_index() == BUTTON_LEFT && curve_del->is_pressed()) || (mb->get_button_index() == BUTTON_RIGHT && curve_edit->is_pressed()))) { for (int i = 0; i < c->get_point_count(); i++) { - real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_pos(i))).distance_to(mbpos); - real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_pos(i) + c->get_point_out(i))).distance_to(mbpos); - real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_pos(i) + c->get_point_in(i))).distance_to(mbpos); + real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_position(i))).distance_to(mbpos); + real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos); + real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_in(i))).distance_to(mbpos); // Find the offset and point index of the place to break up. // Also check for the control points. @@ -403,7 +403,7 @@ bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp UndoRedo *ur = editor->get_undo_redo(); ur->create_action(TTR("Remove Path Point")); ur->add_do_method(c.ptr(), "remove_point", i); - ur->add_undo_method(c.ptr(), "add_point", c->get_point_pos(i), c->get_point_in(i), c->get_point_out(i), i); + ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i); ur->commit_action(); return true; } else if (dist_to_p_out < click_dist) { @@ -496,7 +496,7 @@ void PathEditorPlugin::_close_curve() { return; if (c->get_point_count() < 2) return; - c->add_point(c->get_point_pos(0), c->get_point_in(0), c->get_point_out(0)); + c->add_point(c->get_point_position(0), c->get_point_in(0), c->get_point_out(0)); } void PathEditorPlugin::_notification(int p_what) { diff --git a/editor/plugins/polygon_2d_editor_plugin.cpp b/editor/plugins/polygon_2d_editor_plugin.cpp index f7008298f0..8c4e1b8f27 100644 --- a/editor/plugins/polygon_2d_editor_plugin.cpp +++ b/editor/plugins/polygon_2d_editor_plugin.cpp @@ -52,7 +52,7 @@ void Polygon2DEditor::_notification(int p_what) { uv_button[UV_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons")); b_snap_grid->set_icon(get_icon("Grid", "EditorIcons")); - b_snap_enable->set_icon(get_icon("Snap", "EditorIcons")); + b_snap_enable->set_icon(get_icon("SnapGrid", "EditorIcons")); uv_icon_zoom->set_texture(get_icon("Zoom", "EditorIcons")); get_tree()->connect("node_removed", this, "_node_removed"); @@ -395,7 +395,7 @@ bool Polygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) { if (mm.is_valid()) { - if (edited_point != -1 && (wip_active || mm->get_button_mask() & BUTTON_MASK_LEFT)) { + if (edited_point != -1 && (wip_active || (mm->get_button_mask() & BUTTON_MASK_LEFT))) { Vector2 gpoint = mm->get_position(); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); @@ -554,7 +554,7 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) { if (mm.is_valid()) { - if (mm->get_button_mask() & BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { + if ((mm->get_button_mask() & BUTTON_MASK_MIDDLE) || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { Vector2 drag(mm->get_relative().x, mm->get_relative().y); uv_hscroll->set_value(uv_hscroll->get_value() - drag.x); diff --git a/editor/plugins/resource_preloader_editor_plugin.cpp b/editor/plugins/resource_preloader_editor_plugin.cpp index e157ddbf90..da4a3f84d6 100644 --- a/editor/plugins/resource_preloader_editor_plugin.cpp +++ b/editor/plugins/resource_preloader_editor_plugin.cpp @@ -254,7 +254,7 @@ void ResourcePreloaderEditor::edit(ResourcePreloader *p_preloader) { Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { - TreeItem *ti = tree->get_item_at_pos(p_point); + TreeItem *ti = tree->get_item_at_position(p_point); if (!ti) return Variant(); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index 04be1ba4ab..9af5885a17 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -421,8 +421,10 @@ void ScriptEditor::_go_to_tab(int p_idx) { _update_history_arrows(); _update_script_colors(); _update_members_overview(); + _update_help_overview(); _update_selected_editor_menu(); _update_members_overview_visibility(); + _update_help_overview_visibility(); } void ScriptEditor::_add_recent_script(String p_path) { @@ -555,6 +557,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save) { _update_script_names(); _update_members_overview_visibility(); + _update_help_overview_visibility(); _save_layout(); } @@ -1110,6 +1113,7 @@ void ScriptEditor::_notification(int p_what) { editor->connect("resource_saved", this, "_res_saved_callback"); script_list->connect("item_selected", this, "_script_selected"); members_overview->connect("item_selected", this, "_members_overview_selected"); + help_overview->connect("item_selected", this, "_help_overview_selected"); script_split->connect("dragged", this, "_script_split_dragged"); autosave_timer->connect("timeout", this, "_autosave_scripts"); { @@ -1278,6 +1282,15 @@ void ScriptEditor::_members_overview_selected(int p_idx) { se->ensure_focus(); } +void ScriptEditor::_help_overview_selected(int p_idx) { + Node *current = tab_container->get_child(tab_container->get_current_tab()); + EditorHelp *se = Object::cast_to<EditorHelp>(current); + if (!se) { + return; + } + se->scroll_to_section(help_overview->get_item_metadata(p_idx)); +} + void ScriptEditor::_script_selected(int p_idx) { grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing @@ -1387,14 +1400,58 @@ void ScriptEditor::_update_members_overview() { } } +void ScriptEditor::_update_help_overview_visibility() { + + int selected = tab_container->get_current_tab(); + if (selected < 0 || selected >= tab_container->get_child_count()) + return; + + Node *current = tab_container->get_child(tab_container->get_current_tab()); + EditorHelp *se = Object::cast_to<EditorHelp>(current); + if (!se) { + help_overview->set_visible(false); + return; + } + + if (help_overview_enabled) { + help_overview->set_visible(true); + } else { + help_overview->set_visible(false); + } +} + +void ScriptEditor::_update_help_overview() { + + int selected = tab_container->get_current_tab(); + if (selected < 0 || selected >= tab_container->get_child_count()) + return; + + Node *current = tab_container->get_child(tab_container->get_current_tab()); + EditorHelp *se = Object::cast_to<EditorHelp>(current); + if (!se) { + return; + } + + help_overview->clear(); + + Vector<Pair<String, int> > sections = se->get_sections(); + for (int i = 0; i < sections.size(); i++) { + help_overview->add_item(sections[i].first); + help_overview->set_item_metadata(i, sections[i].second); + } +} + +void _help_overview_selected(int p_idx) { +} + void ScriptEditor::_update_script_colors() { bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_enabled"); bool highlight_current = EditorSettings::get_singleton()->get("text_editor/open_scripts/highlight_current_script"); int hist_size = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_history_size"); - Color hot_color = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_hot_color"); - Color cold_color = EditorSettings::get_singleton()->get("text_editor/open_scripts/script_temperature_cold_color"); + Color hot_color = get_color("accent_color", "Editor"); + Color cold_color = get_color("font_color", "Editor"); for (int i = 0; i < script_list->get_item_count(); i++) { @@ -1531,6 +1588,7 @@ void ScriptEditor::_update_script_names() { } _update_members_overview(); + _update_help_overview(); _update_script_colors(); } @@ -1624,9 +1682,10 @@ bool ScriptEditor::edit(const Ref<Script> &p_script, int p_line, int p_col, bool break; } ERR_FAIL_COND_V(!se, false); - tab_container->add_child(se); + // load script before adding as child else editor will crash at theme loading se->set_edited_script(p_script); + tab_container->add_child(se); se->set_tooltip_request_func("_get_debug_tooltip", this); if (se->get_edit_menu()) { se->get_edit_menu()->hide(); @@ -1785,7 +1844,9 @@ void ScriptEditor::_editor_settings_changed() { use_space_indentation = EditorSettings::get_singleton()->get("text_editor/indent/type"); members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/open_scripts/show_members_overview"); + help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index"); _update_members_overview_visibility(); + _update_help_overview_visibility(); float autosave_time = EditorSettings::get_singleton()->get("text_editor/files/autosave_interval_secs"); if (autosave_time > 0) { @@ -2164,6 +2225,7 @@ void ScriptEditor::_bind_methods() { ClassDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names); ClassDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed); ClassDB::bind_method("_members_overview_selected", &ScriptEditor::_members_overview_selected); + ClassDB::bind_method("_help_overview_selected", &ScriptEditor::_help_overview_selected); ClassDB::bind_method("_script_selected", &ScriptEditor::_script_selected); ClassDB::bind_method("_script_created", &ScriptEditor::_script_created); ClassDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged); @@ -2193,6 +2255,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { pending_auto_reload = false; auto_reload_running_scripts = false; members_overview_enabled = true; + help_overview_enabled = true; editor = p_editor; VBoxContainer *main_container = memnew(VBoxContainer); @@ -2221,6 +2284,11 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { members_overview->set_custom_minimum_size(Size2(0, 100)); //need to give a bit of limit to avoid it from disappearing members_overview->set_v_size_flags(SIZE_EXPAND_FILL); + help_overview = memnew(ItemList); + list_split->add_child(help_overview); + help_overview->set_custom_minimum_size(Size2(0, 100)); //need to give a bit of limit to avoid it from disappearing + help_overview->set_v_size_flags(SIZE_EXPAND_FILL); + tab_container = memnew(TabContainer); tab_container->set_tabs_visible(false); script_split->add_child(tab_container); @@ -2416,6 +2484,9 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) { use_space_indentation = false; ScriptServer::edit_request_func = _open_script_request; + + add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles")); + tab_container->add_style_override("panel", editor->get_gui_base()->get_stylebox("ScriptEditor", "EditorStyles")); } ScriptEditor::~ScriptEditor() { @@ -2517,8 +2588,6 @@ ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) { EDITOR_DEF("text_editor/open_scripts/script_temperature_enabled", true); EDITOR_DEF("text_editor/open_scripts/highlight_current_script", true); EDITOR_DEF("text_editor/open_scripts/script_temperature_history_size", 15); - EDITOR_DEF("text_editor/open_scripts/script_temperature_hot_color", Color::html("ed5e5e")); - EDITOR_DEF("text_editor/open_scripts/script_temperature_cold_color", Color(1, 1, 1, 0.3)); EDITOR_DEF("text_editor/open_scripts/current_script_background_color", Color(1, 1, 1, 0.5)); EDITOR_DEF("text_editor/open_scripts/group_help_pages", true); EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/open_scripts/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path")); diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index d2677c6a4a..03fc4da7ce 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -187,6 +187,8 @@ class ScriptEditor : public PanelContainer { HSplitContainer *script_split; ItemList *members_overview; bool members_overview_enabled; + ItemList *help_overview; + bool help_overview_enabled; VSplitContainer *list_split; TabContainer *tab_container; EditorFileDialog *file_dialog; @@ -294,6 +296,10 @@ class ScriptEditor : public PanelContainer { void _members_overview_selected(int p_idx); void _script_selected(int p_idx); + void _update_help_overview_visibility(); + void _update_help_overview(); + void _help_overview_selected(int p_idx); + void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used); void _tree_changed(); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index fae57eb5d2..d3c75d1c42 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -75,35 +75,98 @@ void ScriptTextEditor::_load_theme_settings() { text_edit->clear_colors(); - /* keyword color */ - - text_edit->add_color_override("background_color", EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0))); - text_edit->add_color_override("completion_background_color", EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0))); - text_edit->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244"))); - text_edit->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf"))); - text_edit->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff"))); - text_edit->add_color_override("completion_font_color", EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa"))); - text_edit->add_color_override("font_color", EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0))); - text_edit->add_color_override("line_number_color", EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0))); - text_edit->add_color_override("caret_color", EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0))); - text_edit->add_color_override("caret_background_color", EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0))); - text_edit->add_color_override("font_selected_color", EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1))); - text_edit->add_color_override("selection_color", EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1))); - text_edit->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2))); - text_edit->add_color_override("current_line_color", EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15))); - text_edit->add_color_override("line_length_guideline_color", EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0))); - text_edit->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15))); - text_edit->add_color_override("number_color", EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2))); - text_edit->add_color_override("function_color", EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8))); - text_edit->add_color_override("member_variable_color", EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3))); - text_edit->add_color_override("mark_color", EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4))); - text_edit->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2))); - text_edit->add_color_override("search_result_color", EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1))); - text_edit->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1))); - text_edit->add_color_override("symbol_color", EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff))); - text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 4)); + Color background_color = EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0)); + Color completion_background_color = EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0)); + Color completion_selected_color = EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244")); + Color completion_existing_color = EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf")); + Color completion_scroll_color = EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff")); + Color completion_font_color = EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa")); + Color text_color = EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0)); + Color line_number_color = EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0)); + Color caret_color = EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0)); + Color caret_background_color = EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0)); + Color text_selected_color = EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1)); + Color selection_color = EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1)); + Color brace_mismatch_color = EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2)); + Color current_line_color = EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15)); + Color line_length_guideline_color = EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0)); + Color word_highlighted_color = EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)); + Color number_color = EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2)); + Color function_color = EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8)); + Color member_variable_color = EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3)); + Color mark_color = EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4)); + Color breakpoint_color = EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)); + Color search_result_color = EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1)); + Color search_result_border_color = EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)); + Color symbol_color = EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff)); Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2)); + Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0)); + Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4)); + Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff)); + Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff)); + + // Adapt + if (EditorSettings::get_singleton()->get("text_editor/theme/color_theme") == "Adaptive") { + Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme(); + + symbol_color = tm->get_color("text_editor/theme/symbol_color", "Editor"); + keyword_color = tm->get_color("text_editor/theme/keyword_color", "Editor"); + basetype_color = tm->get_color("text_editor/theme/basetype_color", "Editor"); + type_color = tm->get_color("text_editor/theme/type_color", "Editor"); + comment_color = tm->get_color("text_editor/theme/comment_color", "Editor"); + string_color = tm->get_color("text_editor/theme/string_color", "Editor"); + background_color = tm->get_color("text_editor/theme/background_color", "Editor"); + completion_background_color = tm->get_color("text_editor/theme/completion_background_color", "Editor"); + completion_selected_color = tm->get_color("text_editor/theme/completion_selected_color", "Editor"); + completion_existing_color = tm->get_color("text_editor/theme/completion_existing_color", "Editor"); + completion_scroll_color = tm->get_color("text_editor/theme/completion_scroll_color", "Editor"); + completion_font_color = tm->get_color("text_editor/theme/completion_font_color", "Editor"); + text_color = tm->get_color("text_editor/theme/text_color", "Editor"); + line_number_color = tm->get_color("text_editor/theme/line_number_color", "Editor"); + caret_color = tm->get_color("text_editor/theme/caret_color", "Editor"); + caret_background_color = tm->get_color("text_editor/theme/caret_background_color", "Editor"); + text_selected_color = tm->get_color("text_editor/theme/text_selected_color", "Editor"); + selection_color = tm->get_color("text_editor/theme/selection_color", "Editor"); + brace_mismatch_color = tm->get_color("text_editor/theme/brace_mismatch_color", "Editor"); + current_line_color = tm->get_color("text_editor/theme/current_line_color", "Editor"); + line_length_guideline_color = tm->get_color("text_editor/theme/line_length_guideline_color", "Editor"); + word_highlighted_color = tm->get_color("text_editor/theme/word_highlighted_color", "Editor"); + number_color = tm->get_color("text_editor/theme/number_color", "Editor"); + function_color = tm->get_color("text_editor/theme/function_color", "Editor"); + member_variable_color = tm->get_color("text_editor/theme/member_variable_color", "Editor"); + mark_color = tm->get_color("text_editor/theme/mark_color", "Editor"); + breakpoint_color = tm->get_color("text_editor/theme/breakpoint_color", "Editor"); + search_result_color = tm->get_color("text_editor/theme/search_result_color", "Editor"); + search_result_border_color = tm->get_color("text_editor/theme/search_result_border_color", "Editor"); + } + + text_edit->add_color_override("background_color", background_color); + text_edit->add_color_override("completion_background_color", completion_background_color); + text_edit->add_color_override("completion_selected_color", completion_selected_color); + text_edit->add_color_override("completion_existing_color", completion_existing_color); + text_edit->add_color_override("completion_scroll_color", completion_scroll_color); + text_edit->add_color_override("completion_font_color", completion_font_color); + text_edit->add_color_override("font_color", text_color); + text_edit->add_color_override("line_number_color", line_number_color); + text_edit->add_color_override("caret_color", caret_color); + text_edit->add_color_override("caret_background_color", caret_background_color); + text_edit->add_color_override("font_selected_color", text_selected_color); + text_edit->add_color_override("selection_color", selection_color); + text_edit->add_color_override("brace_mismatch_color", brace_mismatch_color); + text_edit->add_color_override("current_line_color", current_line_color); + text_edit->add_color_override("line_length_guideline_color", line_length_guideline_color); + text_edit->add_color_override("word_highlighted_color", word_highlighted_color); + text_edit->add_color_override("number_color", number_color); + text_edit->add_color_override("function_color", function_color); + text_edit->add_color_override("member_variable_color", member_variable_color); + text_edit->add_color_override("mark_color", mark_color); + text_edit->add_color_override("breakpoint_color", breakpoint_color); + text_edit->add_color_override("search_result_color", search_result_color); + text_edit->add_color_override("search_result_border_color", search_result_border_color); + text_edit->add_color_override("symbol_color", symbol_color); + + text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 4)); List<String> keywords; script->get_language()->get_reserved_words(&keywords); @@ -113,8 +176,6 @@ void ScriptTextEditor::_load_theme_settings() { } //colorize core types - Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0)); - text_edit->add_keyword_color("String", basetype_color); text_edit->add_keyword_color("Vector2", basetype_color); text_edit->add_keyword_color("Rect2", basetype_color); @@ -140,8 +201,6 @@ void ScriptTextEditor::_load_theme_settings() { text_edit->add_keyword_color("PoolColorArray", basetype_color); //colorize engine types - Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4)); - List<StringName> types; ClassDB::get_class_list(&types); @@ -155,7 +214,6 @@ void ScriptTextEditor::_load_theme_settings() { } //colorize comments - Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff)); List<String> comments; script->get_language()->get_comment_delimiters(&comments); @@ -169,7 +227,6 @@ void ScriptTextEditor::_load_theme_settings() { } //colorize strings - Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff)); List<String> strings; script->get_language()->get_string_delimiters(&strings); @@ -209,6 +266,7 @@ void ScriptTextEditor::_notification(int p_what) { if (p_what == NOTIFICATION_READY) { //emit_signal("name_changed"); + _load_theme_settings(); } } @@ -248,7 +306,7 @@ Variant ScriptTextEditor::get_edit_state() { Dictionary state; - state["scroll_pos"] = code_editor->get_text_edit()->get_v_scroll(); + state["scroll_position"] = code_editor->get_text_edit()->get_v_scroll(); state["column"] = code_editor->get_text_edit()->cursor_get_column(); state["row"] = code_editor->get_text_edit()->cursor_get_line(); @@ -451,7 +509,7 @@ void ScriptTextEditor::ensure_focus() { void ScriptTextEditor::set_edit_state(const Variant &p_state) { Dictionary state = p_state; - code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]); + code_editor->get_text_edit()->set_v_scroll(state["scroll_position"]); code_editor->get_text_edit()->cursor_set_column(state["column"]); code_editor->get_text_edit()->cursor_set_line(state["row"]); code_editor->get_text_edit()->grab_focus(); @@ -492,8 +550,6 @@ void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) { script = p_script; - _load_theme_settings(); - code_editor->get_text_edit()->set_text(script->get_source_code()); code_editor->get_text_edit()->clear_undo_history(); code_editor->get_text_edit()->tag_saved_version(); @@ -624,7 +680,7 @@ void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> } String hint; Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, r_force, hint); - if (hint != "") { + if (err == OK && hint != "") { code_editor->get_text_edit()->set_code_hint(hint); } } @@ -948,13 +1004,26 @@ void ScriptTextEditor::_edit_option(int p_op) { if (tx->get_selection_to_column() == 0) end -= 1; + // Check if all lines in the selected block are commented + bool is_commented = true; + for (int i = begin; i <= end; i++) { + if (!tx->get_line(i).begins_with("#")) { + is_commented = false; + break; + } + } for (int i = begin; i <= end; i++) { String line_text = tx->get_line(i); - if (line_text.begins_with("#")) - line_text = line_text.substr(1, line_text.length()); - else - line_text = "#" + line_text; + if (line_text.strip_edges().empty()) { + line_text = "#"; + } else { + if (is_commented) { + line_text = line_text.substr(1, line_text.length()); + } else { + line_text = "#" + line_text; + } + } tx->set_line(i, line_text); } } else { @@ -1328,7 +1397,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { float alpha = color.size() > 3 ? color[3] : 1.0f; color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha)); } - color_panel->set_position(get_global_transform().xform(get_local_mouse_pos())); + color_panel->set_position(get_global_transform().xform(get_local_mouse_position())); } else { have_color = false; } @@ -1376,7 +1445,7 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) { context_menu->add_separator(); context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR); } - context_menu->set_position(get_global_transform().xform(get_local_mouse_pos())); + context_menu->set_position(get_global_transform().xform(get_local_mouse_position())); context_menu->set_size(Vector2(1, 1)); context_menu->popup(); } diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index b02016c273..b0ee1a32ca 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -60,33 +60,96 @@ void ShaderTextEditor::_load_theme_settings() { get_text_edit()->clear_colors(); - /* keyword color */ - - get_text_edit()->add_color_override("background_color", EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0))); - get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0))); - get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244"))); - get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf"))); - get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff"))); - get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa"))); - get_text_edit()->add_color_override("font_color", EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0))); - get_text_edit()->add_color_override("line_number_color", EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0))); - get_text_edit()->add_color_override("caret_color", EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0))); - get_text_edit()->add_color_override("caret_background_color", EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0))); - get_text_edit()->add_color_override("font_selected_color", EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1))); - get_text_edit()->add_color_override("selection_color", EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1))); - get_text_edit()->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2))); - get_text_edit()->add_color_override("current_line_color", EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15))); - get_text_edit()->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15))); - get_text_edit()->add_color_override("number_color", EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2))); - get_text_edit()->add_color_override("function_color", EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8))); - get_text_edit()->add_color_override("member_variable_color", EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3))); - get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4))); - get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2))); - get_text_edit()->add_color_override("search_result_color", EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1))); - get_text_edit()->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1))); - get_text_edit()->add_color_override("symbol_color", EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff))); + Color background_color = EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0)); + Color completion_background_color = EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0)); + Color completion_selected_color = EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244")); + Color completion_existing_color = EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf")); + Color completion_scroll_color = EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff")); + Color completion_font_color = EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa")); + Color text_color = EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0)); + Color line_number_color = EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0)); + Color caret_color = EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0)); + Color caret_background_color = EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0)); + Color text_selected_color = EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1)); + Color selection_color = EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1)); + Color brace_mismatch_color = EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2)); + Color current_line_color = EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15)); + Color line_length_guideline_color = EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0)); + Color word_highlighted_color = EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)); + Color number_color = EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2)); + Color function_color = EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8)); + Color member_variable_color = EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3)); + Color mark_color = EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4)); + Color breakpoint_color = EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)); + Color search_result_color = EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1)); + Color search_result_border_color = EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)); + Color symbol_color = EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff)); Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2)); + Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0)); + Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4)); + Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff)); + Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff)); + + // Adapt + if (EditorSettings::get_singleton()->get("text_editor/theme/color_theme") == "Adaptive") { + Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme(); + + symbol_color = tm->get_color("text_editor/theme/symbol_color", "Editor"); + keyword_color = tm->get_color("text_editor/theme/keyword_color", "Editor"); + basetype_color = tm->get_color("text_editor/theme/basetype_color", "Editor"); + type_color = tm->get_color("text_editor/theme/type_color", "Editor"); + comment_color = tm->get_color("text_editor/theme/comment_color", "Editor"); + string_color = tm->get_color("text_editor/theme/string_color", "Editor"); + background_color = tm->get_color("text_editor/theme/background_color", "Editor"); + completion_background_color = tm->get_color("text_editor/theme/completion_background_color", "Editor"); + completion_selected_color = tm->get_color("text_editor/theme/completion_selected_color", "Editor"); + completion_existing_color = tm->get_color("text_editor/theme/completion_existing_color", "Editor"); + completion_scroll_color = tm->get_color("text_editor/theme/completion_scroll_color", "Editor"); + completion_font_color = tm->get_color("text_editor/theme/completion_font_color", "Editor"); + text_color = tm->get_color("text_editor/theme/text_color", "Editor"); + line_number_color = tm->get_color("text_editor/theme/line_number_color", "Editor"); + caret_color = tm->get_color("text_editor/theme/caret_color", "Editor"); + caret_background_color = tm->get_color("text_editor/theme/caret_background_color", "Editor"); + text_selected_color = tm->get_color("text_editor/theme/text_selected_color", "Editor"); + selection_color = tm->get_color("text_editor/theme/selection_color", "Editor"); + brace_mismatch_color = tm->get_color("text_editor/theme/brace_mismatch_color", "Editor"); + current_line_color = tm->get_color("text_editor/theme/current_line_color", "Editor"); + line_length_guideline_color = tm->get_color("text_editor/theme/line_length_guideline_color", "Editor"); + word_highlighted_color = tm->get_color("text_editor/theme/word_highlighted_color", "Editor"); + number_color = tm->get_color("text_editor/theme/number_color", "Editor"); + function_color = tm->get_color("text_editor/theme/function_color", "Editor"); + member_variable_color = tm->get_color("text_editor/theme/member_variable_color", "Editor"); + mark_color = tm->get_color("text_editor/theme/mark_color", "Editor"); + breakpoint_color = tm->get_color("text_editor/theme/breakpoint_color", "Editor"); + search_result_color = tm->get_color("text_editor/theme/search_result_color", "Editor"); + search_result_border_color = tm->get_color("text_editor/theme/search_result_border_color", "Editor"); + } + + get_text_edit()->add_color_override("background_color", background_color); + get_text_edit()->add_color_override("completion_background_color", completion_background_color); + get_text_edit()->add_color_override("completion_selected_color", completion_selected_color); + get_text_edit()->add_color_override("completion_existing_color", completion_existing_color); + get_text_edit()->add_color_override("completion_scroll_color", completion_scroll_color); + get_text_edit()->add_color_override("completion_font_color", completion_font_color); + get_text_edit()->add_color_override("font_color", text_color); + get_text_edit()->add_color_override("line_number_color", line_number_color); + get_text_edit()->add_color_override("caret_color", caret_color); + get_text_edit()->add_color_override("caret_background_color", caret_background_color); + get_text_edit()->add_color_override("font_selected_color", text_selected_color); + get_text_edit()->add_color_override("selection_color", selection_color); + get_text_edit()->add_color_override("brace_mismatch_color", brace_mismatch_color); + get_text_edit()->add_color_override("current_line_color", current_line_color); + get_text_edit()->add_color_override("line_length_guideline_color", line_length_guideline_color); + get_text_edit()->add_color_override("word_highlighted_color", word_highlighted_color); + get_text_edit()->add_color_override("number_color", number_color); + get_text_edit()->add_color_override("function_color", function_color); + get_text_edit()->add_color_override("member_variable_color", member_variable_color); + get_text_edit()->add_color_override("mark_color", mark_color); + get_text_edit()->add_color_override("breakpoint_color", breakpoint_color); + get_text_edit()->add_color_override("search_result_color", search_result_color); + get_text_edit()->add_color_override("search_result_border_color", search_result_border_color); + get_text_edit()->add_color_override("symbol_color", symbol_color); List<String> keywords; ShaderLanguage::get_keyword_list(&keywords); @@ -115,8 +178,6 @@ void ShaderTextEditor::_load_theme_settings() { //Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0)); //colorize comments - Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff)); - get_text_edit()->add_color_region("/*", "*/", comment_color, false); get_text_edit()->add_color_region("//", "", comment_color, false); @@ -164,6 +225,8 @@ void ShaderTextEditor::_code_complete_script(const String &p_code, List<String> String calltip; Error err = sl.complete(p_code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_types(), r_options, calltip); + if (err != OK) + ERR_PRINT("Shaderlang complete failed"); if (calltip != "") { get_text_edit()->set_code_hint(calltip); @@ -403,13 +466,7 @@ void ShaderEditorPlugin::edit(Object *p_object) { bool ShaderEditorPlugin::handles(Object *p_object) const { - bool handles = true; Shader *shader = Object::cast_to<Shader>(p_object); - /* - if (Object::cast_to<ShaderGraph>(shader)) // Don't handle ShaderGraph's - handles = false; - */ - return shader != NULL; } diff --git a/editor/plugins/shader_graph_editor_plugin.cpp b/editor/plugins/shader_graph_editor_plugin.cpp index 732344cb78..f1863c3ca4 100644 --- a/editor/plugins/shader_graph_editor_plugin.cpp +++ b/editor/plugins/shader_graph_editor_plugin.cpp @@ -54,7 +54,7 @@ void GraphColorRampEdit::_gui_input(const InputEvent& p_event) { if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && p_event->is_pressed()) { update(); - int x = p_event->get_pos().x; + int x = p_event->get_position().x; int total_w = get_size().width-get_size().height-3; if (x>total_w+3) { @@ -333,7 +333,7 @@ void GraphCurveMapEdit::_gui_input(const InputEvent& p_event) { if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && p_event->is_pressed()) { update(); - Point2 p = Vector2(p_event->get_pos().x,p_event->get_pos().y)/get_size(); + Point2 p = Vector2(p_event->get_position().x,p_event->get_position().y)/get_size(); p.y=1.0-p.y; grabbed=-1; grabbing=true; @@ -384,7 +384,7 @@ void GraphCurveMapEdit::_gui_input(const InputEvent& p_event) { if (p_event.type==InputEvent::MOUSE_MOTION && grabbing && grabbed != -1) { - Point2 p = Vector2(p_event->get_pos().x,p_event->get_pos().y)/get_size(); + Point2 p = Vector2(p_event->get_position().x,p_event->get_position().y)/get_size(); p.y=1.0-p.y; p.x = CLAMP(p.x,0.0,1.0); @@ -1205,7 +1205,7 @@ void ShaderGraphView::_move_node(int p_id,const Vector2& p_to) { ERR_FAIL_COND(!node_map.has(p_id)); node_map[p_id]->set_offset(p_to); - graph->node_set_pos(type,p_id,p_to); + graph->node_set_position(type,p_id,p_to); } void ShaderGraphView::_duplicate_nodes_request() @@ -2463,7 +2463,7 @@ void ShaderGraphView::_create_node(int p_id) { gn->connect("close_request",this,"_node_removed",varray(p_id),CONNECT_DEFERRED); graph_edit->add_child(gn); node_map[p_id]=gn; - gn->set_offset(graph->node_get_pos(type,p_id)); + gn->set_offset(graph->node_get_position(type,p_id)); } @@ -2657,7 +2657,7 @@ void ShaderGraphView::add_node(int p_type, const Vector2 &location) { while(true) { bool valid=true; for(List<int>::Element *E=existing.front();E;E=E->next()) { - Vector2 pos = graph->node_get_pos(type,E->get()); + Vector2 pos = graph->node_get_position(type,E->get()); if (init_ofs==pos) { init_ofs+=Vector2(20,20); valid=false; @@ -2672,7 +2672,7 @@ void ShaderGraphView::add_node(int p_type, const Vector2 &location) { UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo(); ur->create_action(TTR("Add Shader Graph Node")); ur->add_do_method(graph.ptr(),"node_add",type,p_type,newid); - ur->add_do_method(graph.ptr(),"node_set_pos",type,newid,init_ofs); + ur->add_do_method(graph.ptr(),"node_set_position",type,newid,init_ofs); ur->add_undo_method(graph.ptr(),"node_remove",type,newid); ur->add_do_method(this,"_update_graph"); ur->add_undo_method(this,"_update_graph"); @@ -2765,7 +2765,7 @@ void ShaderGraphEditor::_add_node(int p_type) { void ShaderGraphEditor::_popup_requested(const Vector2 &p_position) { Vector2 scroll_ofs=graph_edits[tabs->get_current_tab()]->get_graph_edit()->get_scroll_ofs(); - next_location = get_local_mouse_pos() + scroll_ofs; + next_location = get_local_mouse_position() + scroll_ofs; popup->set_global_position(p_position); popup->set_size( Size2( 200, 0) ); popup->popup(); diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 4e722be44b..6b485aab7c 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -76,40 +76,46 @@ void SpatialEditorViewport::_update_camera(float p_interp_delta) { } else camera->set_perspective(get_fov(), get_znear(), get_zfar()); - Transform new_transform = to_camera_transform(cursor); - Transform old_transform = camera->get_global_transform(); - Transform transform; + //when not being manipulated, move softly + float free_orbit_inertia = EDITOR_DEF("editors/3d/free_orbit_inertia", 0.15); + float free_translation_inertia = EDITOR_DEF("editors/3d/free_translation_inertia", 0.15); + //when being manipulated, move more quickly + float manip_orbit_inertia = EDITOR_DEF("editors/3d/manipulation_orbit_inertia", 0.075); + float manip_translation_inertia = EDITOR_DEF("editors/3d/manipulation_translation_inertia", 0.075); - bool disable_interp = orthogonal || (Input::get_singleton()->get_mouse_button_mask() & (2 | 4)) || Input::get_singleton()->is_key_pressed(KEY_SHIFT) || Input::get_singleton()->is_key_pressed(KEY_ALT) || Input::get_singleton()->is_key_pressed(KEY_CONTROL); + //determine if being manipulated + bool manipulated = (Input::get_singleton()->get_mouse_button_mask() & (2 | 4)) || Input::get_singleton()->is_key_pressed(KEY_SHIFT) || Input::get_singleton()->is_key_pressed(KEY_ALT) || Input::get_singleton()->is_key_pressed(KEY_CONTROL); - if (p_interp_delta && !disable_interp) { - //interpolate - float interp_speed = 14; //maybe should be made configuration - transform = old_transform.interpolate_with(new_transform, MIN(1.0, p_interp_delta * interp_speed)); - } else { - transform = new_transform; + float orbit_inertia = MAX(0.00001, manipulated ? manip_orbit_inertia : free_orbit_inertia); + float translation_inertia = MAX(0.0001, manipulated ? manip_translation_inertia : free_translation_inertia); + + Cursor old_camera_cursor = camera_cursor; + camera_cursor = cursor; + + camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); + camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); + + camera_cursor.pos = old_camera_cursor.pos.linear_interpolate(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia))); + camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN(1.f, p_interp_delta * (1 / translation_inertia))); + + if (p_interp_delta == 0 || is_freelook_active()) { + camera_cursor = cursor; } float tolerance = 0.0001; bool equal = true; - for (int i = 0; i < 3; i++) { - if (transform.basis[i].distance_to(old_transform.basis[i]) > tolerance) { - equal = false; - break; - } - } + if (Math::abs(old_camera_cursor.x_rot - camera_cursor.x_rot) > tolerance || Math::abs(old_camera_cursor.y_rot - camera_cursor.y_rot) > tolerance) + equal = false; - if (equal && transform.origin.distance_to(old_transform.origin) > tolerance) { + if (equal && old_camera_cursor.pos.distance_squared_to(camera_cursor.pos) > tolerance * tolerance) equal = false; - } - if (equal) { - transform = new_transform; - } + if (equal && Math::abs(old_camera_cursor.distance - camera_cursor.distance) > tolerance) + equal = false; + + if (!equal || p_interp_delta == 0 || is_freelook_active()) { - if (!equal || p_interp_delta == 0) { - //print_line(transform); - camera->set_global_transform(transform); + camera->set_global_transform(to_camera_transform(camera_cursor)); update_transform_gizmo_view(); } } @@ -211,7 +217,7 @@ Transform SpatialEditorViewport::_get_camera_transform() const { return camera->get_global_transform(); } -Vector3 SpatialEditorViewport::_get_camera_pos() const { +Vector3 SpatialEditorViewport::_get_camera_position() const { return _get_camera_transform().origin; } @@ -458,7 +464,7 @@ void SpatialEditorViewport::_select_region() { Vector<Plane> frustum; - Vector3 cam_pos = _get_camera_pos(); + Vector3 cam_pos = _get_camera_position(); Set<Ref<SpatialEditorGizmo> > found_gizmos; for (int i = 0; i < 4; i++) { @@ -1540,6 +1546,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { Vector3 pos = camera_transform.xform(Vector3(0, 0, 0)); Vector3 diff = camera->get_translation() - pos; cursor.pos += diff; + freelook_target_position += diff; name = ""; _update_name(); @@ -1661,7 +1668,7 @@ void SpatialEditorViewport::scale_cursor_distance(real_t scale) { Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const { Point2i relative; - if (bool(EditorSettings::get_singleton()->get("editors/3d/warped_mouse_panning"))) { + if (bool(EDITOR_DEF("editors/3d/warped_mouse_panning", false))) { relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect()); } else { relative = p_ev_mouse_motion->get_relative(); @@ -1672,7 +1679,7 @@ Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMous void SpatialEditorViewport::_update_freelook(real_t delta) { if (!is_freelook_active()) { - freelook_velocity = Vector3(); + freelook_target_position = cursor.pos; return; } @@ -1689,60 +1696,47 @@ void SpatialEditorViewport::_update_freelook(real_t delta) { int key_speed_modifier = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_speed_modifier")->get_shortcut().ptr())->get_scancode(); Vector3 direction; - bool pressed = false; bool speed_modifier = false; const Input &input = *Input::get_singleton(); if (input.is_key_pressed(key_left)) { direction -= right; - pressed = true; } if (input.is_key_pressed(key_right)) { direction += right; - pressed = true; } if (input.is_key_pressed(key_forward)) { direction += forward; - pressed = true; } if (input.is_key_pressed(key_backwards)) { direction -= forward; - pressed = true; } if (input.is_key_pressed(key_up)) { direction += up; - pressed = true; } if (input.is_key_pressed(key_down)) { direction -= up; - pressed = true; } if (input.is_key_pressed(key_speed_modifier)) { speed_modifier = true; } - const EditorSettings &s = *EditorSettings::get_singleton(); - real_t inertia = s.get("editors/3d/freelook_inertia"); - if (inertia < 0) - inertia = 0; - - const real_t base_speed = s.get("editors/3d/freelook_base_speed"); - const real_t modifier_speed_factor = s.get("editors/3d/freelook_modifier_speed_factor"); + real_t inertia = EDITOR_DEF("editors/3d/freelook_inertia", 0.2); + inertia = MAX(0, inertia); + const real_t base_speed = EDITOR_DEF("editors/3d/freelook_base_speed", 0.5); + const real_t modifier_speed_factor = EDITOR_DEF("editors/3d/freelook_modifier_speed_factor", 5); real_t speed = base_speed * cursor.distance; if (speed_modifier) speed *= modifier_speed_factor; - Vector3 instant_velocity = direction * speed; - // Higher inertia should increase "lag" (lerp with factor between 0 and 1) - // Inertia of zero should produce instant movement (lerp with factor of 1) - // Takes reference of 60fps for units, so that inertia of 1 gives approximate lerp factor of 0.5 - real_t factor = 1.0 / (1.0 + inertia * delta * 60.f); - freelook_velocity = freelook_velocity.linear_interpolate(instant_velocity, CLAMP(factor, 0, 1)); + // Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1. - cursor.pos += freelook_velocity * delta; + freelook_target_position += direction * speed; + real_t factor = (1.0 / (inertia + 0.001)) * delta; + cursor.pos = cursor.pos.linear_interpolate(freelook_target_position, CLAMP(factor, 0, 1)); } void SpatialEditorViewport::set_message(String p_message, float p_time) { @@ -2372,9 +2366,9 @@ void SpatialEditorViewport::update_transform_gizmo_view() { void SpatialEditorViewport::set_state(const Dictionary &p_state) { - cursor.pos = p_state["pos"]; - cursor.x_rot = p_state["x_rot"]; - cursor.y_rot = p_state["y_rot"]; + cursor.pos = p_state["position"]; + cursor.x_rot = p_state["x_rotation"]; + cursor.y_rot = p_state["y_rotation"]; cursor.distance = p_state["distance"]; bool env = p_state["use_environment"]; bool orth = p_state["use_orthogonal"]; @@ -2416,9 +2410,9 @@ void SpatialEditorViewport::set_state(const Dictionary &p_state) { Dictionary SpatialEditorViewport::get_state() const { Dictionary d; - d["pos"] = cursor.pos; - d["x_rot"] = cursor.x_rot; - d["y_rot"] = cursor.y_rot; + d["position"] = cursor.pos; + d["x_rotation"] = cursor.x_rot; + d["y_rotation"] = cursor.y_rot; d["distance"] = cursor.distance; d["use_environment"] = camera->get_environment().is_valid(); d["use_orthogonal"] = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL; @@ -2528,7 +2522,6 @@ Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const found_gizmos.insert(seg); - int handle = -1; Vector3 hit_point; Vector3 hit_normal; bool inters = seg->intersect_ray(camera, p_pos, hit_point, hit_normal, NULL, false); @@ -2573,11 +2566,18 @@ void SpatialEditorViewport::_create_preview(const Vector<String> &files) const { String path = files[i]; RES res = ResourceLoader::load(path); Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); - if (scene != NULL) { - if (scene.is_valid()) { - Node *instance = scene->instance(); - if (instance) { - preview_node->add_child(instance); + Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); + if (mesh != NULL || scene != NULL) { + if (mesh != NULL) { + MeshInstance *mesh_instance = memnew(MeshInstance); + mesh_instance->set_mesh(mesh); + preview_node->add_child(mesh_instance); + } else { + if (scene.is_valid()) { + Node *instance = scene->instance(); + if (instance) { + preview_node->add_child(instance); + } } } editor->get_scene_root()->add_child(preview_node); @@ -2613,13 +2613,29 @@ bool SpatialEditorViewport::_cyclical_dependency_exists(const String &p_target_s } bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) { - Ref<PackedScene> sdata = ResourceLoader::load(path); - if (!sdata.is_valid()) { // invalid scene - return false; + RES res = ResourceLoader::load(path); + + Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); + Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); + + Node *instanced_scene = NULL; + + if (mesh != NULL || scene != NULL) { + if (mesh != NULL) { + MeshInstance *mesh_instance = memnew(MeshInstance); + mesh_instance->set_mesh(mesh); + mesh_instance->set_name(mesh->get_name()); + instanced_scene = mesh_instance; + } else { + if (!scene.is_valid()) { // invalid scene + return false; + } else { + instanced_scene = scene->instance(); + } + } } - Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE); - if (!instanced_scene) { // error on instancing + if (instanced_scene == NULL) { return false; } @@ -2668,7 +2684,8 @@ void SpatialEditorViewport::_perform_drop_data() { continue; } Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); - if (scene != NULL) { + Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); + if (mesh != NULL || scene != NULL) { bool success = _create_instance(target_node, path, drop_pos); if (!success) { error_files.push_back(path); @@ -2701,9 +2718,11 @@ bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Varian List<String> scene_extensions; ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions); + List<String> mesh_extensions; + ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions); for (int i = 0; i < files.size(); i++) { - if (scene_extensions.find(files[i].get_extension())) { + if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) { RES res = ResourceLoader::load(files[i]); if (res.is_null()) { continue; @@ -2717,6 +2736,13 @@ bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Varian continue; } memdelete(instanced_scene); + } else if (type == "Mesh" || "ArrayMesh" || "PrimitiveMesh") { + Ref<Mesh> mesh = ResourceLoader::load(files[i]); + if (!mesh.is_valid()) { + continue; + } + } else { + continue; } can_instance = true; break; @@ -2861,8 +2887,8 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed ED_SHORTCUT("spatial_editor/freelook_right", TTR("Freelook Right"), KEY_D); ED_SHORTCUT("spatial_editor/freelook_forward", TTR("Freelook Forward"), KEY_W); ED_SHORTCUT("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), KEY_S); - ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_Q); - ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_E); + ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_E); + ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_Q); ED_SHORTCUT("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), KEY_SHIFT); preview_camera = memnew(Button); @@ -3423,13 +3449,6 @@ void SpatialEditor::set_state(const Dictionary &p_state) { settings_znear->set_value(float(d["znear"])); if (d.has("fov")) settings_fov->set_value(float(d["fov"])); - - if (d.has("default_srgb")) { - bool use = d["default_srgb"]; - - //viewport_environment->set_enable_fx(Environment::FX_SRGB,use); - //view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB), use ); - } if (d.has("show_grid")) { bool use = d["show_grid"]; @@ -3721,18 +3740,50 @@ void SpatialEditor::_init_indicators() { origin_colors.push_back(Color(axis.x, axis.y, axis.z)); origin_points.push_back(axis * 4096); origin_points.push_back(axis * -4096); -#define ORIGIN_GRID_SIZE 25 +#define ORIGIN_GRID_SIZE 100 for (int j = -ORIGIN_GRID_SIZE; j <= ORIGIN_GRID_SIZE; j++) { - grid_colors[i].push_back(grid_color); - grid_colors[i].push_back(grid_color); - grid_colors[i].push_back(grid_color); - grid_colors[i].push_back(grid_color); - grid_points[i].push_back(axis_n1 * ORIGIN_GRID_SIZE + axis_n2 * j); - grid_points[i].push_back(-axis_n1 * ORIGIN_GRID_SIZE + axis_n2 * j); - grid_points[i].push_back(axis_n2 * ORIGIN_GRID_SIZE + axis_n1 * j); - grid_points[i].push_back(-axis_n2 * ORIGIN_GRID_SIZE + axis_n1 * j); + for (int k = -ORIGIN_GRID_SIZE; k <= ORIGIN_GRID_SIZE; k++) { + + Vector3 p = axis_n1 * j + axis_n2 * k; + float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / ORIGIN_GRID_SIZE)), 2); + + Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k; + float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / ORIGIN_GRID_SIZE)), 2); + + Vector3 pk = axis_n1 * j + axis_n2 * (k + 1); + float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / ORIGIN_GRID_SIZE)), 2); + + Color trans_color = grid_color; + trans_color.a *= trans; + + Color transk_color = grid_color; + transk_color.a *= transk; + + Color transj_color = grid_color; + transj_color.a *= transj; + + if (j % 10 == 0 || k % 10 == 0) { + trans_color.a *= 2; + } + if ((k + 1) % 10 == 0) { + transk_color.a *= 2; + } + if ((j + 1) % 10 == 0) { + transj_color.a *= 2; + } + + grid_points[i].push_back(p); + grid_points[i].push_back(pk); + grid_colors[i].push_back(trans_color); + grid_colors[i].push_back(transk_color); + + grid_points[i].push_back(p); + grid_points[i].push_back(pj); + grid_colors[i].push_back(trans_color); + grid_colors[i].push_back(transj_color); + } } grid[i] = VisualServer::get_singleton()->mesh_create(); diff --git a/editor/plugins/spatial_editor_plugin.h b/editor/plugins/spatial_editor_plugin.h index db5abe2b53..e0ded6e646 100644 --- a/editor/plugins/spatial_editor_plugin.h +++ b/editor/plugins/spatial_editor_plugin.h @@ -131,7 +131,7 @@ private: float gizmo_scale; bool freelook_active; - Vector3 freelook_velocity; + Vector3 freelook_target_position; PanelContainer *info; Label *info_label; @@ -157,7 +157,7 @@ private: Transform _get_camera_transform() const; int get_selected_count() const; - Vector3 _get_camera_pos() const; + Vector3 _get_camera_position() const; Vector3 _get_camera_normal() const; Vector3 _get_screen_to_space(const Vector3 &p_vector3); @@ -239,7 +239,7 @@ private: distance = 4; region_select = false; } - } cursor; + } cursor, camera_cursor; void scale_cursor_distance(real_t scale); diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp index 7b40f69082..b9cb1788f0 100644 --- a/editor/plugins/sprite_frames_editor_plugin.cpp +++ b/editor/plugins/sprite_frames_editor_plugin.cpp @@ -544,7 +544,7 @@ Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_f if (!frames->has_animation(edited_anim)) return false; - int idx = tree->get_item_at_pos(p_point, true); + int idx = tree->get_item_at_position(p_point, true); if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) return Variant(); @@ -609,7 +609,7 @@ void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da if (!d.has("type")) return; - int at_pos = tree->get_item_at_pos(p_point, true); + int at_pos = tree->get_item_at_position(p_point, true); if (String(d["type"]) == "resource" && d.has("resource")) { RES r = d["resource"]; @@ -643,7 +643,7 @@ void SpriteFramesEditor::_bind_methods() { ClassDB::bind_method(D_METHOD("_empty2_pressed"), &SpriteFramesEditor::_empty2_pressed); ClassDB::bind_method(D_METHOD("_delete_pressed"), &SpriteFramesEditor::_delete_pressed); ClassDB::bind_method(D_METHOD("_paste_pressed"), &SpriteFramesEditor::_paste_pressed); - ClassDB::bind_method(D_METHOD("_file_load_request", "files", "atpos"), &SpriteFramesEditor::_file_load_request, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("_file_load_request", "files", "at_position"), &SpriteFramesEditor::_file_load_request, DEFVAL(-1)); ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false)); ClassDB::bind_method(D_METHOD("_up_pressed"), &SpriteFramesEditor::_up_pressed); ClassDB::bind_method(D_METHOD("_down_pressed"), &SpriteFramesEditor::_down_pressed); diff --git a/editor/plugins/tile_map_editor_plugin.cpp b/editor/plugins/tile_map_editor_plugin.cpp index f943ee5f6d..b85ffd6c67 100644 --- a/editor/plugins/tile_map_editor_plugin.cpp +++ b/editor/plugins/tile_map_editor_plugin.cpp @@ -72,6 +72,14 @@ void TileMapEditor::_menu_option(int p_option) { switch (p_option) { + case OPTION_PAINTING: { + // NOTE: We do not set tool = TOOL_PAINTING as this begins painting + // immediately without pressing the left mouse button first + tool = TOOL_NONE; + + canvas_item_editor->update(); + + } break; case OPTION_BUCKET: { tool = TOOL_BUCKET; @@ -550,7 +558,6 @@ void TileMapEditor::_draw_fill_preview(int p_cell, const Point2i &p_point, bool PoolVector<Vector2> points = _bucket_fill(p_point, false, true); PoolVector<Vector2>::Read pr = points.read(); int len = points.size(); - int time_after = OS::get_singleton()->get_ticks_msec(); for (int i = 0; i < len; ++i) { _draw_cell(p_cell, pr[i], p_flip_h, p_flip_v, p_transpose, p_xform); @@ -704,7 +711,7 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { return true; } else { - + // Mousebutton was released if (tool != TOOL_NONE) { if (tool == TOOL_PAINTING) { @@ -802,6 +809,9 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { undo_redo->add_undo_method(this, "_fill_points", points, pop); undo_redo->commit_action(); + + // We want to keep the bucket-tool active + return true; } tool = TOOL_NONE; @@ -1047,9 +1057,25 @@ bool TileMapEditor::forward_gui_input(const Ref<InputEvent> &p_event) { return true; } - if (tool != TOOL_NONE || !mouse_over) + if (!mouse_over) { + // Editor shortcuts should not fire if mouse not in viewport return false; + } + if (ED_IS_SHORTCUT("tile_map_editor/paint_tile", p_event)) { + // NOTE: We do not set tool = TOOL_PAINTING as this begins painting + // immediately without pressing the left mouse button first + tool = TOOL_NONE; + canvas_item_editor->update(); + + return true; + } + if (ED_IS_SHORTCUT("tile_map_editor/bucket_fill", p_event)) { + tool = TOOL_BUCKET; + canvas_item_editor->update(); + + return true; + } if (ED_IS_SHORTCUT("tile_map_editor/erase_selection", p_event)) { _menu_option(OPTION_ERASE_SELECTION); @@ -1459,7 +1485,7 @@ TileMapEditor::TileMapEditor(EditorNode *p_editor) { ED_SHORTCUT("tile_map_editor/erase_selection", TTR("Erase selection"), KEY_DELETE); ED_SHORTCUT("tile_map_editor/find_tile", TTR("Find tile"), KEY_MASK_CMD + KEY_F); - ED_SHORTCUT("tile_map_editor/transpose", TTR("Transpose")); + ED_SHORTCUT("tile_map_editor/transpose", TTR("Transpose"), KEY_T); ED_SHORTCUT("tile_map_editor/mirror_x", TTR("Mirror X"), KEY_A); ED_SHORTCUT("tile_map_editor/mirror_y", TTR("Mirror Y"), KEY_S); @@ -1513,7 +1539,8 @@ TileMapEditor::TileMapEditor(EditorNode *p_editor) { PopupMenu *p = options->get_popup(); - p->add_item(TTR("Bucket"), OPTION_BUCKET); + p->add_shortcut(ED_SHORTCUT("tile_map_editor/paint_tile", TTR("Paint Tile"), KEY_P), OPTION_PAINTING); + p->add_shortcut(ED_SHORTCUT("tile_map_editor/bucket_fill", TTR("Bucket Fill"), KEY_G), OPTION_BUCKET); p->add_separator(); p->add_item(TTR("Pick Tile"), OPTION_PICK_TILE, KEY_CONTROL); p->add_separator(); diff --git a/editor/plugins/tile_map_editor_plugin.h b/editor/plugins/tile_map_editor_plugin.h index e863c4bf3d..de9b9e8e0d 100644 --- a/editor/plugins/tile_map_editor_plugin.h +++ b/editor/plugins/tile_map_editor_plugin.h @@ -68,7 +68,8 @@ class TileMapEditor : public VBoxContainer { OPTION_PICK_TILE, OPTION_SELECT, OPTION_DUPLICATE, - OPTION_ERASE_SELECTION + OPTION_ERASE_SELECTION, + OPTION_PAINTING, }; TileMap *node; |