From 452eceb3ba4bdbbc77786dc7f7235c03f1c62302 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 15 Nov 2021 01:19:35 +0800 Subject: *class --- Data/DefaultContent/Libraries/GameLab/Class.lua | 93 +++++++++---- .../Libraries/GameLab/Engine/Core/Camera.lua | 5 - .../Libraries/GameLab/Engine/Core/Component.lua | 11 -- .../Libraries/GameLab/Engine/Core/Game.lua | 14 -- .../Libraries/GameLab/Engine/Core/GameObject.lua | 7 - .../Libraries/GameLab/Engine/Core/Sprite.lua | 21 --- .../Libraries/GameLab/Engine/Math/Vector4.lua | 4 +- .../Libraries/GameLab/Engine/Rendering/Color.lua | 18 +-- .../Libraries/GameLab/Engine/Rendering/Color32.lua | 18 +-- .../Libraries/GameLab/GlobalClass.lua | 13 +- .../GameLab/Editor/Window/ContainerWindow.lua | 4 +- Data/Libraries/GameLab/Editor/Window/GUIWindow.lua | 14 +- .../GameLab/Editor/Window/SplitWindow.lua | 30 ++--- "Documents/\347\252\227\345\217\243.xlsx" | Bin 1613696 -> 1725656 bytes Editor/GUI/ContainerWindow.cpp | 32 ++--- Editor/Scripting/IMGUI/GUIButton.bind.cpp | 1 - Editor/Scripting/Window/EditorGUI.bind.cpp | 118 ---------------- Editor/Scripting/Window/Window.bind.cpp | 148 +++++++++++++++++++++ Projects/VisualStudio/Editor/Editor.vcxproj | 3 +- .../VisualStudio/Editor/Editor.vcxproj.filters | 12 +- Runtime/Events/InputEvent.cpp | 32 +++++ Runtime/Events/InputEvent.h | 5 +- 22 files changed, 328 insertions(+), 275 deletions(-) delete mode 100644 Data/DefaultContent/Libraries/GameLab/Engine/Core/Camera.lua delete mode 100644 Data/DefaultContent/Libraries/GameLab/Engine/Core/Component.lua delete mode 100644 Data/DefaultContent/Libraries/GameLab/Engine/Core/Game.lua delete mode 100644 Data/DefaultContent/Libraries/GameLab/Engine/Core/GameObject.lua delete mode 100644 Data/DefaultContent/Libraries/GameLab/Engine/Core/Sprite.lua delete mode 100644 Editor/Scripting/IMGUI/GUIButton.bind.cpp delete mode 100644 Editor/Scripting/Window/EditorGUI.bind.cpp create mode 100644 Editor/Scripting/Window/Window.bind.cpp diff --git a/Data/DefaultContent/Libraries/GameLab/Class.lua b/Data/DefaultContent/Libraries/GameLab/Class.lua index 62369e1..0b424a6 100644 --- a/Data/DefaultContent/Libraries/GameLab/Class.lua +++ b/Data/DefaultContent/Libraries/GameLab/Class.lua @@ -1,38 +1,79 @@ --- Declare class +-- GameLab内置class模块 +-- 保留方法名:New, Ctor, Is +-- 保留字段:_type, _base -local _class = function (className) - local class = {} - local pkgName = (type(className) == "string") and string.match(className, "^(.+)%.%w+$") or "" - local shortName = (type(className) == "string") and string.match(className, "%.*(%w+)$") or "" - class._type = { -- 类型元数据,GameLab的类型都会包含这些 +local TrackClassInstances = false + +local _class = function (name, ctor) + local c = {} + + local isstring = type(name) == "string" + local pkg = "unknown" + local short = "unknown" + if isstring then + pkg = string.match(name, "^(.+)%.%w+$") + short = string.match(name, "%.*(%w+)$") + end + + c._type = { mode = "lua", - name = shortName or "", - package = pkgName or "", - fullName = className + name = short, + package = pkg, + fullName = name } - class.__index = class - class.New = function(...) - local instance = {} - setmetatable(instance, class) - instance:Ctor(...) - return instance + + c.__index = c + + c.New = function(...) + local obj = {} + setmetatable(obj, c) + obj:Ctor(...) + return obj end - return class + + c.Is = function(self, klass) + local m = getmetatable(self) + while m do + if m == klass then + return true + end + m = m._base + end + return false + end + + if ctor and type(ctor) == "function" then + c.Ctor = ctor + end + + local mt = {} + + mt.__call = function(dummy, ...) + return c.New(...) + end + + setmetatable(c, mt) + + return c end --- 提供ClassName和PkgName作为类型的元数据,可以留空,但是最好提供,会作为类型判断的依据 -local Class = function(className) - local cls = _class(className) - cls.Extend = function(childName) +local class = function(name, ctor) + local c = _class(name, ctor) + + c.Extend = function(childName) local child = _class(childName) - if cls then - setmetatable(child, cls) - child.base = cls + if c then + setmetatable(child, c) + child._base = c end return child end - return cls + + c._base = nil + + return c end -GameLab.Class = Class -return Class \ No newline at end of file +GameLab.Class = class + +return class \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Camera.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Core/Camera.lua deleted file mode 100644 index 5fcfbe7..0000000 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Camera.lua +++ /dev/null @@ -1,5 +0,0 @@ -local Camera = {} - - - -return Camera \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Component.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Core/Component.lua deleted file mode 100644 index 00bf2ca..0000000 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Component.lua +++ /dev/null @@ -1,11 +0,0 @@ -local Component = {} - -Component.New = function() - -end - -Component.GetGameObject = function() - -end - -Jin.Component = Component \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Game.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Core/Game.lua deleted file mode 100644 index 51ce25f..0000000 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Game.lua +++ /dev/null @@ -1,14 +0,0 @@ - --- Game entry - -local Game = {} - -Game.OnEvent = function(e) - -end - -Game.MainLoop = function() - -end - -return Game \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Core/GameObject.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Core/GameObject.lua deleted file mode 100644 index ee0d143..0000000 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Core/GameObject.lua +++ /dev/null @@ -1,7 +0,0 @@ -local GameObject = {} - -GameObject.AddComponent = function(self, comp) - -end - -Jin.GameObject = GameObject diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Sprite.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Core/Sprite.lua deleted file mode 100644 index 47cb50d..0000000 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Core/Sprite.lua +++ /dev/null @@ -1,21 +0,0 @@ --- Quadķװʺ2DϷ -local Sprite = {} - -Sprite.New = function(quad) - local spr = {} - spr.quad = Jin.Quad.New(quad) - spr.pivot = Jin.Vector2.New(0.5, 0) - spr.transform = Jin.Transform.New() - spr.depth = 0 - return spr -end - -Sprite.SetDepth = function(self, depth) - spr.depth = depth -end - -Sprite.SetTexture = function(self, tex, reset_quad) - -end - -Jin.Sprite = Sprite \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua index 3655184..ecfc9ff 100644 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Math/Vector4.lua @@ -39,7 +39,7 @@ Vector4.Scale = function(self, scale) end -Vector4.one = Vector4.New(1,1,1,1) -Vector4.zero = Vector4.New(0,0,0,0) +Vector4.one = Vector4(1,1,1,1) +Vector4.zero = Vector4(0,0,0,0) return Vector4 \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua index 2785bdf..e70de4f 100644 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color.lua @@ -9,7 +9,7 @@ end Color.ToColor32 = function(self) local Color32 = GameLab.find("GameLab.Engine.Rendering.Color32") - local c32 = Color32.New() + local c32 = Color32() c32.r = self.r * 255 c32.g = self.g * 255 c32.b = self.b * 255 @@ -24,13 +24,13 @@ Color.Clear = function(self) self.a = 0 end -Color.red = Color.New(1,0,0,1) -Color.green = Color.New(0,1,0,1) -Color.blue = Color.New(0,0,1,1) -Color.magenta = Color.New(1,0,1,1) -Color.red = Color.New(1,0,0,1) -Color.yellow = Color.New(1,1,0,1) -Color.black = Color.New(0,0,0,1) -Color.white = Color.New(1,1,1,1) +Color.red = Color(1,0,0,1) +Color.green = Color(0,1,0,1) +Color.blue = Color(0,0,1,1) +Color.magenta = Color(1,0,1,1) +Color.red = Color(1,0,0,1) +Color.yellow = Color(1,1,0,1) +Color.black = Color(0,0,0,1) +Color.white = Color(1,1,1,1) return Color \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua index 48902b6..d1cacd7 100644 --- a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Color32.lua @@ -9,7 +9,7 @@ end Color32.ToColor = function(self) local Color = GameLab.find("GameLab.Engine.Rendering.Color") - local c = Color.New() + local c = Color() c.r = self.r / 255 c.g = self.g / 255 c.b = self.b / 255 @@ -24,13 +24,13 @@ Color32.Clear = function(self) self.a = 0 end -Color32.red = Color32.New(255,0,0,255) -Color32.green = Color32.New(0,255,0,255) -Color32.blue = Color32.New(0,0,255,255) -Color32.magenta = Color32.New(255,0,255,255) -Color32.red = Color32.New(255,0,0,255) -Color32.yellow = Color32.New(255,255,0,255) -Color32.black = Color32.New(0,0,0,255) -Color32.white = Color32.New(255,255,255,255) +Color32.red = Color32(255,0,0,255) +Color32.green = Color32(0,255,0,255) +Color32.blue = Color32(0,0,255,255) +Color32.magenta = Color32(255,0,255,255) +Color32.red = Color32(255,0,0,255) +Color32.yellow = Color32(255,255,0,255) +Color32.black = Color32(0,0,0,255) +Color32.white = Color32(255,255,255,255) return Color32 \ No newline at end of file diff --git a/Data/DefaultContent/Libraries/GameLab/GlobalClass.lua b/Data/DefaultContent/Libraries/GameLab/GlobalClass.lua index dcb5fba..7104480 100644 --- a/Data/DefaultContent/Libraries/GameLab/GlobalClass.lua +++ b/Data/DefaultContent/Libraries/GameLab/GlobalClass.lua @@ -1,18 +1,19 @@ local Class = GameLab.Class or require("GameLab.Class") -- 声明类的同时添加到G表 -local GlobalClass = function(className) - local cls = Class(className) +local GlobalClass = function(name, ctor) + local c = Class(name, ctor) - local shortName = string.match(className, "%.*(%w+)$") + local short = string.match(name, "%.*(%w+)$") + local pkgs = string.gmatch(name, "%.*(%w+)%.") local t = _G - for pkg in string.gmatch(className, "%.*(%w+)%.") do + for pkg in pkgs do t[pkg] = t[pkg] or {} t = t[pkg] end - t[shortName] = cls + t[short] = c - return cls + return c end GameLab.GlobalClass = GlobalClass diff --git a/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua b/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua index 1e75cdc..4b6f1bc 100644 --- a/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/ContainerWindow.lua @@ -11,8 +11,8 @@ ContainerWindow.Ctor = function(self, position, showMode, min, max) self.m_RootSplitWindow = Window.SplitWindow.New(Window.ESplitMode.Horizontal) end -ContainerWindow.SetTitle = function(self) - self.m_Native:SetTitle(self) +ContainerWindow.SetTitle = function(self, title) + self.m_Native:SetTitle(title) end ContainerWindow.SetIcon = function(self) diff --git a/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua index 51ee128..bbd368e 100644 --- a/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/GUIWindow.lua @@ -1,3 +1,15 @@ +--[[ +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是逻辑窗口,本身不会 +产生输入事件 +]] local GUIWindow = GameLab.GlobalClass("GameLab.Editor.Window.GUIWindow") local NativeGUIWindow = GameLab.Editor.Window.Internal.GUIWindow @@ -38,7 +50,7 @@ GUIWindow.SetContainerWindow = function(self, containerWindow) end GUIWindow.AddEditorWindow = function(self) - + end GUIWindow.SetPosition = function(self, pos) diff --git a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua index 8f126a9..9a398c2 100644 --- a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua @@ -3,6 +3,8 @@ local EEventType = GameLab.Events.EEventType local Math = require "GameLab.Engine.Math" local GUI = require "GameLab.Engine.GUI" local inspect = require "inspect" +local ECursor = GameLab.Editor.Window.ECursor +local Window = GameLab.Editor.Window local Rect = Math.Rect @@ -24,22 +26,21 @@ local SplitState = { } -- 抽象的窗口,用来处理布局 -local SplitWindow = GameLab.GlobalClass("GameLab.Editor.Window.SplitWindow") - -SplitWindow.Ctor = function(self, mode, splitter) +local SplitWindow = GameLab.GlobalClass("GameLab.Editor.Window.SplitWindow", function(self, mode, splitter) + self.m_ControlID = nil self.m_ContainerWindow = nil self.m_SplitMode = mode self.m_Parent = nil -- 父节点是一个split window或者空 self.m_SubWindows = {} -- 子窗口,可以是SplitWindow或GUIWindow self.m_Splitter = {} - self.m_Position = Rect.New() + self.m_Position = Rect() if splitter ~= nil and type(splitter) == "table" then for _, v in ipairs(splitter) do - local sp = Splitter.New(v) + local sp = Splitter(v) table.insert(self.m_Splitter, sp) end end -end +end) -- 布局,设置GUIWindow的大小 SplitWindow.DoSplit = function(self, event) @@ -47,12 +48,11 @@ SplitWindow.DoSplit = function(self, event) self.m_Parent:DoSplit(event) end - local id = GUI.GetControlID() + self.m_ControlID = GUI.GetControlID() if event.type == EEventType.MouseDown then - local bHandled = false for i, sp in ipairs(self.m_Splitter) do - local rect = Rect.New() + local rect = Rect() if self.m_SplitMode == ESplitMode.Horizontal then local x = sp.value * self.m_Position.width + self.m_Position.x - sp.size / 2 local w = sp.size @@ -70,14 +70,13 @@ SplitWindow.DoSplit = function(self, event) end if rect:Contains(event.mousePosition) then SplitState.currentSplitter = sp - GUI.SetHotControl(id) - bHandled = true + GUI.SetHotControl(self.m_ControlID) break end end elseif event.type == EEventType.MouseDrag then local hot = GUI.GetHotControl() - if hot == id then + if hot == self.m_ControlID then local splitter = SplitState.currentSplitter if splitter ~= nil then local mousePos = event.mousePosition @@ -93,7 +92,8 @@ SplitWindow.DoSplit = function(self, event) end end elseif event.type == EEventType.MouseUp then - if GUI.GetHotControl() == id then + if GUI.GetHotControl() == self.m_ControlID then + GUI.SetHotControl(0) SplitState.currentSplitter = nil end end @@ -105,7 +105,7 @@ SplitWindow.SetPosition = function(self, position) for i, subWindow in ipairs(self.m_SubWindows) do local prev = i > 1 and self.m_Splitter[i-1].value or 0 local next = i <= #self.m_Splitter and self.m_Splitter[i].value or 1 - local pos = Rect.New() + local pos = Rect() pos:CopyFrom(position) if self.m_SplitMode == ESplitMode.Horizontal then pos.x = pos.x + prev * position.width @@ -119,7 +119,7 @@ SplitWindow.SetPosition = function(self, position) end SplitWindow.GetPosition = function(self) - local pos = Rect.New() + local pos = Rect() pos:CopyFrom(self.m_Position) return pos end diff --git "a/Documents/\347\252\227\345\217\243.xlsx" "b/Documents/\347\252\227\345\217\243.xlsx" index 8a85923..a82870d 100644 Binary files "a/Documents/\347\252\227\345\217\243.xlsx" and "b/Documents/\347\252\227\345\217\243.xlsx" differ diff --git a/Editor/GUI/ContainerWindow.cpp b/Editor/GUI/ContainerWindow.cpp index f76ac73..7a71d14 100644 --- a/Editor/GUI/ContainerWindow.cpp +++ b/Editor/GUI/ContainerWindow.cpp @@ -43,22 +43,22 @@ LRESULT CALLBACK ContainerWindow::ContainerWndProc(HWND hWnd, UINT message, WPAR } case WM_SETICON: return WM_SETICON; - case WM_PAINT: - { - static PAINTSTRUCT ps; - self->SetAsRenderContext(); - - glEnable(GL_BLEND); - float c = 26 / 255.f; - glClearColor(c, c, c, 1); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glFlush(); - - BeginPaint(self->m_Window, &ps); - EndPaint(self->m_Window, &ps); - UpdateWindow(self->m_Window); - return 0; - } + //case WM_PAINT: + //{ + // static PAINTSTRUCT ps; + // self->SetAsRenderContext(); + + // glEnable(GL_BLEND); + // float c = 26 / 255.f; + // glClearColor(c, c, c, 1); + // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // glFlush(); + + // BeginPaint(self->m_Window, &ps); + // EndPaint(self->m_Window, &ps); + // UpdateWindow(self->m_Window); + // return 0; + //} case WM_ENTERMENULOOP: // ˵ return 0; diff --git a/Editor/Scripting/IMGUI/GUIButton.bind.cpp b/Editor/Scripting/IMGUI/GUIButton.bind.cpp deleted file mode 100644 index 8b13789..0000000 --- a/Editor/Scripting/IMGUI/GUIButton.bind.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Editor/Scripting/Window/EditorGUI.bind.cpp b/Editor/Scripting/Window/EditorGUI.bind.cpp deleted file mode 100644 index 838e752..0000000 --- a/Editor/Scripting/Window/EditorGUI.bind.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#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" - -using namespace LuaBind; - -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} -}; - -// GameLab.Editor.Window -int luaopen_GameLab_Editor_Window(lua_State* L) -{ - log_info_tag("Scripting", "luaopen_GameLab_Editor_Window()"); - - LUA_BIND_STATE(L); - - state.PushGlobalNamespace(); - state.PushNamespace("GameLab"); - state.PushNamespace("Editor"); - state.PushNamespace("Window"); - - state.PushNamespace("Internal"); - state.RegisterNativeClass(); - state.RegisterNativeClass(); - state.PopNamespace(); - - LUA_BIND_REGISTER_ENUM(state, "EShowMode", - { "NormalWindow", ContainerWindow::kShowNormalWindow }, - { "ShowPopupMenu", ContainerWindow::kShowPopupMenu }, - { "Utility ", ContainerWindow::kShowUtility }, - { "NoShadow", ContainerWindow::kShowNoShadow }, - { "MainWindow", ContainerWindow::kShowMainWindow }, - { "AuxWindow", ContainerWindow::kShowAuxWindow } - ); - - 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 new file mode 100644 index 0000000..b720c98 --- /dev/null +++ b/Editor/Scripting/Window/Window.bind.cpp @@ -0,0 +1,148 @@ +#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 int SetCursor(lua_State* L) +{ + LUA_BIND_STATE(L); + LUA_BIND_CHECK(state, "N"); + + int type = state.GetValue(state, 1); + Win::SetCursor((ECursor)type); + + return 0; +} + +static luaL_Reg guiFuncs[] = { + {"Text", Text}, + {"SetCursor", SetCursor}, + {0, 0} +}; + +// GameLab.Editor.Window +int luaopen_GameLab_Editor_Window(lua_State* L) +{ + log_info_tag("Scripting", "luaopen_GameLab_Editor_Window()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Editor"); + state.PushNamespace("Window"); + + state.PushNamespace("Internal"); + state.RegisterNativeClass(); + state.RegisterNativeClass(); + state.PopNamespace(); + + LUA_BIND_REGISTER_ENUM(state, "EShowMode", + { "NormalWindow", ContainerWindow::kShowNormalWindow }, + { "ShowPopupMenu", ContainerWindow::kShowPopupMenu }, + { "Utility ", ContainerWindow::kShowUtility }, + { "NoShadow", ContainerWindow::kShowNoShadow }, + { "MainWindow", ContainerWindow::kShowMainWindow }, + { "AuxWindow", ContainerWindow::kShowAuxWindow } + ); + + LUA_BIND_REGISTER_ENUM(state, "ECursor", + { "AppStarting", Cursor_AppStarting }, + { "Arrow", Cursor_Arrow }, + { "Cross", Cursor_Cross }, + { "Help", Cursor_Help }, + { "IBeam", Cursor_IBeam }, + { "NO", Cursor_NO }, + { "SizeAll", Cursor_SizeAll }, + { "SizeNESW", Cursor_SizeNESW }, + { "SizeNS", Cursor_SizeNS }, + { "SizeNWSE", Cursor_SizeNWSE }, + { "SizeWE", Cursor_SizeWE }, + { "UpArrow", Cursor_UpArrow }, + { "Wait", Cursor_Wait } + ); + + state.RegisterMethods(guiFuncs); + + return 1; +} \ No newline at end of file diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index 8e47f3a..bdf16ca 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -164,10 +164,9 @@ - - + diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 12b1aef..5d837a3 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -31,9 +31,6 @@ {476cedf1-fc4a-48a5-8782-bed16dabb86e} - - {337f607e-8c00-4e7b-a925-9380e08a30f0} - {c1e200c9-0eec-40e2-a69f-ccaaaa208200} @@ -159,9 +156,6 @@ Editor\Scripting - - Editor\Scripting\IMGUI - Editor\Shaders @@ -409,9 +403,6 @@ Editor\Scripting\Window - - Editor\Scripting\Window - Editor\Scripting\Window @@ -421,6 +412,9 @@ Runtime\Scripting\Events + + Editor\Scripting\Window + diff --git a/Runtime/Events/InputEvent.cpp b/Runtime/Events/InputEvent.cpp index e741511..bfa7a1e 100644 --- a/Runtime/Events/InputEvent.cpp +++ b/Runtime/Events/InputEvent.cpp @@ -320,4 +320,36 @@ void InputEvent::RestoreFromTable(LuaBind::State& state, int idx) } state.SetTop(top); +} + +void InputEvent::Init() +{ + mousePosition = Vector2f(0.0F, 0.0F); + mouseDelta = Vector2f(0.0F, 0.0F); + type = InputEvent_Ignore; + keycode = 0; + character = 0; + button = 0; + clickCount = 0; + pressure = 0; + modifiers = 0; + commandString = NULL; +} + +InputEvent InputEvent::RepaintEvent(HWND window) +{ + InputEvent evt; + evt.Init(); + evt.type = InputEvent_Repaint; + + Vector2f p = GetInputMousePosition(window); + evt.mousePosition = p; + evt.button = 0; + bool swapped = GetSystemMetrics(SM_SWAPBUTTON); + if (GetAsyncKeyState(swapped ? VK_LBUTTON : VK_RBUTTON)) + evt.button = Mouse_RightButton; + if (GetAsyncKeyState(swapped ? VK_RBUTTON : VK_LBUTTON)) + evt.button = Mouse_LeftButton; + + return evt; } \ No newline at end of file diff --git a/Runtime/Events/InputEvent.h b/Runtime/Events/InputEvent.h index d19fbea..5502aea 100644 --- a/Runtime/Events/InputEvent.h +++ b/Runtime/Events/InputEvent.h @@ -46,7 +46,7 @@ enum EMouseButton { Mouse_MiddleButton = 3 }; -// ¼ +// ¼ǽűպʹ¼ struct InputEvent : public LuaBind::INativeTable { EInputEventType type; @@ -66,8 +66,11 @@ struct InputEvent : public LuaBind::INativeTable bool use; + static InputEvent RepaintEvent(HWND window); + InputEvent(); InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window); + void Init(); void CastToTable(LuaBind::State& state) const override; void RestoreFromTable(LuaBind::State& state, int index) override; -- cgit v1.1-26-g67d0