diff options
author | chai <chaifix@163.com> | 2021-11-20 14:10:24 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-20 14:10:24 +0800 |
commit | 74ca8143a7c2352425d549b681b2838bda5ee218 (patch) | |
tree | 5aad35e7312b7c1b54769091b30f47d8ad747acb /Runtime/Scripting | |
parent | 850d9c034792b96e2ff5ff3bbfbcc30661757aed (diff) |
*misc
Diffstat (limited to 'Runtime/Scripting')
-rw-r--r-- | Runtime/Scripting/GUI/GUI.bind.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
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 }, |