You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 rivejä
17 KiB

  1. local GameApp = class("GameApp", require("luaScript.gameAppBase"))
  2. function GameApp:ctor()
  3. GameApp.super.ctor(self);
  4. -- 当前的View
  5. self.currentView = nil;
  6. -- 主场景
  7. self.mainScene = nil;
  8. -- 等待窗口管理器
  9. self.waitDialogManager = nil;
  10. -- 滚动公告
  11. self.scrollNoticeUI = nil;
  12. -- 从屏幕上方掉落的公告
  13. self.dropNoticeUI = nil;
  14. --普通主视图层
  15. self.baseLayout = nil
  16. --普通弹框层
  17. self.dialogLayout = nil
  18. --网络请求等待层
  19. self.waitLayout = nil
  20. --系统弹框层
  21. self.systemLayout = nil
  22. --错误弹框层
  23. self.errLayout = nil
  24. --适配刘海层
  25. self.suitLayout = nil
  26. end
  27. function GameApp:initEapSdk()
  28. require("luaScript.ModuleEapSdk.IncludeEapSdk")--接入EapSDK
  29. local LoginConfig = require("luaScript.Views.Login.LoginConfig")
  30. local EapSDK = LoginConfig.EapSDK or {}
  31. G_EapSdkMgr:InitSdk(EapSDK.appid or 0, EapSDK.sourceid or 0)
  32. --接入EAPSDK
  33. local LayerMain = require("luaScript.ModuleEapSdk.ModuleLayers.LayerMain")
  34. local layer = LayerMain.new()
  35. app.mainScene:addChild(layer)
  36. cc.exports.G_LayerMain = layer
  37. end
  38. function GameApp:run()
  39. -- 初始化cocos2d引擎
  40. self:initEngine()
  41. -- 初始化电竞SDK
  42. local LoginConfig = require("luaScript.Views.Login.LoginConfig")
  43. if LoginConfig.EapSDK and not isWin32Platform() then
  44. self:initEapSdk()
  45. end
  46. --仅测试模式下
  47. if RomSetting and RomSetting.SuitScreen and RomSetting.isDevMode and not isIOSReviewVersion()
  48. and not RomSetting.isRuanZhu and cc.Application:getInstance():getTargetPlatform() == 0 then
  49. app:showLiuHaiPing()
  50. end
  51. if isReviewWithMiniGame() then
  52. local mainViewScript = getMiniGameMainViewScript()
  53. if mainViewScript then
  54. app:gotoView(mainViewScript)
  55. end
  56. else
  57. -- 进入登陆页面
  58. app:gotoView("luaScript.Views.Login.LoginView")
  59. end
  60. end
  61. -- 关闭当前的View
  62. function GameApp:closeView(view)
  63. print("GameApp::closeView:", view.__cname, debug.traceback());
  64. cancelLoadBattleView();
  65. self:cancelFadeOut();
  66. self:cancelFadeIn();
  67. local currentViewName
  68. if self.currentView then
  69. currentViewName = self.currentView.__cname
  70. end
  71. if self.currentView == view then
  72. print("关闭当前View:", currentViewName)
  73. self.currentView:removeFromParent();
  74. self.currentView = nil;
  75. else
  76. -- view没有关闭
  77. print("currentView跟传入的View不是同一个,关闭传入的View,currentView名字:", currentViewName)
  78. view:removeFromParent();
  79. end
  80. end
  81. -- 切换到这个视图
  82. function GameApp:gotoView(view)
  83. print("gotoView: " , view.__cname , debug.traceback())
  84. -- if self.fadeUI.fadeOuting then
  85. -- error("在淡出过程中调用gotoView是不允许的");
  86. -- end
  87. -- if not tolua.isnull(self.currentView) then
  88. -- self.currentView:removeFromParent()
  89. -- end
  90. if type(view) == "string" then
  91. view = import(view):new()
  92. end
  93. self.dialogLayout:removeAllChildren()
  94. self.baseLayout:removeAllChildren()
  95. view:setVisible(true)
  96. self.currentView = view
  97. if view:getParent() ~= self.baseLayout then
  98. self.baseLayout:setSize(self.mainScene:getContentSize())
  99. self.baseLayout:addChild(view , UIZOrder.MainLayer);
  100. end
  101. cc.Director:getInstance():getScheduler():setTimeScale(1.0)
  102. end
  103. -- 获得当前视图
  104. function GameApp:getCurrentView()
  105. return self.currentView;
  106. end
  107. function GameApp:isUiView(uiView)
  108. if uiView.__cname == "UIView" then
  109. return true
  110. end
  111. local isUIView = false
  112. local parent = uiView
  113. local isContinue = true
  114. while(isContinue)
  115. do
  116. parent = parent.super
  117. if parent then
  118. if parent.__cname == "UIView" then
  119. isContinue = false
  120. isUIView = true
  121. end
  122. else
  123. isContinue = false
  124. end
  125. end
  126. return isUIView
  127. end
  128. -- 显示模态对话框窗口
  129. -- uiView : ui 窗口
  130. -- transparency :背景透明度 (0:全透明, 255:不透明)
  131. -- closeOnClick : 点击任意位置关闭界面
  132. function GameApp:_showDialog(uiView, transparency, closeOnClick,zOrder,callback)
  133. if type(uiView)=="string" then
  134. uiView = import(uiView):new()
  135. uiView:setAnchorPoint(cc.p(0.5, 0.5))
  136. end
  137. local isUiView = self:isUiView(uiView)
  138. assert(isUiView, "必须传一个UIView");
  139. local className = uiView.__cname;
  140. local modelDialog = loadUI("res/ui/ui_common/ModelDialog.ui");
  141. -- 加载背景图
  142. modelDialog.Items.ImageView_Background:setVisible(false);
  143. -- 遮罩透明度
  144. transparency = tonumber(transparency) or 255
  145. modelDialog.Items.Background:setOpacity(transparency)
  146. --获取普通主视图层size
  147. local mainsize = self.baseLayout:getContentSize()
  148. --modelDialog:setSize(cc.Director:getInstance():getWinSize());
  149. modelDialog:setSize(mainsize);
  150. modelDialog.Items.Container:addChild(uiView)
  151. local clampingNode = modelDialog.Items.ClampingNode;
  152. clampingNode:setSendTouchEventParent(false);
  153. -- 全局事件次数
  154. if globalZOrder then
  155. -- 让其显示在最上层
  156. modelDialog:setLocalZOrder(globalZOrder)
  157. -- 并且阻挡全局事件
  158. modelDialog:setGlobalZOrder(globalZOrder)
  159. end
  160. --self.dialogLayout:setSize(self.mainScene:getContentSize())
  161. self.dialogLayout:setSize(mainsize)
  162. --兼容性addChild
  163. if zOrder and type(zOrder) == "number" and tonumber(zOrder) then
  164. self.dialogLayout:addChild(modelDialog,zOrder)
  165. else
  166. self.dialogLayout:addChild(modelDialog)
  167. end
  168. local function customRemove()
  169. -- 如果有淡出动画则等动画播放完毕了再关闭
  170. -- 重写fadeOutEnd函数
  171. local outEnd = uiView.fadeOutEnd
  172. uiView.fadeOutEnd = function()
  173. if outEnd then
  174. outEnd(uiView);
  175. end
  176. -- 界面移除
  177. if not tolua.isnull(modelDialog) then
  178. modelDialog:removeFromParent()
  179. end
  180. end
  181. -- 淡出过程中阻止点击
  182. clampingNode:setTouchEnabled(true);
  183. local oldFadeOutEnd = uiView.fadeOutEnd;
  184. uiView.fadeOutEnd = function()
  185. clampingNode:setTouchEnabled(false);
  186. oldFadeOutEnd(uiView);
  187. end
  188. uiView:fadeOut();
  189. end
  190. modelDialog:setSendTouchEventParent(false);
  191. local function onClick()
  192. if closeOnClick then
  193. if callback then
  194. callback()
  195. end
  196. logD("进入 界面后-点了空白任意关闭退出")
  197. uiView:removeFromParent()
  198. else
  199. return true;
  200. end;
  201. end
  202. modelDialog:registerClick(onClick,nil,nil,true)
  203. uiView.modelDialogUI = modelDialog;
  204. -- 覆盖函数
  205. uiView.removeFromParent = function(self)
  206. customRemove()
  207. end
  208. end
  209. -- 显示模态对话框窗口,不能点击任意地方关闭,并阻挡鼠标消息
  210. -- transparency : 遮罩透明度 (0:全透明, 255:不透明)
  211. -- closeOnClick : 是否点击任意地方关闭窗口
  212. function GameApp:showWaitDialog(uiView, transparency, closeOnClick,zOrder,callback)
  213. self:_showDialog(uiView, transparency, closeOnClick,zOrder,callback);
  214. end
  215. -- 显示模态对话框窗口,不能点击任意地方关闭,并阻挡鼠标消息
  216. -- transparency : 遮罩透明度 (0:全透明, 255:不透明)
  217. -- closeOnClick : 是否点击任意地方关闭窗口
  218. --zOrder :深度
  219. function GameApp:showWaitUI(ui, transparency, closeOnClick,zOrder)
  220. local uiView = import("luaScript.Views.UIView"):new();
  221. uiView.ui = ui;
  222. uiView:addChild(ui);
  223. self:_showDialog(uiView, transparency, closeOnClick,zOrder);
  224. return uiView;
  225. end
  226. -- 显示模态对话框窗口,并不能关闭的,阻挡鼠标消息
  227. function GameApp:showUI(ui, transparency, useGlobalFadeInOut)
  228. local uiView = import("luaScript.Views.UIView"):new();
  229. uiView.ui = ui;
  230. uiView:addChild(ui);
  231. self:_showDialog(uiView, transparency, useGlobalFadeInOut, true);
  232. return uiView;
  233. end
  234. -- win32输入
  235. function GameApp:showWinInputDialog(title , defaultValue , maxLength , func)
  236. -- 显示一个透明的蒙版,阻挡所有的点击事件(在当前View上面加)
  237. -- 加到当前的view里面去
  238. local mask = loadUI("res/ui/ui_common/TransparencyMask.ui")
  239. -- 加到当前的view里面去
  240. local inputView = loadUI("res/ui/ui_tongyong/t_laiotian_shurukuang.ui")
  241. if self.currentView then
  242. self.currentView:addChild(mask)
  243. self.currentView:addChild(inputView)
  244. else
  245. print("当前View不存在, InputDialog添加到mainScene上面去了")
  246. -- 加到指定的节点里面去
  247. self.dialogLayout:setSize(self.mainScene:getContentSize())
  248. self.dialogLayout:addChild(mask);
  249. self.dialogLayout:addChild(inputView);
  250. end
  251. --设置最大长度
  252. inputView.Items.TextField_5:setMaxLength(maxLength);
  253. --设置默认显示的内容
  254. inputView.Items.TextField_5:setText(defaultValue);
  255. -- 取消按钮
  256. inputView.Items.Button_quxiao:registerClick(function() inputView:removeFromParent();mask:removeFromParent(); end)
  257. -- 确定按钮
  258. local function sendMsg()
  259. --执行函数
  260. func(inputView.Items.TextField_5:getString());
  261. --设置输入文本为空
  262. inputView.Items.TextField_5:setText("");
  263. inputView:removeFromParent();
  264. mask:removeFromParent();
  265. end
  266. inputView.Items.Button_denglu:registerClick(sendMsg)
  267. end
  268. function GameApp:showErrorDialog(view,...)
  269. if type(view)=="string" then
  270. view = import(view):new(...)
  271. view:setAnchorPoint(cc.p(0.5, 0.5))
  272. end
  273. -- self.errLayout:removeAllChildren()
  274. self.errLayout:setSize(self.mainScene:getContentSize())
  275. self.errLayout:addChild(view)
  276. end
  277. function GameApp:showNetWaitDialog(view,...)
  278. if type(view)=="string" then
  279. view = import(view):new(...)
  280. view:setAnchorPoint(cc.p(0.5, 0.5))
  281. end
  282. self.waitLayout:removeAllChildren()
  283. self.waitLayout:setSize(self.mainScene:getContentSize())
  284. self.waitLayout:addChild(view)
  285. end
  286. function GameApp:showSytemDialog(view,...)
  287. if type(view)=="string" then
  288. view = import(view):new(...)
  289. view:setAnchorPoint(cc.p(0.5, 0.5))
  290. end
  291. -- self.systemLayout:removeAllChildren()
  292. self.systemLayout:setSize(self.mainScene:getContentSize())
  293. self.systemLayout:addChild(view)
  294. end
  295. -- 初始化引擎的默认参数
  296. function GameApp:initEngine()
  297. if not IsLuaEditor then
  298. print("初始化引擎");
  299. -- 初始化随机种子
  300. math.randomseed(os.time());
  301. -- initialize director
  302. local director = cc.Director:getInstance();
  303. -- turn on display FPS
  304. director:setDisplayStats(app.config.RomSetting.DisplayFPS);
  305. -- set FPS. the default value is 1.0/60 if you don't call this
  306. --director:setAnimationInterval(1.0 / app.config.RomSetting.FPS);
  307. director:setAnimationInterval(1.0 / 60);
  308. local resolutionSize = director:getOpenGLView():getDesignResolutionSize()
  309. self.mainScene = cc.Scene:create("mainScene");
  310. self.mainScene:setContentSize(resolutionSize);
  311. self.mainScene:setAnchorPoint({x=0,y=0});
  312. -- 创建一个子节点用来处理当前没有VIEW显示的时候,将其他界面加到这个节点上
  313. -- local showDialogNode = cc.Node:create("showDialogNode");
  314. -- self.mainScene:addChild(showDialogNode)
  315. -- self.mainScene.showDialogNode = showDialogNode
  316. --初始化层级
  317. --普通主视图层
  318. self.baseLayout = cc.Layout:create()
  319. self.baseLayout:setSize(resolutionSize)
  320. self.mainScene:addChild(self.baseLayout)
  321. --普通弹框层
  322. self.dialogLayout = cc.Layout:create()
  323. self.dialogLayout:setSize(resolutionSize)
  324. self.mainScene:addChild(self.dialogLayout)
  325. --网络请求等待层
  326. self.waitLayout = cc.Layout:create()
  327. self.waitLayout:setSize(resolutionSize)
  328. self.mainScene:addChild(self.waitLayout)
  329. --系统弹框层
  330. self.systemLayout = cc.Layout:create()
  331. self.systemLayout:setSize(resolutionSize)
  332. self.mainScene:addChild(self.systemLayout)
  333. --错误弹框层
  334. self.errLayout = cc.Layout:create()
  335. self.errLayout:setSize(resolutionSize)
  336. self.mainScene:addChild(self.errLayout)
  337. --刘海适配层
  338. self.suitLayout = cc.Layout:create()
  339. self.suitLayout:setSize(resolutionSize)
  340. self.mainScene:addChild(self.suitLayout)
  341. -- 加载通用资源
  342. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/default/default-textures.plist");
  343. -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_1.plist");
  344. -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_2.plist");
  345. -- 创建一个淡入淡出的蒙版
  346. -- self.fadeUI = loadUI("res/ui/ui_tongyong/t_fadein_fadeout.ui")
  347. -- self.mainScene:addChild(self.fadeUI)
  348. -- self.fadeUI:setLocalZOrder(UIZOrder.FadeOrder)
  349. -- self.fadeUI:setVisible(false)
  350. if director:getRunningScene() then
  351. director:replaceScene(self.mainScene);
  352. else
  353. director:runWithScene(self.mainScene);
  354. end
  355. -- 设置统一的音源方位
  356. cc.AudioListener:getInstance():setOrientation(cc.vec3(0,0,-1) , cc.vec3(0,1,0));
  357. -- 开启光效缓存机制
  358. startEffectCache();
  359. -- 创建等待窗口管理器
  360. self.waitDialogManager = require("luaScript.WaitDialogManager"):new()
  361. -- 全局事件侦听
  362. startGlobalEvent()
  363. -- 使用节点来缓存数据
  364. -- local privateNode = cc.Node:create("privateNode");
  365. -- privateNode:setVisible(false);
  366. -- local privateNode1 = cc.Node:create("privateNode1");
  367. -- privateNode1:setUserObject(cc.TextureCache:getInstance():addImage("res/ui/zy_tongyong/t_tongyong_1.png"));
  368. -- privateNode:addChild(privateNode1);
  369. -- local privateNode2 = cc.Node:create("privateNode2");
  370. -- privateNode2:setUserObject(cc.TextureCache:getInstance():addImage("res/ui/zy_tongyong/t_tongyong_2.png"));
  371. -- privateNode:addChild(privateNode2);
  372. -- self.mainScene:addChild(privateNode);
  373. -- 加载表情
  374. -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/default/emoji.plist");
  375. local winSize = director:getOpenGLView():getFrameSize();
  376. -- 掉落公告的根节点
  377. self.dropNoticeUI = loadUI("res/ui/ui_tongyong/droplayout.ui")
  378. self.dropNoticeUI:setPosition(cc.p(resolutionSize.width * 0.5, resolutionSize.height * 0.6))
  379. self.systemLayout:addChild(self.dropNoticeUI , UIZOrder.DropNotify);
  380. -- 省电模式
  381. local function onSafePower(key , value)
  382. if value then
  383. cc.Director:getInstance():setAnimationInterval(1.0 / 20);
  384. else
  385. cc.Director:getInstance():setAnimationInterval(1.0 / 60);
  386. end
  387. end
  388. if not isReviewWithMiniGame() then
  389. app.systemSetting.info:bind("safePower" , onSafePower);
  390. end
  391. -- 加载所有光效脚本
  392. -- 太慢了!
  393. --preCacheEffect();
  394. -- cc.Director:getInstance():getTextureCache():loadTextureSize("res/textureSize.bin");
  395. -- 加载AnimationClips
  396. -- cc.AnimationClipFileCache:getInstance():loadCacheFile("res/animationClips.bin");
  397. end
  398. end
  399. -- 删除滚动消息
  400. function GameApp:destroyScrollNotice()
  401. if not tolua.isnull(self.scrollNoticeUI) then
  402. self.scrollNoticeUI:removeFromParent();
  403. end
  404. self.scrollNoticeUI = nil;
  405. end
  406. -- 显示滚动消息
  407. function GameApp:showScrollNotice(message)
  408. self:destroyScrollNotice();
  409. local ui = loadUI("res/ui/ui_tongyong/t_gungdong_xiaoxi.ui");
  410. self.baseLayout:addChild(ui , UIZOrder.SystemNotify);
  411. self.scrollNoticeUI = ui;
  412. local htmlCtrl = ui.Items.HtmlCtrl_3;
  413. htmlCtrl:setPositionType(0);
  414. htmlCtrl:setText('<font size="28" color="ff5700" stroke="size:2;color:#000000">' .. message .. '</font>');
  415. local winSize = cc.Director:getInstance():getWinSize();
  416. local size = htmlCtrl:getSize();
  417. local speed = 100;
  418. htmlCtrl:runActions(cc.MoveTo:create((size.width + winSize.width) / speed , cc.p(-size.width , htmlCtrl:getPositionY())) , function()self:destroyScrollNotice();end);
  419. end
  420. -- 从屏幕上方掉落一个消息
  421. --[[
  422. tTexts =
  423. {
  424. {color = cc.c3b(1,1,1), text = ""},
  425. {color = cc.c3b(1,1,1), text = ""},
  426. {color = cc.c3b(1,1,1), text = ""},
  427. }
  428. --]]
  429. function GameApp:dropNotice(tTexts)
  430. if not self.dropNoticeUI then
  431. return
  432. end
  433. local layout = self.dropNoticeUI.Items.Layout_1
  434. local delayTime = 3--gps提示退出房间后还会停留一段时间,因此把这里的时间由6秒改为3秒
  435. local ui = loadUI("res/ui/ui_tongyong/droptips.ui");
  436. local uiTemplate = ui.Items.Text
  437. uiTemplate:setVisible(false);
  438. for k,v in ipairs(tTexts) do
  439. local uiText = uiTemplate:getCopied()
  440. if uiText then
  441. uiText:setText(v.text or "")
  442. uiText:setColor(v.color or cc.c3b(255,255,255))
  443. ui.Items.Layout_1:addChild(uiText)
  444. end
  445. end
  446. ui.Items.Layout_1:requestDoLayout()
  447. ui.Items.Layout_1:doLayout();
  448. layout:addChild(ui);
  449. layout:requestDoLayout()
  450. layout:doLayout();
  451. ui:runDelay(delayTime, function()
  452. if not tolua.isnull(ui) then
  453. ui:removeFromParent();
  454. end
  455. end)
  456. end
  457. -- 滚动公告,播放完成后自动删除
  458. function GameApp:showScrollTips(txt, pos, speed)
  459. local winSize = cc.Director:getInstance():getWinSize();
  460. pos = pos or cc.p(winSize.width * 0.5, winSize.height * 0.75);
  461. txt = txt or "unknow";
  462. speed = speed or 80;
  463. local ui = loadUI("res/ui/ui_tongyong/ui_dating_scroll_tips.ui");
  464. ui:setAnchorPoint(cc.p(0.5, 0.5));
  465. ui:setPosition(pos)
  466. --self.currentView:addChild(ui);
  467. self.baseLayout:addChild(ui , UIZOrder.DropNotify);
  468. local nodeLayout = ui.Items.Layout_Text;
  469. local szLayout = nodeLayout:getContentSize();
  470. local nodeText = ui.Items.Text;
  471. nodeText:setText(txt);
  472. nodeText:setAnchorPoint(cc.p(0,0.5));
  473. nodeText:setPosition(cc.p(szLayout.width, szLayout.height / 2));
  474. local szText = nodeText:getSize();
  475. local actionMove = cc.MoveTo:create((szText.width + szLayout.width) / speed , cc.p(-szText.width , szLayout.height / 2));
  476. local actionEnd = cc.CallFunc:create(function() ui:removeFromParent(); end)
  477. nodeText:runAction(createActionArray(actionMove, actionEnd));
  478. end
  479. -- 加载ui文件到内存
  480. function GameApp:loadUIFiles(uiFiles)
  481. if type(uiFiles) ~= "table" then
  482. return
  483. end
  484. for k,v in pairs(uiFiles) do
  485. loadUI(v);
  486. end
  487. end
  488. -- 显示刘海
  489. function GameApp:showLiuHaiPing()
  490. local modelDialog = loadUI("res/ui/ui_tongyong/ui_tongyong_liuhai.ui");
  491. --获取普通主视图层size
  492. --local mainsize = self.suitLayout:getContentSize()
  493. --modelDialog:setSize(mainsize);
  494. --self.suitLayout:setSize(mainsize)
  495. self.suitLayout:addChild(modelDialog)
  496. end
  497. return GameApp;