Browse Source

Bunch of files missed by last commit

Marko Pintera 11 years ago
parent
commit
f86689ee63

+ 17 - 0
BansheeEditor/Include/BsEditorUtility.h

@@ -0,0 +1,17 @@
+#pragma once
+
+#include "BsEditorPrerequisites.h"
+#include "BsAABox.h"
+
+namespace BansheeEngine
+{
+	class BS_ED_EXPORT EditorUtility
+	{
+	public:
+		static AABox calculateBounds(const HSceneObject& object);
+		static AABox calculateBounds(const Vector<HSceneObject>& objects);
+
+	private:
+		static bool calculateMeshBounds(const HSceneObject& object, AABox& bounds);
+	};
+}

+ 48 - 0
MBansheeEditor/EditorUtility.cs

@@ -0,0 +1,48 @@
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public class EditorUtility
+    {
+        public static AABox CalculateBounds(SceneObject so)
+        {
+            AABox bounds;
+            Internal_CalculateBounds(so, out bounds);
+            return bounds;
+        }
+
+        public static AABox CalculateBounds(SceneObject[] objects)
+        {
+            AABox bounds;
+            Internal_CalculateBoundsArray(objects, out bounds);
+            return bounds;
+        }
+
+        public static SceneObject[] FlattenHierarchy(SceneObject so)
+        {
+            Stack<SceneObject> todo = new Stack<SceneObject>();
+            todo.Push(so);
+
+            List<SceneObject> flattenedHierarchy = new List<SceneObject>();
+            while (todo.Count > 0)
+            {
+                SceneObject cur = todo.Pop();
+                flattenedHierarchy.Add(cur);
+
+                int numChildren = cur.GetNumChildren();
+                for (int i = 0; i < numChildren; i++)
+                    todo.Push(cur.GetChild(i));
+            }
+
+            return flattenedHierarchy.ToArray();
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_CalculateBounds(SceneObject so, out AABox bounds);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_CalculateBoundsArray(SceneObject[] objects, out AABox bounds);
+    }
+}

+ 37 - 0
MBansheeEditor/Scene/DefaultHandle.cs

@@ -0,0 +1,37 @@
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public abstract class DefaultHandle : Handle
+    {
+        protected Vector3 position;
+        protected Quaternion rotation;
+
+        public Vector3 Position
+        {
+            get { return position; }
+            set { position = value; }
+        }
+
+        public Quaternion Rotation
+        {
+            get { return rotation; }
+            set { rotation = value; }
+        }
+
+        internal void DoPreInput()
+        {
+            PreInput();
+        }
+
+        internal void DoPostInput()
+        {
+            PostInput();
+        }
+
+        internal void DoDraw()
+        {
+            Draw();
+        }
+    }
+}

+ 124 - 0
MBansheeEditor/Scene/DefaultHandleManager.cs

