local MJDefine=MJFramework.MJImport("mj.luaScript.MJDefine") local MJ=MJFramework.MJImport("mj.luaScript.Views.Game.MJ") local linshuiTangView = class("linshuiTangView", cc.UIView) function linshuiTangView:ctor(handCards,desktopType) linshuiTangView.super.ctor(self) --self._tingCardData = nil; self:loadUI() self.desktopType = desktopType --self:initData(handCards,desktopType) end function linshuiTangView:loadUI() local ui = loadUI("mj_linshui/res/ui_fangjian/mj_linshui_tangView.ui") self:addChild(ui) self.ui = ui self.ui.Items.Button_QueDing:registerClick(handler(self, self.onClickSure)) self.ui.Items.Button_QuXiao:registerClick(handler(self, self.onClickCancel)) end function linshuiTangView:initData(handCards,desktopType) self:clearTangViewData() self.handCards = self.handCards or {} local thandCards = handCards local handCardsNum = #handCards table.sort( thandCards, function(c1, c2) return c1.card < c2.card end ) --self.ui.Items.Layout_card:setSize((cc.size(70*handCardsNum,115))) local startPos = cc.p(36,46) local startX = startPos.x local iWidth = 0--牌间距 local iHeight = 0--牌高度 for i,v in pairs(thandCards) do --mj=MJ:new(v.card,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType) local CardClass local mjType = MJDefine.MJConfig_2d.MJType.Stand if self.desktopType ~= MJDefine.DesktopType.ThreeD then CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD) else CardClass = require(MJDefine.MJConfig_3d.MAHJONG_CARD) mjType = MJDefine.MJConfig_3d.MJType.Stand end local mj = CardClass:new(v.card, MJDefine.MyViewId, mjType, 1) mj.unique = i local iScale = 0.75 iWidth = mj:getSize().width * iScale iHeight = mj:getSize().height * iScale --mj:setAutoSize(false) --mj:setSize(cc.size(40,65)) --mj:setSize(cc.size(70,95)) mj:setScale(iScale) mj:setPosition(cc.p(startX,startPos.y)) startX = startX + iWidth --[[mj:registerClick(function() playBtnEffect() if not mj.isXuanZhong then mj.isXuanZhong = true --mj:setYellow() local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj.isXuanZhong = false mj:setNormalColor() end end)--]] self.ui.Items.Layout_card:addChild(mj) table.insert(self.handCards,mj) end self.ui.Items.ImageView_bg:setSize((cc.size(iWidth*handCardsNum+20,iHeight+30))) self.ui.Items.Layout_card:setSize((cc.size(iWidth*handCardsNum+10,115))) --注册触摸事件 self:registerTouch(handler(self,self.onTouchBegan), handler(self,self.onTouchMove), handler(self,self.onTouchEnd)) end function linshuiTangView:checkTouches( touch ) local rect = nil local node = nil local touchPos=self:convertToNodeSpace(touch:getLocation()) for k,v in pairs(self.handCards) do local width = v:getContentSize().width*v:getScaleX() local height = v:getContentSize().height*v:getScaleY() local x = v:getWorldPosition().x-width/2 local y = v:getWorldPosition().y-height/2 local rect = cc.Rectangle:new(x,y,width,height) if rect:contains(touchPos.x,touchPos.y) then node=v break end end return node end function linshuiTangView:onTouchBegan(pt) self._selectCards = {} local node = self:checkTouches(pt) local isValidTouch = false if node and node.unique>0 then isValidTouch = true self._selectCards[node.unique] = node end self:doSelectColor() if isValidTouch == true then return true else return false end end function linshuiTangView:onTouchMove( pt ) local node = self:checkTouches(pt) if node and self._selectCards[node.unique]==nil then self._selectCards[node.unique] = node end self:doSelectColor() end function linshuiTangView:onTouchEnd( pt ) self:doSelect() self:checkShowQueDing() end function linshuiTangView:doSelect() local i = 0 for _,mj in pairs(self._selectCards) do i = i + 1 if (not tolua.isnull(mj)) and mj then if not mj.isXuanZhong then mj.isXuanZhong = true --mj:setYellow() local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj.isXuanZhong = false mj:setNormalColor() end end end end function linshuiTangView:checkShowQueDing() local selectCards = self:getSelectCards() if selectCards and type(selectCards) == 'table' and #selectCards> 0 then self.ui.Items.Button_QueDing:setVisible(true) else self.ui.Items.Button_QueDing:setVisible(false) end end function linshuiTangView:doSelectColor() for _,mj in pairs(self._selectCards) do if not tolua.isnull(mj) and mj then if not mj.isXuanZhong then local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj:setNormalColor() end end end end function linshuiTangView:getSelectCards() local tTable = {} for _,mj in pairs(self.handCards) do if not tolua.isnull(mj) and mj then if mj.isXuanZhong and mj.isXuanZhong == true then table.insert(tTable,mj:getValue()) end end end return tTable end -- 确定 function linshuiTangView:onClickSure() playBtnEffect() local selectCards = self:getSelectCards() --请求亮牌 app.room:requestTangCards(selectCards,self.curSelectCard) --self:onClickClose() end -- 取消 function linshuiTangView:onClickCancel() playBtnEffect() if self.quxiaoCB then self.quxiaoCB() end self:onClickClose() end -- 关闭 function linshuiTangView:onClickClose() self:getEventDispatcher():removeEventListenersForTarget(self) self:removeFromParent() end function linshuiTangView:setQuXiaoCallBack(cb) if cb then self.quxiaoCB = cb end end --只显示取消按钮 function linshuiTangView:showCancelBtnOnly() self:getEventDispatcher():removeEventListenersForTarget(self) self.ui.Items.ImageView_bg:setVisible(false) self.ui.Items.Layout_card:setVisible(false) self.ui.Items.Button_QueDing:setVisible(false) self.ui.Items.Button_QuXiao:setVisible(true) self.ui.Items.ImageView_bg_1:setVisible(false) end --清空躺界面牌数据 function linshuiTangView:clearTangViewData() if self.handCards and #self.handCards then for i = #self.handCards,1,-1 do table.remove(self.handCards,i) end end self.handCards = nil self.ui.Items.Layout_card:removeAllChildren() end --显示躺牌界面 function linshuiTangView:showTangView(handCards,sltCard,desktopType)--手牌 选中的牌 牌类型 self.ui.Items.ImageView_bg:setVisible(true) self.ui.Items.Layout_card:setVisible(true) --self.ui.Items.Button_QueDing:setVisible(true) self.ui.Items.Button_QuXiao:setVisible(true) self.ui.Items.ImageView_bg_1:setVisible(true) self.curSelectCard = sltCard for i,v in pairs(handCards) do if v.card == sltCard then table.remove(handCards,i) break end end self:initData(handCards,desktopType) end return linshuiTangView