一个强大的UI node 抽象
基于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 抽象的更多相关文章
- 扪心自问,强大的UI框架,给我们带来了什么?(作者因此写了一个GuiLite)
MFC, QT, MINIGUI, ANDROID, IOS,如果这些平台,你都使用过,在下真心佩服,也相信你对这篇文章最具有发言权,真心期待你的回复和建议. 这些著名的UI说出来都让人如雷贯耳,如果 ...
- Koa – 更加强大的下一代 Node.js Web 框架
Koa 是 Express 的开发团队设计的下一代 Web 框架,其目的是为 Web 应用程序提供更小,更具表现力,更坚实的基础.Koa 没有核捆绑任何中间件,并提供了一套优雅的方法,使服务器端开 ...
- javascript编写一个简单的编译器(理解抽象语法树AST)
javascript编写一个简单的编译器(理解抽象语法树AST) 编译器 是一种接收一段代码,然后把它转成一些其他一种机制.我们现在来做一个在一张纸上画出一条线,那么我们画出一条线需要定义的条件如下: ...
- 建立一个基本的UI
本章让你熟悉Xcode来写应用程序.你会熟悉Xcode项目的结构,并学习如何在基本项目组件中导航.通过整个课程中,您将开始为FoodTracker应用程序制作一个简单的用户界面(UI),并在模拟器查看 ...
- Android:一个高效的UI才是一个拉风的UI(二)
趁今晚老大不在偷偷早下班,所以有时间继续跟大伙扯扯UI设计之痛,也算一个是对上篇<Android:一个高效的UI才是一个拉风的UI(一)>的完整补充吧.写得不好的话大家尽管拍砖~(来!砸死 ...
- 一个强大的jquery分页插件
点击这里查看效果 这个分页插件使用方便,引用keleyidivpager.js和keleyidivpager.css文件,然后在htm(或者php,aspx,jsp等)页面中对分页总数,参数名,前缀后 ...
- android开发之在activity中控制另一个activity的UI更新
转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...
- Dubbo 泛化调用的参数解析问题及一个强大的参数解析工具 PojoUtils
排查了3个多小时,因为一个简单的错误,发现一个强大的参数解析工具,记录一下. 背景 Nodejs 通过 tether 调用 Java Dubbo 服务.请求类的某个参数对象 EsCondition 有 ...
随机推荐
- codeforces803D. Magazine Ad
D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...
- 毕业答辩的PPT攻略
关于内容: 1.一般概括性内容:课题标题.答辩人.课题执行时间.课题指导教师.课题的归属.致谢等. 2.课题研究内容:研究目的.方案设计(流程图).运行过程.研究结果.创新性.应用价值.有关课题延续 ...
- vue+koa+mysql简易demo
功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...
- 转载:Apache commons开源工具简介
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...
- Windows下使用批处理实现启动关闭mysql_DOS/BAT
cls @echo off :设置窗口字体颜色 color 0a :设置窗口标题 TITLE MySQL管理程序 by ThinkVenus call :checkAdmin goto menu :菜 ...
- Selenium tutorial/overview
copy from: http://www.jroller.com/selenium/ Selenium tutorial/overview 1. Selenium Introduction 2. S ...
- jQuery,月历,左右点击事件实现月份的改变
html页面: <div class="recordbriefing-title-top-body"> <span class="record-left ...
- layer close 关闭层IE9-浏览器崩溃问题解决
针对ayer弹出层在IE上关闭导致浏览器崩溃的问题: 导致原因: 查看src源码,layer.close关闭总方法中有这么一行: layer.close = function(index){ ] + ...
- redis基础配置
特点 redis是高性能的key-value的数据库,其支持数据的持久化,可以将内存中的数据保存在磁盘中,重启时再次加载使用:具有丰富的数据类型支持,例如list, set, zset, hash等: ...
- [BZOJ2555]SubString LCT+后缀自动机
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3253 Solved: 975[Submit][Status][Di ...