@@ -0,0 +1,124 @@
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    internal sealed class DefaultHandleManager : Handle
+    {
+        private SceneViewTool activeHandleType = SceneViewTool.View;
+        private DefaultHandle activeHandle;
+
+        protected override void PreInput()
+        {
+            SceneObject[] selectedSceneObjects = Selection.sceneObjects;
+
+            if (activeHandleType != EditorApplication.ActiveSceneTool || selectedSceneObjects.Length == 0)
+            {
+                if (activeHandle != null)
+                {
+                    activeHandle.Destroy();
+                    activeHandle = null;
+                }
+            }
+
+            if (activeHandleType != EditorApplication.ActiveSceneTool && selectedSceneObjects.Length > 0)
+            {
+                if (activeHandle != null)
+                {
+                    activeHandle.Destroy();
+                    activeHandle = null;
+                }
+
+                switch (EditorApplication.ActiveSceneTool)
+                {
+                    case SceneViewTool.Move:
+                        activeHandle = new MoveHandle();
+                        break;
+                    case SceneViewTool.Rotate:
+                        activeHandle = new RotateHandle();
+                        break;
+                    case SceneViewTool.Scale:
+                        activeHandle = new ScaleHandle();
+                        break;
+                }
+
+                activeHandleType = EditorApplication.ActiveSceneTool;
+            }
+
+            if (activeHandle != null)
+            {
+                Quaternion rotation;
+                if (EditorApplication.HandleCoordinateMode == HandleCoordinateMode.World)
+                    rotation = Quaternion.identity;
+                else
+                    rotation = selectedSceneObjects[0].rotation; // We don't average rotation in case of multi-selection
+
+                Vector3 position;
+                if (EditorApplication.HandlePositionMode == HandlePositionMode.Pivot)
+                    position = selectedSceneObjects[0].position; // Just take pivot from the first one, no averaging
+                else
+                {
+                    List<SceneObject> flatenedHierarchy = new List<SceneObject>();
+                    foreach (var so in selectedSceneObjects)
+                    {
+                        flatenedHierarchy.AddRange(EditorUtility.FlattenHierarchy(so));
+                    }
+
+                    AABox selectionBounds = EditorUtility.CalculateBounds(flatenedHierarchy.ToArray());
+                    position = selectionBounds.Center;
+                }
+
+                activeHandle.Position = position;
+                activeHandle.Rotation = rotation;
+
+                activeHandle.DoPreInput();
+            }
+        }
+
+        protected override void PostInput()
+        {
+            if (activeHandle != null)
+            {
+                activeHandle.DoPostInput();
+
+                SceneObject[] selectedSceneObjects = Selection.sceneObjects;
+                switch (activeHandleType)
+                {
+                    case SceneViewTool.Move:
+                        {
+                            MoveHandle moveHandle = (MoveHandle)activeHandle;
+
+                            foreach (var so in selectedSceneObjects)
+                                so.position += moveHandle.Delta;
+                        }
+
+                        break;
+                    case SceneViewTool.Rotate:
+                        {
+                            RotateHandle rotateHandle = (RotateHandle)activeHandle;
+
+                            // TODO - Add delta rotation
+                            //foreach (var so in selectedSceneObjects)
+                            //    so.rotation += rotateHandle.Delta;
+                        }
+                        break;
+                    case SceneViewTool.Scale:
+                        {
+                            ScaleHandle scaleHandle = (ScaleHandle)activeHandle;
+
+                            // TODO - Add delta scale
+                            //foreach (var so in selectedSceneObjects)
+                            //    so.localScale += scaleHandle.Delta;
+                        }
+                        break;
+                }
+            }
+        }
+
+        protected override void Draw()
+        {
+            if (activeHandle != null)
+                activeHandle.DoDraw();
+        }
+    }
+}

+ 25 - 0
MBansheeEditor/Scene/Handle.cs

@@ -0,0 +1,25 @@
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public abstract class Handle
+    {
+        private List<HandleSlider> sliders = new List<HandleSlider>();
+
+        protected abstract void PreInput();
+        protected abstract void PostInput();
+        protected abstract void Draw();
+
+        internal void RegisterSlider(HandleSlider slider)
+        {
+            sliders.Add(slider);
+        }
+
+        public void Destroy()
+        {
+            foreach (var slider in sliders)
+                slider.Destroy();
+        }
+    }
+}

+ 100 - 0
MBansheeEditor/Scene/MoveHandle.cs

