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.

175 lines
5.3 KiB

  1. local OFFSET_X = 20
  2. local OFFSET_OUT_X = 37
  3. local BaseLayer = require("luaScript.ModuleEapSdk.BaseLayers.BaseLayer")
  4. local LayerFloat = EapClass("LayerFloat", BaseLayer)
  5. function LayerFloat.createWithData(__node, __zorder)
  6. local layer = LayerFloat.new()
  7. layer.m_parent = __node
  8. __node:addChild(layer, __zorder)
  9. return layer
  10. end
  11. function LayerFloat:myShow()
  12. self:addListener(true)
  13. self:initUI()
  14. end
  15. function LayerFloat:myClose()
  16. end
  17. function LayerFloat:initUI()
  18. self.imgPath = {
  19. "res/ModuleEapSdk/eapsdk_wj_icon_float.png",
  20. "res/ModuleEapSdk/eapsdk_wj_icon_warn.png",
  21. }
  22. local fix_x = display.width
  23. local fix_y = display.cy
  24. self.imgFloat = G_EapAbbreviation.createImageView(self.imgPath[1], self, cc.p(fix_x-OFFSET_X, fix_y+200))
  25. self.imgFloat:setScale(0.7)
  26. self.imgWarn = G_EapAbbreviation.createImageView(self.imgPath[2], self.imgFloat, cc.p(20, 80), false, 3, 1104, cc.p(1, 0.5))
  27. self.imgWarn:setScale(0.9)
  28. self.m_FloatState = 1 --悬浮球位置 1:右,2:上 3:左 4:下
  29. end
  30. --悬浮球伸缩
  31. function LayerFloat:setFloatOut(__state)
  32. if __state then
  33. if self.m_FloatState == 1 then
  34. self.imgFloat:setPositionX(display.width-OFFSET_OUT_X)
  35. elseif self.m_FloatState == 2 then
  36. self.imgFloat:setPositionY(display.height-OFFSET_OUT_X)
  37. elseif self.m_FloatState == 3 then
  38. self.imgFloat:setPositionX(OFFSET_OUT_X)
  39. elseif self.m_FloatState == 4 then
  40. self.imgFloat:setPositionY(OFFSET_OUT_X)
  41. end
  42. else
  43. if self.m_FloatState == 1 then
  44. self.imgFloat:setPositionX(display.width-OFFSET_X)
  45. elseif self.m_FloatState == 2 then
  46. self.imgFloat:setPositionY(display.height-OFFSET_X)
  47. elseif self.m_FloatState == 3 then
  48. self.imgFloat:setPositionX(OFFSET_X + 40)
  49. elseif self.m_FloatState == 4 then
  50. self.imgFloat:setPositionY(OFFSET_X)
  51. end
  52. end
  53. end
  54. function LayerFloat:setWarnVisible(__state)
  55. self.imgWarn:setVisible(__state)
  56. end
  57. function LayerFloat:onTouchBegin(__touch)
  58. if G_LayerMain and G_LayerMain:isVisible() then
  59. self.beginPos = __touch:getLocation()
  60. -- local rectFloat = self.imgFloat:getBoundingBox()
  61. local img_pos = self.imgFloat:getPosition()
  62. local r_x = img_pos.x - 60
  63. local r_y = img_pos.y - 60
  64. local r_w = 120
  65. local r_h = 120
  66. local rectFloat = cc.rect(r_x, r_y, r_w, r_h)
  67. if cc.rectContainsPoint(rectFloat, self.beginPos) then
  68. self._listener:setSwallowTouches(true)
  69. return true
  70. end
  71. end
  72. end
  73. function LayerFloat:onTouchMove(__touch)
  74. local curPos = __touch:getLocation()
  75. self.imgFloat:setPosition(curPos)
  76. end
  77. function LayerFloat:onTouchEnd(__touch)
  78. local curPos = __touch:getLocation()
  79. local isClick = false
  80. if math.abs(curPos.x - self.beginPos.x) < 20 and
  81. math.abs(curPos.y - self.beginPos.y) < 20 then
  82. print("LayerFloat:onTouchEnd =========== ")
  83. isClick = true
  84. self:setWarnVisible(false)
  85. G_LayerMain:showBoard(true)
  86. G_EapSocketLogin.request_markLog(MARK_LOG10001)
  87. end
  88. local w_ratio = curPos.x/display.width
  89. if w_ratio > 0.5 then
  90. w_ratio = 1 - w_ratio
  91. end
  92. local h_ratio = curPos.y/display.height
  93. if h_ratio > 0.5 then
  94. h_ratio = 1 - h_ratio
  95. end
  96. local offset = OFFSET_X
  97. if self.imgWarn:isVisible() then
  98. offset = OFFSET_OUT_X
  99. end
  100. if w_ratio < h_ratio then
  101. if curPos.x < display.cx then
  102. self.imgFloat:setPositionX(offset + 40)
  103. self.m_FloatState = 3
  104. else
  105. self.imgFloat:setPositionX(display.width-offset)
  106. self.m_FloatState = 1
  107. end
  108. else
  109. if curPos.y < display.cy then
  110. self.imgFloat:setPositionY(offset)
  111. self.m_FloatState = 4
  112. else
  113. self.imgFloat:setPositionY(display.height-offset)
  114. self.m_FloatState = 2
  115. end
  116. end
  117. if isClick then
  118. self:setFloatOut(false)
  119. end
  120. self._listener:setSwallowTouches(false)
  121. end
  122. function LayerFloat:onTouchCancel(__touch)
  123. if self.isTouching then
  124. local curPos = __touch:getLocation()
  125. if math.abs(curPos.x - self.beginPos.x) < 20 and
  126. math.abs(curPos.y - self.beginPos.y) < 20 then
  127. self:setWarnVisible(false)
  128. G_LayerMain:showBoard(true)
  129. else
  130. local w_ratio = curPos.x/display.width
  131. if w_ratio > 0.5 then
  132. w_ratio = 1 - w_ratio
  133. end
  134. local h_ratio = curPos.y/display.height
  135. if h_ratio > 0.5 then
  136. h_ratio = 1 - h_ratio
  137. end
  138. if w_ratio < h_ratio then
  139. if curPos.x < display.cx then
  140. self.imgFloat:setPositionX(OFFSET_X)
  141. else
  142. self.imgFloat:setPositionX(display.width-OFFSET_X)
  143. end
  144. else
  145. if curPos.y < display.cy then
  146. self.imgFloat:setPositionY(OFFSET_X)
  147. else
  148. self.imgFloat:setPositionY(display.height-OFFSET_X)
  149. end
  150. end
  151. end
  152. end
  153. self._listener:setSwallowTouches(false)
  154. end
  155. return LayerFloat