VisualScriptEditor Fix in graph position calculation (do not skip zoom)

(cherry picked from commit 3336453dff)
This commit is contained in:
kleonc 2021-06-29 15:31:53 +02:00 committed by Rémi Verschelde
parent 8c640b8204
commit c476459f2b
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 50 additions and 112 deletions

View File

@ -1208,7 +1208,7 @@ void VisualScriptEditor::_create_function_dialog() {
void VisualScriptEditor::_create_function() {
String name = _validate_name((func_name_box->get_text() == "") ? "new_func" : func_name_box->get_text());
selected = name;
Vector2 ofs = _get_available_pos();
Vector2 pos = _get_available_pos();
Ref<VisualScriptFunction> func_node;
func_node.instance();
@ -1226,7 +1226,7 @@ void VisualScriptEditor::_create_function() {
undo_redo->create_action(TTR("Add Function"));
undo_redo->add_do_method(script.ptr(), "add_function", name);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, pos);
undo_redo->add_undo_method(script.ptr(), "remove_function", name);
undo_redo->add_do_method(this, "_update_members");
undo_redo->add_undo_method(this, "_update_members");
@ -1320,7 +1320,7 @@ void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_butt
String name = _validate_name("new_function");
selected = name;
Vector2 ofs = _get_available_pos();
Vector2 pos = _get_available_pos();
Ref<VisualScriptFunction> func_node;
func_node.instance();
@ -1328,7 +1328,7 @@ void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_butt
undo_redo->create_action(TTR("Add Function"));
undo_redo->add_do_method(script.ptr(), "add_function", name);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, pos);
undo_redo->add_undo_method(script.ptr(), "remove_function", name);
undo_redo->add_do_method(this, "_update_members");
undo_redo->add_undo_method(this, "_update_members");
@ -1528,16 +1528,19 @@ void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id
updating_graph = false;
}
Vector2 VisualScriptEditor::_get_available_pos(bool centered, Vector2 ofs) const {
if (centered)
ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
Vector2 VisualScriptEditor::_get_pos_in_graph(Vector2 p_point) const {
Vector2 pos = (graph->get_scroll_ofs() + p_point) / (graph->get_zoom() * EDSCALE);
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
pos = pos.snapped(Vector2(snap, snap));
}
return pos;
}
ofs /= EDSCALE;
Vector2 VisualScriptEditor::_get_available_pos(bool p_centered, Vector2 p_pos) const {
if (p_centered) {
p_pos = _get_pos_in_graph(graph->get_size() * 0.5);
}
while (true) {
bool exists = false;
@ -1549,8 +1552,8 @@ Vector2 VisualScriptEditor::_get_available_pos(bool centered, Vector2 ofs) const
script->get_node_list(curr_fn, &existing);
for (List<int>::Element *E = existing.front(); E; E = E->next()) {
Point2 pos = script->get_node_position(curr_fn, E->get());
if (pos.distance_to(ofs) < 50) {
ofs += Vector2(graph->get_snap(), graph->get_snap());
if (pos.distance_to(p_pos) < 50) {
p_pos += Vector2(graph->get_snap(), graph->get_snap());
exists = true;
break;
}
@ -1561,7 +1564,7 @@ Vector2 VisualScriptEditor::_get_available_pos(bool centered, Vector2 ofs) const
break;
}
return ofs;
return p_pos;
}
String VisualScriptEditor::_validate_name(const String &p_name) const {
@ -1982,16 +1985,9 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
return;
}
Vector2 ofs = graph->get_scroll_ofs() + p_point;
Vector2 pos = _get_pos_in_graph(p_point);
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
int new_id = _create_new_node_from_name(d["node_type"], ofs, default_func);
int new_id = _create_new_node_from_name(d["node_type"], pos, default_func);
Node *node = graph->get_node(itos(new_id));
if (node) {
@ -2007,13 +2003,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
#else
bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
#endif
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
Ref<VisualScriptNode> vnode;
if (use_set) {
@ -2032,7 +2022,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
int new_id = script->get_available_id();
undo_redo->create_action(TTR("Add Node"));
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
@ -2046,14 +2036,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
}
if (String(d["type"]) == "visual_script_function_drag") {
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
Ref<VisualScriptFunctionCall> vnode;
vnode.instance();
@ -2062,7 +2045,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
int new_id = script->get_available_id();
undo_redo->create_action(TTR("Add Node"));
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, pos);
undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
@ -2079,14 +2062,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
}
if (String(d["type"]) == "visual_script_signal_drag") {
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
Ref<VisualScriptEmitSignal> vnode;
vnode.instance();
@ -2095,7 +2071,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
int new_id = script->get_available_id();
undo_redo->create_action(TTR("Add Node"));
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
@ -2109,14 +2085,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
}
if (String(d["type"]) == "resource") {
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
Ref<VisualScriptPreload> prnode;
prnode.instance();
@ -2125,7 +2094,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
int new_id = script->get_available_id();
undo_redo->create_action(TTR("Add Preload Node"));
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
@ -2139,14 +2108,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
}
if (String(d["type"]) == "files") {
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
Array files = d["files"];
@ -2166,11 +2128,11 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
prnode.instance();
prnode->set_preload(res);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
new_ids.push_back(new_id);
new_id++;
ofs += Vector2(20, 20) * EDSCALE;
pos += Vector2(20, 20);
}
undo_redo->add_do_method(this, "_update_graph");
@ -2205,13 +2167,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
Array nodes = d["nodes"];
Vector2 ofs = graph->get_scroll_ofs() + p_point;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Vector2 pos = _get_pos_in_graph(p_point);
undo_redo->create_action(TTR("Add Node(s) From Tree"));
int base_id = script->get_available_id();
@ -2247,11 +2203,11 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
selecting_method_id = base_id;
}
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, n, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, n, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);
base_id++;
ofs += Vector2(25, 25);
pos += Vector2(25, 25);
}
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
@ -2273,14 +2229,8 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
return;
Node *node = Object::cast_to<Node>(obj);
Vector2 ofs = graph->get_scroll_ofs() + p_point;
Vector2 pos = _get_pos_in_graph(p_point);
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
#ifdef OSX_ENABLED
bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
#else
@ -2319,7 +2269,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
vnode = pget;
}
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, pos);
undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
if (!use_get) {
undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
@ -2366,7 +2316,7 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
}
vnode = pget;
}
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, pos);
undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
if (!use_get) {
undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
@ -3325,7 +3275,9 @@ void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_fro
if (!vsn.is_valid())
return;
port_action_pos = p_release_pos;
if (vsn->get_output_value_port_count() || vsn->get_output_sequence_port_count()) {
port_action_pos = p_release_pos;
}
if (p_from_slot < vsn->get_output_sequence_port_count()) {
@ -3396,14 +3348,6 @@ VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_ac
}
void VisualScriptEditor::_port_action_menu(int p_option, const StringName &func) {
Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
Set<int> vn;
switch (p_option) {
@ -3497,14 +3441,7 @@ void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<Visua
}
void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
if (graph->is_using_snap()) {
int snap = graph->get_snap();
ofs = ofs.snapped(Vector2(snap, snap));
}
ofs /= EDSCALE;
ofs /= graph->get_zoom();
Vector2 pos = _get_pos_in_graph(port_action_pos);
Set<int> vn;
@ -3542,7 +3479,7 @@ void VisualScriptEditor::_selected_connect_node(const String &p_text, const Stri
}
undo_redo->create_action(TTR("Add Node"));
undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode_new, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode_new, pos);
if (vnode_old.is_valid() && p_connecting) {
connect_seq(vnode_old, vnode_new, new_id);
connect_data(vnode_old, vnode_new, new_id);
@ -3614,7 +3551,7 @@ void VisualScriptEditor::_selected_connect_node(const String &p_text, const Stri
int new_id = script->get_available_id();
undo_redo->create_action(TTR("Add Node"));
undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", func, new_id);
undo_redo->add_do_method(this, "_update_graph", new_id);
undo_redo->add_undo_method(this, "_update_graph", new_id);
@ -3820,16 +3757,16 @@ void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, cons
func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
}
Vector2 ofs = _get_available_pos();
Vector2 pos = _get_available_pos();
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, pos);
if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
Ref<VisualScriptReturn> ret_node;
ret_node.instance();
ret_node->set_return_type(minfo.return_val.type);
ret_node->set_enable_return_value(true);
ret_node->set_name(name);
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id() + 1, ret_node, _get_available_pos(false, ofs + Vector2(500, 0)));
undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id() + 1, ret_node, _get_available_pos(false, pos + Vector2(500, 0)));
}
undo_redo->add_undo_method(script.ptr(), "remove_function", name);
@ -4376,7 +4313,7 @@ void VisualScriptEditor::_menu_option(int p_what) {
String new_fn = _validate_name("new_function");
Vector2 ofs = _get_available_pos(false, script->get_node_position(function, start_node) - Vector2(80, 150));
Vector2 pos = _get_available_pos(false, script->get_node_position(function, start_node) - Vector2(80, 150));
Ref<VisualScriptFunction> func_node;
func_node.instance();
@ -4386,7 +4323,7 @@ void VisualScriptEditor::_menu_option(int p_what) {
undo_redo->add_do_method(script.ptr(), "add_function", new_fn);
int fn_id = script->get_available_id();
undo_redo->add_do_method(script.ptr(), "add_node", new_fn, fn_id, func_node, ofs);
undo_redo->add_do_method(script.ptr(), "add_node", new_fn, fn_id, func_node, pos);
undo_redo->add_undo_method(script.ptr(), "remove_function", new_fn);
undo_redo->add_do_method(this, "_update_members");
undo_redo->add_undo_method(this, "_update_members");
@ -4433,8 +4370,8 @@ void VisualScriptEditor::_menu_option(int p_what) {
int ret_id = fn_id + (m++);
selections.insert(ret_id);
Vector2 ofsi = _get_available_pos(false, script->get_node_position(function, G->get()) + Vector2(80, -100));
undo_redo->add_do_method(script.ptr(), "add_node", new_fn, ret_id, ret_node, ofsi);
Vector2 posi = _get_available_pos(false, script->get_node_position(function, G->get()) + Vector2(80, -100));
undo_redo->add_do_method(script.ptr(), "add_node", new_fn, ret_id, ret_node, posi);
undo_redo->add_undo_method(script.ptr(), "remove_node", new_fn, ret_id);
undo_redo->add_do_method(script.ptr(), "sequence_connect", new_fn, G->get(), 0, ret_id);

View File

@ -229,7 +229,8 @@ class VisualScriptEditor : public ScriptEditorBase {
void _update_node_size(int p_id);
void _port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input);
Vector2 _get_available_pos(bool centered = true, Vector2 ofs = Vector2()) const;
Vector2 _get_pos_in_graph(Vector2 p_point) const;
Vector2 _get_available_pos(bool p_centered = true, Vector2 p_pos = Vector2()) const;
StringName _get_function_of_node(int p_id) const;
void _move_nodes_with_rescan(const StringName &p_func_from, const StringName &p_func_to, int p_id);