Exemple #1
0
void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {

	Ref<InputEventMouseMotion> mm = p_ev;
	if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_MIDDLE || (mm->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
		h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
		v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
	}

	if (mm.is_valid() && dragging) {

		just_selected = true;
		// TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats
		//drag_accum+=Vector2(mm->get_relative().x,mm->get_relative().y);
		drag_accum = get_local_mouse_position() - drag_origin;
		for (int i = get_child_count() - 1; i >= 0; i--) {
			GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
			if (gn && gn->is_selected()) {

				Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
				if (is_using_snap()) {
					int snap = get_snap();
					pos = pos.snapped(Vector2(snap, snap));
				}

				gn->set_offset(pos);
			}
		}
	}

	if (mm.is_valid() && box_selecting) {
		box_selecting_to = get_local_mouse_position();

		box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
				MIN(box_selecting_from.y, box_selecting_to.y),
				ABS(box_selecting_from.x - box_selecting_to.x),
				ABS(box_selecting_from.y - box_selecting_to.y));

		for (int i = get_child_count() - 1; i >= 0; i--) {

			GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
			if (!gn)
				continue;

			Rect2 r = gn->get_rect();
			r.size *= zoom;
			bool in_box = r.intersects(box_selecting_rect);

			if (in_box)
				gn->set_selected(box_selection_mode_aditive);
			else
				gn->set_selected(previus_selected.find(gn) != NULL);
		}

		top_layer->update();
	}

	Ref<InputEventMouseButton> b = p_ev;
	if (b.is_valid()) {

		if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
			if (box_selecting) {
				box_selecting = false;
				for (int i = get_child_count() - 1; i >= 0; i--) {

					GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
					if (!gn)
						continue;

					gn->set_selected(previus_selected.find(gn) != NULL);
				}
				top_layer->update();
			} else {
				if (connecting) {
					connecting = false;
					top_layer->update();
				} else {
					emit_signal("popup_request", b->get_global_position());
				}
			}
		}

		if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
			if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
				//deselect current node
				for (int i = get_child_count() - 1; i >= 0; i--) {
					GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));

					if (gn) {
						Rect2 r = gn->get_rect();
						r.size *= zoom;
						if (r.has_point(get_local_mouse_position()))
							gn->set_selected(false);
					}
				}
			}

			if (drag_accum != Vector2()) {

				emit_signal("_begin_node_move");

				for (int i = get_child_count() - 1; i >= 0; i--) {
					GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
					if (gn && gn->is_selected())
						gn->set_drag(false);
				}

				emit_signal("_end_node_move");
			}

			dragging = false;

			top_layer->update();
			update();
			connections_layer->update();
		}

		if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {

			GraphNode *gn = NULL;

			for (int i = get_child_count() - 1; i >= 0; i--) {

				GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));

				if (gn_selected) {
					if (gn_selected->is_resizing())
						continue;

					if (gn_selected->has_point(gn_selected->get_local_mouse_position())) {
						gn = gn_selected;
						break;
					}
				}
			}

			if (gn) {

				if (_filter_input(b->get_position()))
					return;

				dragging = true;
				drag_accum = Vector2();
				drag_origin = get_local_mouse_position();
				just_selected = !gn->is_selected();
				if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
					for (int i = 0; i < get_child_count(); i++) {
						GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
						if (o_gn)
							o_gn->set_selected(o_gn == gn);
					}
				}

				gn->set_selected(true);
				for (int i = 0; i < get_child_count(); i++) {
					GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
					if (!o_gn)
						continue;
					if (o_gn->is_selected())
						o_gn->set_drag(true);
				}

			} else {
				if (_filter_input(b->get_position()))
					return;
				if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
					return;

				box_selecting = true;
				box_selecting_from = get_local_mouse_position();
				if (b->get_control()) {
					box_selection_mode_aditive = true;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
						if (!gn2 || !gn2->is_selected())
							continue;

						previus_selected.push_back(gn2);
					}
				} else if (b->get_shift()) {
					box_selection_mode_aditive = false;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
						if (!gn2 || !gn2->is_selected())
							continue;

						previus_selected.push_back(gn2);
					}
				} else {
					box_selection_mode_aditive = true;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
						if (!gn2)
							continue;

						gn2->set_selected(false);
					}
				}
			}
		}

		if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
			box_selecting = false;
			previus_selected.clear();
			top_layer->update();
		}

		if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
			//too difficult to get right
			//set_zoom(zoom*ZOOM_SCALE);
		}

		if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
			//too difficult to get right
			//set_zoom(zoom/ZOOM_SCALE);
		}
		if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
			v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
		}
		if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
			v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
		}
		if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
			h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
		}
		if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
			h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
		}
	}

	Ref<InputEventKey> k = p_ev;
	if (k.is_valid() && k->get_scancode() == KEY_D && k->is_pressed() && k->get_command()) {
		emit_signal("duplicate_nodes_request");
		accept_event();
	}

	if (k.is_valid() && k->get_scancode() == KEY_DELETE && k->is_pressed()) {
		emit_signal("delete_nodes_request");
		accept_event();
	}

	Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
	if (magnify_gesture.is_valid()) {

		set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
	}

	Ref<InputEventPanGesture> pan_gesture = p_ev;
	if (pan_gesture.is_valid()) {

		h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
		v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
	}
}
Exemple #2
0
void GraphEdit::_gui_input(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)))) {
		h_scroll->set_value(h_scroll->get_value() - p_ev.mouse_motion.relative_x);
		v_scroll->set_value(v_scroll->get_value() - p_ev.mouse_motion.relative_y);
	}

	if (p_ev.type == InputEvent::MOUSE_MOTION && dragging) {

		just_selected = true;
		// TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats
		//drag_accum+=Vector2(p_ev.mouse_motion.relative_x,p_ev.mouse_motion.relative_y);
		drag_accum = get_local_mouse_pos() - drag_origin;
		for (int i = get_child_count() - 1; i >= 0; i--) {
			GraphNode *gn = get_child(i)->cast_to<GraphNode>();
			if (gn && gn->is_selected()) {

				Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
				if (is_using_snap()) {
					int snap = get_snap();
					pos = pos.snapped(Vector2(snap, snap));
				}

				gn->set_offset(pos);
			}
		}
	}

	if (p_ev.type == InputEvent::MOUSE_MOTION && box_selecting) {
		box_selecting_to = get_local_mouse_pos();

		box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
				MIN(box_selecting_from.y, box_selecting_to.y),
				ABS(box_selecting_from.x - box_selecting_to.x),
				ABS(box_selecting_from.y - box_selecting_to.y));

		for (int i = get_child_count() - 1; i >= 0; i--) {

			GraphNode *gn = get_child(i)->cast_to<GraphNode>();
			if (!gn)
				continue;

			Rect2 r = gn->get_rect();
			r.size *= zoom;
			bool in_box = r.intersects(box_selecting_rect);

			if (in_box)
				gn->set_selected(box_selection_mode_aditive);
			else
				gn->set_selected(previus_selected.find(gn) != NULL);
		}

		top_layer->update();
	}

	if (p_ev.type == InputEvent::MOUSE_BUTTON) {

		const InputEventMouseButton &b = p_ev.mouse_button;

		if (b.button_index == BUTTON_RIGHT && b.pressed) {
			if (box_selecting) {
				box_selecting = false;
				for (int i = get_child_count() - 1; i >= 0; i--) {

					GraphNode *gn = get_child(i)->cast_to<GraphNode>();
					if (!gn)
						continue;

					gn->set_selected(previus_selected.find(gn) != NULL);
				}
				top_layer->update();
			} else {
				if (connecting) {
					connecting = false;
					top_layer->update();
				} else {
					emit_signal("popup_request", Vector2(b.global_x, b.global_y));
				}
			}
		}

		if (b.button_index == BUTTON_LEFT && !b.pressed && dragging) {
			if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
				//deselect current node
				for (int i = get_child_count() - 1; i >= 0; i--) {
					GraphNode *gn = get_child(i)->cast_to<GraphNode>();

					if (gn) {
						Rect2 r = gn->get_rect();
						r.size *= zoom;
						if (r.has_point(get_local_mouse_pos()))
							gn->set_selected(false);
					}
				}
			}

			if (drag_accum != Vector2()) {

				emit_signal("_begin_node_move");

				for (int i = get_child_count() - 1; i >= 0; i--) {
					GraphNode *gn = get_child(i)->cast_to<GraphNode>();
					if (gn && gn->is_selected())
						gn->set_drag(false);
				}

				emit_signal("_end_node_move");
			}

			dragging = false;

			top_layer->update();
			update();
			connections_layer->update();
		}

		if (b.button_index == BUTTON_LEFT && b.pressed) {

			GraphNode *gn = NULL;
			GraphNode *gn_selected = NULL;
			for (int i = get_child_count() - 1; i >= 0; i--) {

				gn_selected = get_child(i)->cast_to<GraphNode>();

				if (gn_selected) {

					if (gn_selected->is_resizing())
						continue;

					Rect2 r = gn_selected->get_rect();
					r.size *= zoom;
					if (r.has_point(get_local_mouse_pos()))
						gn = gn_selected;
					break;
				}
			}

			if (gn) {

				if (_filter_input(Vector2(b.x, b.y)))
					return;

				dragging = true;
				drag_accum = Vector2();
				drag_origin = get_local_mouse_pos();
				just_selected = !gn->is_selected();
				if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
					for (int i = 0; i < get_child_count(); i++) {
						GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
						if (o_gn)
							o_gn->set_selected(o_gn == gn);
					}
				}

				gn->set_selected(true);
				for (int i = 0; i < get_child_count(); i++) {
					GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
					if (!o_gn)
						continue;
					if (o_gn->is_selected())
						o_gn->set_drag(true);
				}

			} else {
				if (_filter_input(Vector2(b.x, b.y)))
					return;
				if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
					return;

				box_selecting = true;
				box_selecting_from = get_local_mouse_pos();
				if (b.mod.control) {
					box_selection_mode_aditive = true;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn = get_child(i)->cast_to<GraphNode>();
						if (!gn || !gn->is_selected())
							continue;

						previus_selected.push_back(gn);
					}
				} else if (b.mod.shift) {
					box_selection_mode_aditive = false;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn = get_child(i)->cast_to<GraphNode>();
						if (!gn || !gn->is_selected())
							continue;

						previus_selected.push_back(gn);
					}
				} else {
					box_selection_mode_aditive = true;
					previus_selected.clear();
					for (int i = get_child_count() - 1; i >= 0; i--) {

						GraphNode *gn = get_child(i)->cast_to<GraphNode>();
						if (!gn)
							continue;

						gn->set_selected(false);
					}
				}
			}
		}

		if (b.button_index == BUTTON_LEFT && !b.pressed && box_selecting) {
			box_selecting = false;
			previus_selected.clear();
			top_layer->update();
		}

		if (b.button_index == BUTTON_WHEEL_UP && b.pressed) {
			//too difficult to get right
			//set_zoom(zoom*ZOOM_SCALE);
		}

		if (b.button_index == BUTTON_WHEEL_DOWN && b.pressed) {
			//too difficult to get right
			//set_zoom(zoom/ZOOM_SCALE);
		}
	}

	if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_D && p_ev.key.pressed && p_ev.key.mod.command) {
		emit_signal("duplicate_nodes_request");
		accept_event();
	}

	if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_DELETE && p_ev.key.pressed) {
		emit_signal("delete_nodes_request");
		accept_event();
	}
}