From d7c051cecf0db9056e94d5e80146aa0b066e606b Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Thu, 4 Nov 2021 13:49:05 +0800
Subject: *text alignment

---
 Data/Scripts/Editor/AssetBrowser.lua               |  2 +-
 Projects/VisualStudio/Editor/Editor.vcxproj        |  3 --
 .../VisualStudio/Editor/Editor.vcxproj.filters     | 10 +----
 Runtime/GUI/UITextMesh.cpp                         | 52 +++++++++++++++++-----
 Runtime/GUI/UITextMesh.h                           |  1 -
 Runtime/Math/Math.h                                | 11 ++++-
 Runtime/Math/Vector2.cpp                           |  4 --
 Runtime/Math/Vector2.h                             |  6 ---
 Runtime/Math/Vector3.cpp                           |  4 --
 Runtime/Math/Vector3.h                             | 18 +++++---
 Runtime/Math/Vector4.cpp                           |  4 --
 Runtime/Math/Vector4.h                             | 19 ++++----
 Runtime/Scripting/GUI/Font.bind.cpp                |  2 +-
 13 files changed, 74 insertions(+), 62 deletions(-)
 delete mode 100644 Runtime/Math/Vector2.cpp
 delete mode 100644 Runtime/Math/Vector3.cpp
 delete mode 100644 Runtime/Math/Vector4.cpp

diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua
index 7a41e85..377aa8e 100644
--- a/Data/Scripts/Editor/AssetBrowser.lua
+++ b/Data/Scripts/Editor/AssetBrowser.lua
@@ -35,7 +35,7 @@ AssetBrowser.OnGUI = function(self)
 
 	Engine.Rendering.UseShader(shader)
 	Engine.Rendering.SetMatrix44("gamelab_mat_mvp", ortho)
-	Engine.Rendering.SetVector2("gamelab_ui_position", {0, 100})
+	Engine.Rendering.SetVector2("gamelab_ui_position", {0, 0})
 	--Engine.Rendering.SetTexture("gamelab_main_tex", tex)
 	--Engine.Rendering.DrawUIQuad({0, 0, 200, 200})
 	_G["default_font"]:GetCharacters("你好世界!\nHello,World!\nProject Window Properties", 12)	
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj
index 6df4713..fe39c0f 100644
--- a/Projects/VisualStudio/Editor/Editor.vcxproj
+++ b/Projects/VisualStudio/Editor/Editor.vcxproj
@@ -219,9 +219,6 @@
     <ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindWatchDog.cpp" />
     <ClCompile Include="..\..\..\Runtime\Lua\LuaHelper.cpp" />
     <ClCompile Include="..\..\..\Runtime\Math\Matrix44.cpp" />
-    <ClCompile Include="..\..\..\Runtime\Math\Vector2.cpp" />
-    <ClCompile Include="..\..\..\Runtime\Math\Vector3.cpp" />
-    <ClCompile Include="..\..\..\Runtime\Math\Vector4.cpp" />
     <ClCompile Include="..\..\..\Runtime\Profiling\FrameStats.cpp" />
     <ClCompile Include="..\..\..\Runtime\Rendering\Quad.cpp" />
     <ClCompile Include="..\..\..\Runtime\Scripting\Common\Common.bind.cpp" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters
index 432151c..4a06e34 100644
--- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters
+++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters
@@ -123,12 +123,6 @@
     <ClCompile Include="..\..\..\Editor\GUI\Dock.cpp">
       <Filter>Editor\GUI</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\Runtime\Math\Vector2.cpp">
-      <Filter>Runtime\Math</Filter>
-    </ClCompile>
-    <ClCompile Include="..\..\..\Runtime\Math\Vector3.cpp">
-      <Filter>Runtime\Math</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\..\Runtime\Utilities\Base64.cpp">
       <Filter>Runtime\Utilities</Filter>
     </ClCompile>
