diff options
author | chai <chaifix@163.com> | 2021-12-13 00:07:19 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-12-13 00:07:19 +0800 |
commit | 60cbbdec07ab7a5636eac5b3c024ae44e937f4d4 (patch) | |
tree | b2c7b0a868f18159dbc43d8954e1bd7668549a88 /Client/Source/GUI/UI9Slicing.cpp |
+init
Diffstat (limited to 'Client/Source/GUI/UI9Slicing.cpp')
-rw-r--r-- | Client/Source/GUI/UI9Slicing.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Client/Source/GUI/UI9Slicing.cpp b/Client/Source/GUI/UI9Slicing.cpp new file mode 100644 index 0000000..c0d8f60 --- /dev/null +++ b/Client/Source/GUI/UI9Slicing.cpp @@ -0,0 +1,112 @@ +#include "../Math/Math.h" + +#include "UI9Slicing.h" + +#include <cstring> +#include <vector> + +using namespace std; + +static vector<UIVertexLayout> s_Vertices; +static vector<UIIndex> s_Indices; + +UI9Slicing::UI9Slicing(int mode, Vector2f horizontal, Vector2f vertical, Vector2f texPixelSize, Vector2f 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() +{ + s_Indices.clear(); + s_Vertices.clear(); + + Vector2f tileSize = Vector2f(m_TexSize.x - m_Horizontal[0] - m_Horizontal[1], m_TexSize.y - m_Vertical[0] - m_Vertical[1]); + Vector2f fillSize = Vector2f(m_Size.x - m_Horizontal[0] - m_Horizontal[1], m_Size.y - m_Vertical[0] - m_Vertical[1]); + // horizonal和vertical对应的uv,注意uv在左下角,mesh在左上角 + Vector2f tileUVx = Vector2f(m_Horizontal[0] / m_TexSize.x, (m_TexSize.x - m_Horizontal[1]) / m_TexSize.x); + Vector2f tileUVy = Vector2f((m_TexSize.y - m_Vertical[0]) / m_TexSize.y, m_Vertical[1] / m_TexSize.y); + + // 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)); + + 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; + + 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 |