From bb452bba78dc1870d6316b383180472fe3a8a06a Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 20 Nov 2021 17:32:55 +0800 Subject: *gui --- .../BuiltIn/Libraries/GameLab/BuiltInResources.lua | 3 - .../BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua | 75 +++++++++++----------- .../Libraries/GameLab/Engine/Rendering/init.lua | 1 + 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/Window/GUIWindow.lua | 14 +++- Data/Resources/Shaders/Editor-Shape.glsl | 16 +---- Data/Scripts/EditorResources.lua | 20 +++++- Runtime/Graphics/Shader.h | 2 +- Runtime/Lua/LuaHelper.cpp | 28 ++++++++ Runtime/Scripting/GUI/GUI.bind.cpp | 21 +++++- Runtime/Scripting/Rendering/Shader.bind.cpp | 17 ++++- 13 files changed, 135 insertions(+), 187 deletions(-) delete mode 100644 Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl delete mode 100644 Data/BuiltIn/Resources/Shaders/Editor-Text.glsl delete mode 100644 Data/BuiltIn/Resources/Shaders/Editor-UI.glsl diff --git a/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua b/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua index 03da57e..26cbbb3 100644 --- a/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua +++ b/Data/BuiltIn/Libraries/GameLab/BuiltInResources.lua @@ -18,9 +18,6 @@ local res = {} local loadRes = function() res.shaders = { - ["EditorShape"] = SHADER "Editor-Shape.glsl", - ["EditorText"] = SHADER "Editor-Text.glsl", - ["EditorUI"] = SHADER "Editor-UI.glsl", } res.textures = { diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua index 084cc81..3c99622 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua @@ -2,26 +2,26 @@ local GUI = GameLab.Package("GameLab.Engine.GUI") local find = GameLab.Find -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 -local EEventType = GameLab.Events.EEventType -local Resource = GameLab.Engine.Resource -local Vector2 = GameLab.Engine.Math.Vector2 -local Vector4 = GameLab.Engine.Math.Vector4 -local Color = Rendering.Color - -local EditorRes +local Debug = GameLab.Debug +local GUIState = GUI.GUIState +local Event = GameLab.Events.Event +local GL = GameLab.Engine.GL +local Matrix44 = find "GameLab.Engine.Math.Matrix44" +local Rendering = GameLab.Engine.Rendering +local EEventType = GameLab.Events.EEventType +local Resource = GameLab.Engine.Resource +local Vector2 = GameLab.Engine.Math.Vector2 +local Vector4 = GameLab.Engine.Math.Vector4 +local Color = Rendering.Color +local builtin_res = require "GameLab.BuiltInResources" + +local editor_res if GAMELAB_EDITOR then - EditorRes = find "Scripts/EditorResources" -end + editor_res = find "Scripts/EditorResources" +elseif GAMELAB_RUNNER then +end GUI.BeginOnGUI = function() GUIState.ResetControlID() @@ -52,29 +52,27 @@ GUI.Toggle = function() end GUI.Label = function() - + end -GUI.Box = function(position, color, size) +GUI.Box = function(rect, color) if Event.current.type == EEventType.Repaint then - -- GL.ClearColor({0.13, 0.13, 0.13, 1}) - -- GL.Clear(GL.EBufferType.ColorBuffer) - --Rendering.UseShader(Res.shaders["EditorShape"]) - local ortho = Matrix44() - ortho:SetOrtho(0, size.x, size.y, 0, 0.1, 10) - - Rendering.UseShader(Res.shaders["EditorShape"]) - Rendering.SetMatrix44("gamelab_mat_mvp", ortho) - Rendering.SetVector2("gamelab_ui_position", {0, 0}) - Rendering.SetVector4("gamelab_color", Vector4(0.12, 0.12, 0.12, 1)) - --Rendering.SetTexture("gamelab_main_tex", tex) - --Rendering.DrawUIQuad({0, 0, 200, 200}) - --Rendering.DrawUI9Slicing(1, {25, 25}, {25, 25}, {80, 80}, {400, 30} ) - --GUI.DrawText(_G["default_font"], "你好世界!\nMaterials\nHello,World!\nProject Window Properties", 12) - GUI.Line(Vector2(0, 0), Vector2(100, 100)) + Rendering.UseShader(editor_res.shaders["EditorShape"]) + Rendering.SetVector2("gamelab_ui_position", rect.position) + Rendering.SetColor("gamelab_color", color) + GUI.DrawQuad(Vector2(0,0), rect.size) end end +GUI.BoxFrame = function(rect, color) + if Event.current.type == EEventType.Repaint then + GUI.Line(Vector2(rect.x, rect.y), Vector2(rect.x + rect.w, rect.y), color) + GUI.Line(Vector2(rect.x + rect.w, rect.y), Vector2(rect.x + rect.w, rect.y + rect.h), color) + GUI.Line(Vector2(rect.x + rect.w, rect.y + rect.h), Vector2(rect.x, rect.y + rect.h), color) + GUI.Line(Vector2(rect.x, rect.y + rect.h), Vector2(rect.x, rect.y), color) + end +end + GUI.HorizontalSlider = function() end @@ -103,15 +101,14 @@ GUI.HorizontalScrollbar = function() end -GUI.Line = function(from, to) +GUI.Line = function(from, to, color) if Event.current.type ~= EEventType.Repaint then return end + Rendering.UseShader(editor_res.shaders["EditorShape"]) + Rendering.SetVector2("gamelab_ui_position", {0, 0}) + Rendering.SetColor("gamelab_color", color) GUI.DrawLine(from, to) end -GUI.BoxFrame = function(topleft, size) - -end - return GUI \ 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 daa8bda..ed23b8c 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/Rendering/init.lua @@ -17,6 +17,7 @@ m.SetVector2 = Shader.SetVector2 m.SetVector3 = Shader.SetVector3 m.SetVector4 = Shader.SetVector4 m.SetTexture = Shader.SetTexture +m.SetColor = Shader.SetColor m.UseShader = Shader.Use m.UnuseShader = Shader.Unuse diff --git a/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl b/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl deleted file mode 100644 index 5ce755f..0000000 --- a/Data/BuiltIn/Resources/Shaders/Editor-Shape.glsl +++ /dev/null @@ -1,34 +0,0 @@ -#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 deleted file mode 100644 index d1003c3..0000000 --- a/Data/BuiltIn/Resources/Shaders/Editor-Text.glsl +++ /dev/null @@ -1,46 +0,0 @@ -#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 deleted file mode 100644 index b2b348b..0000000 --- a/Data/BuiltIn/Resources/Shaders/Editor-UI.glsl +++ /dev/null @@ -1,45 +0,0 @@ -#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/Window/GUIWindow.lua b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua index b5d7436..9290b1e 100644 --- a/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua @@ -14,6 +14,7 @@ local Event = Events.Event local EEventType = Events.EEventType local Vector2 = Math.Vector2 local Color = Rendering.Color +local Matrix44 = Math.Matrix44 local clone = Utils.Clone @@ -113,7 +114,18 @@ GUIWindow.OnGUI = function(self) end self:ClearBackground() - GUI.Box(Rect(), Color(), self.position.size) + + local windowSize = self.position.size + + local ortho = Matrix44() + ortho:SetOrtho(0, windowSize.x, windowSize.y, 0, 0.1, 10) + Rendering.SetMatrix44("gamelab_mat_mvp", ortho) + + -- + local guiSize = Vector2(windowSize.x - kSideBorders*2, windowSize.y - kSideBorders*2) + local bgRect = Rect(kSideBorders, kSideBorders, guiSize.x, guiSize.y) + GUI.Box(bgRect, Color(0.22, 0.22, 0.22, 1)) + GUI.BoxFrame(bgRect, Color(0.13, 0.13, 0.13, 1)) end diff --git a/Data/Resources/Shaders/Editor-Shape.glsl b/Data/Resources/Shaders/Editor-Shape.glsl index 235390c..e371f95 100644 --- a/Data/Resources/Shaders/Editor-Shape.glsl +++ b/Data/Resources/Shaders/Editor-Shape.glsl @@ -1,5 +1,5 @@ #version 330 core -//绘制几何图形 +//绘制纯色几何图形:点、线段、三角形 CMD_BEGIN Cull Off @@ -10,22 +10,16 @@ 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; -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 @@ -37,10 +31,6 @@ 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; - //sampled = vec4(1,1,1,1); - FragColor = sampled; + FragColor = gamelab_color; } FSH_END diff --git a/Data/Scripts/EditorResources.lua b/Data/Scripts/EditorResources.lua index c9d2e77..bcefec4 100644 --- a/Data/Scripts/EditorResources.lua +++ b/Data/Scripts/EditorResources.lua @@ -1,9 +1,27 @@ +-- 编辑器用到的资源 local find = GameLab.Find +local Shader = find "GameLab.Engine.Rendering.Shader" +local Texture = find "GameLab.Engine.Rendering.Texture" +local Rendering = find "GameLab.Engine.Rendering" + +local SHADER = function(path) + local file = '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", + } end GameLab.onApplicationStart:Add(loadRes) diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h index 14d022b..8747a7b 100644 --- a/Runtime/Graphics/Shader.h +++ b/Runtime/Graphics/Shader.h @@ -53,7 +53,7 @@ private: LUA_BIND_DECL_METHOD(_SetMatrix3); LUA_BIND_DECL_METHOD(_SetMatrix4); LUA_BIND_DECL_METHOD(_SetTexture); - //LUA_BIND_DECL_METHOD(_SetColor); + LUA_BIND_DECL_METHOD(_SetColor); }; diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index 084d4f6..b7bf543 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -113,6 +113,34 @@ Color32 State::GetValue < Color32 >(int idx, const Color32 value) return col; } +template <> +Color State::GetValue < Color >(int idx, const Color value) +{ + Color col = value; + if (LuaHelper::IsType(*this, "GameLab.Engine.Rendering.Color", idx)) + { + col.r = GetField(idx, "r", 0); + col.g = GetField(idx, "g", 0); + col.b = GetField(idx, "b", 0); + col.a = GetField(idx, "a", 0); + } + else if (LuaHelper::IsType(*this, "GameLab.Engine.Rendering.Color32", idx)) + { + col.r = GetField(idx, "r", 0) / 255.f; + col.g = GetField(idx, "g", 0) / 255.f; + col.b = GetField(idx, "b", 0) / 255.f; + col.a = GetField(idx, "a", 0) / 255.f; + } + else if (LuaHelper::IsType(*this, "table", idx)) + { + col.r = GetField(idx, 1, 0); + col.g = GetField(idx, 2, 0); + col.b = GetField(idx, 3, 0); + col.a = GetField(idx, 4, 0); + } + return col; +} + template <> Matrix44 State::GetValue < Matrix44 >(int idx, const Matrix44 value) { diff --git a/Runtime/Scripting/GUI/GUI.bind.cpp b/Runtime/Scripting/GUI/GUI.bind.cpp index e05d373..cc4ef88 100644 --- a/Runtime/Scripting/GUI/GUI.bind.cpp +++ b/Runtime/Scripting/GUI/GUI.bind.cpp @@ -12,6 +12,7 @@ #include "Runtime/Utilities/AutoInvoke.h" #include "Editor/Win/Win.h" #include "Runtime/GUI/UILine.h" +#include "Runtime/GUI/UIQuad.h" using namespace std; using namespace LuaBind; @@ -103,10 +104,26 @@ static int DrawLine(lua_State* L) return 0; } +// GUI.DrawQuad(topleft, size) +static int DrawQuad(lua_State* L) +{ + LUA_BIND_STATE(L); + + Vector2f tl, size; + tl.RestoreFromLuaObject(state, 1); + size.RestoreFromLuaObject(state, 2); + + UIQuad quad = UIQuad(tl.x, tl.x + size.x, tl.y, tl.y + size.y); + quad.Draw(); + + return 0; +} + static luaL_Reg guiFuncs[] = { { "DrawText", DrawText }, - { "DrawLine", DrawLine }, - {0, 0} + { "DrawLine", DrawLine }, + { "DrawQuad", DrawQuad }, + {0, 0} }; int luaopen_GameLab_Engine_GUI(lua_State* L) diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp index 4c0c321..a001633 100644 --- a/Runtime/Scripting/Rendering/Shader.bind.cpp +++ b/Runtime/Scripting/Rendering/Shader.bind.cpp @@ -16,12 +16,11 @@ LUA_BIND_REGISTRY(Shader) { "IsValid", _IsValid }, { "Use", _Use }, { "Unuse", _UnUse }, - //{ "SetColor", _SetColor }, + { "SetColor", _SetColor }, { "SetTexture", _SetTexture }, { "SetVector2", _SetVector2 }, { "SetVector3", _SetVector3 }, { "SetVector4", _SetVector4 }, - //{ "SetMatrix3", _SetMatrix3 }, { "SetMatrix44", _SetMatrix4 } ); } @@ -165,6 +164,20 @@ LUA_BIND_IMPL_METHOD(Shader, _SetVector4) return 1; } +// SetColor(name, color) +LUA_BIND_IMPL_METHOD(Shader, _SetColor) +{ + LUA_BIND_PREPARE(L, Shader); + LUA_BIND_CHECK(L, "ST"); + + cc8* name = state.GetValue(1, ""); + Color col = state.GetValue(2, Color()); + Vector4 v = Vector4(col.r, col.g, col.b, col.a); + + g_GfxDevice.SetUniformVec4(name, v); + return 1; +} + // shader.SetMatrix4(name, mat4) // shader.SetMatrix4(name, {}) LUA_BIND_IMPL_METHOD(Shader, _SetMatrix4) -- cgit v1.1-26-g67d0