local GameApp = class("GameApp", require("luaScript.gameAppBase")) function GameApp:ctor() GameApp.super.ctor(self); -- 当前的View self.currentView = nil; -- 主场景 self.mainScene = nil; -- 等待窗口管理器 self.waitDialogManager = nil; -- 滚动公告 self.scrollNoticeUI = nil; -- 从屏幕上方掉落的公告 self.dropNoticeUI = nil; --普通主视图层 self.baseLayout = nil --普通弹框层 self.dialogLayout = nil --网络请求等待层 self.waitLayout = nil --系统弹框层 self.systemLayout = nil --错误弹框层 self.errLayout = nil --适配刘海层 self.suitLayout = nil end function GameApp:initEapSdk() require("luaScript.ModuleEapSdk.IncludeEapSdk")--接入EapSDK local LoginConfig = require("luaScript.Views.Login.LoginConfig") local EapSDK = LoginConfig.EapSDK or {} G_EapSdkMgr:InitSdk(EapSDK.appid or 0, EapSDK.sourceid or 0) --接入EAPSDK local LayerMain = require("luaScript.ModuleEapSdk.ModuleLayers.LayerMain") local layer = LayerMain.new() app.mainScene:addChild(layer) cc.exports.G_LayerMain = layer end function GameApp:run() -- 初始化cocos2d引擎 self:initEngine() -- 初始化电竞SDK local LoginConfig = require("luaScript.Views.Login.LoginConfig") if LoginConfig.EapSDK and not isWin32Platform() then self:initEapSdk() end --仅测试模式下 if RomSetting and RomSetting.SuitScreen and RomSetting.isDevMode and not isIOSReviewVersion() and not RomSetting.isRuanZhu and cc.Application:getInstance():getTargetPlatform() == 0 then app:showLiuHaiPing() end if isReviewWithMiniGame() then local mainViewScript = getMiniGameMainViewScript() if mainViewScript then app:gotoView(mainViewScript) end else -- 进入登陆页面 app:gotoView("luaScript.Views.Login.LoginView") end end -- 关闭当前的View function GameApp:closeView(view) print("GameApp::closeView:", view.__cname, debug.traceback()); cancelLoadBattleView(); self:cancelFadeOut(); self:cancelFadeIn(); local currentViewName if self.currentView then currentViewName = self.currentView.__cname end if self.currentView == view then print("关闭当前View:", currentViewName) self.currentView:removeFromParent(); self.currentView = nil; else -- view没有关闭 print("currentView跟传入的View不是同一个,关闭传入的View,currentView名字:", currentViewName) view:removeFromParent(); end end -- 切换到这个视图 function GameApp:gotoView(view) print("gotoView: " , view.__cname , debug.traceback()) -- if self.fadeUI.fadeOuting then -- error("在淡出过程中调用gotoView是不允许的"); -- end -- if not tolua.isnull(self.currentView) then -- self.currentView:removeFromParent() -- end if type(view) == "string" then view = import(view):new() end self.dialogLayout:removeAllChildren() self.baseLayout:removeAllChildren() view:setVisible(true) self.currentView = view if view:getParent() ~= self.baseLayout then self.baseLayout:setSize(self.mainScene:getContentSize()) self.baseLayout:addChild(view , UIZOrder.MainLayer); end cc.Director:getInstance():getScheduler():setTimeScale(1.0) end -- 获得当前视图 function GameApp:getCurrentView() return self.currentView; end function GameApp:isUiView(uiView) if uiView.__cname == "UIView" then return true end local isUIView = false local parent = uiView local isContinue = true while(isContinue) do parent = parent.super if parent then if parent.__cname == "UIView" then isContinue = false isUIView = true end else isContinue = false end end return isUIView end -- 显示模态对话框窗口 -- uiView : ui 窗口 -- transparency :背景透明度 (0:全透明, 255:不透明) -- closeOnClick : 点击任意位置关闭界面 function GameApp:_showDialog(uiView, transparency, closeOnClick,zOrder,callback) if type(uiView)=="string" then uiView = import(uiView):new() uiView:setAnchorPoint(cc.p(0.5, 0.5)) end local isUiView = self:isUiView(uiView) assert(isUiView, "必须传一个UIView"); local className = uiView.__cname; local modelDialog = loadUI("res/ui/ui_common/ModelDialog.ui"); -- 加载背景图 modelDialog.Items.ImageView_Background:setVisible(false); -- 遮罩透明度 transparency = tonumber(transparency) or 255 modelDialog.Items.Background:setOpacity(transparency) --获取普通主视图层size local mainsize = self.baseLayout:getContentSize() --modelDialog:setSize(cc.Director:getInstance():getWinSize()); modelDialog:setSize(mainsize); modelDialog.Items.Container:addChild(uiView) local clampingNode = modelDialog.Items.ClampingNode; clampingNode:setSendTouchEventParent(false); -- 全局事件次数 if globalZOrder then -- 让其显示在最上层 modelDialog:setLocalZOrder(globalZOrder) -- 并且阻挡全局事件 modelDialog:setGlobalZOrder(globalZOrder) end --self.dialogLayout:setSize(self.mainScene:getContentSize()) self.dialogLayout:setSize(mainsize) --兼容性addChild if zOrder and type(zOrder) == "number" and tonumber(zOrder) then self.dialogLayout:addChild(modelDialog,zOrder) else self.dialogLayout:addChild(modelDialog) end local function customRemove() -- 如果有淡出动画则等动画播放完毕了再关闭 -- 重写fadeOutEnd函数 local outEnd = uiView.fadeOutEnd uiView.fadeOutEnd = function() if outEnd then outEnd(uiView); end -- 界面移除 if not tolua.isnull(modelDialog) then modelDialog:removeFromParent() end end -- 淡出过程中阻止点击 clampingNode:setTouchEnabled(true); local oldFadeOutEnd = uiView.fadeOutEnd; uiView.fadeOutEnd = function() clampingNode:setTouchEnabled(false); oldFadeOutEnd(uiView); end uiView:fadeOut(); end modelDialog:setSendTouchEventParent(false); local function onClick() if closeOnClick then if callback then callback() end logD("进入 界面后-点了空白任意关闭退出") uiView:removeFromParent() else return true; end; end modelDialog:registerClick(onClick,nil,nil,true) uiView.modelDialogUI = modelDialog; -- 覆盖函数 uiView.removeFromParent = function(self) customRemove() end end -- 显示模态对话框窗口,不能点击任意地方关闭,并阻挡鼠标消息 -- transparency : 遮罩透明度 (0:全透明, 255:不透明) -- closeOnClick : 是否点击任意地方关闭窗口 function GameApp:showWaitDialog(uiView, transparency, closeOnClick,zOrder,callback) self:_showDialog(uiView, transparency, closeOnClick,zOrder,callback); end -- 显示模态对话框窗口,不能点击任意地方关闭,并阻挡鼠标消息 -- transparency : 遮罩透明度 (0:全透明, 255:不透明) -- closeOnClick : 是否点击任意地方关闭窗口 --zOrder :深度 function GameApp:showWaitUI(ui, transparency, closeOnClick,zOrder) local uiView = import("luaScript.Views.UIView"):new(); uiView.ui = ui; uiView:addChild(ui); self:_showDialog(uiView, transparency, closeOnClick,zOrder); return uiView; end -- 显示模态对话框窗口,并不能关闭的,阻挡鼠标消息 function GameApp:showUI(ui, transparency, useGlobalFadeInOut) local uiView = import("luaScript.Views.UIView"):new(); uiView.ui = ui; uiView:addChild(ui); self:_showDialog(uiView, transparency, useGlobalFadeInOut, true); return uiView; end -- win32输入 function GameApp:showWinInputDialog(title , defaultValue , maxLength , func) -- 显示一个透明的蒙版,阻挡所有的点击事件(在当前View上面加) -- 加到当前的view里面去 local mask = loadUI("res/ui/ui_common/TransparencyMask.ui") -- 加到当前的view里面去 local inputView = loadUI("res/ui/ui_tongyong/t_laiotian_shurukuang.ui") if self.currentView then self.currentView:addChild(mask) self.currentView:addChild(inputView) else print("当前View不存在, InputDialog添加到mainScene上面去了") -- 加到指定的节点里面去 self.dialogLayout:setSize(self.mainScene:getContentSize()) self.dialogLayout:addChild(mask); self.dialogLayout:addChild(inputView); end --设置最大长度 inputView.Items.TextField_5:setMaxLength(maxLength); --设置默认显示的内容 inputView.Items.TextField_5:setText(defaultValue); -- 取消按钮 inputView.Items.Button_quxiao:registerClick(function() inputView:removeFromParent();mask:removeFromParent(); end) -- 确定按钮 local function sendMsg() --执行函数 func(inputView.Items.TextField_5:getString()); --设置输入文本为空 inputView.Items.TextField_5:setText(""); inputView:removeFromParent(); mask:removeFromParent(); end inputView.Items.Button_denglu:registerClick(sendMsg) end function GameApp:showErrorDialog(view,...) if type(view)=="string" then view = import(view):new(...) view:setAnchorPoint(cc.p(0.5, 0.5)) end -- self.errLayout:removeAllChildren() self.errLayout:setSize(self.mainScene:getContentSize()) self.errLayout:addChild(view) end function GameApp:showNetWaitDialog(view,...) if type(view)=="string" then view = import(view):new(...) view:setAnchorPoint(cc.p(0.5, 0.5)) end self.waitLayout:removeAllChildren() self.waitLayout:setSize(self.mainScene:getContentSize()) self.waitLayout:addChild(view) end function GameApp:showSytemDialog(view,...) if type(view)=="string" then view = import(view):new(...) view:setAnchorPoint(cc.p(0.5, 0.5)) end -- self.systemLayout:removeAllChildren() self.systemLayout:setSize(self.mainScene:getContentSize()) self.systemLayout:addChild(view) end -- 初始化引擎的默认参数 function GameApp:initEngine() if not IsLuaEditor then print("初始化引擎"); -- 初始化随机种子 math.randomseed(os.time()); -- initialize director local director = cc.Director:getInstance(); -- turn on display FPS director:setDisplayStats(app.config.RomSetting.DisplayFPS); -- set FPS. the default value is 1.0/60 if you don't call this --director:setAnimationInterval(1.0 / app.config.RomSetting.FPS); director:setAnimationInterval(1.0 / 60); local resolutionSize = director:getOpenGLView():getDesignResolutionSize() self.mainScene = cc.Scene:create("mainScene"); self.mainScene:setContentSize(resolutionSize); self.mainScene:setAnchorPoint({x=0,y=0}); -- 创建一个子节点用来处理当前没有VIEW显示的时候,将其他界面加到这个节点上 -- local showDialogNode = cc.Node:create("showDialogNode"); -- self.mainScene:addChild(showDialogNode) -- self.mainScene.showDialogNode = showDialogNode --初始化层级 --普通主视图层 self.baseLayout = cc.Layout:create() self.baseLayout:setSize(resolutionSize) self.mainScene:addChild(self.baseLayout) --普通弹框层 self.dialogLayout = cc.Layout:create() self.dialogLayout:setSize(resolutionSize) self.mainScene:addChild(self.dialogLayout) --网络请求等待层 self.waitLayout = cc.Layout:create() self.waitLayout:setSize(resolutionSize) self.mainScene:addChild(self.waitLayout) --系统弹框层 self.systemLayout = cc.Layout:create() self.systemLayout:setSize(resolutionSize) self.mainScene:addChild(self.systemLayout) --错误弹框层 self.errLayout = cc.Layout:create() self.errLayout:setSize(resolutionSize) self.mainScene:addChild(self.errLayout) --刘海适配层 self.suitLayout = cc.Layout:create() self.suitLayout:setSize(resolutionSize) self.mainScene:addChild(self.suitLayout) -- 加载通用资源 cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/default/default-textures.plist"); -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_1.plist"); -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_2.plist"); -- 创建一个淡入淡出的蒙版 -- self.fadeUI = loadUI("res/ui/ui_tongyong/t_fadein_fadeout.ui") -- self.mainScene:addChild(self.fadeUI) -- self.fadeUI:setLocalZOrder(UIZOrder.FadeOrder) -- self.fadeUI:setVisible(false) if director:getRunningScene() then director:replaceScene(self.mainScene); else director:runWithScene(self.mainScene); end -- 设置统一的音源方位 cc.AudioListener:getInstance():setOrientation(cc.vec3(0,0,-1) , cc.vec3(0,1,0)); -- 开启光效缓存机制 startEffectCache(); -- 创建等待窗口管理器 self.waitDialogManager = require("luaScript.WaitDialogManager"):new() -- 全局事件侦听 startGlobalEvent() -- 使用节点来缓存数据 -- local privateNode = cc.Node:create("privateNode"); -- privateNode:setVisible(false); -- local privateNode1 = cc.Node:create("privateNode1"); -- privateNode1:setUserObject(cc.TextureCache:getInstance():addImage("res/ui/zy_tongyong/t_tongyong_1.png")); -- privateNode:addChild(privateNode1); -- local privateNode2 = cc.Node:create("privateNode2"); -- privateNode2:setUserObject(cc.TextureCache:getInstance():addImage("res/ui/zy_tongyong/t_tongyong_2.png")); -- privateNode:addChild(privateNode2); -- self.mainScene:addChild(privateNode); -- 加载表情 -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/default/emoji.plist"); local winSize = director:getOpenGLView():getFrameSize(); -- 掉落公告的根节点 self.dropNoticeUI = loadUI("res/ui/ui_tongyong/droplayout.ui") self.dropNoticeUI:setPosition(cc.p(resolutionSize.width * 0.5, resolutionSize.height * 0.6)) self.systemLayout:addChild(self.dropNoticeUI , UIZOrder.DropNotify); -- 省电模式 local function onSafePower(key , value) if value then cc.Director:getInstance():setAnimationInterval(1.0 / 20); else cc.Director:getInstance():setAnimationInterval(1.0 / 60); end end if not isReviewWithMiniGame() then app.systemSetting.info:bind("safePower" , onSafePower); end -- 加载所有光效脚本 -- 太慢了! --preCacheEffect(); -- cc.Director:getInstance():getTextureCache():loadTextureSize("res/textureSize.bin"); -- 加载AnimationClips -- cc.AnimationClipFileCache:getInstance():loadCacheFile("res/animationClips.bin"); end end -- 删除滚动消息 function GameApp:destroyScrollNotice() if not tolua.isnull(self.scrollNoticeUI) then self.scrollNoticeUI:removeFromParent(); end self.scrollNoticeUI = nil; end -- 显示滚动消息 function GameApp:showScrollNotice(message) self:destroyScrollNotice(); local ui = loadUI("res/ui/ui_tongyong/t_gungdong_xiaoxi.ui"); self.baseLayout:addChild(ui , UIZOrder.SystemNotify); self.scrollNoticeUI = ui; local htmlCtrl = ui.Items.HtmlCtrl_3; htmlCtrl:setPositionType(0); htmlCtrl:setText('' .. message .. ''); local winSize = cc.Director:getInstance():getWinSize(); local size = htmlCtrl:getSize(); local speed = 100; htmlCtrl:runActions(cc.MoveTo:create((size.width + winSize.width) / speed , cc.p(-size.width , htmlCtrl:getPositionY())) , function()self:destroyScrollNotice();end); end -- 从屏幕上方掉落一个消息 --[[ tTexts = { {color = cc.c3b(1,1,1), text = ""}, {color = cc.c3b(1,1,1), text = ""}, {color = cc.c3b(1,1,1), text = ""}, } --]] function GameApp:dropNotice(tTexts) if not self.dropNoticeUI then return end local layout = self.dropNoticeUI.Items.Layout_1 local delayTime = 3--gps提示退出房间后还会停留一段时间,因此把这里的时间由6秒改为3秒 local ui = loadUI("res/ui/ui_tongyong/droptips.ui"); local uiTemplate = ui.Items.Text uiTemplate:setVisible(false); for k,v in ipairs(tTexts) do local uiText = uiTemplate:getCopied() if uiText then uiText:setText(v.text or "") uiText:setColor(v.color or cc.c3b(255,255,255)) ui.Items.Layout_1:addChild(uiText) end end ui.Items.Layout_1:requestDoLayout() ui.Items.Layout_1:doLayout(); layout:addChild(ui); layout:requestDoLayout() layout:doLayout(); ui:runDelay(delayTime, function() if not tolua.isnull(ui) then ui:removeFromParent(); end end) end -- 滚动公告,播放完成后自动删除 function GameApp:showScrollTips(txt, pos, speed) local winSize = cc.Director:getInstance():getWinSize(); pos = pos or cc.p(winSize.width * 0.5, winSize.height * 0.75); txt = txt or "unknow"; speed = speed or 80; local ui = loadUI("res/ui/ui_tongyong/ui_dating_scroll_tips.ui"); ui:setAnchorPoint(cc.p(0.5, 0.5)); ui:setPosition(pos) --self.currentView:addChild(ui); self.baseLayout:addChild(ui , UIZOrder.DropNotify); local nodeLayout = ui.Items.Layout_Text; local szLayout = nodeLayout:getContentSize(); local nodeText = ui.Items.Text; nodeText:setText(txt); nodeText:setAnchorPoint(cc.p(0,0.5)); nodeText:setPosition(cc.p(szLayout.width, szLayout.height / 2)); local szText = nodeText:getSize(); local actionMove = cc.MoveTo:create((szText.width + szLayout.width) / speed , cc.p(-szText.width , szLayout.height / 2)); local actionEnd = cc.CallFunc:create(function() ui:removeFromParent(); end) nodeText:runAction(createActionArray(actionMove, actionEnd)); end -- 加载ui文件到内存 function GameApp:loadUIFiles(uiFiles) if type(uiFiles) ~= "table" then return end for k,v in pairs(uiFiles) do loadUI(v); end end -- 显示刘海 function GameApp:showLiuHaiPing() local modelDialog = loadUI("res/ui/ui_tongyong/ui_tongyong_liuhai.ui"); --获取普通主视图层size --local mainsize = self.suitLayout:getContentSize() --modelDialog:setSize(mainsize); --self.suitLayout:setSize(mainsize) self.suitLayout:addChild(modelDialog) end return GameApp;