252 lines
5.6 KiB
Lua
252 lines
5.6 KiB
Lua
--window 窗口基类
|
||
--author:--
|
||
|
||
BaseWindow = {
|
||
--view description
|
||
_view = nil,
|
||
|
||
--View 是否被销毁
|
||
_is_destroy = false,
|
||
--是否播放动画
|
||
_animation = true,
|
||
--弹出动画,0关闭,1左边,2右边
|
||
_anim_pop = 0,
|
||
--关闭摧毁
|
||
_close_destroy = false,
|
||
|
||
--点击窗口以外关闭
|
||
_close_zone = true,
|
||
|
||
--队列
|
||
_queue = true,
|
||
--全屏
|
||
_full = false,
|
||
--全屏偏移
|
||
_full_offset = true,
|
||
--新窗口隐藏队列
|
||
_new_hide = true,
|
||
--模糊组件对象
|
||
_put_map = true
|
||
}
|
||
|
||
--window 列表
|
||
local WindowMap = {
|
||
|
||
}
|
||
|
||
local WindowQueue= {
|
||
|
||
}
|
||
|
||
local M = BaseWindow
|
||
|
||
function BaseWindow.new(url,blur_view)
|
||
local self = setmetatable({}, {__index = M})
|
||
self.class = "BaseWindow"
|
||
-- self._blur_view = blur_view
|
||
self:init(url)
|
||
return self
|
||
end
|
||
|
||
local win_url = {
|
||
"ui://Common/Gcm_Window",
|
||
"ui://Common/Gcm_Window_Full"
|
||
}
|
||
|
||
function M:init(url)
|
||
self._root_view = UIPackage.CreateObjectFromURL(self._full and win_url[2] or win_url[1])
|
||
local contentPane = self._root_view:GetChild("contentPane")
|
||
local ctr_hide_bg = self._root_view:GetController("hide_bg")
|
||
if self._anim_pop ~= 0 then
|
||
ctr_hide_bg.selectedIndex = 1
|
||
PopPanel = contentPane:GetChild("PopPanel")
|
||
else
|
||
ctr_hide_bg.selectedIndex = 0
|
||
end
|
||
self._view = UIPackage.CreateObjectFromURL(url)
|
||
-- self._view.fairyBatching = true
|
||
local btn_close = self._view:GetChild("btn_close")
|
||
if(btn_close) then
|
||
btn_close.onClick:Set(function()
|
||
self:CloseEvent()
|
||
end)
|
||
end
|
||
|
||
|
||
local win_mode = self._root_view:GetChild("win_mode")
|
||
win_mode.onClick:Set(function()
|
||
if not self._close_zone then
|
||
return
|
||
end
|
||
win_mode.touchable = false
|
||
self:CloseEvent()
|
||
end)
|
||
|
||
if self._full then
|
||
local offset = get_offset(self._full_offset)
|
||
if self._anim_pop == 0 then
|
||
self._view:AddRelation(contentPane, RelationType.Size)
|
||
contentPane:AddChild(self._view)
|
||
else
|
||
contentPane:RemoveRelation(self._root_view, RelationType.Center_Center)
|
||
contentPane:AddRelation(self._root_view, RelationType.Middle_Middle)
|
||
PopPanel:AddChild(self._view)
|
||
local click_item = PopPanel:GetChild("click_item")
|
||
if self._anim_pop == 1 then
|
||
contentPane:AddRelation(self._root_view, RelationType.Left_Left)
|
||
self._view.x = 0
|
||
elseif self._anim_pop == 2 then
|
||
contentPane:AddRelation(self._root_view, RelationType.Right_Right)
|
||
self._view.x = GRoot.inst.width - self._view.width - offset
|
||
end
|
||
self._view.y = (PopPanel.height - self._view.height) * 0.5
|
||
click_item.xy = self._view.xy
|
||
click_item.width = self._view.width
|
||
click_item.height = self._view.height
|
||
end
|
||
else
|
||
contentPane:AddChild(self._view)
|
||
contentPane.height = self._view.height
|
||
contentPane.width = self._view.width
|
||
contentPane:Center()
|
||
end
|
||
self._contentPane = contentPane
|
||
if self._put_map then
|
||
WindowMap[#WindowMap + 1] = self
|
||
end
|
||
end
|
||
|
||
-- 显示窗口
|
||
function M:Show()
|
||
local contentPane = self._root_view:GetChild("contentPane")
|
||
if self._anim_pop == 1 then
|
||
contentPane:GetTransition("left_pop"):Play()
|
||
elseif self._anim_pop == 2 then
|
||
contentPane:GetTransition("right_pop"):Play()
|
||
elseif self._animation then
|
||
local ani_in = self._root_view:GetTransition("in")
|
||
if ani_in then
|
||
ani_in:Play()
|
||
end
|
||
end
|
||
-- if self._blur_view then
|
||
-- BlurView(self._blur_view,true)
|
||
-- end
|
||
|
||
-- 判断当前窗口是否已经在队列中,如果在就不重复添加
|
||
local _inQueue = false
|
||
|
||
if self._new_hide then
|
||
for i=1,#WindowQueue do
|
||
local win = WindowQueue[i]
|
||
if win == self then
|
||
_inQueue = true
|
||
end
|
||
if win._queue then
|
||
win._root_view:RemoveFromParent()
|
||
end
|
||
end
|
||
end
|
||
if self._queue and not _inQueue then
|
||
WindowQueue[#WindowQueue + 1] = self
|
||
end
|
||
AddPanel(self._root_view)
|
||
if self._full then
|
||
local offset = get_offset(self._full_offset)
|
||
self._contentPane.width = GRoot.inst.width - 2 * offset
|
||
self._contentPane.height = GRoot.inst.height
|
||
self._contentPane.x = offset
|
||
end
|
||
end
|
||
|
||
-- 关闭窗口
|
||
function M:Close()
|
||
-- if self._blur_view then
|
||
-- BlurView(self._blur_view,false)
|
||
-- end
|
||
if self._queue then
|
||
for i,v in ipairs(WindowQueue) do
|
||
if v == self then
|
||
table.remove(WindowQueue,i)
|
||
break
|
||
end
|
||
end
|
||
end
|
||
|
||
if self._new_hide then
|
||
local win = WindowQueue[#WindowQueue]
|
||
if win and win._queue then
|
||
AddPanel(win._root_view)
|
||
end
|
||
end
|
||
self._root_view:RemoveFromParent()
|
||
end
|
||
|
||
local _destroy_all = false
|
||
-- 销毁窗口
|
||
function M:Destroy()
|
||
if self._is_destroy then
|
||
return
|
||
end
|
||
if not _destroy_all then
|
||
self:Close()
|
||
if self._put_map then
|
||
for i,v in ipairs(WindowMap) do
|
||
if v == self then
|
||
table.remove(WindowMap,i)
|
||
break
|
||
end
|
||
end
|
||
end
|
||
end
|
||
self._is_destroy = true
|
||
self._root_view:Dispose()
|
||
end
|
||
|
||
function M:CloseEvent()
|
||
local win_mode = self._root_view:GetChild("win_mode")
|
||
if self._anim_pop == 0 then
|
||
if self._close_destroy then
|
||
self:Destroy()
|
||
else
|
||
self:Close()
|
||
win_mode.touchable = true
|
||
end
|
||
else
|
||
self:ActionWithAnim(function()
|
||
if self._close_destroy then
|
||
self:Destroy()
|
||
else
|
||
self:Close()
|
||
win_mode.touchable = true
|
||
end
|
||
end)
|
||
end
|
||
end
|
||
|
||
function M:ActionWithAnim(callback)
|
||
local contentPane = self._root_view:GetChild("contentPane")
|
||
if self._anim_pop == 1 then
|
||
contentPane:GetTransition("left_pop_back"):Play()
|
||
elseif self._anim_pop == 2 then
|
||
contentPane:GetTransition("right_pop_back"):Play()
|
||
end
|
||
if callback then
|
||
coroutine.start(function()
|
||
coroutine.wait(0.3)
|
||
callback()
|
||
end)
|
||
end
|
||
end
|
||
|
||
function BaseWindow.DestroyAll()
|
||
_destroy_all = true
|
||
local list = WindowMap
|
||
for i=1,#list do
|
||
local win = list[i]
|
||
win:Destroy()
|
||
end
|
||
_destroy_all = false
|
||
WindowQueue = {}
|
||
WindowMap = {}
|
||
end |