lua写的UI复用模块代码

--[[
declare:复用模块
author:shoulder
--]]


require "code/base/BaseObject"
require "code/tool/UIFactory"


-------CoupleBtn 类start--------------------------------
--用于表示有两种状态激活和没有激活,而表现各异的不同的按钮
CoupleBtn= {}
--继承自BaseObject
setmetatable(CoupleBtn, BaseObject)
CoupleBtn.__index = CoupleBtn


function CoupleBtn:new(fbtn, rbtn, isFocus)
local self = {}
self = BaseObject:new()
setmetatable(self, CoupleBtn)


--初始化成员变量
self.focusBtn = fbtn    --选中时显示的按钮
self.releaseBtn = rbtn  --没有被选中时显示的按钮
self.coupleBtnList = nil --所在的menuBtnsList
self.isFocus = nil    --是否被选中
self.index = nil      --在list中的索引
self.isRegist = false --releaseBtn是否已经注册


--设定两个子按钮的状态
self.isFocus = isFocus
self.focusBtn:setVisible(self.isFocus)
self.releaseBtn:setVisible(not self.isFocus)


--返回对象
return self
end




function CoupleBtn:setFocus()
self.isFocus = true
self.focusBtn:setVisible(true)
self.focusBtn:setTouchEnabled(true)
self.releaseBtn:setVisible(false)
self.releaseBtn:setTouchEnabled(false)
self.coupleBtnList.focusIndex = self.index
end


function CoupleBtn:setRelease()
isFocus = false
self.focusBtn:setVisible(false)
self.focusBtn:setTouchEnabled(false)
self.releaseBtn:setVisible(true)
self.releaseBtn:setTouchEnabled(true)
end


function CoupleBtn:setPosition(x,y)
self.focusBtn:setPosition(ccp(x,y))
end


function CoupleBtn:OnReleaseBtnClick()
--空实现,待根据具体需求重写
end


function CoupleBtn:OnFocusBtnClick()
--空实现,待根据具体需求重写
end
-------coupleBtn 类end--------------------------------




-----CoupleBtnList 类start------------------------------
CoupleBtnList = {}
--继承自BaseObject
setmetatable(CoupleBtnList, BaseObject)
CoupleBtnList.__index = CoupleBtnList


function CoupleBtnList:new()
local self = {}
self = BaseObject:new()
setmetatable(self, CoupleBtnList)


--成员变量
self.coupleBtns = {}
self.focusIndex = 1
self.maxIndex = 0


return self
end


function CoupleBtnList:addCouple(btn)
self.coupleBtns[self.maxIndex+1] = btn
btn.coupleBtnList = self
btn.index = self.maxIndex +1
self.maxIndex = self.maxIndex + 1
end


function CoupleBtnList:findCoupleIndex(btn)
for i = 1,self.maxIndex do
if self.coupleBtns[i].releaseBtn == btn  then
return i
end
end
return -1
end


function CoupleBtnList:removeButton(btn)
btns[btn.index] = nil
btn.menuList = nil
end


function CoupleBtnList:registerOffBtnsClickEvent()
for i =1,self.maxIndex do
local function xxx(obj,etype)
if etype == TOUCH_EVENT_ENDED then
self.coupleBtns[self.focusIndex]:setRelease()
self.coupleBtns[i]:setFocus()
self.coupleBtns[i]:OnReleaseBtnClick()
end
end


local function yyy(obj, etype)
if etype == TOUCH_EVENT_ENDED then
self.coupleBtns[i]:OnFocusBtnClick()
end
end


if self.coupleBtns[i] ~= nil then
if self.coupleBtns[i].isRegist == false then
NodeDelegate:registerWidgetTouchListenerForLua(self.coupleBtns[i].releaseBtn,xxx)
NodeDelegate:registerWidgetTouchListenerForLua(self.coupleBtns[i].focusBtn,yyy)
self.coupleBtns[i].isRegist = true
end
end
end
end
-----CoupleBtnList 类end--------------------------------




