I am implementing all the lua code for the tests.
I try to package the c code but it needs to many extensions so I coded it in lua.
Speed is not really important.
Andre
usage: for the SchedulerTimeScale test (first test in SchedulerTest)
local m_pSliderCtl
m_pSliderCtl = sliderCtl(-3, 3, 0, function() CCDirector:sharedDirector():getScheduler():setTimeScale(m_pSliderCtl.value) end)
m_pSliderCtl:setPosition(ccp(s.width / 2.0, s.height / 3.0))
layer:addChild(m_pSliderCtl.node)
note: you have to use m_pSliderCtl.node instead of m_pSliderCtl, since m_pSliderCtl is a table not truly a CCControlSlider.
local CCControlSlider = {
minimum = 0;
maximum = 1;
value = 0.5;
onNewValue = function () end -- override
}
function CCControlSlider:onTouch(eventType, x, y)
local where = CCPointMake(x,y)
local nodeBB = self.node:boundingBox()
local thumbBB = self.thumb:boundingBox()
thumbBB.origin = ccpAdd(nodeBB.origin, thumbBB.origin)
local isIn = thumbBB:containsPoint(where)
if eventType == CCTOUCHBEGAN then return isIn
elseif eventType == CCTOUCHMOVED then
self:setValue((self.maximum - self.minimum) * (x-nodeBB.origin.x)/nodeBB.size.width + self.minimum)
return true
elseif eventType == CCTOUCHENDED then return true
end
end
function CCControlSlider:create(bgFile, progressFile, thumbFile)
local o = {}
o.bg = CCSprite:create(bgFile)
o.progress = CCSprite:create(progressFile)
o.thumb = CCSprite:create(thumbFile)
setmetatable(o, self)
self.__index = self
o.node = CCLayer:create()
o.node:ignoreAnchorPointForPosition(false)
o.node:setContentSize(o.bg:getContentSize())
o.node:addChild(o.bg, -2)
o.node:addChild(o.progress, -1)
o.node:addChild(o.thumb, 0)
local s = o.node:getContentSize()
o.bg:setAnchorPoint(ccp(0.5, 0.5));
o.bg:setPosition(ccp(s.width/2, s.height/2))
o.progress:setAnchorPoint(ccp(0.0, 0.5));
o.progress:setPosition(ccp(0, s.height/2))
o.thumb:setPosition(ccp(s.width/2, s.height/2))
o.node:setTouchEnabled( true );
o.node:registerScriptTouchHandler(function (eventType, x, y) return o:onTouch(eventType, x, y) end)
return o
end
function CCControlSlider:layout(pt)
if self.minimum > self.maximum then self.minimum = self.maximum - 0.1 end -- sanity check
if self.value < self.minimum then self.value = self.minimum end
if self.value > self.maximum then self.value = self.maximum end
local percent = (self.value - self.minimum)/(self.maximum - self.minimum)
local pos = self.thumb:getPositionLua()
pos.x = percent * self.bg:getContentSize().width
self.thumb:setPosition(pos)
local textureRect = self.progress:getTextureRect();
textureRect = CCRectMake(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height)
self.progress:setTextureRect(textureRect, self.progress:isTextureRectRotated(), textureRect.size)
end
function CCControlSlider:setPosition(pt) self.node:setPosition(pt) end
function CCControlSlider:setMinimumValue(v) self.minimum = v end
function CCControlSlider:setMaximumValue(v) self.maximum = v end
function CCControlSlider:setValue(v)
self.value = v
self:layout()
self:onNewValue()
end
local function sliderCtl(minimum, maximum, initial, onNewValue)
local slider = CCControlSlider:create("extensions/sliderTrack2.png","extensions/sliderProgress2.png" ,"extensions/sliderThumb.png")
slider:setMinimumValue(minimum)
slider:setMaximumValue(maximum)
slider:setValue(initial)
slider.onNewValue = onNewValue
return slider;
end