ButtonToggle class

From GiderosMobile
Revision as of 04:02, 20 November 2023 by MoKaLux (talk | contribs) (Created page with "__TOC__ '''A Button you can toggle on and off''' === ButtonToggle '''Class''' === <syntaxhighlight lang="lua"> -- -- ButtonToggle v1.0 -- Free to modify and use! -- Matja...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Button you can toggle on and off

ButtonToggle Class

--[[
-- ButtonToggle v1.0
-- Free to modify and use!
-- Matjaž Bravc
v1.0 - 19.3.2013 Initial release
]]

ButtonToggle = Core.class(Sprite)

function ButtonToggle:init(upState, downState)
	self.upState = upState
	self.downState = downState

	self.pressed = false
	self.focus = false

	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 ButtonToggle:onMouseDown(event)
	if self:hitTestPoint(event.x, event.y) then
		self.focus = true
		event:stopPropagation()
	end
end

function ButtonToggle:onMouseMove(event)
	if self.focus then
		if not self:hitTestPoint(event.x, event.y) then
			self.focus = false
		end
		event:stopPropagation()
	end
end

function ButtonToggle:onMouseUp(event)
	if self.focus then
		self.focus = false
		self:updateVisualState(not self.pressed)
		self:dispatchEvent(Event.new("clicked"))
		event:stopPropagation()
	end
end

-- if button is on focus, stop propagation of touch events
function ButtonToggle:onTouchesBegin(event)
	if self.focus then
		event:stopPropagation()
	end
end

-- if button is on focus, stop propagation of touch events
function ButtonToggle:onTouchesMove(event)
	if self.focus then
		event:stopPropagation()
	end
end

-- if button is on focus, stop propagation of touch events
function ButtonToggle:onTouchesEnd(event)
	if self.focus then
		event:stopPropagation()
	end
end

-- if touches are cancelled, reset the state of the button
function ButtonToggle: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 ButtonToggle: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 ButtonToggle:setPressed(xbool)
	self:updateVisualState(xbool)
end

function ButtonToggle:isPressed()
	return self.pressed
end

ButtonToggle Demo

-- button Toggle
-- textures
local texoff = Texture.new("gfx/ui/btn_01_disabled.png")
local texon = Texture.new("gfx/ui/btn_01_up.png")
-- buttons
local btntoggle = ButtonToggle.new(Bitmap.new(texoff), Bitmap.new(texon))
local btntoggle2 = ButtonToggle.new(Bitmap.new(texoff), Bitmap.new(texon))
-- params
btntoggle:setScale(0.5, 0.5)
btntoggle:setPressed(true)
btntoggle2:setScale(0.5, 0.5)
btntoggle2:setPressed(false)
-- position
btntoggle:setPosition(16*1, 16*16)
btntoggle2:setPosition(16*6, 16*16)
-- order
stage:addChild(btntoggle)
stage:addChild(btntoggle2)
-- listeners
btntoggle:addEventListener("clicked", function()
	print("button is: ", btntoggle:isPressed())
end)
btntoggle2:addEventListener("clicked", function()
	print("button2 is: ", btntoggle2:isPressed())
end)


UI Buttons