172 lines
4.8 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")
2025-07-18 01:11:49 +08:00
--> Dependencies
local UIList = require(ReplicatedStorage.Base.UIList)
2025-07-17 00:12:35 +08:00
--> Variables
local FolderWindows = ReplicatedStorage:WaitForChild("UI"):WaitForChild("Windows")
2025-07-18 01:11:49 +08:00
local Utils = require(ReplicatedStorage.Tools.Utils)
2025-07-17 00:12:35 +08:00
local LocalPlayer = game.Players.LocalPlayer
local FolderPlayerGui = LocalPlayer:WaitForChild("PlayerGui")
2025-07-23 23:54:19 +08:00
local FolderStarterPlayerScripts = game.StarterPlayer:WaitForChild("StarterPlayerScripts")
local FolderUIInstanceScripts = FolderStarterPlayerScripts:WaitForChild("UI"):WaitForChild("InstanceScripts")
2025-07-17 00:12:35 +08:00
--------------------------------------------------------------------------------
2025-07-18 01:11:49 +08:00
function UIWindow:Init(UIManager: table, Data: table?)
2025-07-17 00:12:35 +08:00
local self = {}
2025-07-18 01:11:49 +08:00
self.UIManager = UIManager
2025-07-19 03:03:02 +08:00
self.Data = Data or {}
2025-07-17 00:12:35 +08:00
self.Variables = {}
2025-07-18 01:11:49 +08:00
self.Connections = {}
self.SignalConnections = {}
2025-07-17 00:12:35 +08:00
self.UIRootName = nil
self.UIParentName = nil
2025-07-18 01:11:49 +08:00
self.CloseDestroy = true
2025-07-17 00:12:35 +08:00
2025-07-18 01:11:49 +08:00
-- 运行时操作变量
2025-07-17 00:12:35 +08:00
self.UIRoot = nil
self.AutoInject = false
return self
end
function UIWindow:SetData(Data: table?)
self.Data = Data
self:OnDataChanged()
end
2025-07-18 01:11:49 +08:00
-- 自动注入
function UIWindow:AutoInjectVariables()
2025-07-17 00:12:35 +08:00
if not self.AutoInject then
for varName, _ in pairs(self.Variables) do
if typeof(varName) == "string" then
local firstChar = string.sub(varName, 1, 1)
2025-07-18 01:11:49 +08:00
local sixChar = string.sub(varName, 1, 6)
2025-07-23 23:54:19 +08:00
local sevenChar = string.sub(varName, 1, 7)
2025-07-17 00:12:35 +08:00
local target
if firstChar == "_" then
2025-07-18 01:11:49 +08:00
if sixChar == "__list" then
local prefab = Utils:FindInDescendantsUI(self.UIRoot, varName)
2025-08-15 00:45:18 +08:00
if not prefab then
warn("没找到预制体", self.UIRoot, varName)
end
2025-07-18 01:11:49 +08:00
target = UIList:Init(prefab)
2025-07-19 03:03:02 +08:00
target.TopUI = self
2025-07-23 23:54:19 +08:00
elseif sevenChar == "__money" then
target = Utils:FindInDescendantsUI(self.UIRoot, varName)
-- 把脚本放到预制体下
local newScript = FolderUIInstanceScripts:FindFirstChild("Money"):Clone()
newScript.Parent = target
2025-07-17 00:12:35 +08:00
else
-- _开头递归查找
2025-07-18 01:11:49 +08:00
target = Utils:FindInDescendantsUI(self.UIRoot, varName)
2025-07-17 00:12:35 +08:00
end
if not target then
error("自动注入失败未找到UI节点 " .. varName)
end
2025-07-18 01:11:49 +08:00
self.Variables[varName] = target
2025-07-17 00:12:35 +08:00
end
end
end
self.AutoInject = true
end
end
2025-07-18 01:11:49 +08:00
function UIWindow:OnOpenWindow()
if not self.UIRoot then
-- 创建UI根节点
self.UIRoot = FolderWindows:FindFirstChild(self.UIRootName):Clone()
self.UIRoot.Parent = FolderPlayerGui:WaitForChild(self.UIParentName)
else
self.UIRoot.Visible = true
self:Refresh()
end
self:AutoInjectVariables()
2025-07-17 00:12:35 +08:00
end
2025-07-18 01:11:49 +08:00
function UIWindow:OnCloseWindow()
2025-07-19 03:03:02 +08:00
if self.SignalConnections then
for _, connection in pairs(self.SignalConnections) do
connection:DisconnectAll()
end
self.SignalConnections = nil
end
if self.Connections then
for _, connection in pairs(self.Connections) do
connection:Disconnect()
end
self.Connections = nil
end
2025-07-18 01:11:49 +08:00
if self.CloseDestroy then
self.UIRoot:Destroy()
self.UIRoot = nil
else
self.UIRoot.Visible = false
end
2025-07-19 03:03:02 +08:00
self:Destroy()
2025-07-18 01:11:49 +08:00
end
2025-07-17 00:12:35 +08:00
--> 覆盖用
function UIWindow:OnDataChanged() end
function UIWindow:Refresh() end
2025-07-18 01:11:49 +08:00
function UIWindow:Destroy()
2025-07-19 03:03:02 +08:00
if self.TopUI then self.TopUI = nil end
if self.SignalConnections then
for _, connection in pairs(self.SignalConnections) do
connection:DisconnectAll()
end
2025-07-18 01:11:49 +08:00
end
self.SignalConnections = nil
2025-07-19 03:03:02 +08:00
if self.Connections then
for _, connection in pairs(self.Connections) do
print("进入断开连接")
connection:Disconnect()
end
2025-07-18 01:11:49 +08:00
end
self.Connections = nil
2025-07-22 01:56:56 +08:00
-- 销毁数据
if self.Data then
if type(self.Data) == "table" then
for k, v in pairs(self.Data) do
self.Data[k] = nil
end
end
self.Data = nil
end
-- 销毁实例内容
if self.Variables then
for k, v in pairs(self.Variables) do
if v.Destroy then
v:Destroy()
end
v = nil
end
end
2025-07-18 01:11:49 +08:00
for k, v in pairs(self) do
self[k] = nil
end
self = nil
end
2025-07-17 00:12:35 +08:00
return UIWindow