--CloseBtn 类 start-----------------------------------
CloseBtn = {}
setmetatable(CloseBtn, BaseObject)
CloseBtn.__index = CloseBtn


function CloseBtn:new(btn, ui,wnd)
local self = {}
self = BaseObject:new()
setmetatable(self, CloseBtn)


self.btn = btn
self.ui = ui
self.wnd = wnd


self:registerClickEvent()


return self
end


function CloseBtn:registerClickEvent()
local function xxx(obj,etype)
if etype == TOUCH_EVENT_ENDED then
self:onClick()
end
end
NodeDelegate:registerWidgetTouchListenerForLua(self.btn,xxx)
end


function CloseBtn:onClick()
local uim = UIManager:getInstance()
uim:removeUI(self.ui)
UIFactory:remove(self.wnd)
end
--CloseButton 类 end ------------------------------------




-----SelectBar start-----------------------------------------------------------------------------------------
--滚动视图中的一条选择条
SelectBar = {}
setmetatable(SelectBar, BaseObject)
SelectBar.__index = SelectBar


--构造函数
function SelectBar:new(width, height)
local self = {}
self = BaseObject:new()
setmetatable(self, SelectBar)


self.layout = nil            --两个按钮放在其上,主要目的是便于控制两个按钮的显示和位置的一次设定
self.coupleBtn = nil         --两个按钮组成的按钮对
self.height = height         --选择条的高度
self.width  = width          --选择条的宽度


self.layout = Layout:create()
self.layout:setSize( CCSizeMake(self.width, self.height))
self.layout:setAnchorPoint(ccp(0,0))

----返回对象
return self
end


---可点击时,调用这个函数
function SelectBar:init()
local btnF,btnR = self:assemble()      --组装选择条,选择条一旦发生变化只要重写这个函数


self.layout:addChild(btnF)
self.layout:addChild(btnR)


----用于控制点击事件的对象
self.coupleBtn = CoupleBtn:new(btnF, btnR, false)
self.layout:setTouchEnabled(true)
end


----不可点击时,调用这个函数
function SelectBar:init2()
local bar = self:assemle()
self.layout:addChild(bar)
end


--拼接组装选择条,让使用者根据需求重写
--选择条可点,要求返回两个Button(第一个是选中时显示的Button)
--选择条不可点击,要求返回一个UI对象即可
function SelectBar:assemble()
local btnF = Button:create()
local btnR = Button:create()


return btnF,btnR
end


--设置位置
function SelectBar:setPosition(pt)
self.layout:setPosition(pt)
end


--设置取消点击时的拉伸效果
function SelectBar:cancelPressAction()
self.coupleBtn.focusBtn:setPressedActionEnabled(false)
self.coupleBtn.releaseBtn:setPressedActionEnabled(false)
end
-----SelectBar end-----------------------------------------------------------------------------------------






-----SelectBarSv start ------------------------------------------------------------------------------------
-----存放SelectBar的滚动视图类
--[[
说明1:使用这个模块,必须保证滚动视图的坐标为ccp(0,0)
说明2:多次用滚动视图显示可变内容时,要在显示前重置滚动视图的状态
说明3:显示的控件只要有两个属性:height,width
--]]
SelectBarSv = {}
setmetatable(SelectBarSv, BaseObject)
SelectBarSv.__index = SelectBarSv


--构造函数
function SelectBarSv:new(sv, selectBarList, xBarNum, isButton, xdis, ydis, xMagin, yMagin)
local self = {}
self = BaseObject:new()
setmetatable(self, SelectBarSv)


--滚动视图
self.sv = sv
--要显示的选择条列表
self.selectBarList = selectBarList 


--用于管理bar之间的关系的按钮对组
if isButton == nil then 
self.isBtn = true
end


if self.isBtn then
local coupleBtnlist = CoupleBtnList:new()
for i = 1,#self.selectBarList do
if self.selectBarList[i] ~= nil then
coupleBtnlist:addCouple(self.selectBarList[i].coupleBtn)
end
end
--注册ScrollView中所有SelectBar被点击事件
coupleBtnlist:registerOffBtnsClickEvent()
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值