1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-07 12:30:27 +00:00

Use popup menu to add new nodes to the shader graph editor in the last clicked location.

This commit is contained in:
Mariano Javier Suligoy
2015-07-16 22:38:12 -03:00
parent f697ec2fe0
commit 5b71fc45b7
3 changed files with 38 additions and 19 deletions

View File

@@ -499,6 +499,14 @@ void GraphEdit::_input_event(const InputEvent& p_ev) {
if (p_ev.type==InputEvent::MOUSE_MOTION && (p_ev.mouse_motion.button_mask&BUTTON_MASK_MIDDLE || (p_ev.mouse_motion.button_mask&BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) { if (p_ev.type==InputEvent::MOUSE_MOTION && (p_ev.mouse_motion.button_mask&BUTTON_MASK_MIDDLE || (p_ev.mouse_motion.button_mask&BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
h_scroll->set_val( h_scroll->get_val() - p_ev.mouse_motion.relative_x ); h_scroll->set_val( h_scroll->get_val() - p_ev.mouse_motion.relative_x );
v_scroll->set_val( v_scroll->get_val() - p_ev.mouse_motion.relative_y ); v_scroll->set_val( v_scroll->get_val() - p_ev.mouse_motion.relative_y );
} else if (p_ev.type== InputEvent::MOUSE_BUTTON) {
const InputEventMouseButton &b=p_ev.mouse_button;
if (b.button_index==2 && b.pressed)
{
emit_signal("popup_request", Vector2(b.global_x, b.global_y));
}
} }
} }
@@ -555,7 +563,7 @@ void GraphEdit::_bind_methods() {
ADD_SIGNAL(MethodInfo("connection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot"))); ADD_SIGNAL(MethodInfo("connection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot")));
ADD_SIGNAL(MethodInfo("disconnection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot"))); ADD_SIGNAL(MethodInfo("disconnection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot")));
ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2,"p_position")));
} }

View File

@@ -2114,7 +2114,7 @@ void ShaderGraphView::_notification(int p_what) {
} }
} }
void ShaderGraphView::add_node(int p_type) { void ShaderGraphView::add_node(int p_type, const Vector2 &location) {
List<int> existing; List<int> existing;
graph->get_node_list(type,&existing); graph->get_node_list(type,&existing);
@@ -2127,7 +2127,7 @@ void ShaderGraphView::add_node(int p_type) {
} }
} }
Vector2 init_ofs(20,20); Vector2 init_ofs = location;
while(true) { while(true) {
bool valid=true; bool valid=true;
for(List<int>::Element *E=existing.front();E;E=E->next()) { for(List<int>::Element *E=existing.front();E;E=E->next()) {
@@ -2222,7 +2222,17 @@ void ShaderGraphEditor::_add_node(int p_type) {
ShaderGraph::ShaderType shader_type=ShaderGraph::ShaderType(tabs->get_current_tab()); ShaderGraph::ShaderType shader_type=ShaderGraph::ShaderType(tabs->get_current_tab());
graph_edits[shader_type]->add_node(p_type); graph_edits[shader_type]->add_node(p_type, next_location);
}
void ShaderGraphEditor::_popup_requested(const Vector2 &p_position)
{
next_location = get_local_mouse_pos();
popup->set_global_pos(p_position);
popup->set_size( Size2( 200, 0) );
popup->popup();
popup->call_deferred("grab_click_focus");
popup->set_invalidate_click_until_motion();
} }
@@ -2243,11 +2253,11 @@ void ShaderGraphEditor::_notification(int p_what) {
if (nn.ends_with(":")) { if (nn.ends_with(":")) {
addsep=true; addsep=true;
} }
menu->get_popup()->add_icon_item(get_icon(ic,"EditorIcons"),v,i); popup->add_icon_item(get_icon(ic,"EditorIcons"),v,i);
if (addsep) if (addsep)
menu->get_popup()->add_separator(); popup->add_separator();
} }
menu->get_popup()->connect("item_pressed",this,"_add_node"); popup->connect("item_pressed",this,"_add_node");
} }
@@ -2256,7 +2266,7 @@ void ShaderGraphEditor::_notification(int p_what) {
void ShaderGraphEditor::_bind_methods() { void ShaderGraphEditor::_bind_methods() {
ObjectTypeDB::bind_method("_add_node",&ShaderGraphEditor::_add_node); ObjectTypeDB::bind_method("_add_node",&ShaderGraphEditor::_add_node);
ObjectTypeDB::bind_method("_popup_requested",&ShaderGraphEditor::_popup_requested);
} }
@@ -2303,9 +2313,8 @@ ShaderGraphEditor::ShaderGraphEditor(bool p_2d) {
_2d=p_2d; _2d=p_2d;
HBoxContainer *hbc = memnew( HBoxContainer ); HBoxContainer *hbc = memnew( HBoxContainer );
menu = memnew( MenuButton ); popup = memnew( PopupMenu );
menu->set_text("Add Node.."); hbc->add_child(popup);
hbc->add_child(menu);
add_child(hbc); add_child(hbc);
@@ -2325,8 +2334,8 @@ ShaderGraphEditor::ShaderGraphEditor(bool p_2d) {
tabs->add_child(graph_edits[i]->get_graph_edit()); tabs->add_child(graph_edits[i]->get_graph_edit());
graph_edits[i]->get_graph_edit()->connect("connection_request",graph_edits[i],"_connection_request"); graph_edits[i]->get_graph_edit()->connect("connection_request",graph_edits[i],"_connection_request");
graph_edits[i]->get_graph_edit()->connect("disconnection_request",graph_edits[i],"_disconnection_request"); graph_edits[i]->get_graph_edit()->connect("disconnection_request",graph_edits[i],"_disconnection_request");
graph_edits[i]->get_graph_edit()->connect("popup_request",this,"_popup_requested");
graph_edits[i]->get_graph_edit()->set_right_disconnects(true); graph_edits[i]->get_graph_edit()->set_right_disconnects(true);
} }
tabs->set_current_tab(1); tabs->set_current_tab(1);

View File

@@ -175,7 +175,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
void add_node(int p_type); void add_node(int p_type, const Vector2 &location);
GraphEdit *get_graph_edit() { return graph_edit; } GraphEdit *get_graph_edit() { return graph_edit; }
void set_graph(Ref<ShaderGraph> p_graph); void set_graph(Ref<ShaderGraph> p_graph);
@@ -186,13 +186,15 @@ class ShaderGraphEditor : public VBoxContainer {
OBJ_TYPE(ShaderGraphEditor,VBoxContainer); OBJ_TYPE(ShaderGraphEditor,VBoxContainer);
MenuButton *menu; PopupMenu *popup;
TabContainer *tabs; TabContainer *tabs;
ShaderGraphView *graph_edits[ShaderGraph::SHADER_TYPE_MAX]; ShaderGraphView *graph_edits[ShaderGraph::SHADER_TYPE_MAX];
static const char* node_names[ShaderGraph::NODE_TYPE_MAX]; static const char* node_names[ShaderGraph::NODE_TYPE_MAX];
Vector2 next_location;
bool _2d; bool _2d;
void _add_node(int p_type); void _add_node(int p_type);
void _popup_requested(const Vector2 &p_position);
protected: protected:
void _notification(int p_what); void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();