HomeView.ts 14 KB

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