daoju_shop_view.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { _decorator, Component, Node, Prefab, UITransform, Sprite, Tween, 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 { ViewManager } from '../adapter/manager/ViewManager';
  9. import ItemDataConfig, { itemData } from '../config/ItemDataConfig';
  10. import { GameMng } from '../GameMng';
  11. import { UIButton } from '../gcommon/UIButton';
  12. import { shop_page } from './shop_page';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('daoju_shop_view')
  15. export class daoju_shop_view extends ScrollAdapter<itemData[]> {
  16. @property(Prefab) shopPrefab: Prefab = null
  17. @property(UITransform) header: UITransform = null
  18. @property(Sprite) loading: Sprite = null
  19. @property(UITransform) foot: UITransform = null
  20. @property(Sprite) foot_loading: Sprite = null
  21. @property(Prefab) item_prefab: Prefab = null
  22. @property({ type: ContentType }) private _contentType: ContentType = ContentType.shop
  23. @property({ type: ContentType }) public get contentType() { return this._contentType }
  24. public set contentType(value: ContentType) {
  25. if (value == this._contentType) return
  26. this._contentType = value
  27. }
  28. private _headerTween: Tween<any>
  29. private _loadTween: Tween<any>
  30. private _footTween: Tween<any>
  31. private _loadFootTween: Tween<any>
  32. private _isHeaderPlay = false
  33. private _isFootPlay = false
  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. var list = []
  53. if(this._contentType===ContentType.bag){
  54. list.push(GameMng.getBagItemList())
  55. }else if(this._contentType===ContentType.shop){
  56. list.push(ItemDataConfig.Instance.getShopDaoJuList())
  57. }
  58. this.modelManager.insert( list,0)
  59. this.scrollManager.scrollToHeader(0)
  60. //this.viewManager.on(ViewManager.Event.ON_MAGNETIC, this._onMagnetic, this)
  61. }
  62. async onPullUp(event: ReleaseEvent) {
  63. if (event.state == ReleaseState.RELEASE) {
  64. if (this._isFootPlay) {
  65. this._loadFootTween.start()
  66. // 等待并锁定头部
  67. event.wait()
  68. // 加载历史记录
  69. var list = await this.loadMore()
  70. // 插入数据
  71. this.modelManager.insert(list,0)
  72. // 释放解锁头部
  73. this.scheduleOnce(() => {
  74. event.release()
  75. })
  76. this._loadFootTween.stop()
  77. }
  78. }
  79. var progress = event.progress
  80. if (event.state == ReleaseState.WAIT) {
  81. progress = 0.8
  82. }
  83. if (progress >= 0.8) {
  84. if (!this._isFootPlay) {
  85. this._footTween = new Tween(this.foot).to(0.518, {
  86. height: this.releaseManager.bottom * this.mainAxisSize
  87. }, { easing: "elasticOut" })
  88. this._footTween.start()
  89. this._isFootPlay = true
  90. }
  91. } else {
  92. this._footTween.stop()
  93. this._isFootPlay = false
  94. this.foot.height = event.offset
  95. var color = new Color()
  96. color.set(this.foot_loading.color)
  97. color.a = 255 * Math.min(progress, 1)
  98. this.foot_loading.color = color
  99. // var y = 70 - 40 * progress
  100. // this.loading.node.setPosition(0, Math.max(y, 0))
  101. }
  102. this.foot_loading.node.angle = -360 * event.progress
  103. }
  104. async onPullDown(event: ReleaseEvent) {
  105. if (event.state == ReleaseState.RELEASE) {
  106. if (this._isHeaderPlay) {
  107. this._loadTween.start()
  108. // this.modelManager.clear()
  109. // 等待并锁定头部
  110. event.wait()
  111. // 加载历史记录
  112. var list = await this.reLoad()
  113. this.scrollToHeader()
  114. // 插入数据
  115. // this.modelManager.insert(list,0)
  116. // 释放解锁头部
  117. this.scheduleOnce(() => {
  118. event.release()
  119. })
  120. this._loadTween.stop()
  121. }
  122. }
  123. var progress = event.progress
  124. if (event.state == ReleaseState.WAIT) {
  125. progress = 0.8
  126. }
  127. if (progress >= 0.8) {
  128. if (!this._isHeaderPlay) {
  129. this._headerTween = new Tween(this.header).to(0.518, {
  130. height: this.releaseManager.top * this.mainAxisSize
  131. }, { easing: "elasticOut" })
  132. this._headerTween.start()
  133. this._isHeaderPlay = true
  134. }
  135. } else {
  136. this._headerTween.stop()
  137. this._isHeaderPlay = false
  138. this.header.height = event.offset
  139. var color = new Color()
  140. color.set(this.loading.color)
  141. color.a = 255 * Math.min(progress, 1)
  142. this.loading.color = color
  143. // var y = 70 - 40 * progress
  144. // this.loading.node.setPosition(0, Math.max(y, 0))
  145. }
  146. this.loading.node.angle = -360 * event.progress
  147. }
  148. reLoad(): Promise<itemData[]> {
  149. return new Promise((resolve, reject) => {
  150. var list = []
  151. this.scheduleOnce(() => {
  152. resolve(list)
  153. }, 1)
  154. })
  155. }
  156. loadMore(): Promise<itemData[]> {
  157. return new Promise((resolve, reject) => {
  158. var list = []
  159. this.scheduleOnce(() => {
  160. resolve(list)
  161. }, 1)
  162. })
  163. }
  164. public getPrefab(data: itemData[]): Node | Prefab {
  165. return this.shopPrefab
  166. }
  167. public getHolder(node: Node, code: string): Holder<itemData[]> {
  168. return new myHolder(node, code, this)
  169. }
  170. public getView(): View<itemData[]> {
  171. return new myView(this)
  172. }
  173. public initElement(element: IElement, data: any): void {
  174. }
  175. scrollToHeader() {
  176. this.scrollManager.scrollToFooter(1)
  177. }
  178. }
  179. class myView extends View<itemData[], daoju_shop_view> {
  180. protected onVisible(): void {
  181. }
  182. protected onDisable(): void {
  183. }
  184. }
  185. class myHolder extends Holder<itemData[], daoju_shop_view>{
  186. private _shopItem: shop_page = null
  187. protected onCreated(): void {
  188. this._shopItem = this.node.getComponent(shop_page)
  189. this._shopItem.show(this,this.adapter.item_prefab,this.data)
  190. }
  191. protected onVisible(): void {
  192. }
  193. protected onDisable(): void {
  194. this._shopItem.hide()
  195. }
  196. }