summaryrefslogtreecommitdiff
path: root/Runtime
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime')
-rw-r--r--Runtime/GUI/UILine.cpp48
-rw-r--r--Runtime/GUI/UILine.h21
-rw-r--r--Runtime/GUI/UIMesh.h1
-rw-r--r--Runtime/GUI/UIQuad.cpp8
-rw-r--r--Runtime/Graphics/DynamicVertexBuffer.cpp4
-rw-r--r--Runtime/Scripting/GUI/GUI.bind.cpp103
6 files changed, 178 insertions, 7 deletions
diff --git a/Runtime/GUI/UILine.cpp b/Runtime/GUI/UILine.cpp
new file mode 100644
index 0000000..8db681e
--- /dev/null
+++ b/Runtime/GUI/UILine.cpp
@@ -0,0 +1,48 @@
+#include "UILine.h"
+
+struct UILineLayout
+{
+ Vector2 position;
+};
+
+static CustomVertexLayout layout;
+
+InitializeStaticVariables([]() {
+ VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UILineLayout));
+
+ layout.attributes.push_back(POSITION);
+});
+
+void UILine::Draw()
+{
+ const int nVerts = 2;
+ const int nIndices = 2;
+
+ float pos[] = {
+ m_From.x, m_From.y,
+ m_To.x, m_To.y,
+ };
+
+ uint16 indices[] = {
+ 0, 1
+ };
+
+
+ uint8* vb;
+ uint16* ib;
+
+ g_SharedVBO.GetChunk(sizeof(UILineLayout), sizeof(uint16), nVerts, nIndices, Primitive_Line, (void**)&vb, (void**)&ib);
+
+ UILineLayout* dst = (UILineLayout*)vb;
+
+ for (int i = 0; i < nVerts; ++i)
+ {
+ dst[i].position.Set(pos[2 * i], pos[2 * i + 1]);
+ }
+
+ for (int i = 0; i < nIndices; ++i)
+ ib[i] = indices[i];
+
+ g_SharedVBO.ReleaseChunk(nVerts, nIndices);
+ g_SharedVBO.DrawChunk(layout);
+} \ No newline at end of file
diff --git a/Runtime/GUI/UILine.h b/Runtime/GUI/UILine.h
new file mode 100644
index 0000000..a47b1f1
--- /dev/null
+++ b/Runtime/GUI/UILine.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "../Utilities/StaticInitiator.h"
+#include "UIMesh.h"
+
+class UILine : public UIMesh
+{
+public:
+ UILine(Vector2f from, Vector2f to)
+ {
+ m_From = from;
+ m_To = to;
+ }
+
+ void Draw() override;
+
+private:
+ Vector2f m_From;
+ Vector2f m_To;
+
+};
diff --git a/Runtime/GUI/UIMesh.h b/Runtime/GUI/UIMesh.h
index 8b6b56d..1de111b 100644
--- a/Runtime/GUI/UIMesh.h
+++ b/Runtime/GUI/UIMesh.h
@@ -9,6 +9,7 @@
#include "Runtime/Graphics/Color.h"
#include "Runtime/Graphics/GfxDevice.h"
+// UI默认的顶点布局
struct UIVertexLayout
{
UIVertexLayout(Vector2 pos = Vector2::zero, Vector2 texCoord = Vector2::zero, Color32 col = Color32())
diff --git a/Runtime/GUI/UIQuad.cpp b/Runtime/GUI/UIQuad.cpp
index c57685c..269b805 100644
--- a/Runtime/GUI/UIQuad.cpp
+++ b/Runtime/GUI/UIQuad.cpp
@@ -30,8 +30,6 @@ void UIQuad::Draw()
m_Left, m_Top, // top-left
};
- int n = sizeof(Vector2);
-
float uv[] = {
0, 0,
1, 0,
@@ -39,7 +37,7 @@ void UIQuad::Draw()
0, 1,
};
- int indices[] = {
+ uint16 indices[] = {
0, 1, 3, // right-top
1, 2, 3, // left-bottom
};
@@ -47,7 +45,7 @@ void UIQuad::Draw()
uint8* vb;
uint16* ib;
- g_SharedVBO.GetChunk(sizeof(UIQuadLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
+ g_SharedVBO.GetChunk(sizeof(UIQuadLayout), sizeof(uint16), nVerts, nIndices, Primitive_Triangle, (void**)&vb, (void**)&ib);
UIQuadLayout* dst = (UIQuadLayout*)vb;
@@ -60,6 +58,6 @@ void UIQuad::Draw()
for (int i = 0; i < nIndices; ++i)
ib[i] = indices[i];
- g_SharedVBO.ReleaseChunk(4, 6);
+ g_SharedVBO.ReleaseChunk(nVerts, nIndices);
g_SharedVBO.DrawChunk(layout);
} \ No newline at end of file
diff --git a/Runtime/Graphics/DynamicVertexBuffer.cpp b/Runtime/Graphics/DynamicVertexBuffer.cpp
index cb59a8b..8a5499a 100644
--- a/Runtime/Graphics/DynamicVertexBuffer.cpp
+++ b/Runtime/Graphics/DynamicVertexBuffer.cpp
@@ -192,11 +192,11 @@ void DynamicVertexBuffer::DrawChunk(CustomVertexLayout& layout)
g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3);
break;
case Primitive_Line:
- glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr);
+ glDrawElements(GL_LINES, m_CurIndexCount, indexFormat, indexPtr);
g_FrameStats.AddDrawCall();
break;
case Primitive_Point:
- glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr);
+ glDrawElements(GL_POINTS, m_CurIndexCount, indexFormat, indexPtr);
g_FrameStats.AddDrawCall();
break;
}
diff --git a/Runtime/Scripting/GUI/GUI.bind.cpp b/Runtime/Scripting/GUI/GUI.bind.cpp
index a4ee772..e05d373 100644
--- a/Runtime/Scripting/GUI/GUI.bind.cpp
+++ b/Runtime/Scripting/GUI/GUI.bind.cpp
@@ -4,9 +4,110 @@
#include "Runtime/FileSystem/FileJobs.h"
#include "Runtime/GUI/Font.h"
#include "Runtime/GUI/UITextMesh.h"
+#include "Runtime/GUI/utf8.h"
+#include "Runtime/Utilities/StaticInitiator.h"
+#include "Runtime/GUI/UITextMesh.h"
+#include "Runtime/Math/Math.h"
+#include "Runtime/GUI/TextMeshGenerator.h"
+#include "Runtime/Utilities/AutoInvoke.h"
+#include "Editor/Win/Win.h"
+#include "Runtime/GUI/UILine.h"
using namespace std;
using namespace LuaBind;
+
+static std::vector<character::Unicode>* s_Codepoints;
+
+InitializeStaticVariables([]() {
+ s_Codepoints = new std::vector<character::Unicode>();
+});
+
+#undef DrawText
+// GUI.DrawText(font, str, pixelSize, lineHeight, color, anchor, alignment, wordwrap, preferred, encoding)
+static int DrawText(lua_State* L)
+{
+ LUA_BIND_STATE(L);
+
+ Font* font = (Font*)state.GetUserdata<Font>(1);
+ char* buf = (char*)state.GetValue<const char*>(2, "");
+ int pixelSize = state.GetValue<int>(3, 12);
+ int lineHeight = state.GetValue<int>(4, pixelSize + 3);
+ Color32 color = state.GetValue<Color32>(5, Color32::white);
+ int anchor = state.GetValue<int>(6, TextAnchor_UpperLeft);
+ int alignment = state.GetValue<int>(7, TextAlignment_Left);
+ bool wordwrap = state.GetValue<bool>(8, false);
+ int preferred = state.GetValue<int>(9, 0);
+ int encoding = state.GetValue<int>(10, EEncoding::Encoding_UTF8);
+
+ s_Codepoints->clear();
+ InvokeWhenLeave([]() {
+ s_Codepoints->clear();
+ });
+
+ if (encoding == EEncoding::Encoding_UTF8)
+ {
+ while (*buf != 0) {
+ int err;
+ s_Codepoints->push_back(utf8::getu8c(&buf, &err));
+ if (err != 0)
+ {
+ log_warning("Illegal utf8 bytes %d", err);
+ }
+ }
+ }
+ else if (encoding == EEncoding::Encoding_UTF16)
+ {
+ while (*buf != 0) {
+ unsigned short* s = (unsigned short*)(buf);
+ s_Codepoints->push_back(*s);
+ buf += 2;
+ }
+ }
+ else if (encoding == EEncoding::Encoding_ASCII)
+ {
+ while (*buf != 0) {
+ s_Codepoints->push_back(*buf);
+ buf += 1;
+ }
+ }
+
+ font->RenderCharacters(*s_Codepoints, pixelSize);
+
+ UnicodeString str;
+ str.str = s_Codepoints->data();
+ str.length = s_Codepoints->size();
+
+ WipeGLError();
+
+ const UITextMesh* tm = g_TextMeshGenerator.GetTextMesh(str, font, pixelSize, lineHeight, color, (ETextAnchor)anchor, (ETextAlignment)alignment, wordwrap, preferred);
+ tm->Draw();
+
+ WipeGLError();
+
+ return 0;
+}
+
+#undef DrawLine
+// GUI.DrawLine(from, to)
+static int DrawLine(lua_State* L)
+{
+ LUA_BIND_STATE(L);
+
+ Vector2f from, to;
+ from.RestoreFromLuaObject(state, 1);
+ to.RestoreFromLuaObject(state, 2);
+
+ UILine line = UILine(from, to);
+ line.Draw();
+
+ return 0;
+}
+
+static luaL_Reg guiFuncs[] = {
+ { "DrawText", DrawText },
+ { "DrawLine", DrawLine },
+ {0, 0}
+};
int luaopen_GameLab_Engine_GUI(lua_State* L)
{
@@ -21,6 +122,8 @@ int luaopen_GameLab_Engine_GUI(lua_State* L)
state.RegisterNativeClass<Font>();
+ state.RegisterMethods(guiFuncs);
+
LUA_BIND_REGISTER_ENUM(state, "EEncoding",
{ "ASCII", Encoding_ASCII },
{ "UTF8", Encoding_UTF8 },