Browse Source

Various fixes in order to get GUICanvas functional

BearishSun 9 years ago
parent
commit
6f0747c8f6

+ 7 - 9
Source/BansheeCore/Source/BsMeshUtility.cpp

@@ -182,12 +182,12 @@ namespace BansheeEngine
 			clipEdge0.verts[1] = idx1;
 
 			ClipEdge& clipEdge1 = mesh.edges[idx1];
-			clipEdge0.verts[0] = idx1;
-			clipEdge0.verts[1] = idx2;
+			clipEdge1.verts[0] = idx1;
+			clipEdge1.verts[1] = idx2;
 
 			ClipEdge& clipEdge2 = mesh.edges[idx2];
-			clipEdge0.verts[0] = idx2;
-			clipEdge0.verts[1] = idx0;
+			clipEdge2.verts[0] = idx2;
+			clipEdge2.verts[1] = idx0;
 
 			ClipFace& clipFace = mesh.faces[i];
 
@@ -204,8 +204,6 @@ namespace BansheeEngine
 				clipFace.normal += Vector3::cross(mesh.verts[verts[j]].point, mesh.verts[verts[j + 1]].point);
 
 			clipFace.normal.normalize();
-
-			mesh.faces.push_back(clipFace);
 		}
 	}
 
@@ -499,13 +497,13 @@ namespace BansheeEngine
 		}
 
 		// Add the first two vertices
-		sortedVerts[0] = mesh.edges[sortedEdges[0]].verts[0];
-		sortedVerts[1] = mesh.edges[sortedEdges[0]].verts[1];
+		sortedVerts[0] = mesh.edges[face.edges[sortedEdges[0]]].verts[0];
+		sortedVerts[1] = mesh.edges[face.edges[sortedEdges[0]]].verts[1];
 
 		// Add the remaining vertices
 		for (UINT32 i = 1; i < numEdges; i++)
 		{
-			const ClipEdge& edge = mesh.edges[sortedEdges[i]];
+			const ClipEdge& edge = mesh.edges[face.edges[sortedEdges[i]]];
 
 			if (edge.verts[0] == sortedVerts[i])
 				sortedVerts[i + 1] = edge.verts[1];

+ 20 - 17
Source/BansheeEngine/Include/BsGUICanvas.h

@@ -141,23 +141,26 @@ namespace BansheeEngine
 
 			union
 			{
-				UINT32 vertexStart;
-				UINT32 numVertices;
-				mutable UINT32 clippedVertexStart;
-				mutable UINT32 clippedNumVertices;
-				float lineWidth;
-			};
-
-			union
-			{
-				ImageSprite* imageSprite;
-				TextureScaleMode scaleMode;
-			};
-
-			union
-			{
-				TextSprite* textSprite;
-				UINT32 size;
+				struct
+				{
+					UINT32 vertexStart;
+					UINT32 numVertices;
+					mutable UINT32 clippedVertexStart;
+					mutable UINT32 clippedNumVertices;
+					float lineWidth;
+				};
+
+				struct
+				{
+					ImageSprite* imageSprite;
+					TextureScaleMode scaleMode;
+				};
+
+				struct
+				{
+					TextSprite* textSprite;
+					UINT32 size;
+				};
 			};
 		};
 

+ 19 - 7
Source/BansheeEngine/Source/BsGUICanvas.cpp

@@ -119,9 +119,18 @@ namespace BansheeEngine
 		// Convert strip to list
 		for(UINT32 i = 2; i < (UINT32)vertices.size(); i++)
 		{
-			mVertexData.push_back(Vector2((float)vertices[i - 2].x, (float)vertices[i - 2].y));
-			mVertexData.push_back(Vector2((float)vertices[i - 1].x, (float)vertices[i - 1].y));
-			mVertexData.push_back(Vector2((float)vertices[i - 0].x, (float)vertices[i - 0].y));
+			if (i % 2 == 0)
+			{
+				mVertexData.push_back(Vector2((float)vertices[i - 2].x, (float)vertices[i - 2].y));
+				mVertexData.push_back(Vector2((float)vertices[i - 1].x, (float)vertices[i - 1].y));
+				mVertexData.push_back(Vector2((float)vertices[i - 0].x, (float)vertices[i - 0].y));
+			}
+			else
+			{
+				mVertexData.push_back(Vector2((float)vertices[i - 0].x, (float)vertices[i - 0].y));
+				mVertexData.push_back(Vector2((float)vertices[i - 1].x, (float)vertices[i - 1].y));
+				mVertexData.push_back(Vector2((float)vertices[i - 2].x, (float)vertices[i - 2].y));
+			}
 		}
 
 		mTriangleElementData.push_back(TriangleElementData());
