基于cocos2d -x的一个强大的 界面对象的基类

 ---@type uinode ui 对象的抽象
--@usage
-- 界面打开的执行流程
-- 带*的是可选重写的函数,不带*的为必须实现的
-- [定义一个对象]
-- test = {}
-- --------------------------------------------
-- ---配置函数
-- [返回CCBI的名字]
-- function test:getBindUINodeStr() end
-- [*显示模式,不实现是直接打开。不会居中]
-- function test:getShowModel() end
-- --------------------------------------------
-- ---流程函数
-- [*打开界面前要执行网络操作]
-- function test:getCmdStr() end
-- [*打开界面前要执行网络操作需要的数据]
-- function test:getCmdData() end
-- 网络命令成功后会进行的操作
-- function test:onRemoteAck(networkArgs) end
-- [*初始化界面上的相关信息]
-- function test:on_initUI(node,openArgs,networkArgs) end
-- [*关闭界面前要进行网络通信]
-- function test:before_close(ackfun) end
-- -----------------------------------------------------------------
-- PS:当有*函数的出现,只要*函数执行成功,才会调用之后的函数
--
-- usage: local obj = test.getInstance()
-- obj:open(args) --args 为要传送到on_initUI中的args数据
uinode ={} require("base")
require("userGuidelines") ---===============================================================
---IUiNode interface
IUiNode = {}
---IUiNode interface
createInterface(IUiNode,"IUiNode",function(self,openArgs) end)
---=============================================================== ---@field 是否为中心显示
uinode.isShowInCenterFlg = false
---@field 当前绑定的节点
--@return #cc.Node cc.node节点
uinode.node = nil
uinode ,supper= inheritance(uinode,object) ------------------------------------------------------------------------
--显示模式定义
uinode.showModel = {
NOTADDTO_CONTENT = -,
NONE = ,
NORMAL = ,
CENTER=,
DELAYED_SHUTDOWN=,
nil
} ----------------------------------------------------------
---资源管理器
local unloadResource = {} function uinode:unloadCustomResources()
local res = self:__getCustomResources()
if res ~= nil and #res > then
xse.base.unloadTextures(res)
end
end function uinode:__getCustomResources()
if self.getCustomResources == nil then return {} end
local resources = self:getCustomResources()
return resources or {}
end function uinode:starLoadResource()
if self.mask == nil then
self.mask = xse.base.loadCCBI("waitting.ccbi",false)
xse.base.Scene:addChild(self.mask,)
end
end function uinode:endLoadResource()
xse.base.Scene:removeChild(self.mask)
self.mask = nil
end ---加载资源
function uinode:loadResource(onloadComplate)
if uinode.__getCustomResources ~= nil then
local res = self:__getCustomResources()
if res == nil or #res == then onloadComplate()
else
local __id = nil
local __index =
local __timerCounter =
self:starLoadResource()
local loaded = false
__id = xse.base.timer.tick.add(function()
if loaded ~= true then loaded = true
for var=, #res do
cc.Director:getInstance():getTextureCache():addImageAsync(res[var],
function()
log("set load command :"..res[var])
__index = __index +
end)
end
end __timerCounter = __timerCounter +
if __index >= #res and __timerCounter > then
onloadComplate()
self:endLoadResource()
xse.base.timer.tick.remove(__id)
end
end)
end
elseif onloadComplate~= nil then
onloadComplate()
end
end -------------------------------------------------------------------------
--帮助函数私有成员函数 ---加载CCBI文件
--@param nodestr string 要加载的资源名字,不包含后缀名
--@return #cc.Node 一个node节点
function uinode:loadCCBI(nodestr)
assert(nodestr~=nil and type(nodestr)=="string","param nodestr must a string type")
-- local node = xse.base.loadScene(nodestr,nil,self:getShowModel()== uinode.showModel.CENTER)
local node = xse.base.loadScene(nodestr,nil)
return node
end ---为当前对象绑定一个ccbi
--@param nodestr string 要加载的资源名字,不包含后缀名
--@return cc.Node 当前对象绑定的节点
function uinode:bindCCBI(nodestr)
if self.node~=nil then
local parent = self.node:getParent()
if parent then parent:removeChild(parent) end
self.node = nil
end if self.node == nil then
self:registeUICallBack()
self.node = self:loadCCBI(nodestr)
self.node.__bindToInstance = self
end
return self.node
end ---绑定脚本到ccbi上
function uinode:bindScriptToNode(node)
node.__bindToInstance = self
end ---卸载ccbi文件
--@param nodestr string 要卸载的资源名字,不包含后缀名
function uinode:unloadCCBI(nodestr)
assert("false","暂时不支持的方法")
end ---重新加载CCBI文件
--@param nodestr string 要加载的资源名字,不包含后缀名
function uinode:reloadCCBI()
local parent = self.node:getParent()
if parent ~= nil then parent:removeChild(self.node)end
self:bindCCBI(self:getBindUINodeStr())
end ---显示界面函数
function uinode:show(onshowComplate)
assert(onshowComplate,"callbakc is nil value")
switch(self:getShowModel(),{ [uinode.showModel.NOTADDTO_CONTENT] = function()
---不设置为添加到窗口中,就直接调用aftershow
self:afterShow()
end, [uinode.showModel.NORMAL] = function()
---添加到界面上
xse.base.show(nil,self.node)
---显示动画
xse.base.animal.show(
self.node,
nil,
function() onshowComplate() ; self:afterShow() end
)
end, [uinode.showModel.CENTER] = function()
onshowComplate();
xse.base.animal.showOnCenter(self.node,nil,
function() self:close(); end,
self,
function ()
self:afterShow();
end)
end, [uinode.showModel.DELAYED_SHUTDOWN] = function()
onshowComplate();
xse.base.show(nil,self.node);
end, [uinode.showModel.NONE] = function()
xse.base.animal.show(self.node,xse.base.Scene,
function()
onshowComplate();
self:afterShow();
end, false
) end, ["default"] = function()
xse.base.animal.show(self.node,
nil,
function() onshowComplate();self:afterShow() end)
end,
}) self:endLoadResource()
end function uinode:afterShow() end function uinode:processClose()
self.moreNode = nil
switch(self:getShowModel(),{
[uinode.showModel.NORMAL] = function()
xse.base.hide(nil,self.node,function()
self:after_close()
end);
self.node = nil;
end,
[uinode.showModel.CENTER] = function()
xse.base.hide(nil,self.node,function()
self:after_close()
end);
self.node = nil;
end,
[uinode.showModel.DELAYED_SHUTDOWN] = function()
local action = cc.Sequence:create(cc.DelayTime:create(),cc.CallFunc:create(function()
xse.base.hide(nil,self.node,function() self:after_close() end);
self.node = nil;
end))
self.node:runAction(action)
end,
["default"] = function()
xse.base.hide(nil,self.node,function()
self:after_close()
end);
self.node = nil;
end,
}) end function uinode:after_close()
self:unloadCustomResources()
end ---关闭界面函数
function uinode:close(sender)
log("#close ".. tostring(self:getHashCode()))
---在关闭前的回掉
if self.before_close ~=nil and type(self.before_close) == "function" then
self:before_close(function(rd,sn,sd)
if type(sender) == "function" then sender()end
end)
self:processClose()
else
if type(sender) == "function" then sender() end
self:processClose()
end
self:unregisteUICallBack() --避免在关闭的时候还有操作造成无法处理的bug
end ---注册自己定义的ui回调事件
function uinode:setCustomUiCallBack()
---不做任何事情,留给子类去处理
end ---注册当前node 的回调事件
function uinode:registeUICallBack()
---自定义事件注册
self:setCustomUiCallBack()
---
local name = self:getName()
if name ~="unkonw" then
for key, var in pairs(self) do
if type(var)== "function"then
xse.base.addEventSupportA(name,key,
function(sender)
assert(sender~=nil,"sender is nil value")
local __callinstance = nil
local tempsender = sender
local obj = sender
while sender ~= nil and (type(sender)== "table" or type(sender)== "userdata") do
assert(not (__callinstance and sender.__bindToInstance),"子节点界面不应该单独绑定到uinode上")
if sender.__bindToInstance~= nil then
__callinstance = sender.__bindToInstance
break;
end
sender = sender:getParent()
end if __callinstance~=nil then
local fun = __callinstance[key]
fun(__callinstance,tempsender)
return
end assert(false,"Have not found the bound script logic in all parent nodes,")
end)
end
end
end
end ---注册当前node ui 的回调事件
function uinode:unregisteUICallBack()
local name = self:getName()
if name ~="unkonw" then
for key, var in pairs(self) do
if type(var)== "function"then
---取消事件注册
xse.base.addEventSupportA(name,key,nil)
end
end
end
end ---------------------------------------------------------------------------
--业务接口 ---禁止重写这个函数®
function uinode:open(openArgs)
if self.UserGuidelineSupportConfig then
self.userGuideLineConfigKey = xse.base.random()
userGuidelineManager:addUserGuidelineSupportA(self,self.userGuideLineConfigKey)
end self:loadResource(function()
local ccbstr = self:getBindUINodeStr()
assert(ccbstr,"self:getBindUINodeStr()返回的数据不合法")
self:bindCCBI(ccbstr)---绑定界面 local function init(self)
self.__openArgs = openArgs
---发送网络命令
if self.getCmdStr and type(self.getCmdStr) == "function" then
local cmdstr = self:getCmdStr()
local dt = self:getCmdData()
---发送网络通信
local remoteTask,protocal,command = require "remoteTask",require "protocal",require "command"
assert(cmdstr,"Rewrite getCmdStr function, but the data returned is empty.")
protocal:adddt(command[cmdstr],dt)
local sn = remoteTask.add(protocal,communication.type.tcp,function(rd,sn,sd,self)
if self.onRemoteAck then self:onRemoteAck(rd) end
self:show(function()
self:on_initUI(self.node,self.__openArgs,rd)
end)
end,nil,self) else
self:on_initUI(self.node,self.__openArgs,rd)
self:show(function()
--self:on_iniself:on_initUI(self.node,self.__openArgs,rd)tUI(self.node,self.__openArgs,rd)
end)
end
end ---DEBUG模式下调用初始化一些必要的基础数据
debugCallFun(function()
require("tcpExt")
---刷新全部依赖的数据
tcpTaskQueue({
{cmd = "WORLD_INIT",dt = {},callback = function(rd,sn,sd) init(self) end,errfun = nil},
{cmd = "COMPANY_INIT",dt = {},callback = function(rd,sn,sd) end,errfun = nil},
})
end,nil) releaseCallFun(init,self)
end)
end ---绑定界面函数 [可重写]
-- @return #string 要绑定的界面,不包含后缀名
function uinode:getBindUINodeStr()
assert(false,"必须实现要绑定的界面")
return nil
end ---显示模式 [可选函数]
--@return #uinode.showModel 现实的模式
--PS:[显示模式,不实现是直接打开。不会居中]
function uinode:getShowModel()
return uinode.showModel.NORMAL
end ---打开界面前要执行网络操作[可选函数]
--@return cmdstr string
--function uinode:getCmdStr()
-- return nil
--end
--uinode.getCmdStr = nil ---发送网络命令需要的数据
--@return table #table,加载界面前发送网络命令时需要的数据
function uinode:getCmdData()
return {}
end -- ---初始化界面上的相关信息[可选函数]
--@param node cc.Node 界面已绑定好的节点
--@param openArgs unknow open方法穿过来的数据
--@param networkArgs unknow 没有使用网络函数为nil,使用了网络函数:就为网络返回的数据
--@return nil
function uinode:on_initUI(node,openArgs,networkArgs)
-- self:on_initUI(node, opendArgs, networkArgs);
end ---关闭界面前要进行网络通信 [可选函数]
--@param networkACKCallBack function(rd,sn,sd) 网络成功后的回掉函数
--@return nil
function uinode:before_close(networkACKCallBack)
return false
end uinode.before_close = nil return uinode

