You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.7 KiB

  1. local MJDefine=MJFramework.MJImport("mj.luaScript.MJDefine")
  2. local MJ=MJFramework.MJImport("mj.luaScript.Views.Game.MJ")
  3. local linshuiTangView = class("linshuiTangView", cc.UIView)
  4. function linshuiTangView:ctor(handCards,desktopType)
  5. linshuiTangView.super.ctor(self)
  6. --self._tingCardData = nil;
  7. self:loadUI()
  8. self.desktopType = desktopType
  9. --self:initData(handCards,desktopType)
  10. end
  11. function linshuiTangView:loadUI()
  12. local ui = loadUI("mj_linshui/res/ui_fangjian/mj_linshui_tangView.ui")
  13. self:addChild(ui)
  14. self.ui = ui
  15. self.ui.Items.Button_QueDing:registerClick(handler(self, self.onClickSure))
  16. self.ui.Items.Button_QuXiao:registerClick(handler(self, self.onClickCancel))
  17. end
  18. function linshuiTangView:initData(handCards,desktopType)
  19. self:clearTangViewData()
  20. self.handCards = self.handCards or {}
  21. local thandCards = handCards
  22. local handCardsNum = #handCards
  23. table.sort(
  24. thandCards,
  25. function(c1, c2)
  26. return c1.card < c2.card
  27. end
  28. )
  29. --self.ui.Items.Layout_card:setSize((cc.size(70*handCardsNum,115)))
  30. local startPos = cc.p(36,46)
  31. local startX = startPos.x
  32. local iWidth = 0--牌间距
  33. local iHeight = 0--牌高度
  34. for i,v in pairs(thandCards) do
  35. --mj=MJ:new(v.card,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  36. local CardClass
  37. local mjType = MJDefine.MJConfig_2d.MJType.Stand
  38. if self.desktopType ~= MJDefine.DesktopType.ThreeD then
  39. CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  40. else
  41. CardClass = require(MJDefine.MJConfig_3d.MAHJONG_CARD)
  42. mjType = MJDefine.MJConfig_3d.MJType.Stand
  43. end
  44. local mj = CardClass:new(v.card, MJDefine.MyViewId, mjType, 1)
  45. mj.unique = i
  46. local iScale = 0.75
  47. iWidth = mj:getSize().width * iScale
  48. iHeight = mj:getSize().height * iScale
  49. --mj:setAutoSize(false)
  50. --mj:setSize(cc.size(40,65))
  51. --mj:setSize(cc.size(70,95))
  52. mj:setScale(iScale)
  53. mj:setPosition(cc.p(startX,startPos.y))
  54. startX = startX + iWidth
  55. --[[mj:registerClick(function()
  56. playBtnEffect()
  57. if not mj.isXuanZhong then
  58. mj.isXuanZhong = true
  59. --mj:setYellow()
  60. local color = cc.c3b(218, 218, 128)
  61. mj:setColor(color)
  62. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  63. mj.isXuanZhong = false
  64. mj:setNormalColor()
  65. end
  66. end)--]]
  67. self.ui.Items.Layout_card:addChild(mj)
  68. table.insert(self.handCards,mj)
  69. end
  70. self.ui.Items.ImageView_bg:setSize((cc.size(iWidth*handCardsNum+20,iHeight+30)))
  71. self.ui.Items.Layout_card:setSize((cc.size(iWidth*handCardsNum+10,115)))
  72. --注册触摸事件
  73. self:registerTouch(handler(self,self.onTouchBegan), handler(self,self.onTouchMove), handler(self,self.onTouchEnd))
  74. end
  75. function linshuiTangView:checkTouches( touch )
  76. local rect = nil
  77. local node = nil
  78. local touchPos=self:convertToNodeSpace(touch:getLocation())
  79. for k,v in pairs(self.handCards) do
  80. local width = v:getContentSize().width*v:getScaleX()
  81. local height = v:getContentSize().height*v:getScaleY()
  82. local x = v:getWorldPosition().x-width/2
  83. local y = v:getWorldPosition().y-height/2
  84. local rect = cc.Rectangle:new(x,y,width,height)
  85. if rect:contains(touchPos.x,touchPos.y) then
  86. node=v
  87. break
  88. end
  89. end
  90. return node
  91. end
  92. function linshuiTangView:onTouchBegan(pt)
  93. self._selectCards = {}
  94. local node = self:checkTouches(pt)
  95. local isValidTouch = false
  96. if node and node.unique>0 then
  97. isValidTouch = true
  98. self._selectCards[node.unique] = node
  99. end
  100. self:doSelectColor()
  101. if isValidTouch == true then
  102. return true
  103. else
  104. return false
  105. end
  106. end
  107. function linshuiTangView:onTouchMove( pt )
  108. local node = self:checkTouches(pt)
  109. if node and self._selectCards[node.unique]==nil then
  110. self._selectCards[node.unique] = node
  111. end
  112. self:doSelectColor()
  113. end
  114. function linshuiTangView:onTouchEnd( pt )
  115. self:doSelect()
  116. self:checkShowQueDing()
  117. end
  118. function linshuiTangView:doSelect()
  119. local i = 0
  120. for _,mj in pairs(self._selectCards) do
  121. i = i + 1
  122. if (not tolua.isnull(mj)) and mj then
  123. if not mj.isXuanZhong then
  124. mj.isXuanZhong = true
  125. --mj:setYellow()
  126. local color = cc.c3b(218, 218, 128)
  127. mj:setColor(color)
  128. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  129. mj.isXuanZhong = false
  130. mj:setNormalColor()
  131. end
  132. end
  133. end
  134. end
  135. function linshuiTangView:checkShowQueDing()
  136. local selectCards = self:getSelectCards()
  137. if selectCards and type(selectCards) == 'table' and #selectCards> 0 then
  138. self.ui.Items.Button_QueDing:setVisible(true)
  139. else
  140. self.ui.Items.Button_QueDing:setVisible(false)
  141. end
  142. end
  143. function linshuiTangView:doSelectColor()
  144. for _,mj in pairs(self._selectCards) do
  145. if not tolua.isnull(mj) and mj then
  146. if not mj.isXuanZhong then
  147. local color = cc.c3b(218, 218, 128)
  148. mj:setColor(color)
  149. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  150. mj:setNormalColor()
  151. end
  152. end
  153. end
  154. end
  155. function linshuiTangView:getSelectCards()
  156. local tTable = {}
  157. for _,mj in pairs(self.handCards) do
  158. if not tolua.isnull(mj) and mj then
  159. if mj.isXuanZhong and mj.isXuanZhong == true then
  160. table.insert(tTable,mj:getValue())
  161. end
  162. end
  163. end
  164. return tTable
  165. end
  166. -- 确定
  167. function linshuiTangView:onClickSure()
  168. playBtnEffect()
  169. local selectCards = self:getSelectCards()
  170. --请求亮牌
  171. app.room:requestTangCards(selectCards,self.curSelectCard)
  172. --self:onClickClose()
  173. end
  174. -- 取消
  175. function linshuiTangView:onClickCancel()
  176. playBtnEffect()
  177. if self.quxiaoCB then
  178. self.quxiaoCB()
  179. end
  180. self:onClickClose()
  181. end
  182. -- 关闭
  183. function linshuiTangView:onClickClose()
  184. self:getEventDispatcher():removeEventListenersForTarget(self)
  185. self:removeFromParent()
  186. end
  187. function linshuiTangView:setQuXiaoCallBack(cb)
  188. if cb then
  189. self.quxiaoCB = cb
  190. end
  191. end
  192. --只显示取消按钮
  193. function linshuiTangView:showCancelBtnOnly()
  194. self:getEventDispatcher():removeEventListenersForTarget(self)
  195. self.ui.Items.ImageView_bg:setVisible(false)
  196. self.ui.Items.Layout_card:setVisible(false)
  197. self.ui.Items.Button_QueDing:setVisible(false)
  198. self.ui.Items.Button_QuXiao:setVisible(true)
  199. self.ui.Items.ImageView_bg_1:setVisible(false)
  200. end
  201. --清空躺界面牌数据
  202. function linshuiTangView:clearTangViewData()
  203. if self.handCards and #self.handCards then
  204. for i = #self.handCards,1,-1 do
  205. table.remove(self.handCards,i)
  206. end
  207. end
  208. self.handCards = nil
  209. self.ui.Items.Layout_card:removeAllChildren()
  210. end
  211. --显示躺牌界面
  212. function linshuiTangView:showTangView(handCards,sltCard,desktopType)--手牌 选中的牌 牌类型
  213. self.ui.Items.ImageView_bg:setVisible(true)
  214. self.ui.Items.Layout_card:setVisible(true)
  215. --self.ui.Items.Button_QueDing:setVisible(true)
  216. self.ui.Items.Button_QuXiao:setVisible(true)
  217. self.ui.Items.ImageView_bg_1:setVisible(true)
  218. self.curSelectCard = sltCard
  219. for i,v in pairs(handCards) do
  220. if v.card == sltCard then
  221. table.remove(handCards,i)
  222. break
  223. end
  224. end
  225. self:initData(handCards,desktopType)
  226. end
  227. return linshuiTangView