基于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

一个强大的UI node 抽象的更多相关文章

  1. 扪心自问,强大的UI框架,给我们带来了什么?(作者因此写了一个GuiLite)

    MFC, QT, MINIGUI, ANDROID, IOS,如果这些平台,你都使用过,在下真心佩服,也相信你对这篇文章最具有发言权,真心期待你的回复和建议. 这些著名的UI说出来都让人如雷贯耳,如果 ...

  2. Koa – 更加强大的下一代 Node.js Web 框架

    Koa 是 Express 的开发团队设计的下一代 Web 框架,其目的是为 Web 应用程序提供更小,更具表现力,更坚实的基础.Koa 没有核捆绑任何中间件,并提供了一​​套优雅的方法,使服务器端开 ...

  3. javascript编写一个简单的编译器(理解抽象语法树AST)

    javascript编写一个简单的编译器(理解抽象语法树AST) 编译器 是一种接收一段代码,然后把它转成一些其他一种机制.我们现在来做一个在一张纸上画出一条线,那么我们画出一条线需要定义的条件如下: ...

  4. 建立一个基本的UI

    本章让你熟悉Xcode来写应用程序.你会熟悉Xcode项目的结构,并学习如何在基本项目组件中导航.通过整个课程中,您将开始为FoodTracker应用程序制作一个简单的用户界面(UI),并在模拟器查看 ...

  5. Android:一个高效的UI才是一个拉风的UI(二)

    趁今晚老大不在偷偷早下班,所以有时间继续跟大伙扯扯UI设计之痛,也算一个是对上篇<Android:一个高效的UI才是一个拉风的UI(一)>的完整补充吧.写得不好的话大家尽管拍砖~(来!砸死 ...

  6. 一个强大的jquery分页插件

    点击这里查看效果 这个分页插件使用方便,引用keleyidivpager.js和keleyidivpager.css文件,然后在htm(或者php,aspx,jsp等)页面中对分页总数,参数名,前缀后 ...

  7. android开发之在activity中控制另一个activity的UI更新

    转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...

  8. 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选

    毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...

  9. Dubbo 泛化调用的参数解析问题及一个强大的参数解析工具 PojoUtils

    排查了3个多小时,因为一个简单的错误,发现一个强大的参数解析工具,记录一下. 背景 Nodejs 通过 tether 调用 Java Dubbo 服务.请求类的某个参数对象 EsCondition 有 ...

随机推荐

  1. 怎么给word加底纹

  2. php模式-数据映射模式

    概念:简言之,数据映射模式就是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 深入理解:数据映射,是在持久化数据存储层(一般是关系型数据库)和驻于内存的数据表现层之间进行双向数据 ...

  3. JavaScript中Array(数组)的属性和方法(转)

    数组有四种定义的方式 使用构造函数:var a = new Array();var b = new Array(8);var c = new Array("first", &quo ...

  4. Spring 中 AbstractExcelView 支持根据模板生成Excel文件. 通过设置 view 的 URL 属性指定模板的路径

     注意:1. 模板需放在 WEB-INF 目录下2. 指定模板路径时不需要添加扩展名, Spring将自动添加 .xls 到URL 属性中.3. 在指定URL前需先设置 view 的 Applicat ...

  5. Drupal7导入语言包

    下载语言包 然后把包存放在profiles\standard\translations目录下,安装. 如果出现如下错误 1.错误有MySql数据库默认引擎innoDB引起,只要mysql\bin\my ...

  6. 地震(quake)

    地震 题目描述 一场地震毁了 Farmer John 的整个农场.他是个有恒心的人,决定重建农场.在重建了所有 n(1<=n<=400)块田野后,他意识到还得修路将它们连起来.完工后,任两 ...

  7. hdu 3374 String Problem (kmp+最大最小表示法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...

  8. Django【进阶】权限管理

    一.权限 RBAC:role basic access control   1.什么是权限: 一个权限就是一个url,不同个权限对应不同的url,拥有权限即可以访问这个url.   2.权限划分: 如 ...

  9. ARM-Linux (临时,正式) 建立页表的比较【转】

    转自:http://blog.csdn.net/edwardlulinux/article/details/38967521 版权声明:本文为博主原创文章,未经博主允许不得转载.   很久没有写博客了 ...

  10. PoI导出列表优化

    针对写了头信息之后,使用for循环遍历会导致数据丢失问题的优化,使用迭代器替代for循环,具体如下: public static boolean ExportDeptLeaderFileToLocal ...