Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Autorepeat Button — Gideros Forum

Autorepeat Button

bravcmbravcm Member
edited February 2014 in General questions
Hi to all,

does anybody already made "autorepeat button" which repeats click event when it is pressed?

Best regards

Comments

  • OZAppsOZApps Guru
    Accepted Answer
    @bravcm,
    you can use the dispatch event
     function autorepeat(theEventObj, oldX, oldY)
       local evtD = theEventObj or Core.class(EventDispatcher)
       local evt = Event.new(Event.MOUSE_DOWN)
       evt.x = oldX
       evt.y = oldY
     
       evtD:dispatchEvent(evt)
    end
    This code is for illustration, and works on the assumption that you will set the event handler and handle all Event.MOUSE_DOWN events. So you call the autorepeat as

    autorepeat( theObj, xPos, yPos)

    where theObj is the object that is handling the event and the position xPos, yPos is taken as the last position to give the impression that the click/tap is at the same location as the last click/tap.

    If you were after something else, please re-iterate what you were after.

    Cheers,
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • bravcmbravcm Member
    edited February 2014
    Thanks, @ozapps, here is modified button class, any improvements welcome!
    --[[
    A generic button class
     
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile 
     
    P.S. Added autorepeat option
    ]]
     
    Button = Core.class(Sprite)
     
    function Button:init(upState, downState, autorepeat)
    	self.upState = upState
    	self.downState = downState
    	self.autorepeat = autorepeat
    	self.timer = Timer.new(50, 6)
     
    	self.focus = false
     
    	self:updateVisualState(false)
     
    	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
     
    	self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    	self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
    	self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
    	self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
    end
     
    function Button:onMouseDown(event)
    	if self:isVisible() == false then return end
     
    	if self:hitTestPoint(event.x, event.y) then
    		self.focus = true
    		self:updateVisualState(true)
     
    		if not self.timer:hasEventListener(Event.TIMER) and self.autorepeat then
    			self.timer:setDelay(300)
    			self.timer:setRepeatCount(0)
    			self.timer:addEventListener(Event.TIMER,self.onPressing, self)
    		end
    		if self.autorepeat then
    			self.timer:start()
    		end
     
    		event:stopPropagation()
    	end
    end
     
    function Button:onPressing(event)
    	if self.timer:getCurrentCount() == 3 then
    		self.timer:setDelay(250)
    	end
    	if self.timer:getCurrentCount() == 7 then
    		self.timer:setDelay(200)
    	end
    	if self.timer:getCurrentCount() == 12 then
    		self.timer:setDelay(150)
    	end
    	if self.timer:getCurrentCount() == 20 then
    		self.timer:setDelay(100)
    	end
    	if self.timer:getCurrentCount() == 60 then
    		self.timer:setDelay(30)
    	end
    	self:dispatchEvent(Event.new("click"))
    end
     
    function Button:onMouseMove(event)
    	if self.focus then
    		if not self:hitTestPoint(event.x, event.y) then
    			self.focus = false;
    			self:updateVisualState(false)
    		end
    		event:stopPropagation()
    	end
    end
     
    function Button:onMouseUp(event)
    	if self.autorepeat then
    		self.timer:stop()
    		self.timer:removeEventListener(Event.TIMER,self.onPressing, self)
    	end
     
    	if self.focus then
    		self.focus = false;
    		self:updateVisualState(false)
    		self:dispatchEvent(Event.new("click"))
    		event:stopPropagation()
    	end
    end
     
    -- if button is on focus, stop propagation of touch events
    function Button:onTouchesBegin(event)
    	if self.focus then
    		event:stopPropagation()
    	end
    end
     
    -- if button is on focus, stop propagation of touch events
    function Button:onTouchesMove(event)
    	if self.focus then
    		event:stopPropagation()
    	end
    end
     
    -- if button is on focus, stop propagation of touch events
    function Button:onTouchesEnd(event)
    	if self.focus then
    		event:stopPropagation()
    	end
    end
     
    -- if touches are cancelled, reset the state of the button
    function Button:onTouchesCancel(event)
    	if self.focus then
    		self.focus = false;
    		self:updateVisualState(false)
    		event:stopPropagation()
    	end
    end
     
    -- if state is true show downState else show upState
    function Button:updateVisualState(state)
    	if state then
    		if self:contains(self.upState) then
    			self:removeChild(self.upState)
    		end
     
    		if self.downState ~= nil and not self:contains(self.downState) then
    			self:addChild(self.downState)
    		end
    	else
    		if self.downState ~= nil and self:contains(self.downState) then
    			self:removeChild(self.downState)
    		end
     
    		if not self:contains(self.upState) then
    			self:addChild(self.upState)
    		end
    	end
    end
Sign In or Register to comment.