@@ -372,9 +366,6 @@
     <ClCompile Include="..\..\..\Runtime\Math\Matrix44.cpp">
       <Filter>Runtime\Math</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\Runtime\Math\Vector4.cpp">
-      <Filter>Runtime\Math</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\..\Editor\FileSystem\FileWatcher.cpp">
       <Filter>Editor\FileSystem</Filter>
     </ClCompile>
@@ -414,6 +405,7 @@
     <ClCompile Include="..\..\..\Runtime\GUI\UIMesh.cpp">
       <Filter>Runtime\GUI</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\Runtime\Graphics\Color.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\Editor\GUI\Dock.h">
diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp
index 94e61c4..7fc2614 100644
--- a/Runtime/GUI/UITextMesh.cpp
+++ b/Runtime/GUI/UITextMesh.cpp
@@ -65,10 +65,21 @@ InitializeStaticVariables([]() {
 // preferredSize �Զ����������С
 UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor, ETextAlignment alignment, bool wordwrap, float preferred)
 {
-	m_Font = font;
 	s_TextInfos.clear();
+
+    // ��¼�ı�ÿ�еij���
+    static unordered_map<int, int> s_LineWidths;
+    s_LineWidths.clear();
+
+    // ��¼ÿ�е�ƫ�Ƴ���
+    static unordered_map<int, int> s_LineOffsets;
+    s_LineOffsets.clear();
+
+    m_Font = font;
     const Vector2 atlasSize = font->GetAtlasSize();
 
+    //-----------------------------------------------------------------
+    // �����˻��к��Զ�����֮����ı������С
     Vector2 textRegion;
     // ���ղ�ͬ��atlas���ൽs_TextInfos
 	float offset = 0;
@@ -84,6 +95,7 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int
         {
             if (offset + ch->bearing.x + ch->position.width > preferred)
             {
+                
                 ++line;
                 offset = 0;
             }
@@ -127,6 +139,10 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int
 		offset += ch->advance;
 
         textRegion.x = max(offset, textRegion.x);
+
+        if (s_LineWidths.count(line) == 0)
+            s_LineWidths.insert(std::pair<int, int>(line, 0));
+        s_LineWidths[line] = max(offset, s_LineWidths[line]);
 	}
 
     textRegion.y = (line + 1) * lineHeight;
@@ -149,6 +165,21 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int
         case TextAnchor_LowerRight:   textOffset.Set(-textRegion.x, -textRegion.y); break;
     }
 
+    for (int i = 0; i < line; ++i)
+    {
+        int lineLen = s_LineWidths.count(i) != 0 ? s_LineWidths[i] : 0;
+        int lineOffset = 0;
+        switch (alignment)
+        {
+            case TextAlignment_Left: lineOffset = 0; break;
+            case TextAlignment_Center: lineOffset = (textRegion.x - lineLen)/2.f; break;
+            case TextAlignment_Right: lineOffset = textRegion.x - lineLen; break;
+        }
+        s_LineOffsets.insert(std::pair<int, int>(i, lineOffset));
+    }
+
+    //-----------------------------------------------------------------
+
     // ���VBO��IBO
 	for (auto iter : s_TextInfos) {
 		unsigned int atlasIndex = iter.first; // atlas atlasIndex
@@ -167,13 +198,14 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int
             TextInfo& text = texts[i];
 
             int vOff = i * s_VertexPerText;
-            int lineOff = text.line * lineHeight;
+            int lineXOff = s_LineOffsets.count(text.line) ? s_LineOffsets[text.line] : 0;
+            int lineYOff = text.line * lineHeight;
 			// ���Ͻ���ԭ��
             float pos[] = {
-				textOffset.x + text.offset + text.ch->bearing.x,                           textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
-				textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right 
-				textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y,                            // top-right 
-				textOffset.x + text.offset + text.ch->bearing.x,                           textOffset.y + lineOff + pixelSize - text.ch->bearing.y,                            // top-left 
+				textOffset.x + lineXOff + text.offset + text.ch->bearing.x,                           textOffset.y + lineYOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
+				textOffset.x + lineXOff + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right 
+				textOffset.x + lineXOff + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y,                            // top-right 
+				textOffset.x + lineXOff + text.offset + text.ch->bearing.x,                           textOffset.y + lineYOff + pixelSize - text.ch->bearing.y,                            // top-left 
             };
             Vector4 uvQuad = Vector4(text.ch->position.x / atlasSize.x, text.ch->position.y / atlasSize.y, text.ch->position.width / atlasSize.x, text.ch->position.height / atlasSize.y);
             float uv[] = {
@@ -222,14 +254,10 @@ void UITextMesh::Draw()
 
         g_GfxDevice.SetUniformTexture("gamelab_main_tex", atlas->altas);
 
-        CheckGLError(
-            throw GLException(error);
-        );
+        WipeGLError();
         vbo->Draw(s_TextMeshVBOLayout);
 
-        CheckGLError(
-            throw GLException(error);
-        );
+        WipeGLError();
         g_GfxDevice.ResetUniformsState();
     }
 }
diff --git a/Runtime/GUI/UITextMesh.h b/Runtime/GUI/UITextMesh.h
index 487bccd..7778785 100644
--- a/Runtime/GUI/UITextMesh.h
+++ b/Runtime/GUI/UITextMesh.h
@@ -23,7 +23,6 @@ enum ETextAlignment {
     TextAlignment_Left,
     TextAlignment_Center,
     TextAlignment_Right,
-    TextAlignment_Auto,
 };
 
 typedef unsigned long long TextMeshHash;
diff --git a/Runtime/Math/Math.h b/Runtime/Math/Math.h
index 80f23f7..0d1590e 100644
--- a/Runtime/Math/Math.h
+++ b/Runtime/Math/Math.h
@@ -8,11 +8,18 @@
 #include "Rect.h"
 #include "MathHelper.h"
 
-typedef Internal::Vector3  Vector3;
-typedef Internal::Vector4  Vector4;
+
 typedef Internal::Matrix44 Matrix44;
 typedef Internal::Rect     Rect;
 
 typedef Internal::Vector2T<int> Vector2i;
 typedef Internal::Vector2T<float> Vector2f;
+typedef Vector2f Vector2;
+
+typedef Internal::Vector3T<float>  Vector3f;
+typedef Internal::Vector3T<int>  Vector3i;
+typedef Vector3f Vector3;
 
+typedef Internal::Vector4T<float>  Vector4f;
+typedef Internal::Vector4T<int>  Vector4i;
+typedef Vector4f Vector4;
diff --git a/Runtime/Math/Vector2.cpp b/Runtime/Math/Vector2.cpp
deleted file mode 100644
index 185e66f..0000000
--- a/Runtime/Math/Vector2.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "Vector2.h"
-
-Internal::Vector2 Internal::Vector2::one = Internal::Vector2(1, 1);
-Internal::Vector2 Internal::Vector2::zero = Internal::Vector2(0, 0);
diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h
index f13e522..7c519b8 100644
--- a/Runtime/Math/Vector2.h
+++ b/Runtime/Math/Vector2.h
@@ -53,10 +53,4 @@ namespace Internal
     Vector2T<T> Vector2T<T>::zero = Vector2T(0, 0);
     template<typename T> 
     Vector2T<T> Vector2T<T>::one = Vector2T(1, 1);
-
-    typedef Internal::Vector2T<float> Vector2;
-
 }
