MouseBetView.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import Global from "../../../SubGamePublic/PG_Public/src/GlobalScript";
  2. import { MouseConst } from "./MouseConst";
  3. const { ccclass, property } = cc._decorator;
  4. class ItemObj {
  5. node: cc.Node;
  6. value: number;
  7. y: number;
  8. constructor(node: cc.Node, value: number, y: number) {
  9. this.node = node;
  10. this.value = value;
  11. this.y = y;
  12. }
  13. }
  14. @ccclass
  15. export default class MouseBetView extends cc.Component {
  16. @property({ type: cc.Node, displayName: '预制node' })
  17. pre_item: cc.Node = null;
  18. @property({ type: cc.Label, displayName: '自己余额' })
  19. label_myBalance: cc.Label = null;
  20. @property({ type: cc.Label, displayName: '下注金额' })
  21. label_bet: cc.Label = null;
  22. @property({ type: cc.ScrollView, displayName: '底注' })
  23. scroll_diZhu: cc.ScrollView = null;
  24. @property({ type: cc.ScrollView, displayName: '翻倍等级' })
  25. scroll_multiple: cc.ScrollView = null;
  26. @property({ type: cc.ScrollView, displayName: '实际下注' })
  27. scroll_bet: cc.ScrollView = null;
  28. @property({ type: cc.Label, displayName: '基础等级' })
  29. label_multipleBase: cc.Label = null;
  30. @property({ type: cc.Label, displayName: '赢分' })
  31. label_winScore: cc.Label = null;
  32. @property(cc.Node)
  33. node_di: cc.Node = null;
  34. @property(cc.Node)
  35. node_bg: cc.Node = null;
  36. @property(cc.Node)
  37. node_mask: cc.Node = null;
  38. @property({ type: [cc.Button] })
  39. btn_bottom: cc.Button[] = [];
  40. /** 底注 */
  41. diZhuArr: number[] = [];
  42. diZhuObjArr: ItemObj[] = [];
  43. /** 翻倍等级 */
  44. multipleLevel: number = 0;
  45. multipleLevelObjArr: ItemObj[] = [];
  46. /** 实际下注 */
  47. betArr: number[] = [];
  48. betObjArr: ItemObj[] = [];
  49. /** 基础翻倍 */
  50. multipleBase: number = 0;
  51. /** 当前底注 */
  52. curDiZhu: number = 0;
  53. /** 当前翻倍等级 */
  54. curMultipleLevel: number = 1;
  55. /** 当前选择的实际下注 */
  56. curBottomBet: number = 0;
  57. /** 当前底注下标 */
  58. curDiZhuIdx: number = 0;
  59. cb: Function = null;
  60. startY: number = 0;
  61. endY: number = 0;
  62. readonly AnimaTime: number = 0.2;
  63. /**
  64. * 初始化
  65. * @param betArr 实际下注额
  66. * @param diZhu 底注
  67. * @param multipleLevel 翻倍等级
  68. * @param multipleBase 基础翻倍
  69. */
  70. initBetView(diZhu: number[], multipleLevel: number, multipleBase: number) {
  71. // 解决适配
  72. let widget = this.node_di.getComponent(cc.Widget);
  73. widget.updateAlignment();
  74. this.startY = this.node_di.y - this.node_di.height;
  75. this.endY = this.node_di.y;
  76. widget.enabled = false;
  77. this.diZhuArr = diZhu;
  78. this.multipleLevel = multipleLevel;
  79. this.multipleBase = multipleBase;
  80. this.label_multipleBase.string = `${this.multipleBase}`;
  81. this.scroll_bet.node.on('scroll-ended', this.scrollBetEnd, this);
  82. this.scroll_diZhu.node.on('scroll-ended', this.scrollDiZhuEnd, this);
  83. this.scroll_multiple.node.on('scroll-ended', this.scrollMultipleEnd, this);
  84. this.scroll_bet.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  85. this.scroll_diZhu.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  86. this.scroll_multiple.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  87. this.scroll_bet.node.on('scroll-began', this.scrollViewBegin, this);
  88. this.scroll_diZhu.node.on('scroll-began', this.scrollViewBegin, this);
  89. this.scroll_multiple.node.on('scroll-began', this.scrollViewBegin, this);
  90. let diZhuParent = this.scroll_diZhu.content;
  91. this.initNodeComp(2, diZhuParent, false, null, false, null, '', true);
  92. this.initNodeComp(this.diZhuArr.length, diZhuParent, false, this.diZhuArr, true, this.diZhuObjArr, 'scrollDiZhuEnd', false, true);
  93. this.initNodeComp(2, diZhuParent, false, null, false, null, '', false);
  94. let t_arr = [];
  95. for (let i = 0; i < this.multipleLevel; i++) { t_arr.push(i + 1); }
  96. let multipleParent = this.scroll_multiple.content;
  97. this.initNodeComp(2, multipleParent, false, null, false, null, '', true);
  98. this.initNodeComp(this.multipleLevel, multipleParent, false, t_arr, true, this.multipleLevelObjArr, 'scrollMultipleEnd', false);
  99. this.initNodeComp(2, multipleParent, false, null, false, null, '', false);
  100. this.initNodeComp(2, this.scroll_bet.content, true, null, false, null, '', true);
  101. this.initBetNode();
  102. this.initNodeComp(2, this.scroll_bet.content, true, null, false, null, '', false);
  103. }
  104. onShow(curBet: number, winScore: string, cb: Function) {
  105. this.node_bg.opacity = 0;
  106. // this.label_myBalance.string = `R$${(Global.instance.balanceTotal * my.ICON_MUTIPLE).toFixed(2)}`;
  107. this.label_myBalance.string = MouseConst.formatAmount(Global.instance.balanceTotal);
  108. this.curBottomBet = curBet;
  109. this.cb = cb;
  110. this.node_mask.opacity = 255;
  111. this.node_mask.active = true;
  112. this.node_di.y = this.startY;
  113. this.label_winScore.string = winScore;
  114. this.node.active = true;
  115. for (let i = 0; i < this.betObjArr.length; i++) {
  116. if (this.curBottomBet == this.betObjArr[i].value) {
  117. // 下一帧更新,当前帧更新不生效
  118. this.scheduleOnce(() => this.scroll_bet.content.y = this.betObjArr[i].y);
  119. break;
  120. }
  121. }
  122. this.updateDiZhuAndMultipleScroll();
  123. cc.tween(this.node_bg).to(this.AnimaTime, { opacity: 180 }).start();
  124. cc.tween(this.node_di)
  125. .to(this.AnimaTime, { y: this.endY })
  126. .call(() => {
  127. cc.tween(this.node_mask)
  128. .to(0.2, { opacity: 0 })
  129. .call(() => this.node_mask.active = false)
  130. .start();
  131. })
  132. .start();
  133. }
  134. /**
  135. *
  136. * @param length 长度
  137. * @param parent 父节点
  138. * @param isColor 更改颜色
  139. * @param value 值
  140. * @param isPush 放入对象数组
  141. * @param itemObj 对象数组
  142. * @param handler btn 回调方法
  143. * @param is__ 是否是杠
  144. */
  145. initNodeComp(length: number, parent: cc.Node, isColor: boolean, value: number[], isPush: boolean, itemObj: ItemObj[], handler: string, is__: boolean, isDiZhu: boolean = false) {
  146. let t_color = cc.color(255, 200, 36, 255);
  147. for (let i = 0; i < length; i++) {
  148. let t_node = cc.instantiate(this.pre_item);
  149. parent.addChild(t_node);
  150. let child = t_node.getChildByName('label');
  151. let l = child.getComponent(cc.Label);
  152. if (isColor) child.color = t_color;
  153. if (value) {
  154. if (isDiZhu) {
  155. // l.string = `R$${(value[i] * 0.01).toFixed(2)}`;
  156. l.string = MouseConst.formatAmount(value[i]);
  157. } else {
  158. l.string = `${value[i]}`;
  159. }
  160. } else {
  161. if (is__) {
  162. l.string = i == 0 ? '' : '-';
  163. } else {
  164. l.string = i == 0 ? '-' : '';
  165. }
  166. }
  167. t_node.active = true;
  168. if (isPush) {
  169. itemObj.push(new ItemObj(t_node, value[i], i * this.pre_item.height));
  170. let clickEventHandler = new cc.Component.EventHandler();
  171. clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
  172. clickEventHandler.component = "MouseBetView";// 这个是代码文件名
  173. clickEventHandler.handler = handler;
  174. clickEventHandler.customEventData = `${i}`;
  175. let button = t_node.getComponent(cc.Button);
  176. button.clickEvents.push(clickEventHandler);
  177. }
  178. }
  179. }
  180. initBetNode() {
  181. let idx = 0;
  182. let betArray = [];
  183. for (let i = 0; i < this.diZhuObjArr.length; i++) {
  184. for (let j = 0; j < this.multipleLevelObjArr.length; j++) {
  185. let bet = this.diZhuObjArr[i].value * this.multipleLevelObjArr[j].value * this.multipleBase;
  186. betArray.push(bet);
  187. }
  188. }
  189. this.betArr = Array.from(new Set(betArray));
  190. this.betArr.sort((a, b) => {
  191. return a - b;
  192. });
  193. this.betArr.forEach((bet, index) => {
  194. let t_node = cc.instantiate(this.pre_item);
  195. this.scroll_bet.content.addChild(t_node);
  196. let child = t_node.getChildByName('label');
  197. child.color = cc.color(255, 200, 36, 255);
  198. let l = child.getComponent(cc.Label);
  199. // l.string = `R$${(bet * 0.01).toFixed(2)}`;
  200. l.string = MouseConst.formatAmount(bet);
  201. this.betArr.push(bet);
  202. this.betObjArr.push(new ItemObj(t_node, bet, idx * this.pre_item.height));
  203. t_node.active = true;
  204. let clickEventHandler = new cc.Component.EventHandler();
  205. clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
  206. clickEventHandler.component = "MouseBetView";// 这个是代码文件名
  207. clickEventHandler.handler = "scrollBetEnd";
  208. clickEventHandler.customEventData = `${idx}`;
  209. let button = t_node.getComponent(cc.Button);
  210. button.clickEvents.push(clickEventHandler);
  211. idx++;
  212. });
  213. }
  214. /** 触摸开始 */
  215. scrollViewBegin() {
  216. this.btn_bottom.forEach((v, i) => v.interactable = false);
  217. }
  218. /**
  219. * 点击和滚动都进
  220. * t_idx有就是点击进的
  221. */
  222. scrollBetEnd(e, t_idx) {
  223. cc.Tween.stopAllByTarget(this.scroll_bet.content);
  224. if (t_idx) this.btn_bottom.forEach((v, i) => v.interactable = false);
  225. let idx = t_idx ? t_idx : Math.round(this.scroll_bet.content.y / this.pre_item.height);
  226. cc.tween(this.scroll_bet.content)
  227. .to(0.1, { y: this.betObjArr[idx].y })
  228. .call(() => {
  229. this.curBottomBet = this.betObjArr[idx].value;
  230. this.updateDiZhuAndMultipleScroll();
  231. this.btn_bottom.forEach((v, i) => v.interactable = true);
  232. })
  233. .start();
  234. }
  235. scrollDiZhuEnd(e, t_idx) {
  236. cc.Tween.stopAllByTarget(this.scroll_diZhu.content);
  237. if (t_idx) this.btn_bottom.forEach((v, i) => v.interactable = false);
  238. let idx = t_idx ? t_idx : Math.round(this.scroll_diZhu.content.y / this.pre_item.height);
  239. cc.tween(this.scroll_diZhu.content)
  240. .to(0.1, { y: this.diZhuObjArr[idx].y })
  241. .call(() => {
  242. this.curDiZhu = this.diZhuObjArr[idx].value;
  243. this.curDiZhuIdx = idx;
  244. this.updateBottomBet();
  245. this.btn_bottom.forEach((v, i) => v.interactable = true);
  246. })
  247. .start();
  248. }
  249. scrollMultipleEnd(e, t_idx) {
  250. cc.Tween.stopAllByTarget(this.scroll_multiple.content);
  251. if (t_idx) this.btn_bottom.forEach((v, i) => v.interactable = false);
  252. let idx = t_idx ? t_idx : Math.round(this.scroll_multiple.content.y / this.pre_item.height);
  253. cc.tween(this.scroll_multiple.content)
  254. .to(0.1, { y: this.multipleLevelObjArr[idx].y })
  255. .call(() => {
  256. this.curMultipleLevel = this.multipleLevelObjArr[idx].value;
  257. this.updateBottomBet();
  258. this.btn_bottom.forEach((v, i) => v.interactable = true);
  259. })
  260. .start();
  261. }
  262. /** 更新底注和翻倍等级 */
  263. updateDiZhuAndMultipleScroll() {
  264. let v = this.curBottomBet / this.multipleBase;
  265. for (let i = 0; i < this.multipleLevelObjArr.length; i++) {
  266. for (let j = 0; j < this.diZhuArr.length; j++) {
  267. if (this.multipleLevelObjArr[i].value * this.diZhuArr[j] == v) {
  268. cc.Tween.stopAllByTarget(this.scroll_multiple.content);
  269. cc.Tween.stopAllByTarget(this.scroll_diZhu.content);
  270. cc.tween(this.scroll_multiple.content)
  271. .to(0.1, { y: this.multipleLevelObjArr[i].y })
  272. .call(() => this.curMultipleLevel = this.multipleLevelObjArr[i].value)
  273. .start();
  274. cc.tween(this.scroll_diZhu.content)
  275. .to(0.1, { y: this.diZhuObjArr[j].y })
  276. .call(() => {
  277. this.curDiZhu = this.diZhuObjArr[j].value;
  278. this.curDiZhuIdx = j;
  279. })
  280. .start();
  281. this.scheduleOnce(() => {
  282. // this.label_bet.string = `R$${(this.curDiZhu * this.multipleBase * this.curMultipleLevel * my.ICON_MUTIPLE).toFixed(2)}`
  283. this.label_bet.string = MouseConst.formatAmount(this.curDiZhu * this.multipleBase * this.curMultipleLevel);
  284. }, 0.11);
  285. return;
  286. }
  287. }
  288. }
  289. }
  290. /** 更新实际下注额 */
  291. updateBottomBet() {
  292. let v = this.curDiZhu * this.curMultipleLevel * this.multipleBase;
  293. for (let i = 0; i < this.betObjArr.length; i++) {
  294. if (this.betObjArr[i].value == v) {
  295. cc.Tween.stopAllByTarget(this.scroll_bet.content);
  296. cc.tween(this.scroll_bet.content)
  297. .to(0.1, { y: this.betObjArr[i].y })
  298. .call(() => {
  299. this.curBottomBet = this.betObjArr[i].value;
  300. // this.label_bet.string = `R$${(this.curDiZhu * this.multipleBase * this.curMultipleLevel * my.ICON_MUTIPLE).toFixed(2)}`;
  301. this.label_bet.string = MouseConst.formatAmount(this.curDiZhu * this.multipleBase * this.curMultipleLevel);
  302. })
  303. .start();
  304. break;
  305. }
  306. }
  307. }
  308. onClickBtn(e, isSure) {
  309. cc.tween(this.node_di)
  310. .to(this.AnimaTime, { y: this.startY })
  311. .call(() => {
  312. cc.tween(this.node_bg)
  313. .to(this.AnimaTime, { opacity: 0 })
  314. .call(() => {
  315. this.node.active = false;
  316. if (isSure) this.cb?.(this.curBottomBet, this.curDiZhuIdx, this.curMultipleLevel - 1);
  317. })
  318. .start();
  319. })
  320. .start();
  321. }
  322. onCancel() {
  323. cc.tween(this.node_di)
  324. .to(this.AnimaTime, { y: this.startY })
  325. .call(() => {
  326. cc.tween(this.node_bg)
  327. .to(this.AnimaTime, { opacity: 0 })
  328. .call(() => {
  329. this.node.active = false;
  330. })
  331. .start();
  332. })
  333. .start();
  334. }
  335. /** 最大下注 */
  336. onClickMaxBet() {
  337. this.btn_bottom.forEach((v, i) => v.interactable = false);
  338. this.curBottomBet = this.betObjArr[this.betObjArr.length - 1].value;
  339. this.updateDiZhuAndMultipleScroll();
  340. for (let i = 0; i < this.betObjArr.length; i++) {
  341. if (this.curBottomBet == this.betObjArr[i].value) {
  342. cc.Tween.stopAllByTarget(this.scroll_bet.content);
  343. cc.tween(this.scroll_bet.content)
  344. .to(0.1, { y: this.betObjArr[i].y })
  345. .call(() => this.btn_bottom.forEach((v, i) => v.interactable = true))
  346. .start();
  347. break;
  348. }
  349. }
  350. }
  351. /** 鼠标滚轮监听 */
  352. onMouseWheeL() {
  353. this.btn_bottom.forEach((v, i) => v.interactable = false);
  354. }
  355. protected onDestroy(): void {
  356. // this.scroll_bet.node.off('scroll-ended', this.scrollBetEnd, this);
  357. // this.scroll_diZhu.node.off('scroll-ended', this.scrollDiZhuEnd, this);
  358. // this.scroll_multiple.node.off('scroll-ended', this.scrollMultipleEnd, this);
  359. // this.scroll_bet.node.off('scroll-began', this.scrollViewBegin, this);
  360. // this.scroll_diZhu.node.off('scroll-began', this.scrollViewBegin, this);
  361. // this.scroll_multiple.node.off('scroll-began', this.scrollViewBegin, this);
  362. // this.scroll_bet.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  363. // this.scroll_diZhu.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  364. // this.scroll_multiple.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheeL, this);
  365. }
  366. }