<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Button_class</id>
	<title>Button class - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Button_class"/>
	<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Button_class&amp;action=history"/>
	<updated>2026-11-17T14:17:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Button_class&amp;diff=23122&amp;oldid=prev</id>
		<title>MoKaLux: Created page with &quot;__TOC__  This button takes two bitmaps as argument. 1 bitmap for the button &#039;&#039;up&#039;&#039; state, and 1 for the button &#039;&#039;down&#039;&#039; state.  If only 1 bitmap is passed, it will be used for...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Button_class&amp;diff=23122&amp;oldid=prev"/>
		<updated>2023-11-26T17:17:27Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__TOC__  This button takes two bitmaps as argument. 1 bitmap for the button &amp;#039;&amp;#039;up&amp;#039;&amp;#039; state, and 1 for the button &amp;#039;&amp;#039;down&amp;#039;&amp;#039; state.  If only 1 bitmap is passed, it will be used for...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
This button takes two bitmaps as argument. 1 bitmap for the button &amp;#039;&amp;#039;up&amp;#039;&amp;#039; state, and 1 for the button &amp;#039;&amp;#039;down&amp;#039;&amp;#039; state.&lt;br /&gt;
&lt;br /&gt;
If only 1 bitmap is passed, it will be used for both up and down states.&lt;br /&gt;
&lt;br /&gt;
=== Button &amp;#039;&amp;#039;&amp;#039;Class&amp;#039;&amp;#039;&amp;#039; ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
--[[&lt;br /&gt;
-- Button&lt;br /&gt;
-- A generic Gideros Button class&lt;br /&gt;
&lt;br /&gt;
-- This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php&lt;br /&gt;
-- (C) 2010 - 2011 Gideros Mobile&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
Button = Core.class(Sprite)&lt;br /&gt;
&lt;br /&gt;
function Button:init(upState, downState) -- upstate and downstate are bitmaps&lt;br /&gt;
	self.upState = upState&lt;br /&gt;
	self.downState = downState&lt;br /&gt;
	self:addChild(self.upState)&lt;br /&gt;
	if self.downState then self:addChild(self.downState) end&lt;br /&gt;
&lt;br /&gt;
	self.focus = false&lt;br /&gt;
&lt;br /&gt;
	-- set the visual state as &amp;quot;up&amp;quot;&lt;br /&gt;
	self:updateVisualState(false)&lt;br /&gt;
&lt;br /&gt;
	-- register to all mouse and touch events&lt;br /&gt;
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)&lt;br /&gt;
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)&lt;br /&gt;
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)&lt;br /&gt;
&lt;br /&gt;
	self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)&lt;br /&gt;
	self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)&lt;br /&gt;
	self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)&lt;br /&gt;
	self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function Button:onMouseDown(ev)&lt;br /&gt;
	if self:hitTestPoint(ev.x, ev.y) then&lt;br /&gt;
		self.focus = true&lt;br /&gt;
		self:updateVisualState(true)&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function Button:onMouseMove(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		if not self:hitTestPoint(ev.x, ev.y) then&lt;br /&gt;
			self.focus = false&lt;br /&gt;
			self:updateVisualState(false)&lt;br /&gt;
		end&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function Button:onMouseUp(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		self.focus = false&lt;br /&gt;
		self:updateVisualState(false)&lt;br /&gt;
		self:dispatchEvent(Event.new(&amp;quot;clicked&amp;quot;)) -- button is clicked, dispatch &amp;quot;clicked&amp;quot; event&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- if button is on focus, stop propagation of touch events&lt;br /&gt;
function Button:onTouchesBegin(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- if button is on focus, stop propagation of touch events&lt;br /&gt;
function Button:onTouchesMove(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- if button is on focus, stop propagation of touch events&lt;br /&gt;
function Button:onTouchesEnd(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- if touches are cancelled, reset the state of the button&lt;br /&gt;
function Button:onTouchesCancel(ev)&lt;br /&gt;
	if self.focus then&lt;br /&gt;
		self.focus = false&lt;br /&gt;
		self:updateVisualState(false)&lt;br /&gt;
		ev:stopPropagation()&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- if state is true show downState else show upState&lt;br /&gt;
function Button:updateVisualState(state)&lt;br /&gt;
	if state then -- downState&lt;br /&gt;
		if self.downState then&lt;br /&gt;
			self.downState:setVisible(true)&lt;br /&gt;
			self.upState:setVisible(false)&lt;br /&gt;
		else&lt;br /&gt;
			self.upState:setVisible(true)&lt;br /&gt;
		end&lt;br /&gt;
	else -- upState&lt;br /&gt;
		if self.downState then&lt;br /&gt;
			self.downState:setVisible(false)&lt;br /&gt;
			self.upState:setVisible(true)&lt;br /&gt;
		else&lt;br /&gt;
			self.upState:setVisible(true)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Button &amp;#039;&amp;#039;&amp;#039;Demo&amp;#039;&amp;#039;&amp;#039; ===&lt;br /&gt;
{{#widget:GApp|app=ButtonDemo1.GApp|width=480|height=320}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- bg&lt;br /&gt;
application:setBackgroundColor(0x6c6c6c)&lt;br /&gt;
-- textures&lt;br /&gt;
local texbtnup = Texture.new(&amp;quot;gfx/ui/btn_01_up.png&amp;quot;)&lt;br /&gt;
local texbtndown = Texture.new(&amp;quot;gfx/ui/btn_01_down.png&amp;quot;)&lt;br /&gt;
-- buttons&lt;br /&gt;
local btns = {}&lt;br /&gt;
btns[#btns+1] = Button.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown))&lt;br /&gt;
btns[#btns+1] = Button.new(Bitmap.new(texbtnup))&lt;br /&gt;
btns[#btns+1] = Button.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown))&lt;br /&gt;
btns[#btns+1] = Button.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown))&lt;br /&gt;
btns[#btns+1] = Button.new(Bitmap.new(texbtnup), Bitmap.new(texbtndown))&lt;br /&gt;
--...&lt;br /&gt;
-- listener&lt;br /&gt;
function clicked(btn)&lt;br /&gt;
	print(&amp;quot;button &amp;quot;..btn..&amp;quot; was clicked!&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
for k, v in ipairs(btns) do&lt;br /&gt;
	v:setScale(0.5)&lt;br /&gt;
	v:setPosition(16*(k+(k-1)*4), 16*1) -- horizontal&lt;br /&gt;
--	v:setPosition(16*1, 16*(k+(k-1)*2)) -- or vertical&lt;br /&gt;
	stage:addChild(v)&lt;br /&gt;
	v:addEventListener(&amp;quot;clicked&amp;quot;, clicked, k)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[UI Buttons]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
</feed>