ClubInvitePageView.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import GameM, { AUDIO_TYPE } from "../../manager/GameM";
  2. import EffectNode from "../EffectNode";
  3. import ClubInviteItem from "./ClubInviteItem";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class ClubInvitePageView extends cc.Component {
  7. @property(cc.Node)
  8. content: cc.Node = null;
  9. @property(cc.Node)
  10. sview: cc.Node = null;
  11. @property(cc.Node)
  12. dragArea: cc.Node = null
  13. @property(cc.Node)
  14. item1: cc.Node = null
  15. @property(cc.Node)
  16. item2: cc.Node = null
  17. @property(cc.Node)
  18. arrLeft: cc.Node = null;
  19. @property(cc.Node)
  20. arrRight: cc.Node = null;
  21. @property(cc.Node)
  22. par: cc.Node = null
  23. @property(cc.Prefab)
  24. clubInviteInfoItem: cc.Prefab = null
  25. curSelDownItem = null
  26. preDownItem = null
  27. curSelItem = null
  28. curData = null
  29. //分帧加载
  30. addNum = 0
  31. addTotal = 7
  32. _n = 0
  33. _dis = 5
  34. private disX = 0
  35. private lastX = 0
  36. private startX = 0
  37. private coolTime = false
  38. private hasChoose = false
  39. onLoad() {
  40. this.addNum = 0
  41. this.addTotal = GameM.ClubData.clubInviteInfo.progressRecord.length
  42. this.dragArea.on(cc.Node.EventType.TOUCH_START, this.onDragStart, this)
  43. this.dragArea.on(cc.Node.EventType.TOUCH_END, this.onDragEnd, this)
  44. }
  45. onEnable() {
  46. this.item1.active = false
  47. this.hasChoose = false
  48. let len = this.content.childrenCount
  49. for (var i = 0; i < len; i++) {
  50. let item: cc.Node = this.content.getChildByName(i.toString())
  51. }
  52. }
  53. onDisable() {
  54. this.curSelDownItem = null
  55. this.preDownItem = null
  56. this.curSelItem = null
  57. this.curData = null
  58. }
  59. onScroll() {
  60. if (Math.abs(this.content.x - this.lastX) >= this.disX) {
  61. this.lastX = this.content.x
  62. this.content.children.forEach(element => {
  63. this.checkVisible(element)
  64. });
  65. }
  66. }
  67. update(dt) {
  68. if (this.addNum < this.addTotal) {
  69. if (this._n <= this._dis) {
  70. this._n++
  71. }
  72. else {
  73. this._n = 0
  74. let item: cc.Node = cc.instantiate(this.clubInviteInfoItem)
  75. item.name = this.addNum.toString()
  76. let s = item.getComponent(ClubInviteItem)
  77. s.initItem(GameM.ClubData.clubInviteInfo.progressRecord[this.addNum],this.addNum)
  78. this.disX = item.width + 10
  79. item.x = item.width * 0.5 + (item.width + 10) * this.addNum
  80. this.content.addChild(item)
  81. this.checkVisible(item)
  82. // to do
  83. // if (!this.hasChoose) {
  84. // let day = GameM.commonData.dailyCfg[this.addNum].getDay
  85. // if (day >= GameM.commonData.loginDays) {
  86. // if (GameM.commonData.dailyGetArr[this.addNum] == '0') {
  87. // this.hasChoose = true
  88. // this.initSel(this.addNum, item, s)
  89. // }
  90. // }
  91. // }
  92. this.content.width = this.addTotal * (item.width + 10)
  93. this.addNum++
  94. if (this.addNum == this.addTotal && !this.hasChoose) {
  95. this.initSel(this.addNum - 1, item, s)
  96. }
  97. item.on(cc.Node.EventType.TOUCH_END, this.onSel, this)
  98. }
  99. }
  100. this.onScroll()
  101. }
  102. initSel(index, item, s) {
  103. this.coolTime = true
  104. this.curData = GameM.ClubData.clubInviteInfo.progressRecord[index]
  105. this.curSelItem = item
  106. this.curSelDownItem = this.item1
  107. this.curSelDownItem.x = 750
  108. this.curSelDownItem.active = true
  109. this.item2.x = 750
  110. cc.tween(this.curSelDownItem).to(0.3, { x: 0 }, cc.easeInOut(1)).call(() => {
  111. this.coolTime = false
  112. }).start()
  113. this.autoScroll()
  114. }
  115. autoScroll() {
  116. let endX = 0
  117. let len = GameM.ClubData.clubInviteInfo.progressRecord.length
  118. if (this.curData.index == 0 || this.curData.index == 1 || this.curData.index == 2) {
  119. endX = 0
  120. if (this.curData.index == 0) {
  121. this.arrLeft.active = false
  122. }
  123. else {
  124. this.arrLeft.active = true
  125. this.arrRight.active = true
  126. }
  127. }
  128. else if (this.curData.index == len - 1 || this.curData.index == len - 2 || this.curData.index == len - 3) {
  129. endX = this.content.width
  130. if (this.curData.index == len - 1) {
  131. this.arrRight.active = false
  132. }
  133. else {
  134. this.arrLeft.active = true
  135. this.arrRight.active = true
  136. }
  137. }
  138. else {
  139. endX = this.content.width * (this.curData.index + 0.5) / len
  140. this.arrLeft.active = true
  141. this.arrRight.active = true
  142. }
  143. this.sview.getComponent(cc.ScrollView).scrollTo(cc.v2(endX / this.content.width, 0), 0.6)
  144. }
  145. checkVisible(item: cc.Node) {
  146. let disX = item.y + this.content.y
  147. if (disX >= 90 + this.disX || disX <= -this.sview.width - 90 - this.disX) {
  148. item.active = false
  149. }
  150. else {
  151. item.active = true
  152. }
  153. }
  154. onSel(evt: cc.Event.EventTouch) {
  155. if (this.coolTime) {
  156. return
  157. }
  158. GameM.audioM.playEffect(AUDIO_TYPE.button)
  159. let index = Number(evt.currentTarget.name)
  160. let data = GameM.ClubData.clubInviteInfo.progressRecord[index]
  161. this.onSelByData(data)
  162. }
  163. onSelByData(data) {
  164. if (this.curData) {
  165. let preTo = -750
  166. if (data.index == this.curData.index) {
  167. return
  168. }
  169. else if (data.index > this.curData.index) {
  170. if (this.curSelDownItem == this.item1) {
  171. this.curSelDownItem = this.item2
  172. this.preDownItem = this.item1
  173. }
  174. else {
  175. this.curSelDownItem = this.item1
  176. this.preDownItem = this.item2
  177. }
  178. this.curSelDownItem.getComponent(ClubInviteItem).init(data)
  179. this.curSelDownItem.x = 750
  180. preTo = -750
  181. }
  182. else {
  183. if (this.curSelDownItem == this.item1) {
  184. this.curSelDownItem = this.item2
  185. this.preDownItem = this.item1
  186. }
  187. else {
  188. this.curSelDownItem = this.item1
  189. this.preDownItem = this.item2
  190. }
  191. this.curSelDownItem.getComponent(ClubInviteItem).initItem(data,data.index)
  192. this.curSelDownItem.x = -750
  193. preTo = 750
  194. }
  195. this.coolTime = true
  196. cc.tween(this.preDownItem).to(0.3, { x: preTo }, cc.easeInOut(1)).start()
  197. cc.tween(this.curSelDownItem).to(0.3, { x: 0 }, cc.easeInOut(1)).call(() => {
  198. this.coolTime = false
  199. }).start()
  200. }
  201. else {
  202. return
  203. }
  204. this.curData = data
  205. this.curSelItem = this.content.getChildByName(this.curData.index.toString())
  206. let s = this.curSelItem.getComponent(ClubInviteItem)
  207. this.autoScroll()
  208. }
  209. onLeft() {
  210. GameM.audioM.playEffect(AUDIO_TYPE.button)
  211. this.onSelDown(2)
  212. }
  213. onRight() {
  214. GameM.audioM.playEffect(AUDIO_TYPE.button)
  215. this.onSelDown(1)
  216. }
  217. /**
  218. * 滑动选择
  219. * @param state 1 向左 2 向右
  220. */
  221. onSelDown(state, slide = false) {
  222. if (this.coolTime) {
  223. return
  224. }
  225. if (!this.curData) {
  226. return
  227. }
  228. let preTo = -750
  229. let data
  230. if (state == 1) {
  231. if (this.curData.index >= GameM.ClubData.clubInviteInfo.progressRecord.length - 1) {
  232. if (slide) {
  233. EffectNode.instance.PlayTip('已到最后一项')
  234. }
  235. return
  236. }
  237. data = GameM.ClubData.clubInviteInfo.progressRecord[this.curData.index + 1]
  238. if (this.curSelDownItem == this.item1) {
  239. this.curSelDownItem = this.item2
  240. this.preDownItem = this.item1
  241. }
  242. else {
  243. this.curSelDownItem = this.item1
  244. this.preDownItem = this.item2
  245. }
  246. this.curSelDownItem.x = 750
  247. preTo = -750
  248. }
  249. else if (state == 2) {
  250. if (this.curData.index <= 0) {
  251. if (slide) {
  252. EffectNode.instance.PlayTip('已到第一项')
  253. }
  254. return
  255. }
  256. data = GameM.ClubData.clubInviteInfo.progressRecord[this.curData.index - 1]
  257. if (this.curSelDownItem == this.item1) {
  258. this.curSelDownItem = this.item2
  259. this.preDownItem = this.item1
  260. }
  261. else {
  262. this.curSelDownItem = this.item1
  263. this.preDownItem = this.item2
  264. }
  265. this.curSelDownItem.getComponent(ClubInviteItem).init(data)
  266. this.curSelDownItem.x = -750
  267. preTo = 750
  268. }
  269. this.coolTime = true
  270. cc.tween(this.preDownItem).to(0.3, { x: preTo }, cc.easeInOut(1)).start()
  271. cc.tween(this.curSelDownItem).to(0.3, { x: 0 }, cc.easeInOut(1)).call(() => {
  272. this.coolTime = false
  273. }).start()
  274. this.autoScroll()
  275. }
  276. onDragStart(evt) {
  277. this.startX = evt.touch._point.x
  278. }
  279. onDragEnd(evt) {
  280. let end = evt.touch._point.x - this.startX
  281. if (Math.abs(end) > 20) {
  282. if (end > 0) {
  283. this.onSelDown(2, true)
  284. }
  285. else {
  286. this.onSelDown(1, true)
  287. }
  288. }
  289. }
  290. }