From 2a97d79a64a9a1e96858221e62762b1b84d171eb Mon Sep 17 00:00:00 2001
From: Franklin Sobrinho <franklin_gs@hotmail.com>
Date: Mon, 18 Jul 2016 15:00:14 -0300
Subject: [PATCH] Fix crash when dropping scene as a sibling of the root node

---
 tools/editor/scene_tree_dock.cpp   | 13 ++++++-------
 tools/editor/scene_tree_editor.cpp | 16 ++++++++--------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/tools/editor/scene_tree_dock.cpp b/tools/editor/scene_tree_dock.cpp
index 2e7d65eadcd..e4dfcc0f626 100644
--- a/tools/editor/scene_tree_dock.cpp
+++ b/tools/editor/scene_tree_dock.cpp
@@ -1574,14 +1574,12 @@ void SceneTreeDock::_normalize_drop(Node*& to_node, int &to_pos, int p_type) {
 
 	to_pos=-1;
 
-	if (p_type==1 && to_node==EditorNode::get_singleton()->get_edited_scene()) {
-		//if at lower sibling of root node
-		to_pos=0; //just insert at begining of root node
-	} else if (p_type==-1) {
+	if (p_type==-1) {
 		//drop at above selected node
 		if (to_node==EditorNode::get_singleton()->get_edited_scene()) {
 			to_node=NULL;
-			ERR_FAIL_COND(to_node==EditorNode::get_singleton()->get_edited_scene());
+			ERR_EXPLAIN("Cannot perform drop above the root node!");
+			ERR_FAIL();
 		}
 		Node* upper_sibling=NULL;
 
@@ -1617,8 +1615,9 @@ void SceneTreeDock::_normalize_drop(Node*& to_node, int &to_pos, int p_type) {
 	} else if (p_type==1) {
 			//drop at below selected node
 			if (to_node==EditorNode::get_singleton()->get_edited_scene()) {
-				to_node=NULL;
-				ERR_FAIL_COND(to_node==EditorNode::get_singleton()->get_edited_scene());
+				//if at lower sibling of root node
+				to_pos=0; //just insert at begining of root node
+				return;
 			}
 
 
diff --git a/tools/editor/scene_tree_editor.cpp b/tools/editor/scene_tree_editor.cpp
index cc11cbc562b..73358e805dd 100644
--- a/tools/editor/scene_tree_editor.cpp
+++ b/tools/editor/scene_tree_editor.cpp
@@ -983,6 +983,14 @@ bool SceneTreeEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_da
 	if (!d.has("type"))
 		return false;
 
+	TreeItem *item = tree->get_item_at_pos(p_point);
+	if (!item)
+		return false;
+
+	int section = tree->get_drop_section_at_pos(p_point);
+	if (section<-1 || (section==-1 && !item->get_parent()))
+		return false;
+
 	if (String(d["type"])=="files") {
 
 		Vector<String> files = d["files"];
@@ -1005,15 +1013,7 @@ bool SceneTreeEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_da
 
 
 	if (String(d["type"])=="nodes") {
-		TreeItem *item = tree->get_item_at_pos(p_point);
-		if (!item)
-			return false;
-		int section = tree->get_drop_section_at_pos(p_point);
-		if (section<-1 || (section==-1 && !item->get_parent()))
-			return false;
-
 		return true;
-
 	}
 
 	return false;