@@ -0,0 +1,100 @@
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public sealed class MoveHandle : DefaultHandle
+    {
+        private const float CONE_HEIGHT = 0.05f;
+        private const float CONE_RADIUS = 0.05f;
+
+        private Vector3 delta;
+
+        private HandleSliderLine xAxis;
+        private HandleSliderLine yAxis;
+        private HandleSliderLine zAxis;
+
+        public Vector3 Delta
+        {
+            get { return delta; }
+        }
+
+        public MoveHandle()
+        {
+            xAxis = new HandleSliderLine(this, Vector3.xAxis, 1.0f);
+            yAxis = new HandleSliderLine(this, Vector3.yAxis, 1.0f);
+            zAxis = new HandleSliderLine(this, Vector3.zAxis, 1.0f);
+        }
+
+        protected override void PreInput()
+        {
+            xAxis.Position = position;
+            yAxis.Position = position;
+            zAxis.Position = position;
+
+            xAxis.Rotation = rotation;
+            yAxis.Rotation = rotation;
+            zAxis.Rotation = rotation;
+        }
+
+        protected override void PostInput()
+        {
+            delta = Vector3.zero;
+            delta += xAxis.Delta * GetXDir();
+            delta += yAxis.Delta * GetYDir();
+            delta += zAxis.Delta * GetZDir();
+        }
+
+        protected override void Draw()
+        {
+            Vector3 center = position;
+            Vector3 xEnd = center + GetXDir();
+            Vector3 yEnd = center + GetYDir();
+            Vector3 zEnd = center + GetZDir();
+
+            if (xAxis.State == HandleSlider.StateType.Active)
+                HandleDrawing.SetColor(Color.white);
+            else if(xAxis.State == HandleSlider.StateType.Hover)
+                HandleDrawing.SetColor(Color.red * 0.8f);
+            else
+                HandleDrawing.SetColor(Color.red);
+            
+            HandleDrawing.DrawLine(center, xEnd);
+            HandleDrawing.DrawCone(xEnd - GetXDir()*CONE_HEIGHT, GetXDir(), CONE_HEIGHT, CONE_RADIUS);
+
+            if (yAxis.State == HandleSlider.StateType.Active)
+                HandleDrawing.SetColor(Color.white);
+            else if (yAxis.State == HandleSlider.StateType.Hover)
+                HandleDrawing.SetColor(Color.green * 0.8f);
+            else
+                HandleDrawing.SetColor(Color.green);
+
+            HandleDrawing.DrawLine(center, yEnd);
+            HandleDrawing.DrawCone(yEnd - GetYDir() * CONE_HEIGHT, GetYDir(), CONE_HEIGHT, CONE_RADIUS);
+
+            if (zAxis.State == HandleSlider.StateType.Active)
+                HandleDrawing.SetColor(Color.white);
+            else if (zAxis.State == HandleSlider.StateType.Hover)
+                HandleDrawing.SetColor(Color.blue * 0.8f);
+            else
+                HandleDrawing.SetColor(Color.blue);
+
+            HandleDrawing.DrawLine(center, zEnd);
+            HandleDrawing.DrawCone(zEnd - GetZDir() * CONE_HEIGHT, GetZDir(), CONE_HEIGHT, CONE_RADIUS);
+        }
+
+        private Vector3 GetXDir()
+        {
+             return rotation.Rotate(Vector3.xAxis);
+        }
+
+        private Vector3 GetYDir()
+        {
+            return rotation.Rotate(Vector3.yAxis);
+        }
+
+        private Vector3 GetZDir()
+        {
+            return rotation.Rotate(Vector3.zAxis);
+        }
+    }
+}

+ 27 - 0
MBansheeEditor/Scene/RotateHandle.cs

@@ -0,0 +1,27 @@
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public sealed class RotateHandle : DefaultHandle
+    {
+        public RotateHandle()
+        {
+
+        }
+
+        protected override void PreInput()
+        {
+
+        }
+
+        protected override void PostInput()
+        {
+
+        }
+
+        protected override void Draw()
+        {
+
+        }
+    }
+}

+ 47 - 0
MBansheeEngine/Math/AABox.cs

@@ -0,0 +1,47 @@
+using System.Runtime.InteropServices;
+
+namespace BansheeEngine
+{
+    [StructLayout(LayoutKind.Sequential), SerializeObject]
+	public struct AABox
+	{
+        private Vector3 minimum;
+		private Vector3 maximum;
+
+        public Vector3 Minimum
+        {
+            get { return minimum; }
+            set { minimum = value; }
+        }
+
+        public Vector3 Maximum
+        {
+            get { return maximum; }
+            set { maximum = value; }
+        }
+
+        public Vector3 Center
+        {
+            get 
+            { 		
+                return new Vector3((maximum.x + minimum.x) * 0.5f,
+			            (maximum.y + minimum.y) * 0.5f,
+			            (maximum.z + minimum.z) * 0.5f);
+            }
+        }
+
+	    public Vector3 Size
+	    {
+	        get
+	        {
+	            return maximum - minimum;
+	        }
+	    }
+
+        public AABox(Vector3 min, Vector3 max)
+        {
+            minimum = min;
+            maximum = max;
+        }
+	};
+}

+ 19 - 0
SBansheeEditor/Include/BsScriptEditorUtility.h

@@ -0,0 +1,19 @@
+#pragma once
+
+#include "BsScriptEditorPrerequisites.h"
+#include "BsScriptObject.h"
+
+namespace BansheeEngine
+{
+	class BS_SCR_BED_EXPORT ScriptEditorUtility : public ScriptObject <ScriptEditorUtility>
+	{
+	public:
+		SCRIPT_OBJ(BansheeEditorAssemblyName, "BansheeEditor", "EditorUtility")
+
+	private:
+		static void internal_CalculateBounds(MonoObject* so, AABox* bounds);
+		static void internal_CalculateBoundsArray(MonoArray* objects, AABox* bounds);
+
+		ScriptEditorUtility(MonoObject* instance);
+	};
+}