changhong/lua_probject/base_project/Game/View/NewGroup/MngView/TimeSettingPanelTog.lua

124 lines
4.1 KiB
Lua
Raw Normal View History

2025-05-28 03:10:00 +08:00
-- 选择时间按钮、选择时间界面
local TimeSettingPanel = {}
local M = TimeSettingPanel
function TimeSettingPanel.new(parent, btn_date1, btn_date2, posX, posY, callback, flag)
local self = setmetatable({}, { __index = M })
self.class = "TimeSettingPanel"
self.parent = parent
self.btn1 = btn_date1
self.btn2 = btn_date2
self.posX = posX or 0
self.posY = posY or 0
self.callback = callback
self.show_day = flag and 7 or 7
self:initView()
return self
end
local function initButton(self, btn_date, today)
btn_date.data = today
btn_date.title = os.date("%Y年%m月%d日", today)
btn_date.onClick:Set(function()
self:ShowSetDatePanel(btn_date)
end)
end
function M:initView()
local now_time = os.date("*t", now)
local today = os.time({ year = now_time.year, month = now_time.month, day = now_time.day, hour = 0, min = 0, sec = 0 })
initButton(self, self.btn1, today)
initButton(self, self.btn2, today)
end
function M:GetDate()
local time1 = self.btn1.data
local time2 = self.btn2.data
local begin_time = math.min(time1, time2)
local end_time = math.max(time1, time2) + 86400
return begin_time, end_time
end
function M:SetTenDay()
local now_time = os.date("*t", now)
local today = os.time({ year = now_time.year, month = now_time.month, day = now_time.day, hour = 0, min = 0, sec = 0 })
local nine_day_ago = today - 86400 * 9
self.btn1.data = nine_day_ago
self.btn1.title = os.date("%Y年%m月%d日", nine_day_ago)
self.btn2.data = today
self.btn2.title = os.date("%Y年%m月%d日", today)
end
local max_pos, min_pos = 0.2, 2.2
-- 添加列表特效:中部的组件放大,两边的渐渐透明
local function addListEffect(lst)
local mid_y = 0.5 * lst.height
local cnt = lst.numChildren
for i = 0, cnt - 1 do
local item = lst:GetChildAt(i)
local index = lst.scrollPane.percY * (lst.numChildren - 5)
local dist = math.abs(index + 2 - i) -- 距离列表中点的位置
if dist <= max_pos then
item.alpha = 1
item:GetChild("title"):SetScale(1, 1)
elseif dist <= min_pos then
local scale = dist / min_pos
item.alpha = 1 - 0.7 * scale
item:GetChild("title"):SetScale(1 - 0.5 * scale, 1 - 0.5 * scale)
else
item.alpha = 0.3
item:GetChild("title"):SetScale(0.5, 0.5)
end
lst.data = lst:GetChildAt(2).data - math.floor(index + 0.5) * 86400
end
end
-- 显示日期
-- +/-2的原因是列表中需要两个看不见的组件
local function initList(lst, date, show_day)
local now_time = os.date("*t", now)
local today = os.time({ year = now_time.year, month = now_time.month, day = now_time.day, hour = 0, min = 0, sec = 0 })
lst:RemoveChildrenToPool()
local index = -1
local num = show_day - 1
for i = 0 - 2, num + 2 do
local item = lst:AddItemFromPool()
if i >= 0 and i <= num then
local time = today - 86400 * i
item.data = time
item.title = os.date("%Y年%m月%d日", time)
if time == date then
index = i
end
else
item.title = " "
end
end
lst.scrollPane.percY = index / num
addListEffect(lst)
lst.scrollPane.onScroll:Add(function()
addListEffect(lst)
end)
end
-- 显示设置时间面板
function M:ShowSetDatePanel(btn)
local date = btn.data
local com_set_date = UIPackage.CreateObjectFromURL("ui://NewGroup/com_set_time")
local lst_y = com_set_date:GetChild("lst_year")
initList(lst_y, date, self.show_day)
com_set_date:GetChild("btn_confirm").onClick:Set(function()
local time = os.date("%Y年%m月%d日", lst_y.data)
btn.title = time
btn.data = lst_y.data
com_set_date:Dispose()
if self.callback then self.callback() end
end)
AddPanel(com_set_date)
-- self.parent:AddChild(com_set_date)
-- com_set_date.x = self.posX
-- com_set_date.y = GRoot.inst.height - com_set_date.height - (GRoot.inst.height - self.parent.height) * 0.5 + self.posY
end
return M