jxlast/lua_probject/main_project/main/zipai/HuTipView.lua

69 lines
2.2 KiB
Lua
Raw Permalink Normal View History

2025-11-14 23:38:35 +08:00
local HuTipView = {
_view_start_pos = nil,
_touch_start_pos = nil,
}
local M = HuTipView
function M.new(main_view)
local self = {}
self.class = "HuTipView"
setmetatable(self,{__index = HuTipView})
self._main_view = main_view
self:init()
return self
end
local function SetObjEnabled(obj, enabled)
obj.visible = enabled
obj.touchable = enabled
end
function M:OnTouchBegin(context)
self._view_start_pos = Vector2(self._view.x, self._view.y)
self._touch_start_pos = self._main_view._view:GlobalToLocal(Vector2(context.inputEvent.x, context.inputEvent.y))
end
function M:OnTouchMove(context)
local xy = self._main_view._view:GlobalToLocal(Vector2.New(context.inputEvent.x,context.inputEvent.y))
local dist = Vector2(xy.x - self._touch_start_pos.x, xy.y - self._touch_start_pos.y)
local posx = self._view_start_pos.x + dist.x
local posy = self._view_start_pos.y + dist.y
local max_x = self._main_view._view.width - self._view.width
local max_y = self._main_view._view.height - self._view.height
self._view.x = posx < 0 and 0 or posx > max_x and max_x or posx
self._view.y = posy < 0 and 0 or posy > max_y and max_y or posy
end
function M:init()
self._view = UIPackage.CreateObjectFromURL("ui://Main_RunBeard/Hu_Tip")
self._main_view._view:AddChild(self._view)
local width = self._view.width
local m_width = self._main_view._view.width
local m_height = self._main_view._view.height
-- self._view.x = 0.5 * m_width - 0.5 * width
self._view.x = 0
self._view.y = 0.7 * m_height
SetObjEnabled(self._view, false)
self._view.onTouchBegin:Add(handler(self, self.OnTouchBegin))
self._view.onTouchMove:Add(handler(self, self.OnTouchMove))
end
function M:FillData(cards)
local lst_card = self._view:GetChild("tingpai")
lst_card:RemoveChildrenToPool()
local num = #cards
if num > 0 then
if num > 4 and num < 28 then
self._view.width = 191 + ((num > 4 and 6 or num) - 3) * 38
self._view.height = 69 + (math.ceil(num / 5) - 1) * 56
end
for i = 1, num do
local item = lst_card:AddItemFromPool()
item:GetChild("icon").icon = "ui://Main_RunBeard/202_1_" .. cards[i]
end
SetObjEnabled(self._view, true)
else
SetObjEnabled(self._view, false)
end
end
return M