使用DEMO

local story = {}

require("depend")
require("tableViewExt")
require("uinode")
local tabView = require "TabView"
local text = require("richtext") inheritanceA(story,object,"object")
inheritanceA(story,uinode,"uinode")
--uinode abstract function
implementInterface(story,"getShowModel",function(self) return uinode.showModel.NONE end )
implementInterface(story,"getBindUINodeStr",function(self) return "start01" end )
implementInterface(story,"getName",function(self) return "message_layer" end )
implementInterface(story,"on_initUI",function(self,node,openArgs,networkArgs) self:init(node,openArgs,networkArgs) end ) function story:init()
cc.SimpleAudioEngine:getInstance():stopMusic()
cc.SimpleAudioEngine:getInstance():playMusic("storyBackgroudn.mp3",true) local daytime = cc.DelayTime:create()
--TODO::增加语言
self.fontConfig = {{fontSize = ,str="23世界末,第三次世界大战\"能源战\"爆发。\"联军\"为了维持庞大的军费开支,开始将阿波罗-188号飞船用于商用。一些富商为了躲避战火纷纷变卖资产换取船票前往“开普勒”,而您,曾经闻名于世的一代商业传奇人物,也随着这波人流,开启了一段新的征程。",color = xse.color.GREEN}}
local funAction = cc.CallFunc:create(function()
self.text = richtext.playMessage(self,self.node, self.fontConfig,function()
local daytime2 = cc.DelayTime:create()
local nextaction = cc.CallFunc:create(function()
xse.base.setCCBottonOnClick(self.node:getChildByTag(),function() self:swithNode() end,self)
--self:swithNode()
self.node:getChildByTag():setVisible(true)
end)
self.node:runAction(cc.Sequence:create(daytime2,nextaction))
end)
end) self.node:runAction(cc.Sequence:create(daytime,funAction))
self.node:getChildByTag():setVisible(false)
-- self.text=richtext.playMessage(self,self.node, self.fontConfig)
-- xse.base.setCCBottonOnClick(self.node:getChildByTag(),function() self:swithNode() end,self)
end function story:swithNode()
local layer = self.node:getParent()
self.text:getParent():removeChild(self.text)
layer:removeChild(self.node)
self.node = xse.base.loadCCBI("start02.ccbi")
xse.base.show(layer,self.node) local daytime2 = cc.DelayTime:create()
local nextaction = cc.CallFunc:create(function()
local instance = require("citadel")
instance:new():open()
end)
self.node:runAction(cc.Sequence:create(daytime2,nextaction))
end return story
05-11 15:20