From 2ab7fad7b308debba0aacbf76831569f360d19a0 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 18 Nov 2021 19:14:35 +0800 Subject: *misc --- .../BuiltIn/Libraries/GameLab/BuiltInResources.lua | 34 +++++++ .../BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua | 33 +++++-- .../Libraries/GameLab/Engine/Rendering/Texture.lua | 3 + .../Libraries/GameLab/Engine/Rendering/init.lua | 8 ++ Data/BuiltIn/Libraries/GameLab/Entity.lua | 11 --- Data/BuiltIn/Libraries/GameLab/Node.lua | 11 +++ Data/BuiltIn/Libraries/GameLab/define.lua | 6 ++ Data/BuiltIn/Libraries/GameLab/init.lua | 19 +++- Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl | 34 +++++++ Data/BuiltIn/Resources/Shaders/Editor-Text.glsl | 46 +++++++++ Data/BuiltIn/Resources/Shaders/Editor-UI.glsl | 45 +++++++++ Data/Libraries/GameLab/Editor/GUI/init.lua | 5 + .../GameLab/Editor/Window/ContainerWindow.lua | 9 ++ Data/Libraries/GameLab/Editor/Window/GUIWindow.lua | 21 +++-- Data/Libraries/GameLab/Editor/Window/init.lua | 20 ++-- Data/Scripts/EditorApplication.lua | 7 +- Editor/Scripting/EditorScripting.cpp | 2 + Editor/Scripting/GUI/EditorGUI.bind.cpp | 105 +++++++++++++++++++++ Editor/Scripting/Window/Window.bind.cpp | 69 -------------- Projects/VisualStudio/Editor/Editor.vcxproj | 3 + .../VisualStudio/Editor/Editor.vcxproj.filters | 12 +++ Runtime/GUI/Font.cpp | 12 ++- Runtime/GUI/Font.h | 2 +- Runtime/GUI/UISquare.cpp | 52 ++++++++++ Runtime/GUI/UISquare.h | 22 +++++ 25 files changed, 477 insertions(+), 114 deletions(-) create mode 100644 Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/Engine/Rendering/Texture.lua delete mode 100644 Data/BuiltIn/Libraries/GameLab/Entity.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/Node.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/define.lua create mode 100644 Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl create mode 100644 Data/BuiltIn/Resources/Shaders/Editor-Text.glsl create mode 100644 Data/BuiltIn/Resources/Shaders/Editor-UI.glsl create mode 100644 Data/Libraries/GameLab/Editor/GUI/init.lua create mode 100644 Editor/Scripting/GUI/EditorGUI.bind.cpp create mode 100644 Runtime/GUI/UISquare.cpp create mode 100644 Runtime/GUI/UISquare.h diff --git a/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua b/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua new file mode 100644 index 0000000..03da57e --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua @@ -0,0 +1,34 @@ +local find = GameLab.Find +local Shader = find "GameLab.Engine.Rendering.Shader" +local Texture = find "GameLab.Engine.Rendering.Texture" +local define = find "GameLab.define" +local Rendering = find "GameLab.Engine.Rendering" + +local SHADER = function(path) + local file = define.builtInPath .. '/Resources/Shaders/' .. path + return Rendering.CreateShaderFromFile(file) +end + +local TEXTURE = function(path) + -- local file = define.builtInPath .. '/Resources/Images/' .. path + -- return Rendering.CreateShaderFromFile(file) +end + +local res = {} + +local loadRes = function() + res.shaders = { + ["EditorShape"] = SHADER "Editor-Shape.glsl", + ["EditorText"] = SHADER "Editor-Text.glsl", + ["EditorUI"] = SHADER "Editor-UI.glsl", + } + + res.textures = { + ["White"] = TEXTURE "white.png", + } +end + +-- 加载默认资源 +GameLab.onApplicationStart:Add(loadRes) + +return res \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua index 3030b38..c2677bc 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua @@ -1,10 +1,17 @@ -local Debug = GameLab.Debug - local GUI = GameLab.Package("GameLab.Engine.GUI") -local GUIState = GUI.GUIState +local find = GameLab.Find -local Event = GameLab.Events.Event +local Res = require "GameLab.BuiltInResources" +local EditorGUI = GameLab.Editor.GUI + +local Debug = GameLab.Debug +local GUIState = GUI.GUIState +local Event = GameLab.Events.Event +local shaders = Res.shaders +local GL = GameLab.Engine.GL +local Matrix44 = find "GameLab.Engine.Math.Matrix44" +local Rendering = GameLab.Engine.Rendering GUI.BeginOnGUI = function() GUIState.ResetControlID() @@ -37,13 +44,23 @@ end GUI.Label = function() end - -GUI.Box = function() - +local shader +GUI.Box = function(position, color, size) + --Rendering.UseShader(Res.shaders["EditorShape"]) + local ortho = Matrix44() + ortho:SetOrtho(0, size.x, size.y, 0, 0.1, 10) + if shader == nil then + shader = Rendering.Shader.CreateFromFile("./Resources/Shaders/Editor-Text.glsl") + end + + Rendering.UseShader(shader) + Rendering.SetMatrix44("gamelab_mat_mvp", ortho) + Rendering.SetVector2("gamelab_ui_position", {0, 0}) + EditorGUI.Text(_G["default_font"], "你好世界!\nMaterials\nHello,World!\nProject Window Properties", 12) end GUI.HorizontalSlider = function() - + end GUI.VerticalSlider = function() diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/Texture.lua b/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/Texture.lua new file mode 100644 index 0000000..4623f27 --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/Texture.lua @@ -0,0 +1,3 @@ +local Texture = GameLab.Engine.Rendering.Texture or {} + +return Texture \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua b/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua index 9966ea2..daa8bda 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua @@ -21,4 +21,12 @@ m.SetTexture = Shader.SetTexture m.UseShader = Shader.Use m.UnuseShader = Shader.Unuse +m.CreateShaderFromFile = function( path ) + local glsl = GameLab.IO.ReadFile(path, GameLab.IO.EFileMode.Text) + if glsl ~= nil then + local shader = Shader.New(glsl) + return shader + end +end + return m \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Entity.lua b/Data/BuiltIn/Libraries/GameLab/Entity.lua deleted file mode 100644 index 4da28f9..0000000 --- a/Data/BuiltIn/Libraries/GameLab/Entity.lua +++ /dev/null @@ -1,11 +0,0 @@ ---https://stackoverflow.com/questions/27897714/whats-the-distinction-between-an-entity-and-a-game-object --- 游戏中的实体 -local Entity = GameLab.GlobalClass("GameLab.Entity") - -Entity.Ctor = function(self) - self.m_Components = {} - -end - - -return Entity \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Node.lua b/Data/BuiltIn/Libraries/GameLab/Node.lua new file mode 100644 index 0000000..7eab18a --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/Node.lua @@ -0,0 +1,11 @@ +--https://stackoverflow.com/questions/27897714/whats-the-distinction-between-an-Node-and-a-game-object +-- 游戏中的实体 +local Node = GameLab.GlobalClass("GameLab.Node") + +Node.Ctor = function(self) + self.m_Components = {} -- node properties + +end + + +return Node \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/define.lua b/Data/BuiltIn/Libraries/GameLab/define.lua new file mode 100644 index 0000000..dd4420e --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/define.lua @@ -0,0 +1,6 @@ + +local define = {} + +define.builtInPath = "./BuiltIn" + +return define diff --git a/Data/BuiltIn/Libraries/GameLab/init.lua b/Data/BuiltIn/Libraries/GameLab/init.lua index de60ca3..2912d68 100644 --- a/Data/BuiltIn/Libraries/GameLab/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/init.lua @@ -14,8 +14,20 @@ GameLab.Import = function(packageName) return import end +local gfind = function (name) + local t = _G + local pkgs = string.gmatch(name, "%.*(%w+)%.*") + for pkg in pkgs do + t = t[pkg] + if t == nil then + return nil + end + end + return t +end + GameLab.Find = function(name) - local m = _G[name] + m = require(name) if m ~= nil then return m end @@ -23,14 +35,13 @@ GameLab.Find = function(name) if m ~= nil then return m end - m = require(name) + local m = gfind(name) return m end GameLab.Include = GameLab.Find local import = GameLab.Import(...) - import "Package" import "Class" import "GlobalClass" @@ -42,4 +53,6 @@ import "EventListener" import "StaticClass" import "GlobalStaticClass" +GameLab.onApplicationStart = GameLab.Delegate() + return GameLab \ No newline at end of file diff --git a/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl b/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl new file mode 100644 index 0000000..5ce755f --- /dev/null +++ b/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl @@ -0,0 +1,34 @@ +#version 330 core +//绘制纯色的几何图形 + +CMD_BEGIN +Cull Off +Blend SrcAlpha OneMinusSrcAlpha +DepthTest Off +CMD_END + +uniform mat4 gamelab_mat_mvp; +uniform sampler2D gamelab_main_tex; +uniform vec2 gamelab_ui_position; +uniform vec4 gamelab_color; + +VSH_BEGIN +layout (location = 0) in vec2 vPos; + +void main() +{ + vec2 pos = vPos + gamelab_ui_position; + vec4 clipPos = gamelab_mat_mvp * vec4(pos, -1, 1.0); + gl_Position = clipPos; +} +VSH_END + +FSH_BEGIN + +out vec4 FragColor; + +void main() +{ + FragColor = gamelab_color; +} +FSH_END diff --git a/Data/BuiltIn/Resources/Shaders/Editor-Text.glsl b/Data/BuiltIn/Resources/Shaders/Editor-Text.glsl new file mode 100644 index 0000000..d1003c3 --- /dev/null +++ b/Data/BuiltIn/Resources/Shaders/Editor-Text.glsl @@ -0,0 +1,46 @@ +#version 330 core + +CMD_BEGIN +Cull Off +Blend SrcAlpha OneMinusSrcAlpha +DepthTest Off +CMD_END + +uniform mat4 gamelab_mat_mvp; +uniform sampler2D gamelab_main_tex; +uniform vec2 gamelab_ui_position; + +VSH_BEGIN +layout (location = 0) in vec2 vPos; +layout (location = 1) in vec2 vUV; +layout (location = 2) in vec4 vColor; + +out vec2 uv; +out vec4 color; + +void main() +{ + vec2 pos = vPos + gamelab_ui_position; + vec4 clip = gamelab_mat_mvp * vec4(pos, -1, 1.0); + gl_Position = clip; + uv = vUV; + color = vColor; +} +VSH_END + +FSH_BEGIN +in vec2 uv; +in vec4 color; + +out vec4 FragColor; + +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; + // if(sampled.a == 0) + // sampled = vec4(1,0,0,1); + FragColor = sampled; +} +FSH_END diff --git a/Data/BuiltIn/Resources/Shaders/Editor-UI.glsl b/Data/BuiltIn/Resources/Shaders/Editor-UI.glsl new file mode 100644 index 0000000..b2b348b --- /dev/null +++ b/Data/BuiltIn/Resources/Shaders/Editor-UI.glsl @@ -0,0 +1,45 @@ +#version 330 core + +CMD_BEGIN +Cull Off +Blend SrcAlpha OneMinusSrcAlpha +DepthTest Off +CMD_END + +uniform mat4 gamelab_mat_mvp; +uniform sampler2D gamelab_main_tex; +uniform vec2 gamelab_ui_position; + +VSH_BEGIN +layout (location = 0) in vec2 vPos; +layout (location = 1) in vec2 vUV; +layout (location = 2) in vec4 vColor; + +out vec2 uv; +out vec4 color; + +void main() +{ + vec2 pos = vPos + gamelab_ui_position; + vec4 clip = gamelab_mat_mvp * vec4(pos, -1, 1.0); + gl_Position = clip; + uv = vUV; + color = vColor; +} +VSH_END + +FSH_BEGIN +in vec2 uv; +in vec4 color; + +out vec4 FragColor; + +void main() +{ + //vec2 uv = vec2(uv.x, 1 - uv.y); + vec4 sampled = texture(gamelab_main_tex, uv); + // sampled *= color; + //sampled = vec4(1,1,1,1); + FragColor = sampled; +} +FSH_END diff --git a/Data/Libraries/GameLab/Editor/GUI/init.lua b/Data/Libraries/GameLab/Editor/GUI/init.lua new file mode 100644 index 0000000..0f01d9f --- /dev/null +++ b/Data/Libraries/GameLab/Editor/GUI/init.lua @@ -0,0 +1,5 @@ +local GUI = GameLab.Package("GameLab.Editor.GUI") + + + +return GUI \ No newline at end of file diff --git a/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua b/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua index bc7cd82..b709982 100644 --- a/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua @@ -8,6 +8,7 @@ local ContainerWindow = GameLab.GlobalClass("GameLab.Editor.Window.ContainerWind ContainerWindow.Ctor = function(self, position, showMode, min, max) self.m_Native = NativeContainWidow.New(self, position, showMode, min, max) + self.m_RootSplitWindow = Window.SplitWindow.New(Window.ESplitMode.Horizontal) end @@ -15,6 +16,14 @@ ContainerWindow.get_native = function(self) return self.m_Native end +ContainerWindow.get_rootSplitWindow = function(self) + return self.m_RootSplitWindow +end + +ContainerWindow.set_rootSplitWindow = function(self, splitWindow) + self.m_RootSplitWindow = splitWindow +end + ContainerWindow.SetTitle = function(self, title) self.m_Native:SetTitle(title) end diff --git a/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua index 83e25c3..e2ceee5 100644 --- a/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua @@ -1,5 +1,3 @@ - -local GUIWindow = GameLab.GlobalClass("GameLab.Editor.Window.GUIWindow") local NativeGUIWindow = GameLab.Editor.Window.Internal.GUIWindow local inspect = require "inspect" @@ -9,10 +7,12 @@ local Math = require "GameLab.Engine.Math" local Rendering = require "GameLab.Engine.Rendering" local Utils = require "GameLab.Utils" local Events = require "GameLab.Events" +local GUI = require "GameLab.Engine.GUI" local Rect = Math.Rect local Event = Events.Event local Vector2 = Math.Vector2 +local Color = Rendering.Color local clone = Utils.Clone @@ -24,8 +24,13 @@ local col = { {0, 1, 1, 1}, } -GUIWindow.Ctor = function(self) +local kSideBorders = 2 -- GUIView的右边距 +local kTabHeight = 17 -- 标题栏高度 +local kBottomBorders = 2 -- 底部边距 + +local GUIWindow = GameLab.GlobalClass("GameLab.Editor.Window.GUIWindow", function(self) self.m_Native = NativeGUIWindow.New(self) -- native guiwindow + self.m_ContainerWindow = nil self.m_SplitWindow = nil -- parent window self.m_Position = Rect(0,0,0,0) -- 在父ContainerWindow中的位置和大小 @@ -34,7 +39,7 @@ GUIWindow.Ctor = function(self) i = i + 1 self.m_ClearColor = col[i] -end +end ) GUIWindow.get_native = function(self) return self.m_Native @@ -98,11 +103,15 @@ GUIWindow.OnGUI = function(self) local event = Event.current - if self.splitWindow ~= nil and event ~= nil then + local sp = self.splitWindow + if sp ~= nil and event ~= nil then local e = clone(event) e.mousePosition:Add(self.position.xy) -- 坐标转换到全局containerWindow的坐标 - self.splitWindow:DoSplit(e) + sp:DoSplit(e) end + + GUI.Box(Rect(), Color(), self.position.size) + end GUIWindow.GetContainerWindow = function(self) diff --git a/Data/Libraries/GameLab/Editor/Window/init.lua b/Data/Libraries/GameLab/Editor/Window/init.lua index 84cb46d..13c75bb 100644 --- a/Data/Libraries/GameLab/Editor/Window/init.lua +++ b/Data/Libraries/GameLab/Editor/Window/init.lua @@ -6,16 +6,16 @@ import "GUIWindow" import "SplitWindow" --[[ -GameLab编辑器窗口 - -窗口层级 触发事件 逻辑窗口 数量 说明 -ContainerWindow 是 [1,n] 有唯一的一个MainWindow,可以有多个其余ShowMode的窗口 -|- SplitWindow 是 1:1 一个ContainerWindow下只有一个root SplitWindow,然后以树形结构嵌套子窗口GUIWindow或SplitWindow - |- GUIWindow 是 1:[n,0] 一个SplitWindow下可能有GUIWindow,也可能是SplitWindow - |- EditorWindow 是 1:[n,1] 一个GUIWindow下至少有一个EditorWindow - -ContinerWindow和GUIWindow是windows事件产生的窗口,SplitWindow和EditorWindow是逻辑窗口,本身不会 -产生输入事件 + GameLab编辑器窗口 + + 窗口层级 触发事件 逻辑窗口 数量 说明 + ContainerWindow 是 [1,n] 有唯一的一个MainWindow,可以有多个其余ShowMode的窗口 + |- SplitWindow 是 1:1 一个ContainerWindow下只有一个root SplitWindow,然后以树形结构嵌套子窗口GUIWindow或SplitWindow + |- GUIWindow 是 1:[n,0] 一个SplitWindow下可能有GUIWindow,也可能是SplitWindow + |- EditorWindow 是 1:[n,1] 一个GUIWindow下至少有一个EditorWindow + + ContinerWindow和GUIWindow是windows事件产生的窗口,SplitWindow和EditorWindow是逻辑窗口,本身不会 + 产生输入事件 ]] return m \ No newline at end of file diff --git a/Data/Scripts/EditorApplication.lua b/Data/Scripts/EditorApplication.lua index aaa0a5e..92df9ad 100644 --- a/Data/Scripts/EditorApplication.lua +++ b/Data/Scripts/EditorApplication.lua @@ -23,6 +23,11 @@ local mainWindow = Window.ContainerWindow.New({400, 400, 800, 500}, Window.EShow mainWindow:SetTitle("GameLab") mainWindow:SetIcon("./Data/Icon/GameLab.ico") +local font = Engine.GUI.Font.New("./Resources/Font/msyh.ttc", {512, 512}, 5, 5) +_G["default_font"] = font + +GameLab.onApplicationStart() + app:SetMainWindow(mainWindow.native) EditorWindowManager.SetMainWindow(mainWindow) @@ -61,8 +66,6 @@ local tex = Engine.Resource.LoadTexture("./Resources/Images/brickwall.jpg") local request = Engine.Resource.LoadImageDataAsync("./Resources/Images/brickwall.jpg") -local font = Engine.GUI.Font.New("./Resources/Font/msyh.ttc", {512, 512}, 5, 5) -_G["default_font"] = font local v = GameLab.Engine.Math.Vector2(10.002, 2.334) diff --git a/Editor/Scripting/EditorScripting.cpp b/Editor/Scripting/EditorScripting.cpp index 12f591c..48d7da8 100644 --- a/Editor/Scripting/EditorScripting.cpp +++ b/Editor/Scripting/EditorScripting.cpp @@ -18,6 +18,7 @@ extern int luaopen_GameLab_Engine_Resource(lua_State* L); // GameLab.Engine.Reso extern int luaopen_GameLab_Engine_GUI(lua_State* L); extern int luaopen_GameLab_Editor(lua_State* L); // GameLab.Editor +extern int luaopen_GameLab_Editor_GUI(lua_State* L); // GameLab.Editor.GUI extern int luaopen_GameLab_Editor_Window(lua_State* L); // GameLab.Editor.Window extern int luaopen_GameLab_Editor_Resource(lua_State* L); // GameLab.Editor.Resource extern int luaopen_GameLab_Editor_Profiling(lua_State* L); // GameLab.Editor.Profiling @@ -49,6 +50,7 @@ bool SetupGameLabEditorScripting(lua_State* L) openlib(luaopen_GameLab_Engine_GUI); openlib(luaopen_GameLab_Editor); + openlib(luaopen_GameLab_Editor_GUI); openlib(luaopen_GameLab_Editor_Window); return true; diff --git a/Editor/Scripting/GUI/EditorGUI.bind.cpp b/Editor/Scripting/GUI/EditorGUI.bind.cpp new file mode 100644 index 0000000..b51e019 --- /dev/null +++ b/Editor/Scripting/GUI/EditorGUI.bind.cpp @@ -0,0 +1,105 @@ +#include "Editor/GUI/EditorWindows.h" +#include "Runtime/GUI/Font.h" +#include "Runtime/Graphics/Shader.h" +#include "Runtime/Debug/Log.h" +#include "Runtime/Graphics/GfxDevice.h" +#include "Runtime/Common/DataBuffer.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" + +using namespace LuaBind; +using namespace Win; + +static std::vector* s_Codepoints; + +InitializeStaticVariables([]() { + s_Codepoints = new std::vector(); +}); + +// Editor.GUI.Text(font, str, pixelSize, lineHeight, color, anchor, alignment, wordwrap, preferred, encoding) +static int Text(lua_State* L) +{ + LUA_BIND_STATE(L); + + Font* font = (Font*)state.GetUserdata(1); + char* buf = (char*)state.GetValue(2, ""); + int pixelSize = state.GetValue(3, 12); + int lineHeight = state.GetValue(4, pixelSize + 3); + Color32 color = state.GetValue(5, Color32::white); + int anchor = state.GetValue(6, TextAnchor_UpperLeft); + int alignment = state.GetValue(7, TextAlignment_Left); + bool wordwrap = state.GetValue(8, false); + int preferred = state.GetValue(9, 0); + int encoding = state.GetValue(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(); + + return 0; +} + +static luaL_Reg guiFuncs[] = { + {"Text", Text}, + {0, 0} +}; + +int luaopen_GameLab_Editor_GUI(lua_State* L) +{ + log_info_tag("Scripting", "luaopen_GameLab_Editor_GUI()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Editor"); + state.PushNamespace("GUI"); + + state.RegisterMethods(guiFuncs); + + return 1; +} \ No newline at end of file diff --git a/Editor/Scripting/Window/Window.bind.cpp b/Editor/Scripting/Window/Window.bind.cpp index b720c98..a3610fc 100644 --- a/Editor/Scripting/Window/Window.bind.cpp +++ b/Editor/Scripting/Window/Window.bind.cpp @@ -15,74 +15,6 @@ using namespace LuaBind; using namespace Win; -static std::vector* s_Codepoints; - -InitializeStaticVariables([]() { - s_Codepoints = new std::vector(); -}); - -// Editor.GUI.Text(font, str, pixelSize, lineHeight, color, anchor, alignment, wordwrap, preferred, encoding) -static int Text(lua_State* L) -{ - LUA_BIND_STATE(L); - - Font* font = (Font*)state.GetUserdata(1); - char* buf = (char*)state.GetValue(2, ""); - int pixelSize = state.GetValue(3, 12); - int lineHeight = state.GetValue(4, pixelSize + 3); - Color32 color = state.GetValue(5, Color32::white); - int anchor = state.GetValue(6, TextAnchor_UpperLeft); - int alignment = state.GetValue(7, TextAlignment_Left); - bool wordwrap = state.GetValue(8, false); - int preferred = state.GetValue(9, 0); - int encoding = state.GetValue(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(); - - return 0; -} - static int SetCursor(lua_State* L) { LUA_BIND_STATE(L); @@ -95,7 +27,6 @@ static int SetCursor(lua_State* L) } static luaL_Reg guiFuncs[] = { - {"Text", Text}, {"SetCursor", SetCursor}, {0, 0} }; diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index f9f04eb..c76c890 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -165,6 +165,7 @@ + @@ -204,6 +205,7 @@ + @@ -304,6 +306,7 @@ + diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 9873bca..2324494 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -112,6 +112,9 @@ {80fdf19a-6158-4fc0-8a3c-c86d51730261} + + {e9c9252b-f302-4c98-bc36-df9e7d06e856} + @@ -415,6 +418,12 @@ Editor\Scripting\Window + + Editor\Scripting\GUI + + + Runtime\GUI + @@ -759,6 +768,9 @@ Runtime\Lua + + Runtime\GUI + diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index 207e658..b611126 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -23,6 +23,7 @@ static const int s_SizePerPixel = sizeof(unsigned char); Font::Font(std::string path, TextGeneratingSettings settings) : LuaBind::NativeClass() + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -43,6 +44,7 @@ Font::Font(std::string path, TextGeneratingSettings settings) Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) : LuaBind::NativeClass(vm) + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -63,6 +65,7 @@ Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) Font::Font(DataBuffer* db, TextGeneratingSettings settings) : LuaBind::NativeClass() + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -83,6 +86,7 @@ Font::Font(DataBuffer* db, TextGeneratingSettings settings) Font::Font(LuaBind::VM* vm, DataBuffer* db, TextGeneratingSettings settings) : LuaBind::NativeClass(vm) + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -112,12 +116,12 @@ character::Hash Font::GetHash(Unicode codepoint, int pixelSize) const Character* Font::GetCharacter(character::Unicode codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); - auto iter = m_Characters.find(hash); + auto iter = m_Characters.find(hash.hashCode); if (iter == m_Characters.end()) { if (RenderCharacter(codepoint, pixelSize)) { - iter = m_Characters.find(hash); + iter = m_Characters.find(hash.hashCode); } else { @@ -148,7 +152,7 @@ void Font::RenderCharacters(std::vector& codepoint, int pixe bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); - if (m_Characters.count(hash) != 0) + if (m_Characters.size() > 0 && m_Characters.count(hash.hashCode) != 0) return true; FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize); @@ -220,7 +224,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize) character.advance = m_FTFace->glyph->advance.x * 1/64.f; - m_Characters.insert(std::pair(hash, character)); + m_Characters.insert(std::pair(hash.hashCode, character)); return true; } diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index 4bcc70b..cf6e4ba 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -127,7 +127,7 @@ private: //------------------------------------------------------------------------- - std::unordered_map m_Characters; // Ⱦ + std::unordered_map m_Characters; // Ⱦ std::vector m_Atlases; // ǰеatlasfontȫӵȨlightuserdata std::unordered_map m_AtlasCache; // ҵõatlas diff --git a/Runtime/GUI/UISquare.cpp b/Runtime/GUI/UISquare.cpp new file mode 100644 index 0000000..214435e --- /dev/null +++ b/Runtime/GUI/UISquare.cpp @@ -0,0 +1,52 @@ +#include "../Graphics/GfxDevice.h" +#include "UISquare.h" +#include "Runtime/Math/Math.h" + +struct UISquareLayout +{ + Vector2 position; +}; + +static CustomVertexLayout layout; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UISquareLayout)); + + layout.attributes.push_back(POSITION); +}); + +void UISquare::Draw() +{ + const int nVerts = 4; + const int nIndices = 6; + + float pos[] = { + m_Left, m_Bottom, // left-bottom + m_Right, m_Bottom, // right-bottom + m_Right, m_Top, // right-top + m_Left, m_Top, // top-left + }; + + int indices[] = { + 0, 1, 3, // right-top + 1, 2, 3, // left-bottom + }; + + uint8* vb; + uint16* ib; + + g_SharedVBO.GetChunk(sizeof(UISquareLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + + UISquareLayout* dst = (UISquareLayout*)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(4, 6); + g_SharedVBO.DrawChunk(layout); +} \ No newline at end of file diff --git a/Runtime/GUI/UISquare.h b/Runtime/GUI/UISquare.h new file mode 100644 index 0000000..8de07f0 --- /dev/null +++ b/Runtime/GUI/UISquare.h @@ -0,0 +1,22 @@ +#pragma once +#include "../Utilities/StaticInitiator.h" +#include "UIMesh.h" + +// ɫ +class UISquare : public UIMesh +{ +public: + UISquare(float l, float r, float t, float b) + { + m_Left = l; + m_Right = r; + m_Top = t; + m_Bottom = b; + } + + void Draw() override; + +private: + float m_Left, m_Right, m_Top, m_Bottom; + +}; -- cgit v1.1-26-g67d0