Bläddra i källkod

C# Time and SceneCamera

Marko Pintera 11 år sedan
förälder
incheckning
e8d30fcee1

+ 2 - 0
MBansheeEditor/MBansheeEditor.csproj

@@ -82,6 +82,8 @@
     <Compile Include="Inspector\InspectableVector4.cs" />
     <Compile Include="Inspector\Inspector.cs" />
     <Compile Include="Inspector\InspectorWindow.cs" />
+    <Compile Include="Scene\SceneCamera.cs" />
+    <Compile Include="Scene\SceneWindow.cs" />
     <Compile Include="Scene\DefaultHandle.cs" />
     <Compile Include="Scene\DefaultHandleManager.cs" />
     <Compile Include="Scene\Handles.cs" />

+ 122 - 0
MBansheeEditor/Scene/SceneCamera.cs

@@ -0,0 +1,122 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    public class SceneCamera : Component
+    {
+        public const string MoveForwardBinding = "SceneForward";
+	    public const string MoveLeftBinding = "SceneLeft";
+	    public const string MoveRightBinding = "SceneRight";
+	    public const string MoveBackBinding = "SceneBackward";
+	    public const string FastMoveBinding = "SceneFastMove";
+	    public const string RotateBinding = "SceneRotate";
+	    public const string HorizontalAxisBinding = "SceneHorizontal";
+	    public const string VerticalAxisBinding = "SceneVertical";
+
+	    private const float StartSpeed = 4.0f;
+	    private const float TopSpeed = 12.0f;
+	    private const float Acceleration = 1.0f;
+	    private const float FastModeMultiplier = 2.0f;
+	    private const float RotationalSpeed = 360.0f; // Degrees/second
+
+        private VirtualButton moveForwardBtn;
+        private VirtualButton moveLeftBtn;
+        private VirtualButton moveRightBtn;
+        private VirtualButton moveBackwardBtn;
+        private VirtualButton fastMoveBtn;
+        private VirtualButton rotateBtn;
+        private VirtualAxis horizontalAxis;
+        private VirtualAxis verticalAxis;
+
+        private float currentSpeed;
+        private Degree pitch;
+        private Degree yaw;
+        private bool lastButtonState;
+
+        public SceneCamera()
+        {
+            moveForwardBtn = new VirtualButton(MoveForwardBinding);
+            moveLeftBtn = new VirtualButton(MoveLeftBinding);
+            moveRightBtn = new VirtualButton(MoveRightBinding);
+            moveBackwardBtn = new VirtualButton(MoveBackBinding);
+            fastMoveBtn = new VirtualButton(FastMoveBinding);
+            rotateBtn = new VirtualButton(RotateBinding);
+            horizontalAxis = new VirtualAxis(HorizontalAxisBinding);
+            verticalAxis = new VirtualAxis(VerticalAxisBinding);
+        }
+
+        private void Update()
+	    {
+            // TODO - Only move if scene view is focused
+
+		    bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
+		    bool goingBack = VirtualInput.IsButtonHeld(moveBackwardBtn);
+		    bool goingLeft = VirtualInput.IsButtonHeld(moveLeftBtn);
+		    bool goingRight = VirtualInput.IsButtonHeld(moveRightBtn);
+		    bool fastMove = VirtualInput.IsButtonHeld(fastMoveBtn);
+		    bool camRotating = VirtualInput.IsButtonHeld(rotateBtn);
+
+		    if (camRotating != lastButtonState)
+		    {
+		        if (camRotating)
+		            Cursor.Hide();
+		        else
+		            Cursor.Show();
+
+			    lastButtonState = camRotating;
+		    }
+
+		    float frameDelta = Time.frameDelta;
+		    if (camRotating)
+		    {
+			    yaw += new Degree(VirtualInput.GetAxisValue(horizontalAxis) * RotationalSpeed * frameDelta);
+			    pitch += new Degree(VirtualInput.GetAxisValue(verticalAxis) * RotationalSpeed * frameDelta);
+
+			    yaw = MathEx.WrapAngle(yaw);
+                pitch = MathEx.WrapAngle(pitch);
+
+		        Quaternion yRot = Quaternion.FromAxisAngle(Vector3.yAxis, yaw);
+                Quaternion xRot = Quaternion.FromAxisAngle(Vector3.xAxis, pitch);
+
+			    Quaternion camRot = yRot * xRot;
+		        camRot.Normalize();
+
+		        sceneObject.Rotate(camRot);
+		    }
+
+		    Vector3 direction = Vector3.zero;
+		    if (goingForward) direction += sceneObject.Forward;
+		    if (goingBack) direction -= sceneObject.Forward;
+		    if (goingRight) direction += sceneObject.Right;
+		    if (goingLeft) direction -= sceneObject.Right;
+
+		    if (direction.sqrdMagnitude != 0)
+		    {
+		        direction.Normalize();
+
+			    float multiplier = 1.0f;
+			    if (fastMove)
+				    multiplier = FastModeMultiplier;
+
+			    currentSpeed = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
+			    currentSpeed *= multiplier;
+		    }
+		    else
+		    {
+			    currentSpeed = 0.0f;
+		    }
+
+		    const float tooSmall = 0.0001f;
+		    if (currentSpeed > tooSmall)
+		    {
+			    Vector3 velocity = direction * currentSpeed;
+			    sceneObject.Move(velocity * frameDelta);
+		    }
+	    }
+    }
+}

