源战役客户端
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.

179 lines
4.5 KiB

  1. --拖住功能
  2. DragViewMgr = DragViewMgr or BaseClass()
  3. local DragViewMgr = DragViewMgr
  4. DragViewMgr.TouchType =
  5. {
  6. TouchDown = 1,
  7. TouchMove = 2,
  8. TouchUp = 3,
  9. }
  10. function DragViewMgr:__init()
  11. self.base_file = false
  12. self.layout_file = false
  13. self.begin_callback = false
  14. self.draging_callback = false
  15. self.end_callback = false
  16. self.item_class = false
  17. self.drag_cache_item = false
  18. self.start_drag = false
  19. self.draging = false
  20. self.old_pos = false
  21. end
  22. function DragViewMgr:Init(info)
  23. self.base_file = info.base_file
  24. self.layout_file = info.layout_file
  25. self.begin_callback = info.begin_callback
  26. self.draging_callback = info.draging_callback
  27. self.end_callback = info.end_callback
  28. self.item_class = info.item_class
  29. self.item_parent = info.item_parent or MainUIController:GetInstance().GoodsFlyView.transform
  30. end
  31. function DragViewMgr:__delete()
  32. if self.drag_cache_item then
  33. self.drag_cache_item:DeleteMe()
  34. self.drag_cache_item = nil
  35. end
  36. end
  37. function DragViewMgr:Start()
  38. self.start_drag = true
  39. self.draging = false
  40. Runner.Instance:AddRunObj(self, 1)
  41. end
  42. function DragViewMgr:Stop()
  43. Runner.Instance:RemoveRunObj(self)
  44. self.start_drag = false
  45. self.draging = false
  46. self.old_pos = false
  47. end
  48. function DragViewMgr:OnDragBegin(pos)
  49. if not pos then return end
  50. if not self.drag_cache_item then
  51. self.drag_cache_item = self:CreatDragItem()
  52. end
  53. if not self.drag_cache_item then
  54. print(">>>>>>>>>>>> 拖拽物体没有创建成功")
  55. return
  56. end
  57. if self.begin_callback then
  58. self.begin_callback(self.drag_cache_item)
  59. end
  60. self.drag_cache_item:SetVisible(true)
  61. local w = ScreenWidth
  62. local h = ScreenHeight
  63. pos.x = w / 2 + pos.x
  64. pos.y = h / 2 + pos.y
  65. self.drag_cache_item:SetPosition(pos.x, pos.y)
  66. end
  67. function DragViewMgr:CreatDragItem()
  68. local item = nil
  69. if self.item_class ~= nil then
  70. item = self.item_class.New(self.item_parent)
  71. self.drag_cache_item = item
  72. end
  73. return item
  74. end
  75. function DragViewMgr:OnDragIng(pos)
  76. local w = ScreenWidth
  77. local h = ScreenHeight
  78. pos.x = w / 2 + pos.x
  79. pos.y = h / 2 + pos.y
  80. if self.drag_cache_item then
  81. self.drag_cache_item:SetPosition(pos.x, pos.y)
  82. end
  83. if self.draging_callback then
  84. self.draging_callback(self.drag_cache_item, pos)
  85. end
  86. end
  87. function DragViewMgr:OnDragEnd(pos)
  88. Runner.Instance:RemoveRunObj(self)
  89. if self.end_callback then
  90. self.end_callback(self.drag_cache_item, pos)
  91. end
  92. if self.drag_cache_item then
  93. self.drag_cache_item:SetVisible(false)
  94. end
  95. end
  96. function DragViewMgr:Update()
  97. if not self.start_drag then return end
  98. local touch_type, pos = self:GetDragTouchTypeAndPos()
  99. if not pos or not touch_type then
  100. if self.draging then
  101. self:Stop()
  102. self:OnDragEnd(self.old_pos)
  103. end
  104. end
  105. if touch_type == DragViewMgr.TouchType.TouchDown or touch_type == DragViewMgr.TouchType.TouchMove then
  106. if not self.draging then
  107. self:OnDragBegin(pos)
  108. self.draging = true
  109. self.old_pos = pos
  110. return
  111. end
  112. self.old_pos = pos
  113. self:OnDragIng(pos)
  114. else
  115. self:OnDragEnd(pos)
  116. self:Stop()
  117. end
  118. end
  119. function DragViewMgr:GetDragTouchTypeAndPos()
  120. local touch_type = nil
  121. local pos = nil
  122. if Input.touchCount > 0 then
  123. local touch = Input.GetTouch(0)
  124. if touch then
  125. pos = touch.position
  126. pos.x = pos.x / ScreenConvertRatio - ScreenWidth*0.5
  127. pos.y = pos.y / ScreenConvertRatio - ScreenHeight*0.5
  128. if touch.phase == TouchPhase.Began then
  129. touch_type = DragViewMgr.TouchType.TouchDown
  130. elseif touch.phase == TouchPhase.Moved then
  131. touch_type = DragViewMgr.TouchType.TouchMove
  132. elseif touch.phase == TouchPhase.Ended then
  133. touch_type = DragViewMgr.TouchType.TouchUp
  134. end
  135. end
  136. else
  137. if Input.GetMouseButtonDown(0) then
  138. -- print(">>>>>>>>>>>> GetMouseButtonDown")
  139. pos = Vector2(0,0)
  140. pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5
  141. pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5
  142. touch_type = DragViewMgr.TouchType.TouchDown
  143. elseif Input.GetMouseButtonUp(0) then
  144. -- print(">>>>>>>>>>>> GetMouseButtonUp")
  145. pos = Vector2(0,0)
  146. pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5
  147. pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5
  148. touch_type = DragViewMgr.TouchType.TouchUp
  149. elseif Input.GetMouseButton(0) then
  150. --print(">>>>>>>>>>>> GetMouseButton")
  151. pos = Vector2(0,0)
  152. pos.x = Input.mousePosition.x / ScreenConvertRatio - ScreenWidth*0.5
  153. pos.y = Input.mousePosition.y / ScreenConvertRatio - ScreenHeight*0.5
  154. touch_type = DragViewMgr.TouchType.TouchMove
  155. end
  156. end
  157. return touch_type, pos
  158. end