qipan_shop_view.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { _decorator, Component, Node, Prefab, Tween, UITransform, Sprite, Color } from 'cc';
  2. import { Holder } from '../adapter/abstract/Holder';
  3. import { ScrollAdapter } from '../adapter/abstract/ScrollAdapter';
  4. import { View } from '../adapter/abstract/View';
  5. import { ContentType, ReleaseState } from '../adapter/define/enum';
  6. import { IElement } from '../adapter/define/interface';
  7. import { ReleaseEvent, ReleaseManager } from '../adapter/manager/ReleaseManager';
  8. import ItemDataConfig, { itemData } from '../config/ItemDataConfig';
  9. import { GameMng } from '../GameMng';
  10. import { shop_qipan_page } from './shop_qipan_page';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('qipan_shop_view')
  13. export class qipan_shop_view extends ScrollAdapter<itemData[]> {
  14. @property(Prefab) shopPrefab: Prefab = null
  15. @property(UITransform) header: UITransform = null
  16. @property(Sprite) loading: Sprite = null
  17. @property(UITransform) foot: UITransform = null
  18. @property(Sprite) foot_loading: Sprite = null
  19. @property(Prefab) item_prefab: Prefab = null
  20. @property({ type: ContentType }) private _contentType: ContentType = ContentType.shop
  21. @property({ type: ContentType }) public get contentType() { return this._contentType }
  22. public set contentType(value: ContentType) {
  23. if (value == this._contentType) return
  24. this._contentType = value
  25. }
  26. private _headerTween: Tween<any>
  27. private _loadTween: Tween<any>
  28. private _footTween: Tween<any>
  29. private _loadFootTween: Tween<any>
  30. private _isHeaderPlay = false
  31. private _isFootPlay = false
  32. public qipan_list_data = []
  33. public cur_page = 0;
  34. start() {
  35. this.initView();
  36. }
  37. initView(){
  38. this.releaseManager.on(ReleaseManager.Event.ON_PULL_UP, this.onPullUp, this)
  39. this.releaseManager.on(ReleaseManager.Event.ON_PULL_DOWN, this.onPullDown, this)
  40. this._headerTween = new Tween(this.header).to(0.518, {
  41. height: this.releaseManager.top * this.mainAxisSize
  42. }, { easing: "elasticOut" })
  43. this._loadTween = new Tween(this.loading.node).by(1, {
  44. angle: -360
  45. }).union().repeatForever()
  46. this._footTween = new Tween(this.foot).to(0.518, {
  47. height: this.releaseManager.top * this.mainAxisSize
  48. }, { easing: "elasticOut" })
  49. this._loadFootTween = new Tween(this.foot_loading.node).by(1, {
  50. angle: -360
  51. }).union().repeatForever()
  52. if(this._contentType===ContentType.bag){
  53. this.qipan_list_data.push(GameMng.getBagQiPanList())
  54. }else if(this._contentType===ContentType.shop){
  55. this.qipan_list_data = ItemDataConfig.Instance.getShopQipanList()
  56. }
  57. this.init_shop_item()
  58. //this.viewManager.on(ViewManager.Event.ON_MAGNETIC, this._onMagnetic, this)
  59. }
  60. init_shop_item(){
  61. var list = []
  62. if(this._contentType===ContentType.bag){
  63. list.push(this.qipan_list_data[0])
  64. }else{
  65. let count = (this.cur_page)===(this.qipan_list_data.length-1)?1:3
  66. for (let index = 0; index < count; index++) {
  67. list.push(this.qipan_list_data[(count-1)-index])
  68. }
  69. this.cur_page=2;
  70. }
  71. this.modelManager.clear()
  72. this.modelManager.insert(list)
  73. this.scrollManager.scrollToFooter(0)
  74. }
  75. async onPullUp(event: ReleaseEvent) {
  76. if (event.state == ReleaseState.RELEASE) {
  77. if (this._isFootPlay) {
  78. this._loadFootTween.start()
  79. // 等待并锁定头部
  80. event.wait()
  81. // 加载历史记录
  82. var list = await this.loadMore()
  83. // 插入数据
  84. this.modelManager.insert(list,0)
  85. // 释放解锁头部
  86. this.scheduleOnce(() => {
  87. event.release()
  88. })
  89. this._loadFootTween.stop()
  90. }
  91. }
  92. var progress = event.progress
  93. if (event.state == ReleaseState.WAIT) {
  94. progress = 0.8
  95. }
  96. if (progress >= 0.8) {
  97. if (!this._isFootPlay) {
  98. this._footTween = new Tween(this.foot).to(0.518, {
  99. height: this.releaseManager.bottom * this.mainAxisSize
  100. }, { easing: "elasticOut" })
  101. this._footTween.start()
  102. this._isFootPlay = true
  103. }
  104. } else {
  105. this._footTween.stop()
  106. this._isFootPlay = false
  107. this.foot.height = event.offset
  108. var color = new Color()
  109. color.set(this.foot_loading.color)
  110. color.a = 255 * Math.min(progress, 1)
  111. this.foot_loading.color = color
  112. // var y = 70 - 40 * progress
  113. // this.loading.node.setPosition(0, Math.max(y, 0))
  114. }
  115. this.foot_loading.node.angle = -360 * event.progress
  116. }
  117. async onPullDown(event: ReleaseEvent) {
  118. if (event.state == ReleaseState.RELEASE) {
  119. if (this._isHeaderPlay) {
  120. this._loadTween.start()
  121. // this.modelManager.clear()
  122. // 等待并锁定头部
  123. event.wait()
  124. // 加载历史记录
  125. //var list = await this.reLoad()
  126. this.scrollToHeader()
  127. // 插入数据
  128. // this.modelManager.insert(list,0)
  129. // 释放解锁头部
  130. this.scheduleOnce(() => {
  131. event.release()
  132. })
  133. this._loadTween.stop()
  134. }
  135. }
  136. var progress = event.progress
  137. if (event.state == ReleaseState.WAIT) {
  138. progress = 0.8
  139. }
  140. if (progress >= 0.8) {
  141. if (!this._isHeaderPlay) {
  142. this._headerTween = new Tween(this.header).to(0.518, {
  143. height: this.releaseManager.top * this.mainAxisSize
  144. }, { easing: "elasticOut" })
  145. this._headerTween.start()
  146. this._isHeaderPlay = true
  147. }
  148. } else {
  149. this._headerTween.stop()
  150. this._isHeaderPlay = false
  151. this.header.height = event.offset
  152. var color = new Color()
  153. color.set(this.loading.color)
  154. color.a = 255 * Math.min(progress, 1)
  155. this.loading.color = color
  156. // var y = 70 - 40 * progress
  157. // this.loading.node.setPosition(0, Math.max(y, 0))
  158. }
  159. this.loading.node.angle = -360 * event.progress
  160. }
  161. reLoad(): Promise<itemData[]> {
  162. return new Promise((resolve, reject) => {
  163. var list = []
  164. this.scheduleOnce(() => {
  165. this.init_shop_item()
  166. resolve(list)
  167. }, 1)
  168. })
  169. }
  170. loadMore(): Promise<itemData[]> {
  171. return new Promise((resolve, reject) => {
  172. this.cur_page++;
  173. if(this.cur_page>=this.qipan_list_data.length){
  174. this.scheduleOnce(() => {
  175. resolve([])
  176. }, 0)
  177. }else{
  178. var list = []
  179. let count = (this.cur_page)>=(this.qipan_list_data.length-1)?1:2
  180. for (let index = 0; index < count; index++) {
  181. list.push(this.qipan_list_data[this.cur_page+(count-1-index)])
  182. }
  183. this.cur_page+=(count-1)
  184. this.scheduleOnce(() => {
  185. resolve(list)
  186. }, 1)
  187. }
  188. })
  189. }
  190. scrollToHeader() {
  191. this.scrollManager.scrollToFooter(1)
  192. }
  193. public getPrefab(data: itemData[]): Node | Prefab {
  194. return this.shopPrefab
  195. }
  196. public getHolder(node: Node, code: string): Holder<itemData[]> {
  197. return new myHolder(node, code, this)
  198. }
  199. public getView(): View<itemData[]> {
  200. return new myView(this)
  201. }
  202. public initElement(element: IElement, data: any): void {
  203. }
  204. }
  205. class myView extends View<itemData[], qipan_shop_view> {
  206. protected onVisible(): void {
  207. }
  208. protected onDisable(): void {
  209. }
  210. }
  211. class myHolder extends Holder<itemData[], qipan_shop_view>{
  212. private _shopItem: shop_qipan_page = null
  213. protected onCreated(): void {
  214. this._shopItem = this.node.getComponent(shop_qipan_page)
  215. }
  216. protected onVisible(): void {
  217. console.log("onVisible",this.data)
  218. this._shopItem.show(this,this.adapter.item_prefab,this.data,this.adapter.contentType)
  219. }
  220. protected onDisable(): void {
  221. this._shopItem.hide()
  222. }
  223. }