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

ToggleButton

bravcmbravcm Member
edited March 2013 in Code snippets
Hi,

here is my ToggleButton class, it might be useful for all Gideros users too. Any suggestions/improvements welcome!

Best Regards
--[[
ToggleButton v1.0
 
v1.0 - 19.3.2013
Initial release
 
Free to modify and use!
Matjaž Bravc
 
Usage:
local btnSoundSwitch = ToggleButton.new(Bitmap.new(Texture.new("gfx/btn/sound_on.png", true)), Bitmap.new(Texture.new("gfx/btn/sound_off.png", true)))
 
]]
 
ToggleButton = Core.class(Sprite)
 
function ToggleButton:init(upState, downState)
	self.pressed = false
	self.focus = false
	self.upState = upState
	self.downState = downState
	self:updateVisualState(self.pressed)
 
	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 ToggleButton:onExitEnd()
	self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
	self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self)
 
	self:removeEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
	self:removeEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
	self:removeEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
	self:removeEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
 
	collectgarbage()
end
 
function ToggleButton:onMouseDown(event)
	if self:hitTestPoint(event.x, event.y) then
		self.focus = true
		event:stopPropagation()
	end
end
 
function ToggleButton: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 ToggleButton:onMouseUp(event)
	if self.focus then
		self.focus = false;
		self:updateVisualState(not self.pressed)
		self:dispatchEvent(Event.new("click"))
		event:stopPropagation()
	end
end
 
-- if button is on focus, stop propagation of touch events
function ToggleButton:onTouchesBegin(event)
	if self.focus then
		event:stopPropagation()
	end
end
 
-- if button is on focus, stop propagation of touch events
function ToggleButton:onTouchesMove(event)
	if self.focus then
		event:stopPropagation()
	end
end
 
-- if button is on focus, stop propagation of touch events
function ToggleButton:onTouchesEnd(event)
	if self.focus then
		event:stopPropagation()
	end
end
 
-- if touches are cancelled, reset the state of the button
function ToggleButton: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 ToggleButton:updateVisualState(state)
	self.pressed = state
 
	-- Modification to allow single state buttons (checkboxes etc)
	if not self.downState then
		if not self:contains(self.upState) then
			self:addChild(self.upState) 
		end
		return
	end
 
	if self.pressed then
		if self:contains(self.upState) then
			self:removeChild(self.upState)
		end
		if not self:contains(self.downState) then
			self:addChild(self.downState)
		end
	else
		if self:contains(self.downState) then
			self:removeChild(self.downState)
		end
		if not self:contains(self.upState) then
			self:addChild(self.upState)
		end
	end
end
 
function ToggleButton:isPressed() 
	return self.pressed 
end
Tagged:
+1 -1 (+2 / -0 )Share on Facebook

Comments

Sign In or Register to comment.