Browse Source

Spawn window in editor

szamq 11 years ago
parent
commit
ddc536e36a

+ 2 - 0
Bin/Data/Scripts/Editor.as

@@ -13,6 +13,8 @@
 #include "Scripts/Editor/EditorSecondaryToolbar.as"
 #include "Scripts/Editor/EditorUI.as"
 #include "Scripts/Editor/EditorImport.as"
+#include "Scripts/Editor/EditorSpawn.as"
+
 
 String configPath;
 String configFileName;

+ 265 - 0
Bin/Data/Scripts/Editor/EditorSpawn.as

@@ -0,0 +1,265 @@
+// Urho3D spawn editor
+
+LineEdit@ randomRotationX;
+LineEdit@ randomRotationY;
+LineEdit@ randomRotationZ;
+LineEdit@ randomScaleMinEdit;
+LineEdit@ randomScaleMaxEdit;
+LineEdit@ NumberSpawnedObjectsEdit;
+
+Window@ spawnWindow;
+Vector3 randomRotation=Vector3(0.f,0.f,0.f);
+float randomScaleMin=1;
+float randomScaleMax=1;
+bool useNormal=true;
+
+int numberSpawnedObjects=1;
+Array<String> spawnedObjectsNames;
+
+void CreateSpawnEditor()
+{
+    if (spawnWindow !is null)
+        return;
+
+    spawnWindow = ui.LoadLayout(cache.GetResource("XMLFile", "UI/EditorSpawnWindow.xml"));
+    ui.root.AddChild(spawnWindow);
+    spawnWindow.opacity = uiMaxOpacity;
+
+    int height = Min(ui.root.height - 60, 500);
+    spawnWindow.SetSize(300, height);
+    CenterDialog(spawnWindow);
+
+    HideSpawnEditor();
+	SubscribeToEvent(spawnWindow.GetChild("CloseButton", true), "Released", "HideSpawnEditor");
+	randomRotationX=spawnWindow.GetChild("RandomRotation.x", true);
+	randomRotationY=spawnWindow.GetChild("RandomRotation.y", true);
+	randomRotationZ=spawnWindow.GetChild("RandomRotation.z", true);
+	randomRotationX.text=String(randomRotation.x);
+	randomRotationY.text=String(randomRotation.y);
+	randomRotationZ.text=String(randomRotation.z);
+	
+	randomScaleMinEdit=spawnWindow.GetChild("RandomScaleMin", true);
+	randomScaleMaxEdit=spawnWindow.GetChild("RandomScaleMax", true);
+	randomScaleMinEdit.text=String(randomScaleMin);
+	randomScaleMaxEdit.text=String(randomScaleMax);
+	CheckBox@ useNormalToggle = spawnWindow.GetChild("UseNormal", true);
+    useNormalToggle.checked = useNormal;
+	
+	NumberSpawnedObjectsEdit=spawnWindow.GetChild("NumberSpawnedObjects", true);
+	NumberSpawnedObjectsEdit.text=String(numberSpawnedObjects);
+	
+	SubscribeToEvent(randomRotationX, "TextChanged", "EditRandomRotation");
+    SubscribeToEvent(randomRotationY, "TextChanged", "EditRandomRotation");
+    SubscribeToEvent(randomRotationZ, "TextChanged", "EditRandomRotation");
+	SubscribeToEvent(randomScaleMinEdit, "TextChanged", "EditRandomScale");
+	SubscribeToEvent(randomScaleMaxEdit, "TextChanged", "EditRandomScale");
+	SubscribeToEvent(useNormalToggle, "Toggled", "ToggleUseNormal");
+	SubscribeToEvent(NumberSpawnedObjectsEdit, "TextFinished", "UpdateNumberSpawnedObjects");
+	SubscribeToEvent(spawnWindow.GetChild("SetSpawnMode", true), "Released", "SetSpawnMode");
+	RefreshPickedObjects();
+}
+
+bool ShowSpawnEditor()
+{
+    //RefreshMaterialEditor();
+    spawnWindow.visible = true;
+    spawnWindow.BringToFront();
+    return true;
+}
+
+void HideSpawnEditor()
+{
+    spawnWindow.visible = false;
+}
+
+
+void PickSpawnObject()
+{
+    @resourcePicker = GetResourcePicker(ShortStringHash("Node"));
+    if (resourcePicker is null)
+        return;
+
+    String lastPath = resourcePicker.lastPath;
+    if (lastPath.empty)
+        lastPath = sceneResourcePath;
+    CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
+    SubscribeToEvent(uiFileSelector, "FileSelected", "PickSpawnObjectDone");
+}
+
+
+void EditRandomRotation(StringHash eventType, VariantMap& eventData)
+{
+    LineEdit@ edit = eventData["Element"].GetPtr();
+    randomRotation = Vector3(randomRotationX.text.ToFloat(), randomRotationY.text.ToFloat(), randomRotationZ.text.ToFloat());
+    if (edit.name == "RandomRotation.x")
+        edit.text = String(randomRotation.x);
+    else if (edit.name == "RandomRotation.y")
+        edit.text = String(randomRotation.y);
+    else if (edit.name == "RandomRotation.z")
+        edit.text = String(randomRotation.z);
+    UpdateHierarchyItem(editorScene);
+}
+
+void EditRandomScale(StringHash eventType, VariantMap& eventData)
+{
+    LineEdit@ edit = eventData["Element"].GetPtr();
+    randomScaleMin = randomScaleMinEdit.text.ToFloat();
+	randomScaleMax = randomScaleMaxEdit.text.ToFloat();
+	if (edit.name == "RandomScaleMin")
+        edit.text = String(randomScaleMin);
+    else if (edit.name == "RandomScaleMax")
+        edit.text = String(randomScaleMax);
+    UpdateHierarchyItem(editorScene);
+}
+
+
+
+void ToggleUseNormal(StringHash eventType, VariantMap& eventData)
+{
+    useNormal = cast<CheckBox>(eventData["Element"].GetPtr()).checked;
+}
+
+
+
+void UpdateNumberSpawnedObjects(StringHash eventType, VariantMap& eventData)
+{
+    LineEdit@ edit = eventData["Element"].GetPtr();
+	numberSpawnedObjects=edit.text.ToFloat();
+	edit.text=String(numberSpawnedObjects);
+	
+	RefreshPickedObjects();
+}
+
+void RefreshPickedObjects()
+{
+	spawnedObjectsNames.Resize(numberSpawnedObjects);
+	ListView@ list = spawnWindow.GetChild("SpawnedObjects", true);
+	list.RemoveAllItems();
+
+	for (uint i = 0; i < numberSpawnedObjects; ++i)
+	{
+		UIElement@ parent = CreateAttributeEditorParentWithSeparatedLabel(list, "Object " +(i+1), i, 0, false);
+		
+		UIElement@ container = UIElement();
+		container.SetLayout(LM_HORIZONTAL, 4, IntRect(10, 0, 4, 0));
+		container.SetFixedHeight(ATTR_HEIGHT);
+		parent.AddChild(container);
+
+		LineEdit@ nameEdit = CreateAttributeLineEdit(container, null, i, 0);
+		nameEdit.name = "TextureNameEdit" + String(i);
+
+		Button@ pickButton = CreateResourcePickerButton(container, null, i, 0, "Pick");
+		SubscribeToEvent(pickButton, "Released", "PickSpawnedObject");
+		nameEdit.text = spawnedObjectsNames[i];
+
+		SubscribeToEvent(nameEdit, "TextFinished", "EditSpawnedObjectName");
+	}
+//UpdateHierarchyItem(editorScene);
+}
+
+void EditSpawnedObjectName(StringHash eventType, VariantMap& eventData)
+{
+    LineEdit@ nameEdit = eventData["Element"].GetPtr();
+	int index = nameEdit.vars["Index"].GetUInt();
+	String resourceName = nameEdit.text;
+	XMLFile@ xml = cache.GetResource("XMLFile", resourceName);
+	if(xml !is null)
+		spawnedObjectsNames[index]=resourceName;
+	else
+		spawnedObjectsNames[index]=String("");
+		
+	RefreshPickedObjects();
+}
+
+void PickSpawnedObject(StringHash eventType, VariantMap& eventData)
+{
+    UIElement@ button = eventData["Element"].GetPtr();
+    resourcePickIndex = button.vars["Index"].GetUInt();
+	CreateFileSelector("Pick spawned object", "Pick", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
+    
+    SubscribeToEvent(uiFileSelector, "FileSelected", "PickSpawnedObjectNameDone");
+}
+
+void PickSpawnedObjectNameDone(StringHash eventType, VariantMap& eventData)
+{
+    StoreResourcePickerPath();
+    CloseFileSelector();
+
+    if (!eventData["OK"].GetBool())
+    {
+        @resourcePicker = null;
+        return;
+    }
+
+    String resourceName = GetResourceNameFromFullName(eventData["FileName"].GetString());
+	XMLFile@ xml = cache.GetResource("XMLFile", resourceName);
+	if(xml !is null)
+		spawnedObjectsNames[resourcePickIndex]=resourceName;
+	else
+		spawnedObjectsNames[resourcePickIndex]=String("");
+    @resourcePicker = null;
+	RefreshPickedObjects();
+}
+
+void SetSpawnMode(StringHash eventType, VariantMap& eventData)
+{
+	editMode=EDIT_SPAWN;
+}
+
+void PlaceObject(Vector3 spawnPosition, Vector3 normal)
+{
+	Quaternion spawnRotation;
+	if(useNormal)spawnRotation=Quaternion(Vector3(0.f,1.f,0.f),normal);
+	int number=RandomInt(0,spawnedObjectsNames.length);
+	XMLFile@ xml = cache.GetResource("XMLFile", spawnedObjectsNames[number]);
+	Node@ spawnedObject =editorScene.InstantiateXML(xml, spawnPosition, spawnRotation);
+	if(spawnedObject is null)
+	{
+		spawnedObjectsNames[number]=spawnedObjectsNames[spawnedObjectsNames.length-1];
+		--numberSpawnedObjects;
+		RefreshPickedObjects();
+		return;
+	}
+	
+	spawnedObject.scale=spawnedObject.scale*Random(randomScaleMin, randomScaleMax);
+	spawnedObject.Rotate(Quaternion(Random(-randomRotation.x,randomRotation.x),
+	Random(-randomRotation.y,randomRotation.y),Random(-randomRotation.z,randomRotation.z)),false);
+	CreateNodeAction action;
+	action.Define(spawnedObject);
+	SaveEditAction(action);
+	SetSceneModified();
+}
+
+void SpawnObject()
+{
+	if(spawnedObjectsNames.length==0) return;
+	IntRect view = activeViewport.viewport.rect;
+	IntVector2 pos = ui.cursorPosition;
+	Ray cameraRay = camera.GetScreenRay(
+		float(pos.x - view.left) / view.width,
+		float(pos.y - view.top) / view.height);
+
+	if (pickMode < PICK_RIGIDBODIES)
+	{
+		if (editorScene.octree is null)
+			return;
+		RayQueryResult result = editorScene.octree.RaycastSingle(cameraRay, RAY_TRIANGLE, camera.farClip,
+			pickModeDrawableFlags[pickMode], 0x7fffffff);
+		if (result.drawable !is null)
+			PlaceObject(result.position, result.normal);
+	}
+	else
+	{
+		if (editorScene.physicsWorld is null)
+			return;
+
+		// If we are not running the actual physics update, refresh collisions before raycasting
+		if (!runUpdate)
+			editorScene.physicsWorld.UpdateCollisions();
+
+		PhysicsRaycastResult result = editorScene.physicsWorld.RaycastSingle(cameraRay, camera.farClip);
+		if (result.body !is null)
+			PlaceObject(result.position, result.normal);
+	}
+}
+

+ 2 - 0
Bin/Data/Scripts/Editor/EditorUI.as

@@ -69,6 +69,7 @@ void CreateUI()
     CreateEditorSettingsDialog();
     CreateEditorPreferencesDialog();
     CreateMaterialEditor();
+	CreateSpawnEditor();
     CreateStatsBar();
     CreateConsole();
     CreateDebugHud();
@@ -405,6 +406,7 @@ void CreateMenuBar()
         popup.AddChild(CreateMenuItem("Hierarchy", @ShowHierarchyWindow, 'H', QUAL_CTRL));
         popup.AddChild(CreateMenuItem("Attribute inspector", @ShowAttributeInspectorWindow, 'I', QUAL_CTRL));
         popup.AddChild(CreateMenuItem("Material editor", @ShowMaterialEditor));
+		popup.AddChild(CreateMenuItem("Spawn editor", @ShowSpawnEditor));
         popup.AddChild(CreateMenuItem("Editor settings", @ShowEditorSettingsDialog));
         popup.AddChild(CreateMenuItem("Editor preferences", @ShowEditorPreferencesDialog));
         CreateChildDivider(popup);

+ 16 - 4
Bin/Data/Scripts/Editor/EditorView.as

@@ -51,7 +51,8 @@ enum EditMode
     EDIT_MOVE = 0,
     EDIT_ROTATE,
     EDIT_SCALE,
-    EDIT_SELECT
+    EDIT_SELECT,
+    EDIT_SPAWN
 }
 
 enum AxisMode
