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

212 lines
5.8 KiB
Lua

local MJPlayerCardInfoView = import(".MJPlayerCardInfoView")
local CardView = {
card = nil,
-- 牌序号
card_item = 0,
-- 索引
index = 0,
-- 原始位置
old_postion = Vector2.zero
}
local function NewCardView(card, cardItem)
local self = {}
setmetatable(self, { __index = CardView })
self.card = card
self.card_item = cardItem
return self
end
local PlayerSelfView = {
_dragCom = nil,
_carViewList = {}
}
local M = PlayerSelfView
function PlayerSelfView.new(view, mainView)
setmetatable(M, { __index = MJPlayerCardInfoView })
local self = setmetatable({}, { __index = M })
self._view = view
self._mainView = mainView
self:init()
return self
end
function M:init()
MJPlayerCardInfoView.init(self)
local filename = 'mjsize' .. DataManager.SelfUser.account_id
local json_data = Utils.LoadLocalFile(filename)
if json_data then
local _data = json.decode(json_data)
if _data.size == 0 then
self._area_handcard_list:SetScale(1, 1.1)
self._area_fz_list:SetScale(0.6, 0.6)
elseif _data.size == 1 then
self._area_handcard_list:SetScale(1, 1)
self._area_fz_list:SetScale(1, 1)
elseif _data.size == 2 then
self._area_handcard_list:SetScale(0.8, 0.8)
self._area_fz_list:SetScale(0.8, 0.8)
end
end
end
function M:setHandCardPos(btn_card, i, getcard)
btn_card.x = 71 + i * btn_card.width * 1.05 + (getcard and 20 or 0)
end
function M:UpdateHandCard(getcard, mp)
if self.outcard_button then
self.outcard_button:Dispose()
self.outcard_button = nil
end
getcard = getcard or false
mp = mp or false
local pv = self
local _carViewList = self._carViewList
local _lit = pv._area_handcard_list
for i = 1, #_carViewList do
_carViewList[i].card:Dispose()
end
_carViewList = {}
local _dragCom = self._dragCom
if (_dragCom == nil) then
_dragCom = UIPackage.CreateObject("Common", "UIPanel")
_dragCom.size = Vector2(1, 1)
pv._view:AddChild(_dragCom)
end
_dragCom.xy = pv._area_handcard_list.xy
self._dragCom = _dragCom
self._carViewList = _carViewList
local b3d = ""
if self._current_card_type == 2 then
b3d = "_3d"
end
local cards = DataManager.CurrenRoom.self_player.card_list
if (not mp) then
for i = 0, (#cards) - 1 do
local tem_card = cards[i + 1]
local btn_card = UIPackage.CreateObject(self:GetCardObjPack(), "Btn_Card" .. b3d)
btn_card:SetScale(1.05, 1.05)
-- btn_card.icon = UIPackage.GetItemURL("Main_Majiang", "201_" .. tem_card)
self:fillCard(btn_card, "201_", tem_card)
self:setHandCardPos(btn_card, i, i == #cards - 1 and getcard)
local c_v = NewCardView(btn_card, tem_card)
c_v.index = i
c_v.old_postion = btn_card.xy
_carViewList[#_carViewList + 1] = c_v
_lit:AddChild(btn_card)
if (not self._player.auto_out_card) then
btn_card.data = c_v
-- btn_card.draggable = true;
-- btn_card.onDragStart:Set(function(evt)
-- local _agent = evt.sender
-- _agent:RemoveFromParent()
-- _dragCom:AddChild(_agent)
-- if self.__OnDragStart then
-- self:__OnDragStart(evt)
-- end
-- _agent.onDragEnd:Set(self.__OnDragEnd,self)
-- end)
btn_card.onTouchBegin:Set(handler(self, self.onTouchBegin))
btn_card.onTouchMove:Set(handler(self, self.onTouchMove))
btn_card.onTouchEnd:Set(handler(self, self.__OnDragEnd))
btn_card.onClick:Set(self.__OnClickHandCard, self)
else
btn_card:GetChild("icon").color = Color(0.5, 0.5, 0.5)
btn_card.touchable = false
end
end
else
for i = 0, (#cards) - 1 do
local mp_card = UIPackage.CreateObject("Main_Majiang", "Mp_self_card" .. b3d)
-- mp_card.icon = UIPackage.GetItemURL("Main_Majiang", "202_" .. cards[i+1])
self:fillCard(mp_card, "202_", cards[i + 1])
mp_card:SetScale(1.05, 1.05)
-- mp_card.x = i * mp_card.width + (i == #cards - 1 and getcard and 20 or 0)
self:setHandCardPos(mp_card, i, i == #cards - 1 and getcard)
_lit:AddChild(mp_card)
end
end
end
function M:onTouchBegin(context)
local button = context.sender
local card = button.data
local xy = self._area_handcard_list:GlobalToLocal(Vector2.New(context.inputEvent.x, context.inputEvent.y))
card.touch_pos = xy - button.xy
if self.__OnDragStart then
self:__OnDragStart(card.card_item)
end
end
function M:onTouchMove(context)
local button = context.sender
local card = button.data
local xy = self._area_handcard_list:GlobalToLocal(Vector2.New(context.inputEvent.x, context.inputEvent.y))
button.xy = xy - card.touch_pos
end
function M:__OnDragEnd(context)
if self.outcard_button then
self.outcard_button:Dispose()
self.outcard_button = nil
end
local button = context.sender
-- button.onDragEnd:Set(nil)
--button:RemoveFromParent()
local card = button.data
local _room = DataManager.CurrenRoom
if not _room or _room:GetReloadStatus() then return end
-- print("button.y"..button.y .. "_room.curren_outcard_seat".._room.curren_outcard_seat)
if (button.y < -150 and _room.curren_outcard_seat == _room.self_player.seat) then
self._mainView:OutCard(card.card_item)
button.touchable = false
self.outcard_button = buttond
else
self._area_handcard_list:AddChildAt(button, card.index)
button:TweenMove(card.old_postion, 0.2)
end
end
function M:__OnClickHandCard(context)
local button = context.sender
local _carViewList = self._carViewList
for i = 1, #_carViewList do
if (_carViewList[i].card ~= button) then
_carViewList[i].card.selected = false
end
end
local _room = DataManager.CurrenRoom
if (Utils.IsDoubleClick(context) and _room.curren_outcard_seat == _room.self_player.seat) then
local card = button.data
self._mainView:OutCard(card.card_item)
end
end
-- 获取手牌资源位置,可以在扩展中复写
function M:GetCardObjPack()
return "Main_Majiang"
end
function M:Clear()
MJPlayerCardInfoView.Clear(self)
for i = 1, #self._carViewList do
self._carViewList[i].card:Dispose()
end
self._carViewList = {}
end
return M