-
-typedef Internal::Vector2T<float> Vector2;
-
diff --git a/Runtime/Math/Vector3.cpp b/Runtime/Math/Vector3.cpp
deleted file mode 100644
index 13bc9fd..0000000
--- a/Runtime/Math/Vector3.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "Vector3.h"
-
-Internal::Vector3 Internal::Vector3::one = Internal::Vector3(1, 1, 1);
-Internal::Vector3 Internal::Vector3::zero = Internal::Vector3(0, 0, 0);
diff --git a/Runtime/Math/Vector3.h b/Runtime/Math/Vector3.h
index e81e880..c7e5232 100644
--- a/Runtime/Math/Vector3.h
+++ b/Runtime/Math/Vector3.h
@@ -3,17 +3,17 @@
 
 namespace Internal
 {
-
-    struct Vector3
+    template<typename T>
+    struct Vector3T
     {
-        float x, y, z;
-        Vector3(float x = 0, float y = 0, float z = 0)
+        T x, y, z;
+        Vector3T(T x = 0, T y = 0, T z = 0)
         {
             this->x = x;
             this->y = y;
             this->z = z;
         }
-        inline void Set(float x, float y, float z)
+        inline void Set(T x, T y, T z)
         {
             this->x = x;
             this->y = y;
@@ -21,11 +21,15 @@ namespace Internal
         }
 
 
-        static Vector3 zero;
-        static Vector3 one;
+        static Vector3T zero;
+        static Vector3T one;
 
     };
 
+    template<typename T>
+    Vector3T<T> Vector3T<T>::zero = Vector3T(0, 0, 0);
+    template<typename T>
+    Vector3T<T> Vector3T<T>::one = Vector3T(1, 1, 1);
 
 }
 #endif
