156 lines
5.7 KiB
Lua
156 lines
5.7 KiB
Lua
-- 选择时间按钮、选择时间界面
|
|
local TimeSettingPanel = {}
|
|
|
|
local M = TimeSettingPanel
|
|
|
|
function TimeSettingPanel.new(parent, btn_date1, btn_date2, posX, posY, callback, data)
|
|
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 = data and (data.showDay or 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
|
|
|
|
local function initList(list_start, list_end, showday, day1, day2)
|
|
local now_time = os.date("*t")
|
|
local today = os.time({ year = now_time.year, month = now_time.month, day = now_time.day, hour = 0, min = 0, sec = 0 })
|
|
list_start:SetVirtual()
|
|
list_start.itemRenderer = function(index, obj)
|
|
local time = today - 86400 * index
|
|
obj.data = time
|
|
obj.text = os.date("%m月%d日", time)
|
|
obj.selected = day1 == time
|
|
end
|
|
list_end:SetVirtual()
|
|
list_end.itemRenderer = function(index, obj)
|
|
local time = today - 86400 * index
|
|
obj.data = time
|
|
obj.text = os.date("%m月%d日", time)
|
|
obj.selected = day2 == time
|
|
end
|
|
list_start.numItems = showday
|
|
list_end.numItems = showday
|
|
list_start:RefreshVirtualList()
|
|
list_end:RefreshVirtualList()
|
|
end
|
|
|
|
-- 显示设置时间面板
|
|
function M:ShowSetDatePanel()
|
|
local com_set_date = UIPackage.CreateObjectFromURL("ui://NewGroup/com_set_time_together")
|
|
local list_start = com_set_date:GetChild("list_start")
|
|
local list_end = com_set_date:GetChild("list_end")
|
|
local now_time = os.date("*t")
|
|
local today = os.time({ year = now_time.year, month = now_time.month, day = now_time.day, hour = 0, min = 0, sec = 0 })
|
|
initList(list_start, list_end, self.show_day, self.btn1.data, self.btn2.data)
|
|
com_set_date:GetChild("btn_ok").onClick:Set(function()
|
|
self.btn1.data = today - 86400 * list_start.selectedIndex
|
|
self.btn1.title = os.date("%Y年%m月%d日", self.btn1.data)
|
|
self.btn2.data = today - 86400 * list_end.selectedIndex
|
|
self.btn2.title = os.date("%Y年%m月%d日", self.btn2.data)
|
|
com_set_date:Dispose()
|
|
if self.callback then self.callback() end
|
|
end)
|
|
com_set_date:GetChild("btn_cancel").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()
|
|
end)
|
|
AddPanelCenter(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
|