summaryrefslogtreecommitdiff
path: root/Runtime
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-03 09:52:26 +0800
committerchai <chaifix@163.com>2021-11-03 09:52:26 +0800
commitc10e0d92f46e5eaf25a69e1fafe5f4dbd8eaab9d (patch)
tree2eb1a91339b35fea68f48b2774355f496519db83 /Runtime
parent3898f2c648b1a731dead8337aad8912d2b8b80d7 (diff)
*misc
Diffstat (limited to 'Runtime')
-rw-r--r--Runtime/GUI/Font.cpp7
-rw-r--r--Runtime/GUI/TextMeshGenerator.h4
-rw-r--r--Runtime/GUI/UI9Slicing.cpp30
-rw-r--r--Runtime/GUI/UI9Slicing.h25
-rw-r--r--Runtime/GUI/UIMesh.cpp15
-rw-r--r--Runtime/GUI/UIMesh.h32
-rw-r--r--Runtime/GUI/UIQuad.h4
-rw-r--r--Runtime/GUI/UITextMesh.cpp (renamed from Runtime/GUI/TextMesh.cpp)27
-rw-r--r--Runtime/GUI/UITextMesh.h (renamed from Runtime/GUI/TextMesh.h)44
-rw-r--r--Runtime/Math/Math.h4
-rw-r--r--Runtime/Math/MathHelper.h11
-rw-r--r--Runtime/Math/Vector2.h17
-rw-r--r--Runtime/Rendering/DynamicMesh.h3
-rw-r--r--Runtime/Scripting/GUI/Font.bind.cpp4
14 files changed, 184 insertions, 43 deletions
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp
index 8cdfdf3..614792a 100644
--- a/Runtime/GUI/Font.cpp
+++ b/Runtime/GUI/Font.cpp
@@ -146,8 +146,8 @@ void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize)
int w = m_FTFace->glyph->bitmap.width;
int h = m_FTFace->glyph->bitmap.rows;
-
- //TextHelper::print_glyph(m_FTFace->glyph->bitmap.buffer, w, h);
+
+ TextHelper::print_glyph(m_FTFace->glyph->bitmap.buffer, w, h);
GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h));
Assert(atlas);
@@ -173,6 +173,9 @@ void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize)
character.advance = m_FTFace->glyph->advance.x * 1/64.f;
m_Characters.insert(std::pair<character::Hash, Character>(hash, character));
+/*
+ FT_Done_Face(m_FTFace);
+ FT_Done_FreeType(m_FTLibrary);*/
}
const GlyphAtals* Font::GetGlyphAtlas(int index)
diff --git a/Runtime/GUI/TextMeshGenerator.h b/Runtime/GUI/TextMeshGenerator.h
index baeaf16..94bcaa2 100644
--- a/Runtime/GUI/TextMeshGenerator.h
+++ b/Runtime/GUI/TextMeshGenerator.h
@@ -1,6 +1,6 @@
#pragma once
-#include "TextMesh.h"
+#include "UITextMesh.h"
#include "Runtime/Utilities/IIncrementalTask.h"
#include "Font.h"
@@ -24,7 +24,7 @@ public:
class TextMeshGenerator : public Singleton<TextMeshGenerator>
{
public:
- TextMesh* GetTextMesh(const UnicodeString& str);
+ UITextMesh* GetTextMesh(const UnicodeString& str);
private:
diff --git a/Runtime/GUI/UI9Slicing.cpp b/Runtime/GUI/UI9Slicing.cpp
new file mode 100644
index 0000000..01a0143
--- /dev/null
+++ b/Runtime/GUI/UI9Slicing.cpp
@@ -0,0 +1,30 @@
+#include "UI9Slicing.h"
+
+UI9Slicing::UI9Slicing(ESlicing mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)
+{
+ m_Slicing = mode;
+ m_Horizontal = horizontal.Clamp(0, texPixelSize.x, 0, texPixelSize.x);
+ m_Vertical = vertical.Clamp(0, texPixelSize.y, 0, texPixelSize.y);
+
+ if (m_Horizontal[0] + m_Horizontal[1] > texPixelSize.x || m_Vertical[0] + m_Vertical[1] > texPixelSize.y)
+ {
+ throw UIMeshException("UI9Slicing wrong parameter.");
+ }
+ m_TexSize = texPixelSize;
+ m_Size = size;
+}
+
+void UI9Slicing::Draw()
+{
+
+
+ uint8* vb;
+ uint16* ib;
+
+ g_SharedVBO.GetChunk(sizeof(UIVertexLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
+
+
+
+ g_SharedVBO.ReleaseChunk(4, 6);
+ g_SharedVBO.DrawChunk(UIMesh::s_UIVertexLayout);
+}
diff --git a/Runtime/GUI/UI9Slicing.h b/Runtime/GUI/UI9Slicing.h
new file mode 100644
index 0000000..c88ba05
--- /dev/null
+++ b/Runtime/GUI/UI9Slicing.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "UIMesh.h"
+
+enum ESlicing
+{
+ Slicing_Simple,
+ Slicing_Tiled,
+};
+
+// 九宫格
+class UI9Slicing : public UIMesh
+{
+public:
+ UI9Slicing(ESlicing mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)/*throw UIMeshException*/;
+
+ void Draw() override;
+
+private:
+ ESlicing m_Slicing;
+ Vector2 m_Horizontal; // 左右两条切割线到左边和右边的距离
+ Vector2 m_Vertical; // 同上
+ Vector2 m_TexSize;
+ Vector2 m_Size;
+};
diff --git a/Runtime/GUI/UIMesh.cpp b/Runtime/GUI/UIMesh.cpp
new file mode 100644
index 0000000..a68b6fd
--- /dev/null
+++ b/Runtime/GUI/UIMesh.cpp
@@ -0,0 +1,15 @@
+#include "UIMesh.h"
+
+CustomVertexLayout UIMesh::s_UIVertexLayout;
+unsigned int UIMesh::s_SizePerVertex;
+
+InitializeStaticVariables([]() {
+ VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIVertexLayout));
+ VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(UIVertexLayout));
+ VertexAttributeDescriptor COLOR = VertexAttributeDescriptor(sizeof(Vector2) * 2, 4, VertexAttrFormat_Unsigned_Byte, sizeof(UIVertexLayout), true);
+ UIMesh::s_UIVertexLayout.attributes.push_back(POSITION);
+ UIMesh::s_UIVertexLayout.attributes.push_back(UV);
+ UIMesh::s_UIVertexLayout.attributes.push_back(COLOR);
+
+ UIMesh::s_SizePerVertex = sizeof(UIVertexLayout);
+});
diff --git a/Runtime/GUI/UIMesh.h b/Runtime/GUI/UIMesh.h
new file mode 100644
index 0000000..dd83e6e
--- /dev/null
+++ b/Runtime/GUI/UIMesh.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "Runtime/Utilities/Exception.h"
+#include "Runtime/Rendering/DynamicMesh.h"
+#include "Runtime/Math/Math.h"
+#include "Runtime/Utilities/StaticInitiator.h"
+#include "Runtime/Graphics/CustomVertexLayout.h"
+#include "Runtime/Graphics/DefaultVertexLayout.h"
+#include "Runtime/Graphics/Color.h"
+#include "Runtime/Graphics/GfxDevice.h"
+
+struct UIVertexLayout
+{
+ Vector2 position;
+ Vector2 uv;
+ Color32 color;
+};
+
+CustomException(UIMeshException);
+
+// 所有的UIMesh都是左上角为原点
+class UIMesh : public DynamicMesh
+{
+public:
+ UIMesh() : DynamicMesh() {}
+ virtual ~UIMesh() {}
+
+ virtual void Draw() = 0;
+
+ static CustomVertexLayout s_UIVertexLayout;
+ static unsigned int s_SizePerVertex;
+};
diff --git a/Runtime/GUI/UIQuad.h b/Runtime/GUI/UIQuad.h
index bcd95a0..278848a 100644
--- a/Runtime/GUI/UIQuad.h
+++ b/Runtime/GUI/UIQuad.h
@@ -1,8 +1,8 @@
#pragma once
-#include "../Rendering/DynamicMesh.h"
#include "../Utilities/StaticInitiator.h"
+#include "UIMesh.h"
-class UIQuad : public DynamicMesh
+class UIQuad : public UIMesh
{
public :
UIQuad(float l, float r, float t, float b)
diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/UITextMesh.cpp
index 2f66170..6a10f88 100644
--- a/Runtime/GUI/TextMesh.cpp
+++ b/Runtime/GUI/UITextMesh.cpp
@@ -2,7 +2,7 @@
#include "Runtime/Utilities/StaticInitiator.h"
#include "../Math/Math.h"
#include "../Graphics/Color.h"
-#include "TextMesh.h"
+#include "UITextMesh.h"
#include "../Graphics/DefaultVertexLayout.h"
#include "Runtime/Debug/Log.h"
#include <vector>
@@ -50,9 +50,16 @@ InitializeStaticVariables([]() {
s_SizePerText = sizeof(TextMeshVBOLayout) * 4;
});
-// 一段文字里面的网格可能会来自不同的atlas,在生成TextMesh时做好合批
+// 一段文字里面的网格可能会来自不同的atlas,在生成UITextMesh时做好合批
-TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
+// 需要支持
+// * 大小
+// * 锚点
+// * 对齐方式
+// * 换行
+// * 颜色
+
+UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
{
m_Font = font;
s_TextInfos.clear();
@@ -101,13 +108,13 @@ TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnch
TextInfo& text = texts[i];
int vOff = i * s_VertexPerText;
+ // 左上角是原点
float pos[] = {
- text.offset + text.ch->bearing.x, text.ch->bearing.y - text.ch->position.height, // bottom-left
- text.offset + text.ch->bearing.x + text.ch->position.width, text.ch->bearing.y - text.ch->position.height, // bottom-right
- text.offset + text.ch->bearing.x + text.ch->position.width, text.ch->bearing.y, // top-right
- text.offset + text.ch->bearing.x, text.ch->bearing.y, // top-left
+ text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
+ text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right
+ text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y, // top-right
+ text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y, // top-left
};
- // 左上角为UV原点,所以在shader里采样的时候要镜像y
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[] = {
uvQuad.x, uvQuad.y + uvQuad.w,
@@ -132,14 +139,14 @@ TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnch
}
vb->FlushChunk(s_VertexPerText * textCount, s_IndicesPerText * textCount);
-
+
m_VBOs.insert(std::pair<int, VertexBuffer*>(atlasIndex, vb));
}
WipeGLError();
}
-void TextMesh::Draw()
+void UITextMesh::Draw()
{
for (auto subText : m_VBOs)
{
diff --git a/Runtime/GUI/TextMesh.h b/Runtime/GUI/UITextMesh.h
index c5f05a2..c88e52f 100644
--- a/Runtime/GUI/TextMesh.h
+++ b/Runtime/GUI/UITextMesh.h
@@ -1,13 +1,13 @@
-#pragma once
-#include "../Graphics/VertexBuffer.h"
-#include "Font.h"
-#include "Runtime/Utilities/Exception.h"
-#include <unordered_map>
-
-CustomException(TextMeshException);
-
-enum ETextAnchor
-{
+#pragma once
+#include "../Graphics/VertexBuffer.h"
+#include "Font.h"
+#include "Runtime/Utilities/Exception.h"
+#include <unordered_map>
+
+CustomException(TextMeshException);
+
+enum ETextAnchor
+{
TextAnchor_UpperLeft,
TextAnchor_UpperCenter,
TextAnchor_UpperRight,
@@ -17,9 +17,9 @@ enum ETextAnchor
TextAnchor_LowerLeft,
TextAnchor_LowerCenter,
TextAnchor_LowerRight,
- TextAnchor_DontCare
-};
-
+ TextAnchor_DontCare
+};
+
enum ETextAlignment {
TextAlignment_Left,
TextAlignment_Center,
@@ -27,19 +27,19 @@ enum ETextAlignment {
TextAlignment_Auto,
};
-typedef unsigned long long TextMeshHash;
-
-namespace TextHelper
-{
- TextMeshHash GetTextMeshHash();
-}
+typedef unsigned long long TextMeshHash;
+
+namespace TextHelper
+{
+ TextMeshHash GetTextMeshHash();
+}
-class TextMesh
+class UITextMesh
{
public:
- TextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/;
+ UITextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/;
- ~TextMesh();
+ ~UITextMesh();
void Draw();
diff --git a/Runtime/Math/Math.h b/Runtime/Math/Math.h
index aaeb264..5e08613 100644
--- a/Runtime/Math/Math.h
+++ b/Runtime/Math/Math.h
@@ -6,9 +6,7 @@
#include "Matrix44.h"
#include "FloatConversion.h"
#include "Rect.h"
-
-#define max(a, b)\
-(a)>(b)?(a):(b)
+#include "MathHelper.h"
typedef Internal::Vector2 Vector2;
typedef Internal::Vector3 Vector3;
diff --git a/Runtime/Math/MathHelper.h b/Runtime/Math/MathHelper.h
new file mode 100644
index 0000000..3f8754a
--- /dev/null
+++ b/Runtime/Math/MathHelper.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#define max(a, b)\
+(a)>(b)?(a):(b)
+
+#define min(a, b)\
+(a)<(b)?(a):(b)
+
+#define clamp(v, lo, hi)\
+max((lo), min((v), (hi)))
+
diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h
index 27cf312..9d7e4e9 100644
--- a/Runtime/Math/Vector2.h
+++ b/Runtime/Math/Vector2.h
@@ -1,4 +1,6 @@
#pragma once
+#include "MathHelper.h"
+#include "Runtime/Utilities/Assert.h"
namespace Internal
{
@@ -15,6 +17,21 @@ namespace Internal
this->y = y;
}
+ Vector2 Clamp(float xmin, float xmax, float ymin, float ymax)
+ {
+ Vector2 v;
+ v.x = clamp(x, xmin, xmax);
+ v.y = clamp(y, ymin, ymax);
+ return v;
+ }
+
+ float operator[](int i)
+ {
+ if (i == 0) return x;
+ else if (i == 1) return y;
+ Assert(false);
+ }
+
bool operator == (const Vector2& v) const
{
return v.x == x && v.y == y;
diff --git a/Runtime/Rendering/DynamicMesh.h b/Runtime/Rendering/DynamicMesh.h
index 3cbb6f6..01babfc 100644
--- a/Runtime/Rendering/DynamicMesh.h
+++ b/Runtime/Rendering/DynamicMesh.h
@@ -5,6 +5,9 @@
class DynamicMesh
{
public:
+ DynamicMesh() {};
+ virtual ~DynamicMesh() {};
+
virtual void Draw() = 0;
};
diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp
index 733fcf1..2d01925 100644
--- a/Runtime/Scripting/GUI/Font.bind.cpp
+++ b/Runtime/Scripting/GUI/Font.bind.cpp
@@ -5,7 +5,7 @@
#include "Runtime/Common/DataBuffer.h"
#include "Runtime/GUI/utf8.h"
#include "Runtime/Utilities/StaticInitiator.h"
-#include "Runtime/GUI/TextMesh.h"
+#include "Runtime/GUI/UITextMesh.h"
static std::vector<character::Codepoint>* s_Codepoints;
@@ -134,7 +134,7 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
WipeGLError();
- TextMesh* tm = new TextMesh(str, self, size, ETextAnchor::TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left);
+ UITextMesh* tm = new UITextMesh(str, self, size, ETextAnchor::TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left);
tm->Draw();
return 0;