\ No newline at end of file
diff --git a/Runtime/Math/Vector4.cpp b/Runtime/Math/Vector4.cpp
deleted file mode 100644
index 1d392b7..0000000
--- a/Runtime/Math/Vector4.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "Vector4.h"
-
-Internal::Vector4 Internal::Vector4::one = Internal::Vector4(1, 1, 1, 1);
-Internal::Vector4 Internal::Vector4::zero = Internal::Vector4(0, 0, 0, 0);
diff --git a/Runtime/Math/Vector4.h b/Runtime/Math/Vector4.h
index c0f99f2..e56a1a7 100644
--- a/Runtime/Math/Vector4.h
+++ b/Runtime/Math/Vector4.h
@@ -2,12 +2,12 @@
 
 namespace Internal
 {
-
-    struct Vector4
+    template<typename T>
+    struct Vector4T
     {
-        float x, y, z, w;
+        T x, y, z, w;
 
-        Vector4(float x = 0, float y = 0, float z = 0, float w = 0)
+        Vector4T(T x = 0, T y = 0, T z = 0, T w = 0)
         {
             this->x = x;
             this->y = y;
@@ -15,7 +15,7 @@ namespace Internal
             this->w = w;
         }
 
-        inline void Set(float x, float y, float z, float w)
+        inline void Set(T x, T y, T z, T w)
         {
             this->x = x;
             this->y = y;
@@ -23,10 +23,13 @@ namespace Internal
             this->w = w;
         }
 
-
-        static Vector4 zero;
-        static Vector4 one;
+        static Vector4T zero;
+        static Vector4T one;
     };
 
+    template<typename T>
+    Vector4T<T> Vector4T<T>::zero = Vector4T(0, 0, 0, 0);
+    template<typename T>
+    Vector4T<T> Vector4T<T>::one = Vector4T(1, 1, 1, 1);
 
 }
diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp
index 1928e0e..699b97f 100644
--- a/Runtime/Scripting/GUI/Font.bind.cpp
+++ b/Runtime/Scripting/GUI/Font.bind.cpp
@@ -137,7 +137,7 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
 
     WipeGLError();
 
-    UITextMesh* tm = new UITextMesh(str, self, size, size + 3, TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left, wordwrap, preferred);
+    UITextMesh* tm = new UITextMesh(str, self, size, size + 3, TextAnchor_UpperLeft, TextAlignment_Left, wordwrap, preferred);
     tm->Draw();
 
     return 0;
-- 
cgit v1.1-26-g67d0