yunque9/lua_probject/main_project/main/majiang/HuTipView.lua

118 lines
3.5 KiB
Lua

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_Majiang/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
self._view.y = 0.675 * 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("lst_card")
-- lst_card:RemoveChildrenToPool()
-- local num = #cards
-- if num > 0 then
-- if num > 3 and num <= 28 then
-- self._view.width = 191 + ((num > 7 and 7 or num) - 3) * 38
-- self._view.height = 69 + (math.ceil(num / 7) - 1) * 56
-- else
-- self._view.width = 191
-- self._view.height = 69
-- end
-- for i = 1, num do
-- local item = lst_card:AddItemFromPool()
-- item:GetChild("icon").icon = "ui://Main_Majiang/202_" .. cards[i]
-- end
-- SetObjEnabled(self._view, true)
-- else
-- SetObjEnabled(self._view, false)
-- end
-- end
function M:FillData(cards, posX)
-- local btn_showtip = self._main_view._view:GetChild("btn_showtip")
local lst_card = self._view:GetChild("lst_card")
lst_card:RemoveChildrenToPool()
local num = #cards
if num > 0 then
local o_width = 192
local i_width = 93
local column_gap = 43
lst_card.columnGap = column_gap
if num > 1 then
self._view.width = o_width + (num - 1) * i_width
else
self._view.width = o_width
end
local count = 0
for i = 1, num do
local item = lst_card:AddItemFromPool()
local card = cards[i]
item:GetChild("icon").icon = "ui://Main_Majiang/" .. self:GetPrefix() .. "201_" .. card
local left_num = self._main_view:CountCardLeftNum(card)
local tex_num = item:GetChild("tex_num")
tex_num.text = left_num .. ""
tex_num.visible = true
count = count + left_num
end
self._view:GetChild("tex_num").text = count
if posX then
if posX + self._view.width > 1300 then
posX = 1300 - self._view.width
end
self._view.x = posX
SetObjEnabled(self._view, true)
else
-- self._view.x = self._main_view._view.width * 0.2 - self._view.width * 0.5
SetObjEnabled(self._view, true)
end
else
SetObjEnabled(self._view, false)
end
end
function M:GetPrefix()
return get_majiang_prefix(DataManager.CurrenRoom.game_id)
end
return M