@@ -366,7 +367,8 @@ Array<String> editModeText = {
     "Move",
     "Rotate",
     "Scale",
-    "Select"
+    "Select",
+    "Spawn"
 };
 
 Array<String> axisModeText = {
@@ -1384,14 +1386,24 @@ void ViewRaycast(bool mouseClick)
     if (ui.HasModalElement())
         return;
 
+	IntVector2 pos = ui.cursorPosition;
+    UIElement@ elementAtPos = ui.GetElementAt(pos, pickMode != PICK_UI_ELEMENTS);
+	if(editMode==EDIT_SPAWN)
+	{
+		if(mouseClick && input.mouseButtonPress[MOUSEB_LEFT] && elementAtPos is null)
+			SpawnObject();
+		return;
+	}
+	
+	
+	
     // Do not raycast / change selection if hovering over the gizmo
     if (IsGizmoSelected())
         return;
 
     DebugRenderer@ debug = editorScene.debugRenderer;
 
-    IntVector2 pos = ui.cursorPosition;
-    UIElement@ elementAtPos = ui.GetElementAt(pos, pickMode != PICK_UI_ELEMENTS);
+ 
     if (pickMode == PICK_UI_ELEMENTS)
     {
         bool leftClick = mouseClick && input.mouseButtonPress[MOUSEB_LEFT];