summaryrefslogtreecommitdiff
path: root/Runtime/GUI
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-03 18:52:30 +0800
committerchai <chaifix@163.com>2021-11-03 18:52:30 +0800
commit08ddd44b634d4da78edd0964f539a310544c7883 (patch)
tree108317d9138c3e8a19f3cc3f2ffcfba4768f22d5 /Runtime/GUI
parent6f62a3d5ad405dbab5ac031fb8eeb03bdb395904 (diff)
! UI9Slicing
Diffstat (limited to 'Runtime/GUI')
-rw-r--r--Runtime/GUI/Font.cpp14
-rw-r--r--Runtime/GUI/Font.h18
-rw-r--r--Runtime/GUI/UI9Slicing.cpp93
-rw-r--r--Runtime/GUI/UI9Slicing.h8
-rw-r--r--Runtime/GUI/UIMesh.h9
-rw-r--r--Runtime/GUI/UITextMesh.cpp2
6 files changed, 116 insertions, 28 deletions
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp
index 8a5acea..1993126 100644
--- a/Runtime/GUI/Font.cpp
+++ b/Runtime/GUI/Font.cpp
@@ -16,7 +16,7 @@ using namespace character;
static std::string s_FontError;
static std::vector<unsigned char> s_PixelBuffer;
-static int s_SizePerPixel = sizeof(unsigned char);
+static const int s_SizePerPixel = sizeof(unsigned char);
Font::Font(std::string path, TextGeneratingSettings settings)
: LuaBind::NativeClass<Font>()
@@ -98,7 +98,7 @@ Font::Font(LuaBind::VM* vm, DataBuffer* db, TextGeneratingSettings settings)
}
}
-character::Hash Font::GetHash(Codepoint codepoint, int pixelSize)
+character::Hash Font::GetHash(Unicode codepoint, int pixelSize)
{
character::Hash hash;
hash.codepoint = codepoint;
@@ -106,7 +106,7 @@ character::Hash Font::GetHash(Codepoint codepoint, int pixelSize)
return hash;
}
-const Character* Font::GetCharacter(character::Codepoint codepoint, int pixelSize)
+const Character* Font::GetCharacter(character::Unicode codepoint, int pixelSize)
{
character::Hash hash = GetHash(codepoint, pixelSize);
auto iter = m_Characters.find(hash);
@@ -125,7 +125,7 @@ const Character* Font::GetCharacter(character::Codepoint codepoint, int pixelSiz
return &iter->second;
}
-void Font::RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize)
+void Font::RenderCharacters(character::Unicode* codepoint, int n, int pixelSize)
{
for (int i = 0; i < n; ++i)
{
@@ -133,7 +133,7 @@ void Font::RenderCharacters(character::Codepoint* codepoint, int n, int pixelSiz
}
}
-void Font::RenderCharacters(std::vector<character::Codepoint>& codepoint, int pixelSize)
+void Font::RenderCharacters(std::vector<character::Unicode>& codepoint, int pixelSize)
{
int n = codepoint.size();
for (int i = 0; i < n; ++i)
@@ -142,7 +142,7 @@ void Font::RenderCharacters(std::vector<character::Codepoint>& codepoint, int pi
}
}
-bool Font::RenderCharacter(character::Codepoint codepoint, int pixelSize)
+bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
{
character::Hash hash = GetHash(codepoint, pixelSize);
if (m_Characters.count(hash) != 0)
@@ -318,7 +318,7 @@ bool Font::HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSize)
return false;
}
-Character GetCharacter(character::Codepoint Codepoint, int pixelSize)
+Character GetCharacter(character::Unicode Unicode, int pixelSize)
{
return Character();
}
diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h
index 067a364..5fbe981 100644
--- a/Runtime/GUI/Font.h
+++ b/Runtime/GUI/Font.h
@@ -24,15 +24,15 @@ struct Character {
namespace character
{
#if GAMELAB_WIN
- typedef unsigned short Codepoint; // unicode Codepoint(BMP,U+0000至U+FFFF)
+ typedef unsigned short Unicode; // unicode codepoint(BMP,U+0000至U+FFFF)
#else
- typedef unsigned int Codepoint; // unicode Codepoint(BMP,U+0000至U+FFFF)
+ typedef unsigned int Unicode; // unicode codepoint(BMP,U+0000至U+FFFF)
#endif
union Hash {
unsigned int hashCode;
struct {
- Codepoint codepoint;
+ Unicode codepoint;
unsigned short size;//字体大小
};
bool operator==(const Hash &other) const
@@ -44,7 +44,7 @@ namespace character
struct UnicodeString
{
- character::Codepoint* str;
+ character::Unicode* str;
int length;
};
@@ -104,13 +104,13 @@ public:
Font(DataBuffer* db, TextGeneratingSettings setting)/*throw FontException*/;
Font(LuaBind::VM* vm, DataBuffer* db, TextGeneratingSettings setting)/*throw FontException*/;
- const Character* GetCharacter(character::Codepoint codepoint, int pixelSize);
+ const Character* GetCharacter(character::Unicode codepoint, int pixelSize);
const GlyphAtals* GetGlyphAtlas(int index);
// pre-bake
- bool RenderCharacter(character::Codepoint codepoint, int pixelSize);
- void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize);
- void RenderCharacters(std::vector<character::Codepoint>& codepoint, int pixelSize);
+ bool RenderCharacter(character::Unicode codepoint, int pixelSize);
+ void RenderCharacters(character::Unicode* codepoint, int n, int pixelSize);
+ void RenderCharacters(std::vector<character::Unicode>& codepoint, int pixelSize);
GET(Vector2, AtlasSize, m_AtlasSize);
@@ -119,7 +119,7 @@ private:
GlyphAtals* RequestAtlas(int pixelSize, Vector2 preferSize);
Rect GetRenderChartAndMove(GlyphAtals* atlas, Vector2 preferSize);
bool HasEnoughSpace(GlyphAtals* atlas, Vector2 preferSize);
- character::Hash GetHash(character::Codepoint Codepoint, int pixelSize);
+ character::Hash GetHash(character::Unicode Unicode, int pixelSize);
//-------------------------------------------------------------------------
diff --git a/Runtime/GUI/UI9Slicing.cpp b/Runtime/GUI/UI9Slicing.cpp
index 01a0143..d4c48ca 100644
--- a/Runtime/GUI/UI9Slicing.cpp
+++ b/Runtime/GUI/UI9Slicing.cpp
@@ -1,6 +1,13 @@
#include "UI9Slicing.h"
+#include "Runtime/Math/MathHelper.h"
+#include <cstring>
-UI9Slicing::UI9Slicing(ESlicing mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)
+using namespace std;
+
+static vector<UIVertexLayout> s_Vertices;
+static vector<UIIndex> s_Indices;
+
+UI9Slicing::UI9Slicing(int mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)
{
m_Slicing = mode;
m_Horizontal = horizontal.Clamp(0, texPixelSize.x, 0, texPixelSize.x);
@@ -16,15 +23,87 @@ UI9Slicing::UI9Slicing(ESlicing mode, Vector2 horizontal, Vector2 vertical, Vect
void UI9Slicing::Draw()
{
+ s_Indices.clear();
+ s_Vertices.clear();
+ Vector2 tileSize = Vector2(m_TexSize.x - m_Horizontal[0] - m_Horizontal[1], m_TexSize.y - m_Vertical[0] - m_Vertical[1]);
+ Vector2 fillSize = Vector2(m_Size.x - m_Horizontal[0] - m_Horizontal[1], m_Size.y - m_Vertical[0] - m_Vertical[1]);
+ // horizonal和vertical对应的uv,注意uv在左下角,mesh在左上角
+ Vector2 tileUVx = Vector2(m_Horizontal[0] / m_TexSize.x, (m_TexSize.x - m_Horizontal[1]) / m_TexSize.x);
+ Vector2 tileUVy = Vector2((m_TexSize.y - m_Vertical[0]) / m_TexSize.y, m_Vertical[1] / m_TexSize.y);
- uint8* vb;
- uint16* ib;
+ // fill vertices
+ int row = 2 + ((fillSize.y <= 0) ? 0 : (m_Slicing == Slicing_Simple ? 2 : ceilf((float)fillSize.y / tileSize.y) + 1));
+ int colum = 2 + ((fillSize.x <= 0) ? 0 : (m_Slicing == Slicing_Simple ? 2 : ceilf((float)fillSize.x / tileSize.x) + 1));
- g_SharedVBO.GetChunk(sizeof(UIVertexLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
+ for (int r = 0; r < row; ++r)
+ {
+ UIVertexLayout vert;
+ vert.color = Color32::white;
+ if (r == 0)
+ {
+ vert.position.y = 0;
+ vert.uv.y = 1;
+ }
+ else if (r == row - 1)
+ {
+ vert.position.y = m_Size.y;
+ vert.uv.y = 0;
+ }
+ else {
+ if (m_Slicing == Slicing_Tiled) {
+ vert.position.y = clamp(m_Vertical[0] + (r - 1) * tileSize.y, m_Vertical[0], m_Size.y - m_Vertical[1]);
+ vert.uv.y = odd(r - 1) ? tileUVy[1] : tileUVy[0];
+ } else {
+ vert.position.y = odd(r - 1) ? m_Size.y - m_Vertical[1] : m_Vertical[0];
+ vert.uv.y = odd(r - 1) ? tileUVy[1] : tileUVy[0];
+ }
+ }
+ for (int c = 0; c < colum; ++c)
+ {
+ if (c == 0)
+ {
+ vert.position.x = 0;
+ vert.uv.x = 0;
+ }
+ else if (c == colum - 1)
+ {
+ vert.position.x = m_Size.x;
+ vert.uv.x = 1;
+ }
+ else {
+ if (m_Slicing == Slicing_Tiled) {
+ vert.position.x = clamp(m_Horizontal[0] + (c - 1) * tileSize.x, m_Horizontal[0], m_Size.x - m_Horizontal[0]);
+ vert.uv.x = odd(c - 1) ? tileUVx[1] : tileUVx[0];
+ } else {
+ vert.position.x = odd(c - 1) ? (m_Size.x - m_Horizontal[1]) : m_Horizontal[0];
+ vert.uv.x = odd(c - 1) ? tileUVx[1] : tileUVx[0];
+ }
+ }
+ s_Vertices.push_back(vert);
+ if (c < colum - 1 && r < row - 1) {
+ int index = c + r * colum;
+ s_Indices.push_back(index); s_Indices.push_back(index + colum); s_Indices.push_back(index + colum + 1);
+ s_Indices.push_back(index + colum + 1); s_Indices.push_back(index + 1); s_Indices.push_back(index);
+ }
+ }
+ }
+ void* vb;
+ void* ib;
- g_SharedVBO.ReleaseChunk(4, 6);
- g_SharedVBO.DrawChunk(UIMesh::s_UIVertexLayout);
-}
+ int vertCount = s_Vertices.size();
+ int indicesCount = s_Indices.size();
+
+ g_SharedVBO.GetChunk(sizeof(UIVertexLayout), sizeof(UIIndex), vertCount, indicesCount, Primitive_Triangle, &vb, &ib);
+
+ memcpy(vb, &s_Vertices[0], vertCount * sizeof(UIVertexLayout));
+ memcpy(ib, &s_Indices[0], indicesCount* sizeof(UIIndex));
+
+ s_Indices.resize(0);
+ s_Vertices.resize(0);
+
+ g_SharedVBO.ReleaseChunk(vertCount, indicesCount);
+ g_SharedVBO.DrawChunk(s_UIVertexLayout);
+} \ No newline at end of file
diff --git a/Runtime/GUI/UI9Slicing.h b/Runtime/GUI/UI9Slicing.h
index c88ba05..98d8cf8 100644
--- a/Runtime/GUI/UI9Slicing.h
+++ b/Runtime/GUI/UI9Slicing.h
@@ -12,14 +12,14 @@ enum ESlicing
class UI9Slicing : public UIMesh
{
public:
- UI9Slicing(ESlicing mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)/*throw UIMeshException*/;
+ UI9Slicing(int mode, Vector2 horizontal, Vector2 vertical, Vector2 texPixelSize, Vector2 size)/*throw UIMeshException*/;
void Draw() override;
private:
- ESlicing m_Slicing;
- Vector2 m_Horizontal; // 左右两条切割线到左边和右边的距离
- Vector2 m_Vertical; // 同上
+ int m_Slicing;
+ Vector2 m_Horizontal; // 左右两条切割线到左边和右边的距离
+ Vector2 m_Vertical; // 上下两条切割线到上下的距离
Vector2 m_TexSize;
Vector2 m_Size;
};
diff --git a/Runtime/GUI/UIMesh.h b/Runtime/GUI/UIMesh.h
index dd83e6e..8b6b56d 100644
--- a/Runtime/GUI/UIMesh.h
+++ b/Runtime/GUI/UIMesh.h
@@ -11,11 +11,20 @@
struct UIVertexLayout
{
+ UIVertexLayout(Vector2 pos = Vector2::zero, Vector2 texCoord = Vector2::zero, Color32 col = Color32())
+ {
+ position = pos;
+ uv = texCoord;
+ color = col;
+ }
+
Vector2 position;
Vector2 uv;
Color32 color;
};
+typedef uint16 UIIndex;
+
CustomException(UIMeshException);
// 所有的UIMesh都是左上角为原点
diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp
index 823335f..abb5231 100644
--- a/Runtime/GUI/UITextMesh.cpp
+++ b/Runtime/GUI/UITextMesh.cpp
@@ -69,7 +69,7 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText
float offset = 0;
for (int i = 0; i < str.length; ++i)
{
- character::Codepoint c = str.str[i];
+ character::Unicode c = str.str[i];
const Character* ch = font->GetCharacter(c, pixelSize);
if (ch == NULL)
continue;