HomeView.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import { _decorator, Button, EventTouch, find, instantiate, Label, Node, Prefab, ScrollView, size, Size, Sprite, Toggle, UIOpacity } from 'cc';
  2. import { AppKernel } from '@scripts/app/AppKernel';
  3. import { UIView } from '@scripts/framework/ui/UIView';
  4. import { UIViewID } from '@scripts/define/UIDefine';
  5. import { addBtnClick, setAlpha } from '@scripts/utils/UIHelper';
  6. import { formatVip } from '@scripts/utils/VipHelper';
  7. import { CURRENCY_TYPE, CurrencyType, GAME_TABS } from '@scripts/define/Define';
  8. import { UISwiper } from '@scripts/components/swiper/UISwiper';
  9. import VirtualList from '@scripts/components/virtualList/VirtualList';
  10. import { IGameInfo } from '@scripts/store/gameStore/data';
  11. import { GameItem } from './GameItem';
  12. import { RED_POINTS } from '@scripts/app/AppRedpoint';
  13. const { ccclass, property } = _decorator;
  14. const GameTabValus = [
  15. GAME_TABS.Home,
  16. GAME_TABS.Hot,
  17. GAME_TABS.New,
  18. GAME_TABS.Recent,
  19. GAME_TABS.Favs,
  20. ]
  21. @ccclass('HomeView')
  22. export class HomeView extends UIView {
  23. readonly viewId = UIViewID.HomeView;
  24. @property(UISwiper)
  25. bannerNode: UISwiper;
  26. @property(Node)
  27. homeGamesNode: Node;
  28. @property(VirtualList)
  29. homeGamesScroll: VirtualList;
  30. @property(Node)
  31. tabGamesNode: Node;
  32. @property(VirtualList)
  33. tabGamesScroll: VirtualList;
  34. @property(Prefab)
  35. gamePrefab: Prefab;
  36. private userNode: UserNode;
  37. private gcModel: CoinModel;
  38. private scModel: CoinModel;
  39. private gameTabs: GameTab[] = [];
  40. private tabGameFlags: { [key: string]: Node } = {};
  41. private gameTabIndex = -1;
  42. private coinModel: CurrencyType = CURRENCY_TYPE.GC;
  43. private gameDatas: IGameInfo[] = [];
  44. private menuPopup: MenuPopup;
  45. protected onCreate(): void | Promise<void> {
  46. AppKernel.events.on('login:success', this.updateView, this);
  47. this.homeGamesScroll.renderEvent.target = this.node;
  48. this.homeGamesScroll.renderEvent.component = "HomeView";
  49. this.homeGamesScroll.renderEvent.handler = "onGameItemRender";
  50. this.tabGamesScroll.renderEvent.target = this.node;
  51. this.tabGamesScroll.renderEvent.component = "HomeView";
  52. this.tabGamesScroll.renderEvent.handler = "onGameItemRender";
  53. this.tabGameFlags = {
  54. [GAME_TABS.Hot]: find("tabGames/sptHot", this.node),
  55. [GAME_TABS.New]: find("tabGames/sptNew", this.node),
  56. [GAME_TABS.Recent]: find("tabGames/sptRecent", this.node),
  57. [GAME_TABS.Favs]: find("tabGames/sptFavs", this.node),
  58. }
  59. this.menuPopup = new MenuPopup(find("menuNode", this.node));
  60. this.userNode = new UserNode(find("topNode/userNode", this.node));
  61. this.gcModel = new CoinModel(find("topNode/coinModel/gcNode", this.node), "lblGC");
  62. this.scModel = new CoinModel(find("topNode/coinModel/scNode", this.node), "lblSC");
  63. addBtnClick(this.gcModel.node, this.onGCModelClick, this);
  64. addBtnClick(this.scModel.node, this.onSCModelClick, this);
  65. const homeTab = new GameTab(find("tabsNode/btnHome", this.node));
  66. const hotTab = new GameTab(find("tabsNode/btnHot", this.node));
  67. const newTab = new GameTab(find("tabsNode/btnNew", this.node));
  68. const recentTab = new GameTab(find("tabsNode/btnRecent", this.node));
  69. const favsTab = new GameTab(find("tabsNode/btnFavs", this.node));
  70. this.gameTabs = [homeTab, hotTab, newTab, recentTab, favsTab];
  71. addBtnClick(homeTab.node, this.onHomeTabClick, this);
  72. addBtnClick(hotTab.node, this.onHotTabClick, this);
  73. addBtnClick(newTab.node, this.onNewTabClick, this);
  74. addBtnClick(recentTab.node, this.onRecentTabClick, this);
  75. addBtnClick(favsTab.node, this.onFavsTabClick, this);
  76. const btnBuy = find("topNode/btnBuy", this.node);
  77. const btnFirst = find("topNode/btnFirst", this.node);
  78. const btnInvite = find("topNode/btnInvite", this.node);
  79. const btnMail = find("topNode/btnMail", this.node);
  80. const btnService = find("topNode/btnService", this.node);
  81. const btnMenu = find("topNode/btnMenu", this.node);
  82. const btnTask = find("bottomNode/btnTask", this.node);
  83. const btnDaily = find("bottomNode/btnDaily", this.node);
  84. const btnRedeem = find("bottomNode/btnRedeem", this.node);
  85. const btnWeekly = find("bottomNode/btnWeekly", this.node);
  86. const btnVip = find("bottomNode/btnVip", this.node);
  87. addBtnClick(this.userNode.node, this.onUserNodeClick, this);
  88. addBtnClick(this.gcModel.node, this.onGCModelClick, this);
  89. addBtnClick(this.scModel.node, this.onSCModelClick, this);
  90. addBtnClick(btnBuy, this.onBtnBuyClick, this);
  91. addBtnClick(btnFirst, this.onBtnFirstClick, this);
  92. addBtnClick(btnInvite, this.onBtnInviteClick, this);
  93. addBtnClick(btnMail, this.onBtnMailClick, this);
  94. addBtnClick(btnService, this.onBtnServiceClick, this);
  95. addBtnClick(btnMenu, this.onBtnMenuClick, this);
  96. addBtnClick(btnTask, this.onBtnTaskClick, this);
  97. addBtnClick(btnDaily, this.onBtnDailyClick, this);
  98. addBtnClick(btnRedeem, this.onBtnRedeemClick, this);
  99. addBtnClick(btnWeekly, this.onBtnWeeklyClick, this);
  100. addBtnClick(btnVip, this.onBtnVipClick, this);
  101. const mailPoint = find("redPoint", btnMail);
  102. const taskPoint = find("redPoint", btnTask);
  103. const dailyPoint = find("redPoint", btnDaily);
  104. AppKernel.redpoint.on(RED_POINTS.Mail, mailPoint, this);
  105. AppKernel.redpoint.on(RED_POINTS.DailyTask, taskPoint, this);
  106. AppKernel.redpoint.on(RED_POINTS.DailyReward, dailyPoint, this);
  107. }
  108. protected onDestroy(): void {
  109. AppKernel.redpoint.offTarget(this);
  110. }
  111. private updateGameModel(type: CurrencyType) {
  112. this.coinModel = type;
  113. this.gcModel.setActive(type === CURRENCY_TYPE.GC);
  114. this.scModel.setActive(type === CURRENCY_TYPE.SC);
  115. }
  116. private updateGameTabs(index: number) {
  117. if (index === this.gameTabIndex) return;
  118. if (!GameTabValus[index]) return;
  119. this.gameTabIndex = index;
  120. this.gameTabs.forEach((tab, i) => {
  121. tab.setActive(index === i);
  122. });
  123. }
  124. private updateGameList() {
  125. const currency = this.coinModel;
  126. const tag = GameTabValus[this.gameTabIndex];
  127. const isHome = tag === GAME_TABS.Home;
  128. const gameStore = AppKernel.stores.gameStore;
  129. const games = gameStore.filterGames(tag, currency);
  130. this.homeGamesNode.active = isHome;
  131. this.tabGamesNode.active = !isHome;
  132. this.gameDatas = games;
  133. const scrollView = isHome ? this.homeGamesScroll : this.tabGamesScroll;
  134. scrollView.numItems = games.length;
  135. scrollView.scrollTo(0, 0.2);
  136. if (!isHome) {
  137. for (const key in this.tabGameFlags) {
  138. const flag = this.tabGameFlags[key];
  139. flag.active = key === tag;
  140. }
  141. }
  142. }
  143. onGameItemRender(item: Node, index: number) {
  144. const data = this.gameDatas[index];
  145. if (!item["_gameItem"]) {
  146. item["_gameItem"] = item.getComponent(GameItem);
  147. }
  148. const gameItem = item['_gameItem'] as GameItem;
  149. gameItem.updateItem(data, index);
  150. }
  151. private onHomeTabClick() {
  152. this.updateGameTabs(0);
  153. this.updateGameList();
  154. }
  155. private onHotTabClick() {
  156. this.updateGameTabs(1);
  157. this.updateGameList();
  158. }
  159. private onNewTabClick() {
  160. this.updateGameTabs(2);
  161. this.updateGameList();
  162. }
  163. private onRecentTabClick() {
  164. this.updateGameTabs(3);
  165. this.updateGameList();
  166. }
  167. private onFavsTabClick() {
  168. this.updateGameTabs(4);
  169. this.updateGameList();
  170. }
  171. private onBtnDailyClick() {
  172. }
  173. private onBtnTaskClick() {
  174. }
  175. private onBtnRedeemClick() {
  176. }
  177. private onBtnWeeklyClick() {
  178. }
  179. private onBtnVipClick() {
  180. }
  181. private onBtnMenuClick() {
  182. this.menuPopup.open();
  183. }
  184. private onBtnServiceClick() {
  185. }
  186. private onBtnInviteClick() {
  187. }
  188. private onBtnFirstClick() {
  189. }
  190. private onBtnBuyClick() {
  191. AppKernel.ui.open(UIViewID.StoreView);
  192. }
  193. private onUserNodeClick() {
  194. }
  195. private onGCModelClick() {
  196. this.updateGameModel(CURRENCY_TYPE.GC);
  197. this.updateGameList();
  198. }
  199. private onSCModelClick() {
  200. this.updateGameModel(CURRENCY_TYPE.SC);
  201. this.updateGameList();
  202. }
  203. protected onOpen(_params: unknown): void | Promise<void> {
  204. // this.updateView();
  205. }
  206. private updateView() {
  207. const mainStore = AppKernel.stores.mainStore;
  208. const userInfo = mainStore.userInfo;
  209. this.userNode.lblVip.string = formatVip(userInfo.vip);
  210. this.updateGameTabs(0);
  211. this.updateGameModel(CURRENCY_TYPE.GC);
  212. this.updateGameList();
  213. this.bannerNode.init(5);
  214. // // this.gameRoot.destroyAllChildren();
  215. // const gameStore = AppKernel.stores.gameStore;
  216. // const gameList = gameStore.getGameList();
  217. // // gameList.map(game => this.initGameItem(game));
  218. // LoopList.ins<IGameInfo>("HomeGameList").init(this.gameScroll, this.gameRender, 20);
  219. // LoopList.ins<IGameInfo>("HomeGameList").initData(gameList, this.gamePrefab, size(115, 140))
  220. }
  221. private onBtnMailClick() {
  222. AppKernel.ui.open(UIViewID.MailView);
  223. }
  224. private onBtnStoreClick() {
  225. AppKernel.ui.open(UIViewID.StoreView);
  226. }
  227. }
  228. class UserNode {
  229. lblVip: Label;
  230. sptAvatar: Sprite;
  231. constructor(readonly node: Node) {
  232. this.lblVip = find("lblLv", node).getComponent(Label);
  233. this.sptAvatar = find("sptAvatar", node).getComponent(Sprite);
  234. }
  235. }
  236. class CoinModel {
  237. lblValue: Label;
  238. private sptIcon: Sprite;
  239. private sptActive: Sprite;
  240. constructor(readonly node: Node, lblName: string) {
  241. this.lblValue = find(lblName, node.parent).getComponent(Label);
  242. this.sptActive = find("sptActive", node).getComponent(Sprite);
  243. this.sptIcon = find("sptIcon", node).getComponent(Sprite);
  244. }
  245. setActive(active) {
  246. setAlpha(this.sptActive, active ? 1 : 0);
  247. setAlpha(this.lblValue, active ? 1 : 0.54);
  248. setAlpha(this.sptIcon, active ? 1 : 0.54);
  249. }
  250. }
  251. class GameTab {
  252. private sptActive: UIOpacity;
  253. constructor(readonly node: Node) {
  254. this.sptActive = find("sptActive", node).getComponent(UIOpacity);
  255. }
  256. setActive(active: boolean) {
  257. this.sptActive.opacity = active ? 255 : 0;
  258. }
  259. }
  260. class MenuPopup {
  261. private togSound: Toggle;
  262. private togMusic: Toggle;
  263. constructor(readonly node: Node) {
  264. node.active = false;
  265. this.togSound = find("content/togSound", node).getComponent(Toggle);
  266. this.togMusic = find("content/togMusic", node).getComponent(Toggle);
  267. const btnFAQ = find("content/btnFAQ", node);
  268. const btnPhone = find("content/btnPhone", node);
  269. const btnEmail = find("content/btnEmail", node);
  270. const btnVerify = find("content/btnVerify", node);
  271. const btnSafe = find("content/btnSafe", node);
  272. const btnHistory = find("content/btnHistory", node);
  273. const btnInfo = find("content/btnInfo", node);
  274. const btnLogout = find("content/btnLogout", node);
  275. addBtnClick(btnFAQ, this.onBtnFAQClick, this);
  276. addBtnClick(btnPhone, this.onBtnPhoneClick, this);
  277. addBtnClick(btnEmail, this.onBtnEmailClick, this);
  278. addBtnClick(btnVerify, this.onBtnVerifyClick, this);
  279. addBtnClick(btnSafe, this.onBtnSafeClick, this);
  280. addBtnClick(btnHistory, this.onBtnHistoryClick, this);
  281. addBtnClick(btnInfo, this.onBtnInfoClick, this);
  282. addBtnClick(btnLogout, this.onBtnLogoutClick, this);
  283. const mask = find("mask", node);
  284. mask.on(Node.EventType.TOUCH_END, this.onMaskClick, this);
  285. }
  286. private onMaskClick(event: EventTouch) {
  287. this.node.active = false;
  288. }
  289. private onBtnFAQClick() {
  290. }
  291. private onBtnPhoneClick() {
  292. }
  293. private onBtnEmailClick() {
  294. }
  295. private onBtnVerifyClick() {
  296. }
  297. private onBtnSafeClick() {
  298. }
  299. private onBtnHistoryClick() {
  300. AppKernel.ui.open(UIViewID.PlayHistory);
  301. }
  302. private onBtnInfoClick() {
  303. AppKernel.ui.open(UIViewID.GamePolicyView);
  304. }
  305. private onBtnLogoutClick() {
  306. }
  307. open() {
  308. this.node.active = true;
  309. this.togMusic.isChecked = true;
  310. this.togSound.isChecked = true;
  311. }
  312. }