summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Data/Resources/Shaders/BaseColor.glsl43
-rw-r--r--Data/Resources/Shaders/Editor-Shape.glsl3
-rw-r--r--Data/Resources/Shaders/Editor-Text.glsl3
-rw-r--r--Data/Scripts/Editor/AssetBrowser.lua4
-rw-r--r--Runner/README.txt1
-rw-r--r--Runtime/Debug/Log.cpp6
-rw-r--r--Runtime/GUI/Font.cpp21
-rw-r--r--Runtime/GUI/Font.h1
-rw-r--r--Runtime/GUI/UITextMesh.cpp67
-rw-r--r--Runtime/GUI/UITextMesh.h3
-rw-r--r--Runtime/Math/Math.h5
-rw-r--r--Runtime/Math/MathHelper.h2
-rw-r--r--Runtime/Math/Vector2.h34
-rw-r--r--Runtime/Scripting/GUI/Font.bind.cpp10
14 files changed, 118 insertions, 85 deletions
diff --git a/Data/Resources/Shaders/BaseColor.glsl b/Data/Resources/Shaders/BaseColor.glsl
deleted file mode 100644
index b1aed17..0000000
--- a/Data/Resources/Shaders/BaseColor.glsl
+++ /dev/null
@@ -1,43 +0,0 @@
-#version 330 core
-
-VSH_BEGIN
-
-vec3 vPos;
-vec3 vNormal;
-vec4 vTangent;
-vec4 vColor;
-vec2 vUV;
-vec2 vUV2;
-vec2 vUV3;
-vec2 vUV4;
-
-uniform mat4 gamelab_mat_model;
-uniform mat4 gamelab_mat_vp;
-uniform vec4 gamelab_screen;
-uniform vec4 gamelab_time;
-
-uniform mat4 mvp;
-
-out vec2 uv;
-
-void main()
-{
- vec4 clip = mvp * vec4(vPos, -1, 1.0);
- gl_Position = clip;
- uv = vUV;
-}
-
-VSH_END
-
-FSH_BEGIN
-uniform sampler2D uiTex;
-
-in vec2 uv;
-
-out vec4 FragColor;
-
-void main()
-{
- FragColor = texture(uiTex, uv);
-}
-FSH_END
diff --git a/Data/Resources/Shaders/Editor-Shape.glsl b/Data/Resources/Shaders/Editor-Shape.glsl
index 7548507..235390c 100644
--- a/Data/Resources/Shaders/Editor-Shape.glsl
+++ b/Data/Resources/Shaders/Editor-Shape.glsl
@@ -1,6 +1,5 @@
-// 渲染编辑器文字
-
#version 330 core
+//绘制几何图形
CMD_BEGIN
Cull Off
diff --git a/Data/Resources/Shaders/Editor-Text.glsl b/Data/Resources/Shaders/Editor-Text.glsl
index 03ec5a7..d1003c3 100644
--- a/Data/Resources/Shaders/Editor-Text.glsl
+++ b/Data/Resources/Shaders/Editor-Text.glsl
@@ -39,7 +39,8 @@ void main()
//vec2 uv = vec2(uv.x, 1 - uv.y);
vec4 sampled = vec4(0.8,0.8,0.8,texture(gamelab_main_tex, uv).r);
sampled *= color;
- //sampled = vec4(1,1,1,1);
+ // if(sampled.a == 0)
+ // sampled = vec4(1,0,0,1);
FragColor = sampled;
}
FSH_END
diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua
index 89d4eba..7a41e85 100644
--- a/Data/Scripts/Editor/AssetBrowser.lua
+++ b/Data/Scripts/Editor/AssetBrowser.lua
@@ -35,10 +35,10 @@ AssetBrowser.OnGUI = function(self)
Engine.Rendering.UseShader(shader)
Engine.Rendering.SetMatrix44("gamelab_mat_mvp", ortho)
- Engine.Rendering.SetVector2("gamelab_ui_position", {0, 30})
+ Engine.Rendering.SetVector2("gamelab_ui_position", {0, 100})
--Engine.Rendering.SetTexture("gamelab_main_tex", tex)
--Engine.Rendering.DrawUIQuad({0, 0, 200, 200})
- _G["default_font"]:GetCharacters("你好世界!Hello,World! Project Window Properties", 12)
+ _G["default_font"]:GetCharacters("你好世界!\nHello,World!\nProject Window Properties", 12)
--Engine.Rendering.DrawUI9Slicing(1, {25, 25}, {25, 25}, {80, 80}, {400, 50} )
Engine.Rendering.ResetUniformState()
end
diff --git a/Runner/README.txt b/Runner/README.txt
new file mode 100644
index 0000000..14a625f
--- /dev/null
+++ b/Runner/README.txt
@@ -0,0 +1 @@
+游戏运行时 \ No newline at end of file
diff --git a/Runtime/Debug/Log.cpp b/Runtime/Debug/Log.cpp
index c44d7af..301e15a 100644
--- a/Runtime/Debug/Log.cpp
+++ b/Runtime/Debug/Log.cpp
@@ -101,7 +101,7 @@ void log_info_tag(const char* tag, const char* fmt, ...)
SetOutputColor(0);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Info] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Info] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
@@ -114,7 +114,7 @@ void log_warning_tag(const char* tag, const char* fmt, ...)
SetOutputColor(1);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Warning] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Warning] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
@@ -127,7 +127,7 @@ void log_error_tag(const char* tag, const char* fmt, ...)
SetOutputColor(2);
va_list pArgs = NULL;
va_start(pArgs, fmt);
- printf("%s [Error] %s ", currentDateTime().c_str(), tag);
+ printf("%s [Error] [%s] ", currentDateTime().c_str(), tag);
vprintf(fmt, pArgs);
printf("\n");
va_end(pArgs);
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp
index 1993126..4425b17 100644
--- a/Runtime/GUI/Font.cpp
+++ b/Runtime/GUI/Font.cpp
@@ -7,11 +7,14 @@
using namespace character;
-//https://unicode-table.com/en/#cjk-unified-ideographs-extension-a
-//https://learnopengl.com/In-Practice/Text-Rendering
-//https://www.zhihu.com/question/294660079
-//https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078
-//https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme
+/*
+* http://madig.github.io/freetype-web/documentation/tutorial/
+* https://unicode-table.com/en/#cjk-unified-ideographs-extension-a
+* https://learnopengl.com/In-Practice/Text-Rendering
+* https://www.zhihu.com/question/294660079
+* https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078
+* https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme
+*/
static std::string s_FontError;
@@ -161,7 +164,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
int h = m_FTFace->glyph->bitmap.rows;
const unsigned char* pixels = m_FTFace->glyph->bitmap.buffer;
- if (pixels != NULL) // ǿո
+ if (pixels != NULL) // Щspacetabûε
{
s_PixelBuffer.resize(w * h);
if (m_FTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
@@ -183,7 +186,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
memcpy(&s_PixelBuffer[0], pixels, s_SizePerPixel * w * h);
}
- //TextHelper::print_glyph(&s_PixelBuffer[0], w, h);
+ TextHelper::print_glyph(&s_PixelBuffer[0], w, h);
GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h));
Assert(atlas);
@@ -208,7 +211,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
character.position = rect;
character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top);
}
- else // ո
+ else // spacetab
{
character.atlas = FONT_NOT_IN_ATLAS_PLACEHOLDER;
character.position = Rect(0,0,0,0);
@@ -295,7 +298,7 @@ GlyphAtals* Font::RequestAtlas(int pixelSize, Internal::Vector2 preferSize)
Texture* Font::CreateAtlas()
{
TextureSetting setting;
- setting.filterMode = ETextureFilterMode::Linear;
+ setting.filterMode = ETextureFilterMode::Nearest;
setting.wrapMode = ETextureWrapMode::Clamp;
setting.format = ETextureFormat::R8;
setting.type = ETextureType::TEX_2D;
diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h
index 5fbe981..cd546af 100644
--- a/Runtime/GUI/Font.h
+++ b/Runtime/GUI/Font.h
@@ -93,6 +93,7 @@ enum EEncoding
Encoding_UTF16,
};
+// ûΣadvance֣spacetab
#define FONT_NOT_IN_ATLAS_PLACEHOLDER (INT_MAX)
//
diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp
index abb5231..94e61c4 100644
--- a/Runtime/GUI/UITextMesh.cpp
+++ b/Runtime/GUI/UITextMesh.cpp
@@ -29,6 +29,7 @@ static unsigned int s_IndicesPerText;
struct TextInfo {
const Character* ch;
float offset;
+ int line; // 0ʼ
};
static unordered_map<unsigned int, vector<TextInfo>> s_TextInfos;
@@ -59,27 +60,56 @@ InitializeStaticVariables([]() {
// *
// * ɫ
-UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
+// lineheight Сê
+// wordwrap Զ
+// preferredSize ԶС
+UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor, ETextAlignment alignment, bool wordwrap, float preferred)
{
m_Font = font;
s_TextInfos.clear();
const Vector2 atlasSize = font->GetAtlasSize();
+ Vector2 textRegion;
// ղͬatlasൽs_TextInfos
float offset = 0;
+ int line = 0;
for (int i = 0; i < str.length; ++i)
{
character::Unicode c = str.str[i];
const Character* ch = font->GetCharacter(c, pixelSize);
if (ch == NULL)
continue;
+
+ if (wordwrap) // Զ
+ {
+ if (offset + ch->bearing.x + ch->position.width > preferred)
+ {
+ ++line;
+ offset = 0;
+ }
+ }
+
unsigned int atlasIndex = ch->atlas;
-
if (atlasIndex != FONT_NOT_IN_ATLAS_PLACEHOLDER) //ǿո
{
+
+// зUnix'\n', Windows'\r\n', MacOS '\r'
+#define CHECK_BREAKS() \
+ if (c == '\n' || c == '\r') \
+ { \
+ ++line; \
+ offset = 0; \
+ if (c == '\r' && ((i + 1) < str.length && str.str[i + 1] == '\n')) /*skip\n*/ \
+ ++i; \
+ continue; \
+ }
+
+ CHECK_BREAKS();
+
TextInfo info;
info.ch = ch;
info.offset = offset;
+ info.line = line;
auto list = s_TextInfos.find(atlasIndex);
if (list == s_TextInfos.end())
@@ -88,13 +118,37 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText
vector<TextInfo>& v = s_TextInfos[atlasIndex];
v.push_back(info);
}
+ else
+ {
+ // Щ廻ûΣҲҪﴦ
+ CHECK_BREAKS();
+ }
offset += ch->advance;
+
+ textRegion.x = max(offset, textRegion.x);
}
+ textRegion.y = (line + 1) * lineHeight;
+
if (s_TextInfos.size() == 0)
return;
+ Vector2i textOffset;
+ Vector2 halfRegion = Vector2(textRegion.x/ 2.f, textRegion.y / 2.f);
+ switch (anchor)
+ {
+ case TextAnchor_UpperLeft: textOffset.Set(0, 0); break;
+ case TextAnchor_UpperCenter: textOffset.Set(-halfRegion.x, 0); break;
+ case TextAnchor_UpperRight: textOffset.Set(-textRegion.x, 0); break;
+ case TextAnchor_MiddleLeft: textOffset.Set(0, -halfRegion.y); break;
+ case TextAnchor_MiddleCenter: textOffset.Set(-halfRegion.x, -halfRegion.y); break;
+ case TextAnchor_MiddleRight: textOffset.Set(-textRegion.x, -halfRegion.y); break;
+ case TextAnchor_LowerLeft: textOffset.Set(0, -textRegion.y); break;
+ case TextAnchor_LowerCenter: textOffset.Set(-halfRegion.x, -textRegion.y); break;
+ case TextAnchor_LowerRight: textOffset.Set(-textRegion.x, -textRegion.y); break;
+ }
+
// VBOIBO
for (auto iter : s_TextInfos) {
unsigned int atlasIndex = iter.first; // atlas atlasIndex
@@ -113,12 +167,13 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, EText
TextInfo& text = texts[i];
int vOff = i * s_VertexPerText;
+ int lineOff = text.line * lineHeight;
// Ͻԭ
float pos[] = {
- 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
+ textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
+ textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right
+ textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-right
+ textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-left
};
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[] = {
diff --git a/Runtime/GUI/UITextMesh.h b/Runtime/GUI/UITextMesh.h
index c88e52f..487bccd 100644
--- a/Runtime/GUI/UITextMesh.h
+++ b/Runtime/GUI/UITextMesh.h
@@ -17,7 +17,6 @@ enum ETextAnchor
TextAnchor_LowerLeft,
TextAnchor_LowerCenter,
TextAnchor_LowerRight,
- TextAnchor_DontCare
};
enum ETextAlignment {
@@ -37,7 +36,7 @@ namespace TextHelper
class UITextMesh
{
public:
- UITextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/;
+ UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor = TextAnchor_UpperLeft, ETextAlignment alignment = TextAlignment_Left, bool wordwrap = false, float preferred = 0)/*throw TextMeshException*/;
~UITextMesh();
diff --git a/Runtime/Math/Math.h b/Runtime/Math/Math.h
index 5e08613..80f23f7 100644
--- a/Runtime/Math/Math.h
+++ b/Runtime/Math/Math.h
@@ -8,8 +8,11 @@
#include "Rect.h"
#include "MathHelper.h"
-typedef Internal::Vector2 Vector2;
typedef Internal::Vector3 Vector3;
typedef Internal::Vector4 Vector4;
typedef Internal::Matrix44 Matrix44;
typedef Internal::Rect Rect;
+
+typedef Internal::Vector2T<int> Vector2i;
+typedef Internal::Vector2T<float> Vector2f;
+
diff --git a/Runtime/Math/MathHelper.h b/Runtime/Math/MathHelper.h
index c41eec9..ad64dcf 100644
--- a/Runtime/Math/MathHelper.h
+++ b/Runtime/Math/MathHelper.h
@@ -1,7 +1,7 @@
#pragma once
#define max(a, b)\
-(a)>(b)?(a):(b)
+((a)>(b)?(a):(b))
#define min(a, b)\
(a)<(b)?(a):(b)
diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h
index 9d7e4e9..f13e522 100644
--- a/Runtime/Math/Vector2.h
+++ b/Runtime/Math/Vector2.h
@@ -4,47 +4,59 @@
namespace Internal
{
- struct Vector2
+ template<typename T>
+ struct Vector2T
{
- Vector2(float x = 0, float y = 0)
+ Vector2T(T x = 0, T y = 0)
{
this->x = x;
this->y = y;
}
- inline void Set(float x, float y)
+ inline void Set(T x, T y)
{
this->x = x;
this->y = y;
}
- Vector2 Clamp(float xmin, float xmax, float ymin, float ymax)
+ Vector2T Clamp(T xmin, T xmax, T ymin, T ymax)
{
- Vector2 v;
+ Vector2T v;
v.x = clamp(x, xmin, xmax);
v.y = clamp(y, ymin, ymax);
return v;
}
- float operator[](int i)
+ T operator[](int i)
{
if (i == 0) return x;
else if (i == 1) return y;
Assert(false);
}
- bool operator == (const Vector2& v) const
+ bool operator == (const Vector2T& v) const
{
return v.x == x && v.y == y;
}
- bool operator != (const Vector2& v) const
+ bool operator != (const Vector2T& v) const
{
return v.x != x || v.y != y;
}
- float x, y;
+ T x, y;
- static Vector2 zero;
- static Vector2 one;
+ static Vector2T<T> zero;
+ static Vector2T<T> one;
};
+
+ template<typename T>
+ Vector2T<T> Vector2T<T>::zero = Vector2T(0, 0);
+ template<typename T>
+ Vector2T<T> Vector2T<T>::one = Vector2T(1, 1);
+
+ typedef Internal::Vector2T<float> Vector2;
+
}
+
+typedef Internal::Vector2T<float> Vector2;
+
diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp
index 7172027..e749303 100644
--- a/Runtime/Scripting/GUI/Font.bind.cpp
+++ b/Runtime/Scripting/GUI/Font.bind.cpp
@@ -88,15 +88,17 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacter)
return 1;
}
-// font:GetCharacter(str, size, encoding)
+// font:GetCharacter(str, size, wordwrap, preferredWidth, encoding)
LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
{
LUA_BIND_PREPARE(L, Font);
- LUA_BIND_CHECK(L, "USN*");
+ //LUA_BIND_CHECK(L, "US");
char* buf = (char*)state.GetValue<const char*>(2, "");
int size = state.GetValue<int>(3, 12);
- int encoding = state.GetValue<int>(4, EEncoding::Encoding_UTF8);
+ bool wordwrap = state.GetValue<bool>(4, false);
+ float preferred = state.GetValue<float>(5, 0);
+ int encoding = state.GetValue<int>(6, EEncoding::Encoding_UTF8);
s_Codepoints->clear();
if (encoding == EEncoding::Encoding_UTF8)
@@ -134,7 +136,7 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
WipeGLError();
- UITextMesh* tm = new UITextMesh(str, self, size, ETextAnchor::TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left);
+ UITextMesh* tm = new UITextMesh(str, self, size, size + 3, TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left, wordwrap, preferred);
tm->Draw();
return 0;