101 lines
2.6 KiB
Plaintext
Raw Normal View History

2025-07-17 00:12:35 +08:00
local UIWindow = {}
UIWindow.__index = UIWindow
--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--> Variables
local FolderWindows = ReplicatedStorage:WaitForChild("UI"):WaitForChild("Windows")
local LocalPlayer = game.Players.LocalPlayer
local FolderPlayerGui = LocalPlayer:WaitForChild("PlayerGui")
--------------------------------------------------------------------------------
-- 递归查找
local function FindInDescendants(root, name)
if root:FindFirstChild(name) then
return root:FindFirstChild(name)
end
for _, child in ipairs(root:GetChildren()) do
local found = FindInDescendants(child, name)
if found then
return found
end
end
return nil
end
-- 非递归查找
local function FindInRoot(root, name)
return root:FindFirstChild(name)
end
--------------------------------------------------------------------------------
function UIWindow:Init(Data: table?)
local self = {}
self.Data = Data
self.Variables = {}
self.UIRootName = nil
self.UIParentName = nil
self.UIRoot = nil
self.AutoInject = false
return self
end
function UIWindow:SetData(Data: table?)
self.Data = Data
self:OnDataChanged()
end
function UIWindow:OnOpenWindow()
if not self.UIRoot then
-- 创建UI根节点
self.UIRoot = FolderWindows:FindFirstChild(self.UIRootName):Clone()
self.UIRoot.Parent = FolderPlayerGui:FindFirstChild(self.UIParentName)
end
-- 自动注入
if not self.AutoInject then
for varName, _ in pairs(self.Variables) do
if typeof(varName) == "string" then
local firstChar = string.sub(varName, 1, 1)
local secondChar = string.sub(varName, 2, 2)
local target
if firstChar == "_" then
if secondChar == "_" then
-- __开头只查根目录
target = FindInRoot(self.UIRoot, varName)
else
-- _开头递归查找
target = FindInDescendants(self.UIRoot, varName)
end
if not target then
error("自动注入失败未找到UI节点 " .. varName)
end
self[varName] = target
end
end
end
self.AutoInject = true
end
end
function UIWindow:OnCloseWindow()
end
--> 覆盖用
function UIWindow:OnDataChanged() end
function UIWindow:Refresh() end
return UIWindow