From fdf79d1dc8baa38d273c0ba56cc8e8360c661b7d Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 16 Nov 2021 13:47:19 +0800 Subject: * static class --- .../Libraries/GameLab/Engine/GUI/GUIState.lua | 23 +++++++++++ .../BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua | 32 ++++----------- Data/BuiltIn/Libraries/GameLab/Engine/GUI/init.lua | 7 ++-- .../Libraries/GameLab/Engine/Math/Vector2.lua | 5 +++ Data/BuiltIn/Libraries/GameLab/Enum.lua | 6 +-- .../Libraries/GameLab/GlobalStaticClass.lua | 20 ++++++++++ Data/BuiltIn/Libraries/GameLab/StaticClass.lua | 45 ++++++++++++++++++++++ Data/BuiltIn/Libraries/GameLab/Utils/init.lua | 4 ++ Data/BuiltIn/Libraries/GameLab/init.lua | 2 + .../GameLab/Editor/Window/SplitWindow.lua | 42 ++++++++++++++------ 10 files changed, 144 insertions(+), 42 deletions(-) create mode 100644 Data/BuiltIn/Libraries/GameLab/Engine/GUI/GUIState.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/GlobalStaticClass.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/StaticClass.lua (limited to 'Data') diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/GUIState.lua b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/GUIState.lua new file mode 100644 index 0000000..bb4884e --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/GUIState.lua @@ -0,0 +1,23 @@ +local GUIState = GameLab.GlobalStaticClass("GameLab.Engine.GUI.GUIState") + +local hotControl = 0 +local currentId = 0 -- 当前可分配的controlID + +GUIState.get_hotControl = function() + return hotControl +end + +GUIState.set_hotControl = function(value) + hotControl = value +end + +GUIState.GetControlID = function() + currentId = currentId + 1 + return currentId +end + +GUIState.ResetControlID = function() + currentId = 0 +end + +return GUIState \ 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 6b324d3..acf0737 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/IMGUI.lua @@ -1,42 +1,25 @@ local Debug = GameLab.Debug -local GUI = GameLab.Engine.GUI or {} -GameLab.Engine.GUI = GUI +local GUI = GameLab.Package("GameLab.Engine.GUI") -local Event = GameLab.Events.Event - -local imgui = { - currentId = 0, -- 当前可分配的controlID - hotControl = 0, -} +local GUIState = GUI.GUIState -GUI.GetControlID = function() - imgui.currentId = imgui.currentId + 1 - return imgui.currentId -end +local Event = GameLab.Events.Event GUI.BeginOnGUI = function() - imgui.currentId = 0 + GUIState.ResetControlID() end GUI.EndOnGUI = function() - imgui.currentId = 0 + GUIState.ResetControlID() end GUI.BeginFrame = function() - imgui.hotControl = 0 + GUIState.ResetControlID() end GUI.EndFrame = function() - imgui.hotControl = 0 -end - -GUI.SetHotControl = function(id) - imgui.hotControl = id -end - -GUI.GetHotControl = function() - return imgui.hotControl + GUIState.ResetControlID() end ------------------------------------------------------------------------------------------------ @@ -44,7 +27,6 @@ end ------------------------------------------------------------------------------------------------ GUI.Button = function(rect, content ) - local id = GUI.GetControlID() end diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/init.lua b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/init.lua index 6667847..3012551 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/GUI/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/GUI/init.lua @@ -1,6 +1,7 @@ -local GUI = GameLab.Package("GameLab.Engine.GUI") -local import = GameLab.Import(...) +local GUI = GameLab.Package("GameLab.Engine.GUI") +local import = GameLab.Import(...) -import("imgui") +import "GUIState" +import "imgui" return GUI \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/Math/Vector2.lua b/Data/BuiltIn/Libraries/GameLab/Engine/Math/Vector2.lua index 4706423..8aeb30a 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/Math/Vector2.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/Math/Vector2.lua @@ -11,6 +11,11 @@ function Vector2:__add(other) return self end +function Vector2:Set(other) + self.x = other.x + self.y = other.y +end + function Vector2:Add(other) self.x = self.x + other.x self.y = self.y + other.y diff --git a/Data/BuiltIn/Libraries/GameLab/Enum.lua b/Data/BuiltIn/Libraries/GameLab/Enum.lua index 93b3944..fb76a7f 100644 --- a/Data/BuiltIn/Libraries/GameLab/Enum.lua +++ b/Data/BuiltIn/Libraries/GameLab/Enum.lua @@ -4,12 +4,12 @@ local Debug = GameLab.Debug local unmodified = { __newindex = function(t, k, val) - Debug.LogError("Enum is readonly. key=" .. k) + Debug.LogError("Enum is readonly. key is " .. k) end, - __index = function(t, k, val) Debug.LogError("Invalid key " .. k) - end + end, + __metatable = false } local Enum = function(tb) diff --git a/Data/BuiltIn/Libraries/GameLab/GlobalStaticClass.lua b/Data/BuiltIn/Libraries/GameLab/GlobalStaticClass.lua new file mode 100644 index 0000000..c41916e --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/GlobalStaticClass.lua @@ -0,0 +1,20 @@ +local StaticClass = GameLab.StaticClass or require("GameLab.StaticClass") + +local GlobalStaticClass = function(name) + local c = StaticClass(name) + + local short = string.match(name, "%.*(%w+)$") + local pkgs = string.gmatch(name, "%.*(%w+)%.") + local t = _G + for pkg in pkgs do + t[pkg] = t[pkg] or {} + t = t[pkg] + end + t[short] = c + + return c +end + +GameLab.GlobalStaticClass = GlobalStaticClass + +return GlobalStaticClass \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/StaticClass.lua b/Data/BuiltIn/Libraries/GameLab/StaticClass.lua new file mode 100644 index 0000000..309754a --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/StaticClass.lua @@ -0,0 +1,45 @@ +local static_class = function(name) + local c = {} + + local pkg = "unknown" + local short = "unknown" + if type(name) == "string" then + pkg = string.match(name, "^(.+)%.%w+$") + short = string.match(name, "%.*(%w+)$") + end + + c._type = { + mode = "lua", + name = short, + package = pkg, + fullName = name + } + + local mt = {} + + mt.__index = function(_, key) + local getter = rawget(c, 'get_' .. key) + if getter then + return getter() + end + end + + mt.__newindex = function(_, key, value) + local setter = rawget(c, 'set_' .. key) + if setter then + setter(value) + return + end + rawset(c, key, value) + end + + mt.__metatable = false + + setmetatable(c, mt) + + return c +end + +GameLab.StaticClass = static_class + +return static_class \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Utils/init.lua b/Data/BuiltIn/Libraries/GameLab/Utils/init.lua index 53188fa..da1050b 100644 --- a/Data/BuiltIn/Libraries/GameLab/Utils/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Utils/init.lua @@ -18,6 +18,10 @@ function utils.Deepcopy(object) return _copy(object) end +function utils.Clone(object) + return utils.Deepcopy(object) +end + function utils.RunInEnvironment(fn, fnenv) setfenv(fn, fnenv) return xpcall(fn, debug.traceback) diff --git a/Data/BuiltIn/Libraries/GameLab/init.lua b/Data/BuiltIn/Libraries/GameLab/init.lua index 89a3efb..7305b9d 100644 --- a/Data/BuiltIn/Libraries/GameLab/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/init.lua @@ -37,5 +37,7 @@ import("GlobalEnum") import("GlobalTable") import("Delegate") import("EventListener") +import("StaticClass") +import("GlobalStaticClass") return GameLab \ No newline at end of file diff --git a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua index f842ec5..2a01e53 100644 --- a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua @@ -1,12 +1,16 @@ +local inspect = require "inspect" + local Debug = GameLab.Debug local EEventType = GameLab.Events.EEventType -local Math = require "GameLab.Engine.Math" -local GUI = require "GameLab.Engine.GUI" -local inspect = require "inspect" +local Math = GameLab.Engine.Math +local GUI = GameLab.Engine.GUI local ECursor = GameLab.Editor.Window.ECursor local Window = GameLab.Editor.Window +local Vector2 = GameLab.Engine.Math.Vector2 +local Utils = GameLab.Utils local Rect = Math.Rect +local GUIState = GUI.GUIState local Splitter = GameLab.Class("GameLab.Editor.Window.Internal.Splitter") @@ -46,14 +50,31 @@ SplitWindow.Ctor = function(self, mode, splitter) end end +SplitWindow.get_controlID = function(self) + return self.m_ControlID +end + +SplitWindow.set_controlID = function(self, value) + self.m_ControlID = value +end + +SplitWindow.get_containerWindow = function(self) + return self.m_ContainerWindow +end + +SplitWindow.set_containerWindow = function(self, wnd) + self.m_ContainerWindow = wnd +end + -- 布局,设置GUIWindow的大小 SplitWindow.DoSplit = function(self, event) if self.m_Parent ~= nil then self.m_Parent:DoSplit(event) end - self.m_ControlID = GUI.GetControlID() + self.controlID = GUIState.GetControlID() + local mousePosition = event.mousePosition if event.type == EEventType.MouseDown then for _, sp in ipairs(self.m_Splitter) do local rect = Rect() @@ -72,18 +93,17 @@ SplitWindow.DoSplit = function(self, event) rect.width = self.m_Position.width rect.height = h end - if rect:Contains(event.mousePosition) then + if rect:Contains(mousePosition) then SplitState.currentSplitter = sp - GUI.SetHotControl(self.m_ControlID) + GUIState.hotControl = self.controlID break end end elseif event.type == EEventType.MouseDrag then - local hot = GUI.GetHotControl() - if hot == self.m_ControlID then + if GUIState.hotControl == self.controlID then local splitter = SplitState.currentSplitter if splitter ~= nil then - local mousePos = event.mousePosition + local mousePos = Utils.Clone(mousePosition) mousePos.x = mousePos.x - self.m_Position.x mousePos.y = mousePos.y - self.m_Position.y if self.m_SplitMode == ESplitMode.Horizontal then @@ -96,8 +116,8 @@ SplitWindow.DoSplit = function(self, event) end end elseif event.type == EEventType.MouseUp then - if GUI.GetHotControl() == self.m_ControlID then - GUI.SetHotControl(0) + if GUIState.hotControl == self.controlID then + GUIState.hotControl = 0 SplitState.currentSplitter = nil end end -- cgit v1.1-26-g67d0