@@ -183,8 +192,11 @@ namespace BansheeEngine
 	{
 		for (auto& element : mElements)
 		{
-			if(element.imageSprite != nullptr)
+			if(element.type == CanvasElementType::Image && element.imageSprite != nullptr)
 				bs_delete(element.imageSprite);
+
+			if (element.type == CanvasElementType::Text && element.textSprite != nullptr)
+				bs_delete(element.textSprite);
 		}
 
 		mElements.clear();
@@ -526,14 +538,14 @@ namespace BansheeEngine
 	const GUICanvas::CanvasElement& GUICanvas::findElement(UINT32 renderElementIdx) const
 	{
 		INT32 start = 0;
-		INT32 end = (INT32)mElements.size();
+		INT32 end = (INT32)(mElements.size() - 1);
 
-		while (start < end)
+		while (start <= end)
 		{
 			INT32 middle = (start + end) / 2;
 			const CanvasElement& current = mElements[middle];
 
-			if (current.renderElemStart >= renderElementIdx && renderElementIdx < current.renderElemEnd)
+			if (renderElementIdx >= current.renderElemStart && renderElementIdx < current.renderElemEnd)
 				return current;
 
 			if (renderElementIdx < current.renderElemStart)

+ 3 - 1
Source/BansheeEngine/Source/BsGUIElementBase.cpp

@@ -57,7 +57,9 @@ namespace BansheeEngine
 		mDimensions.x = x;
 		mDimensions.y = y;
 
-		_markMeshAsDirty();
+		// Note: I could call _markMeshAsDirty with a little more work. If parent is layout then this call can be ignored
+		// and if it's a panel, we can immediately change the position without a full layout rebuild.
+		_markLayoutAsDirty();
 	}
 
 	void GUIElementBase::setWidth(UINT32 width)

+ 7 - 7
Source/BansheeEngine/Source/BsGUIManager.cpp

@@ -406,12 +406,6 @@ namespace BansheeEngine
 					const Vector<GUIMeshData>& cachedMeshes = *meshSet;
 					for (auto& entry : cachedMeshes)
 					{
-						if (entry.matInfo.texture == nullptr || !entry.matInfo.texture.isLoaded())
-						{
-							meshIdx++;
-							continue;
-						}
-
 						if (entry.mesh == nullptr)
 						{
 							meshIdx++;
@@ -421,8 +415,14 @@ namespace BansheeEngine
 						cameraData.push_back(GUICoreRenderData());
 						GUICoreRenderData& newEntry = cameraData.back();
 
+						SPtr<TextureCore> textureCore;
+						if (entry.matInfo.texture.isLoaded())
+							textureCore = entry.matInfo.texture->getCore();
+						else
+							textureCore = nullptr;
+
 						newEntry.material = entry.material;
-						newEntry.texture = entry.matInfo.texture->getCore();
+						newEntry.texture = textureCore;
 						newEntry.tint = entry.matInfo.tint;
 						newEntry.mesh = entry.mesh->getCore();
 						newEntry.worldTransform = entry.widget->getWorldTfrm();

+ 7 - 1
Source/BansheeEngine/Source/BsSpriteMaterial.cpp

@@ -60,7 +60,13 @@ namespace BansheeEngine
 		const SPtr<SamplerStateCore>& sampler, const Color& tint, const Matrix4& worldTransform,
 		const Vector2& invViewportSize, const SPtr<SpriteMaterialExtraInfo>& additionalData) const
 	{
-		mTextureParam.set(texture);
+		SPtr<TextureCore> spriteTexture;
+		if (texture != nullptr)
+			spriteTexture = texture;
+		else
+			spriteTexture = TextureCore::WHITE;
+
+		mTextureParam.set(spriteTexture);
 		mSamplerParam.set(sampler);
 		mTintParam.set(tint);
 		mInvViewportWidthParam.set(invViewportSize.x);

+ 5 - 5
Source/BansheeUtility/Source/BsPlane.cpp

@@ -24,7 +24,7 @@ namespace BansheeEngine
 	Plane::Plane(const Vector3& normal, float d)
 	{
 		this->normal = normal;
-		this->d = -d;
+		this->d = d;
 	}
 
 	Plane::Plane(float a, float b, float c, float _d)
@@ -34,7 +34,7 @@ namespace BansheeEngine
 	Plane::Plane(const Vector3& normal, const Vector3& point)
 	{
 		this->normal = normal;
-		d = -normal.dot(point);
+		d = normal.dot(point);
 	}
 
 	Plane::Plane(const Vector3& point0, const Vector3& point1, const Vector3& point2)
@@ -43,12 +43,12 @@ namespace BansheeEngine
 		Vector3 kEdge2 = point2 - point0;
 		normal = kEdge1.cross(kEdge2);
 		normal.normalize();
-		d = -normal.dot(point0);
+		d = normal.dot(point0);
 	}
 
 	float Plane::getDistance(const Vector3& point) const
 	{
-		return normal.dot(point) + d;
+		return normal.dot(point) - d;
 	}
 
 	Plane::Side Plane::getSide(const Vector3& point) const
@@ -150,7 +150,7 @@ namespace BansheeEngine
 		}
 		else
 		{
-			float nom = normal.dot(ray.getOrigin()) + d;
+			float nom = normal.dot(ray.getOrigin()) - d;
 			float t = -(nom/denom);
 			return std::pair<bool, float>(t >= 0.0f, t);
 		}

+ 2 - 2
Source/BansheeUtility/Source/BsTime.cpp

@@ -26,9 +26,9 @@ namespace BansheeEngine
 		UINT64 currentFrameTime = mTimer->getMicroseconds();
 
 		mFrameDelta = (float)((currentFrameTime - mLastFrameTime) * MICROSEC_TO_SEC);
-		mTimeSinceStart = (float)(currentFrameTime * MICROSEC_TO_SEC);
 		mTimeSinceStartMs = (UINT64)(currentFrameTime / 1000);
-
+		mTimeSinceStart = (mTimeSinceStartMs - mAppStartTime) / 1000.0f;
+		
 		mLastFrameTime = currentFrameTime;
 
 		mCurrentFrame.fetch_add(1, std::memory_order_relaxed);

+ 1 - 0
Source/MBansheeEditor/MBansheeEditor.csproj

@@ -45,6 +45,7 @@
     <Compile Include="Inspectors\AudioSourceInspector.cs" />
     <Compile Include="Inspectors\PostProcessSettingsInspector.cs" />
     <Compile Include="Windows\AboutBox.cs" />
+    <Compile Include="Windows\AnimationWindow.cs" />
     <Compile Include="Windows\BrowseDialog.cs" />
     <Compile Include="Windows\Build\BuildManager.cs" />
     <Compile Include="Windows\Build\BuildWindow.cs" />

+ 69 - 0
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -0,0 +1,69 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /** @addtogroup Windows
+     *  @{
+     */
+
+    /// <summary>
+    /// Displays animation curve editor window.
+    /// </summary>
+    internal class AnimationWindow : EditorWindow
+    {
+        private GUICanvas canvas;
+
+        /// <summary>
+        /// Opens the animation window.
+        /// </summary>
+        [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
+        private static void OpenGameWindow()
+        {
+            OpenWindow<AnimationWindow>();
+        }
+
+        /// <inheritdoc/>
+        protected override LocString GetDisplayName()
+        {
+            return new LocEdString("Animation");
+        }
+
+        private void OnInitialize()
+        {
+            canvas = new GUICanvas(GUIOption.FixedWidth(200), GUIOption.FixedHeight(200));
+
+            {
+                Vector2I a = new Vector2I(0, 0);
+                Vector2I b = new Vector2I(200, 0);
+                Vector2I c = new Vector2I(200, 200);
+                Vector2I d = new Vector2I(0, 200);
+
+                canvas.DrawTriangleStrip(new Vector2I[] { b, c, a, d }, Color.BansheeOrange);
+            }
+
+            {
+                Vector2I a = new Vector2I(150, 20);
+                Vector2I b = new Vector2I(230, 20);
+                Vector2I c = new Vector2I(150, 70);
+
+                canvas.DrawTriangleList(new Vector2I[] {a, b, c});
+            }
+            GUI.AddElement(canvas);
+        }
+
+        private void OnEditorUpdate()
+        {
+            int position = (int)(MathEx.Sin(Time.RealElapsed)*50.0f + 50.0f);
+            canvas.SetPosition(position, 0);
+        }
+
+        private void OnDestroy()
+        {
+            // TODO
+        }
+    }
+
+    /** @} */
+}

+ 3 - 4
Source/SBansheeEngine/Source/BsScriptGUICanvas.cpp

@@ -7,7 +7,6 @@
 #include "BsGUILayout.h"
 #include "BsGUICanvas.h"
 #include "BsGUIOptions.h"
-#include "BsScriptVector2I.h"
 #include "BsScriptFont.h"
 
 namespace BansheeEngine
@@ -59,7 +58,7 @@ namespace BansheeEngine
 		UINT32 size = verticesArray.size();
 
 		Vector<Vector2I> nativeVertices(size);
-		memcpy(nativeVertices.data(), verticesArray.getRawPtr<ScriptVector2I>(), sizeof(Vector2));
+		memcpy(nativeVertices.data(), verticesArray.getRawPtr<Vector2I>(), sizeof(Vector2I) * size);
 
 		canvas->drawPolyLine(nativeVertices, width, *color);
 	}
@@ -84,7 +83,7 @@ namespace BansheeEngine
 		UINT32 size = verticesArray.size();
 
 		Vector<Vector2I> nativeVertices(size);
-		memcpy(nativeVertices.data(), verticesArray.getRawPtr<ScriptVector2I>(), sizeof(Vector2));
+		memcpy(nativeVertices.data(), verticesArray.getRawPtr<Vector2I>(), sizeof(Vector2I) * size);
 
 		canvas->drawTriangleStrip(nativeVertices, *color);
 	}
@@ -97,7 +96,7 @@ namespace BansheeEngine
 		UINT32 size = verticesArray.size();
 
 		Vector<Vector2I> nativeVertices(size);
-		memcpy(nativeVertices.data(), verticesArray.getRawPtr<ScriptVector2I>(), sizeof(Vector2));
+		memcpy(nativeVertices.data(), verticesArray.getRawPtr<Vector2I>(), sizeof(Vector2I) * size);
 
 		canvas->drawTriangleList(nativeVertices, *color);
 	}