源战役客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

337 linhas
7.7 KiB

  1. AdventureBookView = AdventureBookView or BaseClass(BaseView)
  2. local AdventureBookView = AdventureBookView
  3. AdventureBookView.MOVE_OFFSET = 15
  4. AdventureBookView.ITEM_OFFSET = 380
  5. local math_abs = math.abs
  6. function AdventureBookView:__init()
  7. self.base_file = "adventureBook"
  8. self.layout_file = "AdventureBookView"
  9. self.layer_name = "Activity"
  10. self.append_to_ctl_queue = true
  11. self.use_background = true
  12. self.model = AdventureBookModel:getInstance()
  13. self.item_list = {}
  14. --self.use_local_view = true
  15. self.curr_index = -1
  16. self.max_book_index = 10
  17. self.is_moving = false
  18. self.drag_begin_x = 0
  19. self.drag_offset_x = 0
  20. self.is_response_click = true -- 点击事件会在onDragend执行,如果是拖动的话不用执行点击
  21. self.load_callback = function()
  22. self:LoadSuccess()
  23. end
  24. self.open_callback = function()
  25. self:InitView()
  26. end
  27. self.close_callback = function()
  28. end
  29. self.destroy_callback = function()
  30. self:Remove()
  31. end
  32. end
  33. function AdventureBookView:Open(type)
  34. self.type = type
  35. BaseView.Open(self)
  36. end
  37. function AdventureBookView:LoadSuccess()
  38. self.transform.localPosition = Vector3.zero
  39. self.transform.sizeDelta = Vector3.zero
  40. if ClientConfig.iphone_x_model then
  41. self.transform.offsetMin = Vector2(ClientConfig.iphone_x_offset_left,0)
  42. self.transform.offsetMax = Vector2(-ClientConfig.iphone_x_offset_right,0)
  43. end
  44. self.bg = self:GetChildImages({
  45. "bg"
  46. })
  47. self.item_parent = self:GetChildTransforms({
  48. "Mask/ItemParent"
  49. })
  50. self.bg.transform.sizeDelta = Vector2(ScreenWidth, ScreenHeight)
  51. lua_resM:setOutsideImageSprite(self,self.bg,GameResPath.GetJpgImage("zjui_bg.jpg"))
  52. self.backBtn,
  53. self.tipBtn,
  54. self.leftBtn,
  55. self.rightBtn,
  56. self.touch_btn = self:GetChildGameObjects({
  57. "backBtn",
  58. "tipBtn",
  59. "leftBtn",
  60. "rightBtn",
  61. "Mask/Touch"
  62. })
  63. self:InitEvent()
  64. end
  65. function AdventureBookView:InitView()
  66. self.max_book_index = TableSize(Config.Adventurebook)
  67. self:SwitchView(self.type)
  68. end
  69. function AdventureBookView:InitEvent()
  70. local function onBtnClickHandler(target)
  71. if target == self.backBtn then
  72. self:Close()
  73. elseif target == self.tipBtn then
  74. EventSystem.Fire(GlobalEventSystem,EventName.OPEN_INSTRUCTION_VIEW, 601)
  75. elseif target == self.leftBtn then
  76. if self.is_moving then
  77. return
  78. end
  79. self:ShowPreItem()
  80. elseif target == self.rightBtn then
  81. if self.is_moving then
  82. return
  83. end
  84. self:ShowNextItem()
  85. end
  86. end
  87. AddClickEvent(self.backBtn, onBtnClickHandler, 1)
  88. AddClickEvent(self.tipBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
  89. AddClickEvent(self.leftBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
  90. AddClickEvent(self.rightBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
  91. local function onDragBegin(target,x,y)
  92. if self.is_moving then
  93. return
  94. end
  95. self:OnDragBegin(x,y)
  96. end
  97. local function onDragEnd(target,x,y)
  98. if self.is_moving then
  99. return
  100. end
  101. self:OnDragEnd(x,y)
  102. end
  103. local function onDrag(target,x, y)
  104. if self.is_moving then
  105. return
  106. end
  107. self:OnDrag(x, y)
  108. end
  109. local function onTouchClickHandler(target,x,y)
  110. if target == self.touch_btn then
  111. if self.is_moving then
  112. return
  113. end
  114. self:OnTouchClick(x,y)
  115. end
  116. end
  117. AddClickEvent(self.touch_btn, onTouchClickHandler,LuaSoundManager.SOUND_UI.SWITCH)
  118. AddDragBeginEvent(self.touch_btn,onDragBegin)
  119. AddDragEndEvent(self.touch_btn,onDragEnd)
  120. AddDragEvent(self.touch_btn,onDrag)
  121. --绑定更新item显示的方法
  122. local function onUpdateItem(type, subtype)
  123. self:SwitchView(type)
  124. end
  125. self.update_item_id = self.model:Bind(AdventureBookModel.UPDATE_ADVENTURE_BOOK_INFO, onUpdateItem)
  126. end
  127. function AdventureBookView:OnTouchClick(x,y)
  128. if not self.is_response_click then
  129. return
  130. end
  131. x,y = ScreenToViewportPoint(x,y)
  132. x = x - ScreenWidth / 2
  133. y = y - ScreenHeight / 2
  134. if y > -310 and y < 190 then
  135. local is_click_center = x >= -170 and x <= 170
  136. local is_click_left = x >= -500 and x <= -260
  137. local is_click_right = x >= 260 and x <= 500
  138. if is_click_center or is_click_left or is_click_right then
  139. if is_click_left then
  140. self:ShowPreItem()
  141. elseif is_click_right then
  142. self:ShowNextItem()
  143. elseif is_click_center then
  144. local click_item,pos
  145. for index,item in pairs(self.item_list) do
  146. pos = item:GetAnchoredPosition()
  147. if pos and pos.x >= -50 and pos.x <= 50 then
  148. click_item = item
  149. break
  150. end
  151. end
  152. if click_item then
  153. click_item:OnClickItem()
  154. end
  155. end
  156. end
  157. end
  158. end
  159. function AdventureBookView:ShowPreItem()
  160. for index,item in ipairs(self.item_list) do
  161. item:DragBegin()
  162. end
  163. self:SetMoveEndState(AdventureBookView.ITEM_OFFSET)
  164. end
  165. function AdventureBookView:ShowNextItem()
  166. for index,item in ipairs(self.item_list) do
  167. item:DragBegin()
  168. end
  169. self:SetMoveEndState(-AdventureBookView.ITEM_OFFSET)
  170. end
  171. function AdventureBookView:OnDragBegin(x,y)
  172. self.is_response_click = false
  173. self.drag_begin_x = x
  174. for index,item in ipairs(self.item_list) do
  175. item:DragBegin()
  176. end
  177. end
  178. function AdventureBookView:OnDrag(x,y)
  179. self.drag_offset_x = x - self.drag_begin_x
  180. for index,item in ipairs(self.item_list) do
  181. item:Drag(self.drag_offset_x)
  182. end
  183. end
  184. function AdventureBookView:OnDragEnd(x,y)
  185. self.is_response_click = true
  186. local min_distance = -1
  187. local pos
  188. for index,item in ipairs(self.item_list) do
  189. item:DragEnd()
  190. pos = item:GetAnchoredPosition()
  191. if pos then
  192. if min_distance == -1 or math_abs(min_distance) > math_abs(pos.x) then
  193. min_distance = pos.x
  194. end
  195. end
  196. end
  197. if min_distance ~= -1 then
  198. self:SetMoveEndState( -min_distance)
  199. end
  200. end
  201. --设置移动到最后的状态
  202. function AdventureBookView:SetMoveEndState(distance)
  203. self.is_moving = true
  204. self.drag_begin_x = 0
  205. self.move_end_dis = 0
  206. self.move_end_total_dis = distance
  207. self.move_end_offset = distance > 0 and AdventureBookView.MOVE_OFFSET or - AdventureBookView.MOVE_OFFSET
  208. self:StartUpdateBeat()
  209. end
  210. function AdventureBookView:StartUpdateBeat( )
  211. Runner:getInstance():AddRunObj(self, 1)
  212. end
  213. function AdventureBookView:RemoveUpdateBeat( )
  214. Runner.Instance:RemoveRunObj(self)
  215. end
  216. --update是模拟drag的过程
  217. function AdventureBookView:Update(now_time, elapse_time)
  218. if self.is_moving then
  219. self.move_end_dis = self.move_end_dis + self.move_end_offset
  220. self:OnDrag(self.move_end_dis,0)
  221. if math_abs(self.move_end_dis - self.move_end_total_dis) <= AdventureBookView.MOVE_OFFSET then
  222. self:OnDrag(self.move_end_total_dis,0)
  223. for index,item in ipairs(self.item_list) do
  224. item:DragEnd()
  225. end
  226. self:RemoveUpdateBeat()
  227. setTimeout(function()
  228. self.is_moving = false
  229. end,0.1)
  230. end
  231. end
  232. end
  233. function AdventureBookView:SwitchView(type)
  234. local book_list = self.model:GetAllBookByType(type)
  235. local show_index = self.model:GetBookShowIndex(type)
  236. local show_list = {}
  237. local real_index,data
  238. for i = show_index - 2,show_index + 1 do
  239. real_index = i
  240. if real_index > self.max_book_index then
  241. real_index = real_index - self.max_book_index
  242. elseif real_index < 1 then
  243. real_index = real_index + self.max_book_index
  244. end
  245. data = book_list[real_index]
  246. if data then
  247. table.insert(show_list,data)
  248. end
  249. end
  250. local len = #show_list
  251. local item,X
  252. for i,v in ipairs(show_list) do
  253. item = self.item_list[i]
  254. if item == nil then
  255. item = AdventureBookItem.New(self.item_parent)
  256. table.insert(self.item_list,item)
  257. end
  258. item:SetVisible(true)
  259. item:SetData(v)
  260. x = (3 - i) * - AdventureBookView.ITEM_OFFSET
  261. item:SetDragBeginX(x)
  262. item:SetAnchoredPosition(x,0)
  263. end
  264. for i=#show_list + 1,#self.item_list do
  265. self.item_list[i]:SetVisible(false)
  266. end
  267. end
  268. function AdventureBookView:Remove()
  269. if self.update_item_id then
  270. self.model:UnBind(self.update_item_id )
  271. self.update_item_id = nil
  272. end
  273. for i, v in pairs(self.item_list) do
  274. v:DeleteMe()
  275. v = nil
  276. end
  277. self.item_list = {}
  278. self:RemoveUpdateBeat()
  279. end