Files
Main/Assets/GameAssets/Resources/Lua/UI/Base/UIBaseForm.lua
2025-01-25 04:38:09 +08:00

253 lines
6.8 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

------------------------------------------------
--作者: gzg
--日期: 2019-03-25
--文件: UIBaseForm.lua
--模块: UIBaseForm
--描述: 定义Form的基础类, 用于在gameObject上挂载Lua脚本
------------------------------------------------
------:::模块定义:::-----------
local UIBaseForm = {}
------:::模块内成员变量定义:::-----------
--设置__index为模块,
UIBaseForm.__index = UIBaseForm
--=======定义静态方法,用于绑定卸载LuaForm的操作========
--绑定
function UIBaseForm.BindCSForm(form, gobj)
--把UIBaseForm设置为form的父类
setmetatable(form, UIBaseForm);
--获取UILuaForm的脚本
local _csForm = UIUtils.FindLuaForm(gobj.transform)
if _csForm == nil then
_csForm = UIUtils.FindExtendLuaForm(gobj.transform)
end
form.CSForm = _csForm;
form._ActiveSelf_ = _csForm.IsVisible;
form._OnShowAfter_ = function(obj)
if obj.OnShowAfter then
obj:OnShowAfter();
end
form._ActiveSelf_ = true;
end
form._OnHideBefore_ = function(obj)
obj:DestroyAllVFX()
if obj.UISkinDic then
obj.UISkinDic:Clear();
end
if obj.OnHideBefore then
return not (obj:OnHideBefore() == false);
end
return true;
end
form._OnHideAfter_ = function(obj)
if obj.OnHideAfter then
obj:OnHideAfter();
end
form._ActiveSelf_ = false;
end
--绑定回调处理函数给CS的窗体
--1.实例化窗体后,先注册事件
_csForm.UIRegisterEvent = Utils.Handler(form.OnRegisterEvents, form);
--2.然后再Load其他子预制
if form.OnLoad then
_csForm.UILoadEvent = Utils.Handler(form.OnLoad, form);
end
--3.判断是否为第一次显示
if form.OnFirstShow then
_csForm.UIFirstShowEvent = Utils.Handler(form.OnFirstShow, form);
end
--4.显示之前的回调
if form.OnShowBefore then
_csForm.UIShowBefore = Utils.Handler(form.OnShowBefore, form);
end
--打开动画开始
if form.OnShowAnimStart then
_csForm.UIShowAnimStart = Utils.Handler(form.OnShowAnimStart, form);
end
--打开动画结束如果有动画则在OnShowAfter
if form.OnShowAnimFinish then
_csForm.UIShowAnimFinish = Utils.Handler(form.OnShowAnimFinish, form);
end
--5.显示之后的回调
_csForm.UIShowAfter = Utils.Handler(form._OnShowAfter_, form);
--1.隐藏
if form.OnTryHide then
_csForm.UITryHideEvent = Utils.Handler(form.OnTryHide, form);
end
--2.隐藏之前的回调
_csForm.UIHideBeforeEvent = Utils.Handler(form._OnHideBefore_, form);
--关闭动画开始
if form.OnHideAnimStart then
_csForm.UIHideAnimStart = Utils.Handler(form.OnHideAnimStart, form);
end
--3.隐藏之后的回调(关闭动画已结束)
_csForm.UIHideAfterEvent = Utils.Handler(form._OnHideAfter_, form);
--4.注销事件
if form.OnUnRegisterEvents then
_csForm.UIUnRegisterEvent = Utils.Handler(form.OnUnRegisterEvents, form);
end
--5.卸载
if form.OnUnload then
_csForm.UIUnLoadEvent = Utils.Handler(form.OnUnload, form);
end
--关闭动画结束的回调
if form.OnHideAnimFinish then
_csForm.UIHideAnimFinish = Utils.Handler(form.OnHideAnimFinish, form);
end
--屏幕切换
if form.OnScreenOrientationChanged then
_csForm.UIScreenOrientationChanged = Utils.Handler(form.OnScreenOrientationChanged, form);
end
_csForm.UIFormDestroyEvent = function()
local _formName = form._Name_;
if form.OnFormDestroy ~= nil then
form:OnFormDestroy();
end
if GameCenter.UIFormManager ~= nil then
GameCenter.UIFormManager:DestroyForm(_formName);
end
end
--窗体激活
if form.OnFormActive then
_csForm.UIFormOnActiveEvent = Utils.Handler(form.OnFormActive, form);
end
end
--解绑定
function UIBaseForm.UnBindCSForm(form)
local _csForm = form.CSForm
_csForm:ClearAllEvent()
_csForm:SetHasEverShowed(false)
end
--=======定义Lua端调用CSForm的一些函数封装========
--注册消息
function UIBaseForm:RegisterEvent(eid, func, caller)
if eid == nil then
Debug.LogError("UIBaseForm:RegisterEvent(eid=nil)");
return;
end
if func == nil then
Debug.LogError(string.format("UIBaseForm:RegisterEvent(eid=%s,func=nil)",tostring(eid)));
return;
end
if self.CSForm ~= nil then
if caller == nil then
self.CSForm:RegisterEvent(eid, Utils.Handler(func,self));
else
self.CSForm:RegisterEvent(eid, Utils.Handler(func,caller));
end
else
Debug.LogError("self.CSForm == nil");
end
end
--设置Texture
function UIBaseForm:LoadTexture(uiTex,type,name,cbFunc,caller)
if uiTex == nil or type == nil or name == nil or name == "" then
Debug.LogError("UIBaseForm:LoadTexture param is invalid!");
return;
end
if self.CSForm == nil then
Debug.LogError("self.CSForm == nil!");
return;
end
if caller == nil then
self.CSForm:LoadTexture(uiTex,type,name,cbFunc and Utils.Handler(cbFunc,self));
else
self.CSForm:LoadTexture(uiTex,type,name,cbFunc and Utils.Handler(cbFunc,caller));
end
end
--由子类调用的打开窗体函数
function UIBaseForm:OnOpen(object,sender)
if self.CSForm ~= nil then
self.CSForm:OnOpen(object,sender);
end
end
--由子类调用的关闭窗体函数
function UIBaseForm:OnClose(object,sender)
if self.CSForm ~= nil then
self.CSForm:OnClose(object,sender);
end
end
--获取显示的参数
function UIBaseForm:GetShowParam()
if self.CSForm ~= nil then
return self.CSForm.ShowParamObj;
end
return nil;
end
--获取默认隐藏的参数
function UIBaseForm:GetHideParam()
if self.CSForm ~= nil then
return self.CSForm.HideParamObj;
end
return nil;
end
--创建特效(一个对象只能创建一个特效)
function UIBaseForm:CreateVFX(trans, id, modelTypeCode, OnFinishedCallBack, isNotSynRQ, layer)
if not self.VFXDic then
self.VFXDic = Dictionary:New();
end
if self.VFXDic:ContainsKey(trans) then
self.VFXDic[trans]:OnPlay()
return self.VFXDic[trans];
end
local _uiVfx = UIUtils.RequireUIVfxSkinCompoent(trans);
self.VFXDic:Add(trans, _uiVfx);
_uiVfx:OnCreateAndPlay(modelTypeCode or ModelTypeCode.UIVFX, id, layer or LayerUtils.AresUI, OnFinishedCallBack, not isNotSynRQ)
return _uiVfx;
end
--删除特效
function UIBaseForm:DestroyVFX(trans)
if self.VFXDic then
if self.VFXDic:ContainsKey(trans) then
self.VFXDic[trans]:OnDestory();
self.VFXDic:Remove(trans)
end
end
end
--删除所有特效
function UIBaseForm:DestroyAllVFX()
if self.VFXDic then
local _keys = self.VFXDic:GetKeys();
for i=1, #_keys do
self.VFXDic[_keys[i]]:OnDestory();
end
self.VFXDic:Clear();
end
end
--设置该窗体的层级 UIFormRegion.cs
-- NoticRegion = 1500000, //显示提示,自动删除等处理,不用玩家操作的窗体
-- TopRegion = 1000000, //Top层,
-- MiddleRegion = 0, //主要功能的窗体都在这个层次中
-- MainRegion = -1000000, //主界面层
-- BottomRegion = -2000000, //类似HUD在这个层次中,属于最低层次
function UIBaseForm:SetRegion(FormRegion)
self.CSForm.UIRegion = FormRegion or UIFormRegion.MiddleRegion
end
return UIBaseForm