qizi_shop_view.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 { Orientation, ReleaseState, ContentType, StretchDirection } 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_qizi_page } from './shop_qizi_page';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('qizi_shop_view')
  13. export class qizi_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 qizi_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.qizi_list_data.push(GameMng.getBagQiZiList())
  54. }else{
  55. this.qizi_list_data = ItemDataConfig.Instance.getShopQiZiList()
  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. list.push(this.qizi_list_data)
  63. this.modelManager.clear()
  64. this.modelManager.insert(list)
  65. this.scrollManager.scrollToFooter(0)
  66. }
  67. async onPullUp(event: ReleaseEvent) {
  68. if (event.state == ReleaseState.RELEASE) {
  69. if (this._isFootPlay) {
  70. this._loadFootTween.start()
  71. // 等待并锁定头部
  72. event.wait()
  73. // 加载历史记录
  74. var list = await this.loadMore()
  75. // 插入数据
  76. this.modelManager.insert(list,0)
  77. // 释放解锁头部
  78. this.scheduleOnce(() => {
  79. event.release()
  80. })
  81. this._loadFootTween.stop()
  82. }
  83. }
  84. var progress = event.progress
  85. if (event.state == ReleaseState.WAIT) {
  86. progress = 0.8
  87. }
  88. if (progress >= 0.8) {
  89. if (!this._isFootPlay) {
  90. this._footTween = new Tween(this.foot).to(0.518, {
  91. height: this.releaseManager.bottom * this.mainAxisSize
  92. }, { easing: "elasticOut" })
  93. this._footTween.start()
  94. this._isFootPlay = true
  95. }
  96. } else {
  97. this._footTween.stop()
  98. this._isFootPlay = false
  99. this.foot.height = event.offset
  100. var color = new Color()
  101. color.set(this.foot_loading.color)
  102. color.a = 255 * Math.min(progress, 1)
  103. this.foot_loading.color = color
  104. // var y = 70 - 40 * progress
  105. // this.loading.node.setPosition(0, Math.max(y, 0))
  106. }
  107. this.foot_loading.node.angle = -360 * event.progress
  108. }
  109. async onPullDown(event: ReleaseEvent) {
  110. if (event.state == ReleaseState.RELEASE) {
  111. if (this._isHeaderPlay) {
  112. this._loadTween.start()
  113. // this.modelManager.clear()
  114. // 等待并锁定头部
  115. event.wait()
  116. // 加载历史记录
  117. var list = await this.reLoad()
  118. this.scrollToHeader()
  119. // 插入数据
  120. // this.modelManager.insert(list,0)
  121. // 释放解锁头部
  122. this.scheduleOnce(() => {
  123. event.release()
  124. })
  125. this._loadTween.stop()
  126. }
  127. }
  128. var progress = event.progress
  129. if (event.state == ReleaseState.WAIT) {
  130. progress = 0.8
  131. }
  132. if (progress >= 0.8) {
  133. if (!this._isHeaderPlay) {
  134. this._headerTween = new Tween(this.header).to(0.518, {
  135. height: this.releaseManager.top * this.mainAxisSize
  136. }, { easing: "elasticOut" })
  137. this._headerTween.start()
  138. this._isHeaderPlay = true
  139. }
  140. } else {
  141. this._headerTween.stop()
  142. this._isHeaderPlay = false
  143. this.header.height = event.offset
  144. var color = new Color()
  145. color.set(this.loading.color)
  146. color.a = 255 * Math.min(progress, 1)
  147. this.loading.color = color
  148. // var y = 70 - 40 * progress
  149. // this.loading.node.setPosition(0, Math.max(y, 0))
  150. }
  151. this.loading.node.angle = -360 * event.progress
  152. }
  153. reLoad(): Promise<itemData[]> {
  154. return new Promise((resolve, reject) => {
  155. var list = []
  156. this.scheduleOnce(() => {
  157. this.init_shop_item()
  158. resolve(list)
  159. }, 1)
  160. })
  161. }
  162. loadMore(): Promise<itemData[]> {
  163. return new Promise((resolve, reject) => {
  164. this.cur_page++;
  165. this.scheduleOnce(() => {
  166. resolve([])
  167. }, 0)
  168. })
  169. }
  170. scrollToHeader() {
  171. this.scrollManager.scrollToFooter(1)
  172. }
  173. public getPrefab(data: itemData[]): Node | Prefab {
  174. return this.shopPrefab
  175. }
  176. public getHolder(node: Node, code: string): Holder<itemData[]> {
  177. return new myHolder(node, code, this)
  178. }
  179. public getView(): View<itemData[]> {
  180. return new myView(this)
  181. }
  182. public initElement(element: IElement, data: any): void {
  183. }
  184. }
  185. class myView extends View<itemData[], qizi_shop_view> {
  186. protected onVisible(): void {
  187. }
  188. protected onDisable(): void {
  189. }
  190. }
  191. class myHolder extends Holder<itemData[], qizi_shop_view>{
  192. private _shopItem: shop_qizi_page = null
  193. protected onCreated(): void {
  194. this._shopItem = this.node.getComponent(shop_qizi_page)
  195. }
  196. protected onVisible(): void {
  197. console.log("onVisible",this.data)
  198. this._shopItem.show(this,this.adapter.item_prefab,this.data,this.adapter.contentType)
  199. }
  200. protected onDisable(): void {
  201. this._shopItem.hide()
  202. }
  203. }