+ 21 - 0
MBansheeEditor/Scene/SceneWindow.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BansheeEditor
+{
+    internal sealed class SceneWindow : EditorWindow
+    {
+        internal SceneWindow()
+        {
+            
+        }
+
+        protected override void WindowResized(int width, int height)
+        {
+            base.WindowResized(width, height);
+        }
+    }
+}

+ 1 - 0
MBansheeEngine/MBansheeEngine.csproj

@@ -121,6 +121,7 @@
     <Compile Include="Math\Vector4.cs" />
     <Compile Include="Texture3D.cs" />
     <Compile Include="TextureCube.cs" />
+    <Compile Include="Time.cs" />
     <Compile Include="VirtualInput.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

+ 15 - 0
MBansheeEngine/Math/MathEx.cs

@@ -261,5 +261,20 @@ namespace BansheeEngine
             
             return value;
         }
+
+        public static Degree WrapAngle(Degree angle)
+        {
+            const float inv360 = 1.0f/360.0f;
+
+            float angleVal = angle.GetDegrees();
+            float wrapCount = MathEx.Floor(MathEx.Abs(angleVal*inv360));
+
+            if (angleVal > 0.0f)
+                angleVal -= 360.0f * wrapCount;
+            else
+                angleVal += 360.0f * wrapCount;
+
+            return new Degree(angleVal);
+        }
     }
 }

+ 44 - 0
MBansheeEngine/Time.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace BansheeEngine
+{
+    public static class Time
+    {
+        public static float elapsed
+        {
+            get
+            {
+                return Internal_GetElapsed();
+            }
+        }
+
+        public static float frameDelta
+        {
+            get
+            {
+                return Internal_GetFrameDelta();
+            }
+        }
+
+        public static int frameNumber
+        {
+            get
+            {
+                return Internal_GetFrameNumber();
+            }
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern float Internal_GetElapsed();
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern float Internal_GetFrameDelta();
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern int Internal_GetFrameNumber();
+    }
+}

+ 20 - 0
SBansheeEngine/Include/BsScriptTime.h

@@ -0,0 +1,20 @@
+#pragma once
+
+#include "BsScriptEnginePrerequisites.h"
+#include "BsScriptObject.h"
+
+namespace BansheeEngine
+{
+	class BS_SCR_BE_EXPORT ScriptTime : public ScriptObject <ScriptTime>
+	{
+	public:
+		SCRIPT_OBJ(BansheeEngineAssemblyName, "BansheeEngine", "Time")
+
+	private:
+		static float internal_getElapsed();
+		static float internal_getFrameDelta();
+		static UINT32 internal_getFrameNumber();
+
+		ScriptTime(MonoObject* instance);
+	};
+}

+ 2 - 0
SBansheeEngine/SBansheeEngine.vcxproj

@@ -307,6 +307,7 @@
     <ClInclude Include="Include\BsManagedSerializableObjectInfo.h" />
     <ClInclude Include="Include\BsScriptTexture3D.h" />
     <ClInclude Include="Include\BsScriptTextureCube.h" />
+    <ClInclude Include="Include\BsScriptTime.h" />
     <ClInclude Include="Include\BsScriptVector2I.h" />
     <ClInclude Include="Include\BsScriptVirtualButton.h" />
     <ClInclude Include="Include\BsScriptVirtualInput.h" />
@@ -375,6 +376,7 @@
     <ClCompile Include="Source\BsScriptGUIContent.cpp" />
     <ClCompile Include="Source\BsScriptTexture3D.cpp" />
     <ClCompile Include="Source\BsScriptTextureCube.cpp" />
+    <ClCompile Include="Source\BsScriptTime.cpp" />
     <ClCompile Include="Source\BsScriptVector2I.cpp" />
     <ClCompile Include="Source\BsScriptVirtualInput.cpp" />
     <ClCompile Include="Source\BsScriptVirtualButton.cpp" />

+ 6 - 0
SBansheeEngine/SBansheeEngine.vcxproj.filters

@@ -267,6 +267,9 @@
     <ClInclude Include="Include\BsScriptRenderTexture2D.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Include\BsScriptTime.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsScriptTexture2D.cpp">
@@ -467,5 +470,8 @@
     <ClCompile Include="Source\BsScriptRenderTexture2D.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Source\BsScriptTime.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 35 - 0
SBansheeEngine/Source/BsScriptTime.cpp

@@ -0,0 +1,35 @@
+#include "BsScriptTime.h"
+#include "BsMonoManager.h"
+#include "BsMonoClass.h"
+#include "BsMonoMethod.h"
+#include "BsMonoUtil.h"
+#include "BsTime.h"
+
+namespace BansheeEngine
+{
+	ScriptTime::ScriptTime(MonoObject* instance)
+		:ScriptObject(instance)
+	{ }
+
+	void ScriptTime::initRuntimeData()
+	{
+		metaData.scriptClass->addInternalCall("Internal_GetElapsed", &ScriptTime::internal_getElapsed);
+		metaData.scriptClass->addInternalCall("Internal_GetFrameDelta", &ScriptTime::internal_getFrameDelta);
+		metaData.scriptClass->addInternalCall("Internal_GetFrameNumber", &ScriptTime::internal_getFrameNumber);
+	}
+
+	float ScriptTime::internal_getElapsed()
+	{
+		return gTime().getTime();
+	}
+
+	float ScriptTime::internal_getFrameDelta()
+	{
+		return gTime().getFrameDelta();
+	}
+
+	UINT32 ScriptTime::internal_getFrameNumber()
+	{
+		return gTime().getCurrentFrameNumber();
+	}
+}